public class EntriesQueue
extends java.lang.Object
This class has been introduced to avoid keeping in memory a
never ending queue of QueueEntry 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, EntriesQueue, 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
QueueEntry items are read only with the get
method and pushed with the put.
Adding entries:
Getting entries:
If there is enough room in inMemoryQueue
(i.e. inMemoryQueue.size()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.
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).
| Modifier and Type | Field and Description |
|---|---|
static int |
MAX_QUEUE_LENGTH
The max number of entries kept in memory.
|
static int |
PAGE_LEN
The number of
QueueEntry to read/write from/to disk
on each I/O |
static int |
THRESHOLD
When in the
LinkedBlockingQueue there are less
entries then the THRESHOLD then the
entries in the buffer are flushed in the queue |
| Constructor and Description |
|---|
EntriesQueue() |
| Modifier and Type | Method and Description |
|---|---|
void |
clear()
Clear the queue and the file (if any)
|
QueueEntry |
get()
Get the next value from the queue.
|
boolean |
isEmpty() |
void |
put(QueueEntry entry)
Put an entry in Cache.
|
int |
size()
Return the number of cache entries waiting in queue
|
public static final int MAX_QUEUE_LENGTH
public static final int PAGE_LEN
QueueEntry to read/write from/to disk
on each I/Opublic static final int THRESHOLD
LinkedBlockingQueue there are less
entries then the THRESHOLD then the
entries in the buffer are flushed in the queuepublic void put(QueueEntry entry) throws java.io.IOException
If the cache is full the entry is added to the buffer.
entry - The not null QueueEntry to add to the queuejava.io.IOException - In case of I/O error while flushing the cache on diskpublic QueueEntry get() throws java.io.IOException
null if the
queue is emptyjava.io.IOException - In case of error during I/Opublic void clear()
public int size()
public boolean isEmpty()
true if the queue is empty;
false otherwise.