Classes | |
class | LogCacheInfo |
Public Member Functions | |
synchronized int | getSize () |
long | getFileSize () throws IOException |
synchronized void | clear () throws LogCacheException |
synchronized ILogEntry | getLog (Integer key) throws LogCacheException |
synchronized int | add (ILogEntry log) throws LogCacheException |
void | replaceLog (Integer key, ILogEntry log) throws LogCacheException |
synchronized void | deleteLog (Integer key) throws LogCacheException |
synchronized void | deleteLogs (Collection< Integer > keys) throws LogCacheException |
Integer | getFirstLog () |
int | getFirstLogs (int n, Collection< Integer > keys) |
Integer | getLastLog () |
Set< Integer > | keySet () |
Iterator< ILogEntry > | iterator () |
Protected Member Functions | |
void | initCache () throws IOException |
String | toCacheString (ILogEntry log) |
Protected Attributes | |
RandomAccessFile | file = null |
int | logID = 0 |
TreeMap< Integer, LogCacheInfo > | index = new TreeMap<Integer,LogCacheInfo>() |
HashMap< Integer, ILogEntry > | replacedLogs = new HashMap<Integer,ILogEntry>() |
This class implements the cache in order to be able to manage long log files. It is implemented by a file of logs and a data structure to associate the position in a file (LogCacheInfo) to each log. Such data structure is realized by a TreeMap whith the key given by the (progressive, unique) number of a log.
Actually it is not possible to remove a log from the file. To effectively delete logs from disk we need some kind of garbage collector that removes the logs marked as delete from the file. This garbage collector is needed to avoid the file on disk grows indefinitely.
Another potential problem is given by the integer identifier of the logs that grows indefinitely. A temporary solution could be that of using long instad of integers.
synchronized int com.cosylab.logging.client.cache.LogFileCache.add | ( | ILogEntry | log | ) | throws LogCacheException |
Append a log to the end of the cache
log | The log to append in the cache |
Implements com.cosylab.logging.client.cache.ILogMap.
References com.cosylab.logging.client.cache.LogFileCache.file, com.cosylab.logging.client.cache.LogFileCache.index, com.cosylab.logging.client.cache.LogFileCache.initCache(), com.cosylab.logging.client.cache.LogFileCache.logID, and com.cosylab.logging.client.cache.LogFileCache.toCacheString().
synchronized void com.cosylab.logging.client.cache.LogFileCache.clear | ( | ) | throws LogCacheException |
Empty the cache.
IOException |
Implements com.cosylab.logging.client.cache.ILogMap.
References com.cosylab.logging.client.cache.LogFileCache.file, com.cosylab.logging.client.cache.LogFileCache.index, and com.cosylab.logging.client.cache.LogFileCache.logID.
synchronized void com.cosylab.logging.client.cache.LogFileCache.deleteLog | ( | Integer | key | ) | throws LogCacheException |
Delete a log The log is marked as deleted and moved from the index to the deleteLogIndex. The log still exists in the file but is not accessible.
pos | The key of the log to remove |
Implements com.cosylab.logging.client.cache.ILogMap.
References com.cosylab.logging.client.cache.LogFileCache.index, com.cosylab.logging.client.cache.LogFileCache.logID, and com.cosylab.logging.client.cache.LogFileCache.replacedLogs.
Referenced by com.cosylab.logging.client.cache.LogFileCache.deleteLogs().
synchronized void com.cosylab.logging.client.cache.LogFileCache.deleteLogs | ( | Collection< Integer > | keys | ) | throws LogCacheException |
Delete a set of logs
keys | The keys of logs to delete |
Implements com.cosylab.logging.client.cache.ILogMap.
References com.cosylab.logging.client.cache.LogFileCache.deleteLog().
long com.cosylab.logging.client.cache.LogFileCache.getFileSize | ( | ) | throws IOException |
Return the length of the file on disk
IOException | in case of I/O error |
References com.cosylab.logging.client.cache.LogFileCache.file.
Integer com.cosylab.logging.client.cache.LogFileCache.getFirstLog | ( | ) |
Return the key of the first valid log (FIFO). The key of the first log is 0 but it can change if the log 0 has been deleted.
Implements com.cosylab.logging.client.cache.ILogMap.
References com.cosylab.logging.client.cache.LogFileCache.index.
int com.cosylab.logging.client.cache.LogFileCache.getFirstLogs | ( | int | n, | |
Collection< Integer > | keys | |||
) |
Append at most n keys from the first valid logs to the collection. First here means first in the FIFO policy.
The number of added keys can be less then n if the cache doesn't contain enough logs.
n | The desired number of keys of first logs | |
keys | The collection to add they keys to |
Implements com.cosylab.logging.client.cache.ILogMap.
References com.cosylab.logging.client.cache.LogFileCache.index.
Integer com.cosylab.logging.client.cache.LogFileCache.getLastLog | ( | ) |
Return the key of the last valid log (FIFO) The key of the last log is the key of the last inserted log but it can cheang if such log has been deleted
Implements com.cosylab.logging.client.cache.ILogMap.
References com.cosylab.logging.client.cache.LogFileCache.index.
synchronized ILogEntry com.cosylab.logging.client.cache.LogFileCache.getLog | ( | Integer | key | ) | throws LogCacheException |
Return the log in the given position
pos | The position of the log |
Implements com.cosylab.logging.client.cache.ILogMap.
References com.cosylab.logging.client.cache.LogFileCache.index, com.cosylab.logging.client.cache.LogFileCache.logID, and com.cosylab.logging.client.cache.LogFileCache.replacedLogs.
synchronized int com.cosylab.logging.client.cache.LogFileCache.getSize | ( | ) |
Implements com.cosylab.logging.client.cache.ILogMap.
References com.cosylab.logging.client.cache.LogFileCache.index.
void com.cosylab.logging.client.cache.LogFileCache.initCache | ( | ) | throws IOException [protected] |
Init the file where the cache stores the logs If the file already exists it is truncated to 0 length this situation might happen whenever the cache is cleared
IOException |
References com.cosylab.logging.client.cache.LogFileCache.file, com.cosylab.logging.client.cache.LogFileCache.index, and com.cosylab.logging.client.cache.LogFileCache.replacedLogs.
Referenced by com.cosylab.logging.client.cache.LogFileCache.add(), and com.cosylab.logging.client.cache.LogBufferedFileCache.flushBuffer().
Iterator<ILogEntry> com.cosylab.logging.client.cache.LogFileCache.iterator | ( | ) |
Return an iterator over the logs in cache
Implements com.cosylab.logging.client.cache.ILogMap.
Set<Integer> com.cosylab.logging.client.cache.LogFileCache.keySet | ( | ) |
Return a set with all the keys of the logs in cache
Implements com.cosylab.logging.client.cache.ILogMap.
References com.cosylab.logging.client.cache.LogFileCache.index.
void com.cosylab.logging.client.cache.LogFileCache.replaceLog | ( | Integer | key, | |
ILogEntry | log | |||
) | throws LogCacheException |
Replace the log in the given position with the new one NOTE: the new log is kept in memory
position | The position of the log to replace | |
log | The key (identifier) ot the log |
Implements com.cosylab.logging.client.cache.ILogMap.
References com.cosylab.logging.client.cache.LogFileCache.index, and com.cosylab.logging.client.cache.LogFileCache.replacedLogs.
String com.cosylab.logging.client.cache.LogFileCache.toCacheString | ( | ILogEntry | log | ) | [protected] |
References com.cosylab.logging.engine.log.ILogEntry.getAdditionalData(), com.cosylab.logging.engine.log.ILogEntry.getField(), and com.cosylab.logging.engine.log.ILogEntry.hasDatas().
Referenced by com.cosylab.logging.client.cache.LogFileCache.add(), com.cosylab.logging.client.cache.LogBufferedFileCache.add(), and com.cosylab.logging.client.cache.LogBufferedFileCache.replaceLog().
RandomAccessFile com.cosylab.logging.client.cache.LogFileCache.file = null [protected] |
The file of logs is accessed in a random way (the positions are stored in the index)
Referenced by com.cosylab.logging.client.cache.LogFileCache.add(), com.cosylab.logging.client.cache.LogFileCache.clear(), com.cosylab.logging.client.cache.LogBufferedFileCache.flushBuffer(), com.cosylab.logging.client.cache.LogFileCache.getFileSize(), and com.cosylab.logging.client.cache.LogFileCache.initCache().
TreeMap<Integer,LogCacheInfo> com.cosylab.logging.client.cache.LogFileCache.index = new TreeMap<Integer,LogCacheInfo>() [protected] |
The index of the log is a SortedMap having the number identifying a log as key. Each entry is a LogCacheInfo containing the starting and ending position of the log in the file
Referenced by com.cosylab.logging.client.cache.LogFileCache.add(), com.cosylab.logging.client.cache.LogFileCache.clear(), com.cosylab.logging.client.cache.LogFileCache.deleteLog(), com.cosylab.logging.client.cache.LogBufferedFileCache.flushBuffer(), com.cosylab.logging.client.cache.LogFileCache.getFirstLog(), com.cosylab.logging.client.cache.LogFileCache.getFirstLogs(), com.cosylab.logging.client.cache.LogFileCache.getLastLog(), com.cosylab.logging.client.cache.LogFileCache.getLog(), com.cosylab.logging.client.cache.LogFileCache.getSize(), com.cosylab.logging.client.cache.LogFileCache.initCache(), com.cosylab.logging.client.cache.LogFileCache.keySet(), and com.cosylab.logging.client.cache.LogFileCache.replaceLog().
int com.cosylab.logging.client.cache.LogFileCache.logID = 0 [protected] |
Each log is identified by a unique integer used as key to get the log from the file (throw the index data structure) logID is incremented whenever a new log is added
NOTE: in this implementation this value is incremented without taking care of the logs deleted.
Referenced by com.cosylab.logging.client.cache.LogFileCache.add(), com.cosylab.logging.client.cache.LogBufferedFileCache.add(), com.cosylab.logging.client.cache.LogFileCache.clear(), com.cosylab.logging.client.cache.LogFileCache.deleteLog(), and com.cosylab.logging.client.cache.LogFileCache.getLog().
HashMap<Integer,ILogEntry> com.cosylab.logging.client.cache.LogFileCache.replacedLogs = new HashMap<Integer,ILogEntry>() [protected] |
The logs replaced (for example the logs with some info added) They are usually a few so we keep them in memory
Referenced by com.cosylab.logging.client.cache.LogFileCache.deleteLog(), com.cosylab.logging.client.cache.LogFileCache.getLog(), com.cosylab.logging.client.cache.LogFileCache.initCache(), and com.cosylab.logging.client.cache.LogFileCache.replaceLog().