-
- All Implemented Interfaces:
-
java.io.Closeable,java.lang.AutoCloseable
public class QueueFile implements Closeable
A reliable, efficient, file-based, FIFO queue. Additions and removals are O(1). All operations are atomic. Writes are synchronous; data will be written to disk before an operation returns. The underlying file is structured to survive process and even system crashes. If an I/O exception is thrown during a mutating change, the change is aborted. It is safe to continue to use a
{@code * QueueFile}instance after an exception.All operations are synchronized. In a traditional queue, the remove operation returns an element. In this queue, peek and remove are used in conjunction. Use
{@code * peek}to retrieve the first element, and then{@code remove}to remove it after successful processing. If the system crashes after{@code 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.
-
-
Method Summary
Modifier and Type Method Description voidadd(Array<byte> data)Adds an element to the end of the queue. synchronized voidadd(Array<byte> data, int offset, int count)Adds an element to the end of the queue. synchronized booleanisEmpty()Returns true if this queue contains no entries. synchronized Array<byte>peek()Reads the eldest element. synchronized intforEach(PayloadQueue.ElementVisitor reader)Invokes the given reader once for each element in the queue, from eldest to most recentlyadded. synchronized intsize()Returns the number of elements in this queue. synchronized voidremove()Removes the eldest element. synchronized voidremove(int n)Removes the eldest {@code n}elements.synchronized voidclear()Clears this queue. synchronized voidclose()Closes the underlying file. StringtoString()-
-
Constructor Detail
-
QueueFile
QueueFile(File file)
Constructs a new queue backed by the given file.
-
-
Method Detail
-
add
void add(Array<byte> data)
Adds an element to the end of the queue.
- Parameters:
data- to copy bytes from
-
add
synchronized void add(Array<byte> data, int offset, int count)
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
-
isEmpty
synchronized boolean isEmpty()
Returns true if this queue contains no entries.
-
forEach
synchronized int forEach(PayloadQueue.ElementVisitor reader)
Invokes the given reader once for each element in the queue, from eldest to most recentlyadded. Continues until all elements are read or reader.read() returns
{@code false}.
-
size
synchronized int size()
Returns the number of elements in this queue.
-
remove
synchronized void remove()
Removes the eldest element.
-
remove
synchronized void remove(int n)
Removes the eldest
{@code n}elements.
-
clear
synchronized void clear()
Clears this queue. Truncates the file to the initial size.
-
close
synchronized void close()
Closes the underlying file.
-
-
-
-