com::cosylab::logging::client::cache::LogFileCache Class Reference

Inheritance diagram for com::cosylab::logging::client::cache::LogFileCache:
Inheritance graph
[legend]
Collaboration diagram for com::cosylab::logging::client::cache::LogFileCache:
Collaboration graph
[legend]

List of all members.

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< ILogEntryiterator ()

Protected Member Functions

void initCache () throws IOException
String toCacheString (ILogEntry log)

Protected Attributes

RandomAccessFile file = null
int logID = 0
TreeMap< Integer, LogCacheInfoindex = new TreeMap<Integer,LogCacheInfo>()
HashMap< Integer, ILogEntryreplacedLogs = new HashMap<Integer,ILogEntry>()

Private Member Functions

String getFile () throws IOException
String getLogAsString (int idx)
ILogEntry fromCacheString (String str)

Private Attributes

String logFileName
StringBuilder sb = new StringBuilder()

Static Private Attributes

static final String SEPARATOR = new String (""+((char)0))

Detailed Description

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.

Author:
acaproni

Member Function Documentation

synchronized int com::cosylab::logging::client::cache::LogFileCache::add ( ILogEntry  log  )  throws LogCacheException [inline]
synchronized void com::cosylab::logging::client::cache::LogFileCache::clear (  )  throws LogCacheException [inline]
synchronized void com::cosylab::logging::client::cache::LogFileCache::deleteLog ( Integer  key  )  throws LogCacheException [inline]

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.

Parameters:
pos The key of the log to remove

Implements com::cosylab::logging::client::cache::ILogMap.

Reimplemented in com::cosylab::logging::client::cache::LogBufferedFileCache.

References index, logID, and replacedLogs.

Referenced by deleteLogs(), alma::acs::jlog::test::DeleteLogTest::testGetFirstLogLogFileCache(), alma::acs::jlog::test::DeleteLogTest::testGetLastLogLogFileCache(), and alma::acs::jlog::test::DeleteLogTest::testLogFileCacheDelete().

synchronized void com::cosylab::logging::client::cache::LogFileCache::deleteLogs ( Collection< Integer >  keys  )  throws LogCacheException [inline]

Delete a set of logs

Parameters:
keys The keys of logs to delete

Implements com::cosylab::logging::client::cache::ILogMap.

Reimplemented in com::cosylab::logging::client::cache::LogBufferedFileCache.

References deleteLog().

Referenced by alma::acs::jlog::test::DeleteLogTest::testDeleteLogsFromLogFileCache().

ILogEntry com::cosylab::logging::client::cache::LogFileCache::fromCacheString ( String  str  )  [inline, private]

References SEPARATOR.

Referenced by getLog().

String com::cosylab::logging::client::cache::LogFileCache::getFile (  )  throws IOException [inline, private]

Create the file for the cache trying in several places before giving up.

@ return The name of the file for the temporary log file

Exceptions:
IOException If an error happened while creating the new file

References logFileName.

Referenced by initCache().

long com::cosylab::logging::client::cache::LogFileCache::getFileSize (  )  throws IOException [inline]

Return the length of the file on disk

Returns:
The size of the file cache
Exceptions:
IOException in case of I/O error
See also:
java.io.RandomAccessFile

Reimplemented in com::cosylab::logging::client::cache::LogBufferedFileCache.

References file.

Integer com::cosylab::logging::client::cache::LogFileCache::getFirstLog (  )  [inline]

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.

Returns:
The key of the first log null if the cache is empty

Implements com::cosylab::logging::client::cache::ILogMap.

Reimplemented in com::cosylab::logging::client::cache::LogBufferedFileCache.

References index.

Referenced by alma::acs::jlog::test::DeleteLogTest::testDeleteLogsFromLogFileCache(), alma::acs::jlog::test::DeleteLogTest::testGetFirstLogLogFileCache(), and alma::acs::jlog::test::DeleteLogTest::testGetLastLogLogFileCache().

int com::cosylab::logging::client::cache::LogFileCache::getFirstLogs ( int  n,
Collection< Integer >  keys 
) [inline]

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.

Parameters:
n The desired number of keys of first logs
keys The collection to add they keys to
Returns:
The number of keys effectively added

Implements com::cosylab::logging::client::cache::ILogMap.

Reimplemented in com::cosylab::logging::client::cache::LogBufferedFileCache.

References index.

Integer com::cosylab::logging::client::cache::LogFileCache::getLastLog (  )  [inline]

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

