
Public Member Functions | |
| synchronized void | put (CacheEntry entry) throws IOException |
| synchronized CacheEntry | get () throws IOException |
| synchronized void | clear () |
| synchronized int | size () |
Static Public Attributes | |
| static final int | MAX_QUEUE_LENGTH = 20000 |
| static final int | PAGE_LEN = 5000 |
| static final int | THRESHOLD = 12500 |
Private Member Functions | |
| File | getNewFile () throws IOException |
| void | flushEntriesInQueue () throws IOException |
| void | readNextPageFromFile () throws IOException |
| void | writePageOnFile () throws IOException |
Private Attributes | |
| final List< CacheEntry > | inMemoryQueue = new LinkedList<CacheEntry>() |
| byte[] | fileBuffer = new byte[PAGE_SIZE] |
| byte[] | entryBuffer = new byte[CacheEntry.ENTRY_LENGTH] |
| List< CacheEntry > | cachedEntries = new LinkedList<CacheEntry>() |
| File | file = null |
| RandomAccessFile | raFile = null |
| volatile int | pagesOnFile = 0 |
| volatile int | nextPageToRead = 0 |
Static Private Attributes | |
| static final int | PAGE_SIZE = PAGE_LEN*CacheEntry.ENTRY_LENGTH |
The queue of entries.
This class has been introduced to avoid keeping in memory a never ending queue of CacheEntry and reduce the chance to face an out of memory at run-time.
This class is composed of two lists and a file. inMemoryQueue is the list of the entries to get. When this list contains few items then some more items are read from the file.
The other list, CacheEntriesQueue, is a buffer where the entries are stored ready to be flushed on disk. This is done to write more entries at once reducing the I/O and increasing the performances.
Implementation note
CacheEntry items are read only with the get method and pushed with the put.
Adding entries:
If there is enough room in inMemoryQueue (i.e. inMemoryQueue.size()<MAX_QUEUE_LENGTH) then a new entry is stored directly in that list; otherwise it is added to cachedEntries ready to be written on file. If the size of cachedEntries is greater then PAGE_LEN, the size of a page, then a page is flushed on disk. Note that what is in the list, cacheEntries is added at the end of the file.
Getting entries:
The entry to get is always in inMemoryQueue. After getting an entry, it checks if the the size of the queue allows to get new entries from the file or from the cachedEntries. Note that the right order is first the file and then cachedEntries. In fact cachedEntries, contains the last received entries, packed to be transferred on a new page on disk while the first entries to push in the queue are on a page disk (if any).
| synchronized void com::cosylab::logging::engine::cache::CacheEntriesQueue::clear | ( | ) | [inline] |
Clear the queue and the file (if any)
References cachedEntries, entryBuffer, file, fileBuffer, inMemoryQueue, nextPageToRead, pagesOnFile, and raFile.
Referenced by alma::acs::jlog::test::TestEngineCacheEntriesQueue::tearDown(), and alma::acs::jlog::test::TestEngineCacheEntriesQueue::testClear().
| void com::cosylab::logging::engine::cache::CacheEntriesQueue::flushEntriesInQueue | ( | ) | throws IOException [inline, private] |
Move the entries from the file or the vector into the queue
The vector contains the last added entries so if there are pages in the file they are flushed before the vector
| IOException | In case of error during I/O |
References cachedEntries, inMemoryQueue, pagesOnFile, and readNextPageFromFile().
Referenced by get().
| synchronized CacheEntry com::cosylab::logging::engine::cache::CacheEntriesQueue::get | ( | ) | throws IOException [inline] |
Get the next value from the queue.
null if the queue is empty| IOException | In case of error during I/O |
References cachedEntries, flushEntriesInQueue(), inMemoryQueue, pagesOnFile, and THRESHOLD.
Referenced by com::cosylab::logging::engine::cache::EngineCache::pop(), alma::acs::jlog::test::TestEngineCacheEntriesQueue::testPushPopQueue(), alma::acs::jlog::test::TestEngineCacheEntriesQueue::testPushPopQueueVector(), alma::acs::jlog::test::TestEngineCacheEntriesQueue::testPushPopQueueVectorFile(), and alma::acs::jlog::test::TestEngineCacheEntriesQueue::testRandomOrder().
| File com::cosylab::logging::engine::cache::CacheEntriesQueue::getNewFile | ( | ) | throws IOException [inline, private] |
Attempts to create the file for the strings in several places before giving up.
null if it was not possible to create a new file | IOException | In case of error creating the temporary file |
Referenced by writePageOnFile().
| synchronized void com::cosylab::logging::engine::cache::CacheEntriesQueue::put | ( | CacheEntry | entry | ) | throws IOException [inline] |
Put an entry in Cache.
If the cache is full the entry is added to the buffer.
| entry | The not null CacheEntry to add to the queue |
| IOException | In case of I/O error while flushing the cache on disk |
References cachedEntries, inMemoryQueue, MAX_QUEUE_LENGTH, PAGE_LEN, pagesOnFile, and writePageOnFile().
Referenced by com::cosylab::logging::engine::cache::EngineCache::push(), alma::acs::jlog::test::TestEngineCacheEntriesQueue::testClear(), alma::acs::jlog::test::TestEngineCacheEntriesQueue::testPushPopQueue(), alma::acs::jlog::test::TestEngineCacheEntriesQueue::testPushPopQueueVector(), alma::acs::jlog::test::TestEngineCacheEntriesQueue::testPushPopQueueVectorFile(), and alma::acs::jlog::test::TestEngineCacheEntriesQueue::testRandomOrder().
| void com::cosylab::logging::engine::cache::CacheEntriesQueue::readNextPageFromFile | ( | ) | throws IOException [inline, private] |
Read page from the file putting all the CacheEntry it contains in the queue.
| IOException | In case of error during I/O |
References cachedEntries, entryBuffer, file, fileBuffer, inMemoryQueue, nextPageToRead, PAGE_LEN, PAGE_SIZE, pagesOnFile, and raFile.
Referenced by flushEntriesInQueue().
| synchronized int com::cosylab::logging::engine::cache::CacheEntriesQueue::size | ( | ) | [inline] |
Return the number of cache entries waiting in queue
References cachedEntries, inMemoryQueue, PAGE_LEN, and pagesOnFile.
Referenced by com::cosylab::logging::engine::cache::EngineCache::size(), alma::acs::jlog::test::TestEngineCacheEntriesQueue::testClear(), alma::acs::jlog::test::TestEngineCacheEntriesQueue::testPushPopQueue(), alma::acs::jlog::test::TestEngineCacheEntriesQueue::testPushPopQueueVector(), alma::acs::jlog::test::TestEngineCacheEntriesQueue::testPushPopQueueVectorFile(), and alma::acs::jlog::test::TestEngineCacheEntriesQueue::testRandomOrder().
| void com::cosylab::logging::engine::cache::CacheEntriesQueue::writePageOnFile | ( | ) | throws IOException [inline, private] |
Write a page of CacheEntry in the file
| IOException | In case of error creating a new temporary file |
References cachedEntries, file, fileBuffer, getNewFile(), PAGE_LEN, pagesOnFile, raFile, and com::cosylab::logging::engine::cache::CacheEntry::toHexadecimal().
Referenced by put().
List<CacheEntry> com::cosylab::logging::engine::cache::CacheEntriesQueue::cachedEntries = new LinkedList<CacheEntry>() [private] |
This Vector contains the entries that will be written on the file.
Referenced by clear(), flushEntriesInQueue(), get(), put(), readNextPageFromFile(), size(), and writePageOnFile().
byte [] com::cosylab::logging::engine::cache::CacheEntriesQueue::entryBuffer = new byte[CacheEntry.ENTRY_LENGTH] [private] |
The buffer containing the hexadecimal string of a CacheEntry
Referenced by clear(), and readNextPageFromFile().
File com::cosylab::logging::engine::cache::CacheEntriesQueue::file = null [private] |
The file to buffer entries on disk.
Referenced by clear(), readNextPageFromFile(), and writePageOnFile().
byte [] com::cosylab::logging::engine::cache::CacheEntriesQueue::fileBuffer = new byte[PAGE_SIZE] [private] |
The buffer for each I/O
Referenced by clear(), readNextPageFromFile(), and writePageOnFile().
final List<CacheEntry> com::cosylab::logging::engine::cache::CacheEntriesQueue::inMemoryQueue = new LinkedList<CacheEntry>() [private] |
The entries to keep in memory.
Referenced by clear(), flushEntriesInQueue(), get(), put(), readNextPageFromFile(), and size().
final int com::cosylab::logging::engine::cache::CacheEntriesQueue::MAX_QUEUE_LENGTH = 20000 [static] |
The max number of entries kept in memory.
Referenced by put().
volatile int com::cosylab::logging::engine::cache::CacheEntriesQueue::nextPageToRead = 0 [private] |
The number of the next page to read from file.
Note: a new page is always added at the end of the file while the reading happens in a different order.
Referenced by clear(), and readNextPageFromFile().
final int com::cosylab::logging::engine::cache::CacheEntriesQueue::PAGE_LEN = 5000 [static] |
The number of CacheEntry to read/write from/to disk on each I/O
Referenced by put(), readNextPageFromFile(), size(), and writePageOnFile().
final int com::cosylab::logging::engine::cache::CacheEntriesQueue::PAGE_SIZE = PAGE_LEN*CacheEntry.ENTRY_LENGTH [static, private] |
The size (in bytes) of a page
Referenced by readNextPageFromFile().
volatile int com::cosylab::logging::engine::cache::CacheEntriesQueue::pagesOnFile = 0 [private] |
The number of pages written on file and not yet read
Referenced by clear(), flushEntriesInQueue(), get(), put(), readNextPageFromFile(), size(), and writePageOnFile().
RandomAccessFile com::cosylab::logging::engine::cache::CacheEntriesQueue::raFile = null [private] |
The RandomAccessFile to read/write entries created from bufferFile.
The I/O is paginated i.e. each read or write is done for a block of PAGE_LEN entries.
Referenced by clear(), readNextPageFromFile(), and writePageOnFile().
final int com::cosylab::logging::engine::cache::CacheEntriesQueue::THRESHOLD = 12500 [static] |
When in the LinkedBlockingQueue there are less entries then the THRESHOLD then the entries in the buffer are flushed in the queue
Referenced by get().
1.6.2