public class LogFileCache extends java.lang.Object implements ILogMap
| Modifier and Type | Class and Description |
|---|---|
protected static class |
LogFileCache.LogCacheInfo
An entry in the cache on disk.
|
| Modifier and Type | Field and Description |
|---|---|
protected java.io.RandomAccessFile |
file
The file of logs is accessed in a random way
(the positions are stored in the index)
|
protected java.util.TreeMap<java.lang.Integer,LogFileCache.LogCacheInfo> |
index
The index of the log is a SortedMap having the number identifying a log as key.
|
protected int |
logID
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.
|
protected java.util.HashMap<java.lang.Integer,ILogEntry> |
replacedLogs
The logs replaced (for example the logs with some info added)
They are usually a few so we keep them in memory
|
| Constructor and Description |
|---|
LogFileCache() |
| Modifier and Type | Method and Description |
|---|---|
int |
add(ILogEntry log)
Append a log to the end of the cache
|
void |
clear()
Empty the cache.
|
void |
deleteLog(java.lang.Integer key)
Delete a log
The log is marked as deleted and moved from the index to
the deleteLogIndex.
|
void |
deleteLogs(java.util.Collection<java.lang.Integer> keys)
Delete a set of logs
|
long |
getFileSize()
Return the length of the file on disk
|
java.lang.Integer |
getFirstLog()
Return the key of the first valid log (FIFO).
|
int |
getFirstLogs(int n,
java.util.Collection<java.lang.Integer> keys)
Append at most n keys from the first valid logs to the collection.
|
java.lang.Integer |
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
|
ILogEntry |
getLog(java.lang.Integer key)
Return the log in the given position
|
int |
getSize() |
protected void |
initCache()
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
|
java.util.Iterator<ILogEntry> |
iterator()
Return an iterator over the logs in cache
|
java.util.Set<java.lang.Integer> |
keySet()
Return a set with all the keys of the logs in cache
|
void |
replaceLog(java.lang.Integer key,
ILogEntry log)
Replace the log in the given position with the new one
NOTE: the new log is kept in memory
|
protected java.lang.String |
toCacheString(ILogEntry log) |
protected java.io.RandomAccessFile file
protected int logID
protected java.util.TreeMap<java.lang.Integer,LogFileCache.LogCacheInfo> index
protected java.util.HashMap<java.lang.Integer,ILogEntry> replacedLogs
public int getSize()
public long getFileSize()
throws java.io.IOException
java.io.IOException - in case of I/O errorRandomAccessFileprotected void initCache()
throws java.io.IOException
java.io.IOExceptionpublic void clear()
throws LogCacheException
clear in interface ILogMapjava.io.IOExceptionLogCacheExceptionpublic ILogEntry getLog(java.lang.Integer key) throws LogCacheException
getLog in interface ILogMappos - The position of the logLogCacheExceptionpublic int add(ILogEntry log) throws LogCacheException
add in interface ILogMaplog - The log to append in the cacheLogCacheExceptionpublic void replaceLog(java.lang.Integer key,
ILogEntry log)
throws LogCacheException
replaceLog in interface ILogMapposition - The position of the log to replacelog - The key (identifier) ot the logLogCacheExceptionprotected java.lang.String toCacheString(ILogEntry log)
public void deleteLog(java.lang.Integer key)
throws LogCacheException
deleteLog in interface ILogMappos - The key of the log to removeLogCacheExceptionpublic void deleteLogs(java.util.Collection<java.lang.Integer> keys)
throws LogCacheException
deleteLogs in interface ILogMapkeys - The keys of logs to deleteLogCacheExceptionpublic java.lang.Integer getFirstLog()
getFirstLog in interface ILogMappublic int getFirstLogs(int n,
java.util.Collection<java.lang.Integer> keys)
getFirstLogs in interface ILogMapn - The desired number of keys of first logskeys - The collection to add they keys topublic java.lang.Integer getLastLog()
getLastLog in interface ILogMappublic java.util.Set<java.lang.Integer> keySet()