Interface KeyValueStorage
-
- All Superinterfaces:
java.lang.AutoCloseable
,java.io.Closeable
public interface KeyValueStorage extends java.io.Closeable
Responsible for storing values against keys.Behaviour expected with regard to key to value mapping is that of a map, one key maps to one value, when a new value is added with an existing key, that key now points at the new value.
All keys and values must be non-null.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description void
clear()
Deletes all keys and values from the storage.boolean
containsKey(byte[] key)
Whether the key-value storage contains the given key.java.util.Optional<byte[]>
get(byte[] key)
Retrieves the value associated with a given key.java.util.Set<byte[]>
getAllKeysThat(java.util.function.Predicate<byte[]> returnCondition)
Performs an evaluation against each key in the store, returning the set of entries that pass.KeyValueStorageTransaction
startTransaction()
Begins a fresh transaction, for sequencing operations for later atomic execution.java.util.stream.Stream<byte[]>
streamKeys()
Returns a stream of all keys.boolean
tryDelete(byte[] key)
Delete the value corresponding to the given key if a write lock can be instantly acquired on the underlying storage.
-
-
-
Method Detail
-
clear
void clear() throws StorageException
Deletes all keys and values from the storage.- Throws:
StorageException
- problem encountered when attempting to clear storage.
-
containsKey
boolean containsKey(byte[] key) throws StorageException
Whether the key-value storage contains the given key.- Parameters:
key
- a key that might be contained in the key-value storage.- Returns:
true
when the given key is present in keyset,false
otherwise.- Throws:
StorageException
- problem encountered when interacting with the key set.
-
get
java.util.Optional<byte[]> get(byte[] key) throws StorageException
Retrieves the value associated with a given key.- Parameters:
key
- whose associated value is being retrieved.- Returns:
- an
Optional
containing the value associated with the specified key, otherwise empty. - Throws:
StorageException
- problem encountered during the retrieval attempt.
-
streamKeys
java.util.stream.Stream<byte[]> streamKeys() throws StorageException
Returns a stream of all keys.- Returns:
- A stream of all keys in storage.
- Throws:
StorageException
- problem encountered during the retrieval attempt.
-
tryDelete
boolean tryDelete(byte[] key) throws StorageException
Delete the value corresponding to the given key if a write lock can be instantly acquired on the underlying storage. Do nothing otherwise.- Parameters:
key
- The key to delete.- Returns:
- false if the lock on the underlying storage could not be instantly acquired, true otherwise
- Throws:
StorageException
- any problem encountered during the deletion attempt.
-
getAllKeysThat
java.util.Set<byte[]> getAllKeysThat(java.util.function.Predicate<byte[]> returnCondition)
Performs an evaluation against each key in the store, returning the set of entries that pass.- Parameters:
returnCondition
- predicate to evaluate each key against, unless the result isnull
, the key is added to the returned list of keys.- Returns:
- the set of keys that pass the condition.
-
startTransaction
KeyValueStorageTransaction startTransaction() throws StorageException
Begins a fresh transaction, for sequencing operations for later atomic execution.- Returns:
- transaciton to sequence key-value operations.
- Throws:
StorageException
- problem encountered when starting a new transaction.
-
-