Interface IKeyValueStore
Provides a minimum set of operations for interacting with the key-value store
- Since:
- 11.0
- Author:
- Mihai Bob, Petru Galanton
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfaceAn iterator over keys in a key-value store. -
Method Summary
Modifier and TypeMethodDescriptionvoidRemoves the specifiedkeyvoidRemoves the specified keys.<T> Tdeserialize(byte[] bytes, Class<T> valueType) Deserializes the given byte array as an instance of the given class.booleanSets the expiration time for the given key.<T extends Serializable>
TReturns the value for the specifiedkeyReturns a builder for creating key patterns.Returns the string which is used to separate namespaces in the key-value store.byte[]This method reads the byte array stored at the given key and returns the raw byte array without deserializing it first.booleanChecks if the given key exists in the key-value store.keyIterator(IKeyPattern pattern) Provides a means to enumerate all keys in the key-value store, optionally matching a pattern.keys(IKeyPattern pattern) Returns all keys in the key-value store, optionally with a pattern to scope to a namespace.byte[]serialize(Serializable value) Serializes the given value, so it may be saved in raw byte-array for usingsetRaw(String, byte[]).voidset(String key, Serializable value) Saves aSerializableobject in the key-value store.voidset(String key, Serializable value, long timeout, TimeUnit unit) Saves aSerializableobject in the key-value store with a specified expiration time.voidSimilar toset(String, Serializable), except the byte array value is written in the key-value store as-is, without serializing it first.voidSimilar toset(String, Serializable, long, TimeUnit), except the byte array value is written in the key-value store as-is, without serializing it first.
-
Method Details
-
set
Saves aSerializableobject in the key-value store.If a value already exists for the given key, the value will be overwritten.
- Parameters:
key- - the key where the value will be saved, cannot benullvalue- -Serializableobject to persist in the key-value store- Throws:
IllegalArgumentException- if parameter key isnullKeyValueStoreException- when there is an error communicating with the key-value store- See Also:
-
set
Saves aSerializableobject in the key-value store with a specified expiration time.The key-value store will remove the data at the specified key in the specified time. If a value already exists for the given the key, it will be overwritten.
- Parameters:
key- - the key where the value will be saved under, cannot benullvalue- -Serializableobject to persist in the key-value storetimeout- - specified expiration timeunit- - time unit for the timeout parameter- Throws:
IllegalArgumentException- if parameterkeyorunitisnullKeyValueStoreException- when there is an error communicating with the key-value store- See Also:
-
setRaw
Similar toset(String, Serializable), except the byte array value is written in the key-value store as-is, without serializing it first.This method should be used for byte arrays to avoid an extra serialization. Values written with this method should be read with
getRaw(String).- Parameters:
key- - the key where the byte array will be saved, cannot benullvalue- - the byte array to persist in the key-value store- Throws:
IllegalArgumentException- if parameter key isnullKeyValueStoreException- when there is an error communicating with the key-value store- See Also:
-
setRaw
Similar toset(String, Serializable, long, TimeUnit), except the byte array value is written in the key-value store as-is, without serializing it first.This method should be used for byte arrays to avoid an extra serialization. Values written with this method should be read with
getRaw(String).- Parameters:
key- - the key where the value will be saved under, cannot benullvalue- - the byte array to persist in the key-value storetimeout- - specified expiration timeunit- - time unit for the timeout parameter- Throws:
IllegalArgumentException- if parameterkeyorunitisnullKeyValueStoreException- when there is an error communicating with the key-value store- See Also:
-
get
Returns the value for the specifiedkey- Type Parameters:
T- the return type which extendsSerializable- Parameters:
key- - the key from where to retrieve the datavalueType- - the Java type of the value- Returns:
- saved value or
nullif nothing was found - Throws:
IllegalArgumentException- ifkeyisnull, or if thevalueis not assignment compatible withvalueTypeKeyValueStoreException- when there is an error communicating with the key-value store- See Also:
-
getRaw
This method reads the byte array stored at the given key and returns the raw byte array without deserializing it first.This method should be used to read raw byte arrays written with
setRaw(String, byte[])orsetRaw(String, byte[], long, TimeUnit). Values written withset(String, Serializable)orset(String, Serializable, long, TimeUnit)may also be read, in which case the client code is responsible to deserialize it.- Parameters:
key- - the key from where to retrieve the data- Returns:
- the saved byte array or
nullif nothing was found - Throws:
IllegalArgumentException- ifkeyisnullKeyValueStoreException- when there is an error communicating with the key-value store- See Also:
-
delete
Removes the specifiedkeyIf the given key does not exist in the key-value store, no exception is thrown.
- Parameters:
key- - the key to delete, cannot benull- Throws:
IllegalArgumentException- ifkeyisnullKeyValueStoreException- when there is an error communicating with the key-value store
-
deleteAll
Removes the specified keys.This method is conceptually equivalent to calling {
delete(String)multiple times. However, some implementations may provide a more efficient implementation of deleting multiple keys in a single call.- Parameters:
keys- - keys to be removed, cannot benull, but may be empty- Throws:
IllegalArgumentException- if parameterkeyis nullKeyValueStoreException- when there is an error communicating with the key-value store
-
hasKey
Checks if the given key exists in the key-value store.- Parameters:
key- - the key to check for existence, cannot benull- Returns:
- true if the key exists, otherwise false
- Throws:
IllegalArgumentException- if parameterkeyisnullKeyValueStoreException- when there is an error communicating with the key-value store
-
expire
Sets the expiration time for the given key.If the key previously did not have an expiration time, it will have the specified expiration time after this method returns. If the key previously had an expiration time it will be reset to the new expiration time.
- Parameters:
key- - the key which will have its expiration time changedtimeout- - the expiration timeunit- - the units for the expiration time- Returns:
- true if the given key already exists, otherwise false
- Throws:
IllegalArgumentException- if parameterkeyisnullKeyValueStoreException- when there is an error communicating with the key-value store
-
keyIterator
Provides a means to enumerate all keys in the key-value store, optionally matching a pattern.The return value is an iterator, as some implementations may choose to not load all keys in the key-value store all at once. NOTE: The iterator must be closed after it is no longer needed.
This interface does not mandate that implementations provide any guarantees related to modifications in the key-value store during iteration. For example:
- keys added after iteration has started may or may not be returned by the iterator
- keys returned by the iterator may be deleted before iteration has completed
Since keys may be fetched up-front or incrementally, the
hasNext()andnext()methods may also throwKeyValueStoreException. Theremove()method may optionally be supported, but this interface does not require implementations to support it.Since the iterator may internally hold a connection to the key-value store, the iterator must be closed once iteration has completed and the iterator is no longer needed.
- Parameters:
pattern- the pattern the returned keys should match, ornullif all keys should be returned- Returns:
- an iterator over all matching keys in the key-value store
- Throws:
KeyValueStoreException- when there is an error communicating with the key-value store
-
keys
Returns all keys in the key-value store, optionally with a pattern to scope to a namespace.Only use this method when you need to retrieve ALL keys (within a namespace). When you want a subset, use the
keyIterator(IKeyPattern)method instead.Warning: this method can greatly affect performance when ran against large databases. Use it with caution.
- Parameters:
pattern- the pattern the scope to a namespace, ornullif all keys should be returned- Returns:
- A set of keys in the key-value store, never null
-
getNamespaceSeparator
String getNamespaceSeparator()Returns the string which is used to separate namespaces in the key-value store. If the key-value store does not support namespaces then this method returns an empty string.- Returns:
- the namespace separator
-
getKeyPatternBuilder
IKeyPatternBuilder getKeyPatternBuilder()Returns a builder for creating key patterns.- Returns:
- a key pattern builder.
-
serialize
Serializes the given value, so it may be saved in raw byte-array for usingsetRaw(String, byte[]).- Parameters:
value- the value to be serialized- Returns:
- the serialized value
- Throws:
KeyValueStoreException- if a serialization error occurs
-
deserialize
Deserializes the given byte array as an instance of the given class.- Type Parameters:
T- the return type- Parameters:
bytes- the bytes to deserializevalueType- the expected type- Returns:
- the deserialized value
- Throws:
IllegalArgumentException- if the deserialized value is not assignment compatible withvalueTypeKeyValueStoreException- if a deserialization error occurs
-