Returns:
The key of the last inserted log null if th cache is empty

Implements com::cosylab::logging::client::cache::ILogMap.

Reimplemented in com::cosylab::logging::client::cache::LogBufferedFileCache.

References index.

Referenced by alma::acs::jlog::test::DeleteLogTest::testGetLastLogLogFileCache().

synchronized ILogEntry com::cosylab::logging::client::cache::LogFileCache::getLog ( Integer  key  )  throws LogCacheException [inline]

Return the log in the given position

Parameters:
pos The position of the log
Returns:
The LogEntryXML or null in case of error

Implements com::cosylab::logging::client::cache::ILogMap.

Reimplemented in com::cosylab::logging::client::cache::LogBufferedFileCache.

References fromCacheString(), getLogAsString(), index, logID, and replacedLogs.

Referenced by alma::acs::jlog::test::DeleteLogTest::testDeleteLogsFromLogFileCache(), and alma::acs::jlog::test::DeleteLogTest::testLogFileCacheDelete().

String com::cosylab::logging::client::cache::LogFileCache::getLogAsString ( int  idx  )  [inline, private]

Return the string representation (XML) of the log in position idx The string is read from the temporary file

Parameters:
idx The position of the log; valid position are between 0 and size-1
Returns:
A String representing the log in the given position

References file, index, com::cosylab::logging::client::cache::LogFileCache::LogCacheInfo::len, logID, and com::cosylab::logging::client::cache::LogFileCache::LogCacheInfo::start.

Referenced by getLog().

synchronized int com::cosylab::logging::client::cache::LogFileCache::getSize (  )  [inline]
void com::cosylab::logging::client::cache::LogFileCache::initCache (  )  throws IOException [inline, 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

Exceptions:
IOException 

References file, getFile(), index, and replacedLogs.

Referenced by add(), and com::cosylab::logging::client::cache::LogBufferedFileCache::flushBuffer().

Iterator<ILogEntry> com::cosylab::logging::client::cache::LogFileCache::iterator (  )  [inline]

Return an iterator over the logs in cache

Implements com::cosylab::logging::client::cache::ILogMap.

Reimplemented in com::cosylab::logging::client::cache::LogBufferedFileCache.

Set<Integer> com::cosylab::logging::client::cache::LogFileCache::keySet (  )  [inline]

Return a set with all the keys of the logs in cache

Returns:
The keys of the logs in cache

Implements com::cosylab::logging::client::cache::ILogMap.

Reimplemented in com::cosylab::logging::client::cache::LogBufferedFileCache.

References index.

void com::cosylab::logging::client::cache::LogFileCache::replaceLog ( Integer  key,
ILogEntry  log 
) throws LogCacheException [inline]

Replace the log in the given position with the new one NOTE: the new log is kept in memory

Parameters:
position The position of the log to replace
log The key (identifier) ot the log

Implements com::cosylab::logging::client::cache::ILogMap.

Reimplemented in com::cosylab::logging::client::cache::LogBufferedFileCache.

References index, and replacedLogs.

String com::cosylab::logging::client::cache::LogFileCache::toCacheString ( ILogEntry  log  )  [inline, protected]

Member Data Documentation

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 add(), clear(), com::cosylab::logging::client::cache::LogBufferedFileCache::flushBuffer(), getFileSize(), getLogAsString(), and 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 add(), clear(), deleteLog(), com::cosylab::logging::client::cache::LogBufferedFileCache::flushBuffer(), getFirstLog(), getFirstLogs(), getLastLog(), getLog(), getLogAsString(), getSize(), initCache(), keySet(), and replaceLog().

The name of the log file of the cache This file is filled of logs when they arrive from the notification channel or by reading an input file The file will be destroyed when the object is destroyed.

Referenced by clear(), and getFile().

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 add(), com::cosylab::logging::client::cache::LogBufferedFileCache::add(), clear(), deleteLog(), getLog(), and getLogAsString().

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 deleteLog(), getLog(), initCache(), and replaceLog().

StringBuilder com::cosylab::logging::client::cache::LogFileCache::sb = new StringBuilder() [private]

The buffer to build the logs to write in cache

Referenced by toCacheString().

final String com::cosylab::logging::client::cache::LogFileCache::SEPARATOR = new String (""+((char)0)) [static, private]

The separator between the field of the logs written in cache

Referenced by fromCacheString(), and toCacheString().


The documentation for this class was generated from the following file:

Generated by  doxygen 1.6.2