public interface KVStore
extends java.io.Closeable
There are two main features provided by the implementations of this interface:
If the underlying data store requires serialization, data will be serialized to and deserialized
using a KVStoreSerializer
, which can be customized by the application. The serializer is
based on Jackson, so it supports all the Jackson annotations for controlling the serialization of
app-defined types.
Data is also automatically compressed to save disk space.
When using the built-in key management, the implementation will automatically create unique keys for each type written to the store. Keys are based on the type name, and always start with the "+" prefix character (so that it's easy to use both manual and automatic key management APIs without conflicts).
Another feature of automatic key management is indexing; by annotating fields or methods of
objects written to the store with KVIndex
, indices are created to sort the data
by the values of those properties. This makes it possible to provide sorting without having
to load all instances of those types from the store.
KVStore instances are thread-safe for both reads and writes.
Modifier and Type | Method and Description |
---|---|
long |
count(Class<?> type)
Returns the number of items of the given type currently in the store.
|
long |
count(Class<?> type,
String index,
Object indexedValue)
Returns the number of items of the given type which match the given indexed value.
|
void |
delete(Class<?> type,
Object naturalKey)
Removes an object and all data related to it, like index entries, from the store.
|
<T> T |
getMetadata(Class<T> klass)
Returns app-specific metadata from the store, or null if it's not currently set.
|
<T> T |
read(Class<T> klass,
Object naturalKey)
Read a specific instance of an object.
|
void |
setMetadata(Object value)
Writes the given value in the store metadata key.
|
<T> KVStoreView<T> |
view(Class<T> type)
Returns a configurable view for iterating over entities of the given type.
|
void |
write(Object value)
Writes the given object to the store, including indexed fields.
|
<T> T getMetadata(Class<T> klass) throws Exception
The metadata type is application-specific. This is a convenience method so that applications don't need to define their own keys for this information.
Exception
void setMetadata(Object value) throws Exception
Exception
<T> T read(Class<T> klass, Object naturalKey) throws Exception
naturalKey
- The object's "natural key", which uniquely identifies it. Null keys
are not allowed.java.util.NoSuchElementException
- If an element with the given key does not exist.Exception
void write(Object value) throws Exception
Writes may be slower when the object already exists in the store, since it will involve updating existing indices.
value
- The object to write.Exception
void delete(Class<?> type, Object naturalKey) throws Exception
type
- The object's type.naturalKey
- The object's "natural key", which uniquely identifies it. Null keys
are not allowed.java.util.NoSuchElementException
- If an element with the given key does not exist.Exception
<T> KVStoreView<T> view(Class<T> type) throws Exception
Exception
long count(Class<?> type) throws Exception
Exception
long count(Class<?> type, String index, Object indexedValue) throws Exception
Exception