Class QueueFile
- All Implemented Interfaces:
Closeable
,AutoCloseable
,Iterable<byte[]>
QueueFile
instance after an exception.
Note that this implementation is not synchronized.
In a traditional queue, the remove operation returns an element. In this queue, peek()
and remove()
are used in conjunction. Use peek
to retrieve the first element, and
then remove
to remove it after successful processing. If the system crashes after
peek
and during processing, the element will remain in the queue, to be processed when the
system restarts.
NOTE: The current implementation is built for file systems that support atomic segment writes (like YAFFS). Most conventional file systems don't support this; if the power goes out while writing a segment, the segment will contain garbage and the file will be corrupt. We'll add journaling support so this class can be used with more file systems later.
Construct instances with QueueFile.Builder
.
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionvoid
add
(byte[] data) Adds an element to the end of the queue.void
add
(byte[] data, int offset, int count) Adds an element to the end of the queue.void
clear()
Clears this queue.void
close()
file()
The underlyingFile
backing this queue.boolean
Returnstrue
if the capacity limit of this queue has been reached, i.e.boolean
isEmpty()
Returns true if this queue contains no entries.Iterator<byte[]>
iterator()
Returns an iterator over elements in this QueueFile.@org.jetbrains.annotations.Nullable byte[]
peek()
Reads the eldest element.void
remove()
Removes the eldest element.void
remove
(int n) Removes the eldestn
elements.int
size()
Returns the number of elements in this queue.toString()
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Method Details
-
add
Adds an element to the end of the queue.- Parameters:
data
- to copy bytes from- Throws:
IOException
-
add
Adds an element to the end of the queue.- Parameters:
data
- to copy bytes fromoffset
- to start from in buffercount
- number of bytes to copy- Throws:
IndexOutOfBoundsException
- ifoffset < 0
orcount < 0
, or ifoffset + count
is bigger than the length ofbuffer
.IOException
-
isEmpty
public boolean isEmpty()Returns true if this queue contains no entries. -
peek
Reads the eldest element. Returns null if the queue is empty.- Throws:
IOException
-
iterator
Returns an iterator over elements in this QueueFile.The iterator disallows modifications to be made to the QueueFile during iteration. Removing elements from the head of the QueueFile is permitted during iteration using
Iterator.remove()
.The iterator may throw an unchecked
IOException
duringIterator.next()
orIterator.remove()
. -
size
public int size()Returns the number of elements in this queue. -
remove
Removes the eldest element.- Throws:
NoSuchElementException
- if the queue is emptyIOException
-
remove
Removes the eldestn
elements.- Throws:
NoSuchElementException
- if the queue is emptyIOException
-
clear
Clears this queue. Truncates the file to the initial size.- Throws:
IOException
-
isAtFullCapacity
public boolean isAtFullCapacity()Returnstrue
if the capacity limit of this queue has been reached, i.e. the number of elements stored in the queue equals its maximum size.- Returns:
true
if the capacity limit has been reached,false
otherwise
-
file
The underlyingFile
backing this queue. -
close
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
-
toString
-