public class TSafeKeyValueStore<K extends util.object.Identifiable,V> extends java.lang.Object implements KeyValueStore<K,V>
KeyValueStore
to make it thread safe.
This class uses lock striping to serialise operations on the same key while
at the same time allowing operations on different keys to run in parallel.Constructor and Description |
---|
TSafeKeyValueStore(KeyValueStore<K,V> target,
PositiveN numberOfStripes)
Creates a new instance to add thread-safety to the underlying
KeyValueStore . |
Modifier and Type | Method and Description |
---|---|
protected java.util.concurrent.locks.Lock |
lookupLock(K key) |
void |
modify(K key,
java.util.function.Function<V,V> operation)
Updates the value associated to the specified key.
|
void |
put(K key,
V value)
Maps a key to a value.
|
void |
remove(K key)
Deletes the mapping identified by the specified key.
|
protected void |
withLock(K key,
java.lang.Runnable action) |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
get
public TSafeKeyValueStore(KeyValueStore<K,V> target, PositiveN numberOfStripes)
KeyValueStore
.target
- the underlying object providing the functionality.numberOfStripes
- how many locks to use for striping.java.lang.NullPointerException
- if any argument is null
.protected java.util.concurrent.locks.Lock lookupLock(K key)
protected void withLock(K key, java.lang.Runnable action)
public void put(K key, V value)
KeyValueStore
put
in interface KeyValueStore<K extends util.object.Identifiable,V>
key
- identifies the mapping.value
- the value to store.public void modify(K key, java.util.function.Function<V,V> operation)
KeyValueStore
modify
in interface KeyValueStore<K extends util.object.Identifiable,V>
key
- identifies the mapping.operation
- is given the current value, does something with it and
then returns the new value.public void remove(K key)
KeyValueStore
remove
in interface KeyValueStore<K extends util.object.Identifiable,V>
key
- identifies the mapping.