The Persistence API provides an easy and fast way to store data. It supports two main storages, namely PersistentSequence
and PersistentQueue
.
...
PersistentSequence
provides functionality for storing sequential data (for example, logs). Each record should have a unique index. Indexer
implementation is used to provide an index for a certain storing record. Also, PersistentSequence
requires custom Serializer
implementation to serialize objects to byte buffer and restore it back.
...
In addition to storing data, PersistentSequence
provides methods for retrieving records from the storage. It supports reading a single item by index or iterating through items:
...
There is a possibility to review stored records and remove some of them from the storage:
Code Block | ||
---|---|---|
| ||
sequence.cleanUp(new CleanupItemListener<MyRecord>() { @Override public boolean checkForClean(long id, MyRecord item) { //return true to removed this record from storage return false; } }); |
Or remove all and reset the sequence:
Code Block | ||
---|---|---|
| ||
//remove all items and reset index sequence.reset(); |
The default PersistentSequence
implementation is optimized for writing data and , so reading operations may take a bit more time.
...
PersistentQueue
works like a queue but persist persists all items to the disk. Thus, it can restore its state after application restart. PersistentQueue
has methods similar to java.util.Queue
methods:
Code Block | ||
---|---|---|
| ||
final PersistentQueue<MyRecord> queue = factory.buildQueue("queue_sample", serializer); //push item to the tail of queue queue.add(record); // read iem from head but doesn't remove record = queue.peek(); // extract item from head and remove it from queue record = queue.poll(); |
Also PersistentQueue
allow to iterate allows iterating all its items:
Code Block | ||
---|---|---|
| ||
queue.iterate(new RetrieveQueueItemsListener<MyRecord>() { @Override public void onItem(MyRecord record) { //.... } }); |
To remove all the items from the queue, you can use the clean method:
...