Class MemoryAtomicKVStore
- All Implemented Interfaces:
KVStore
,AtomicKVStore
,Serializable
,Cloneable
KVStore
view of an underlying ConcurrentNavigableMap<byte[], byte[]>
whose keys are sorted lexicographically as unsigned bytes.
Implementaions are serializable if the underlying map is.
- See Also:
-
Field Summary
Fields inherited from class io.permazen.kv.util.MemoryKVStore
map
-
Constructor Summary
ConstructorDescriptionConvenience constructor.MemoryAtomicKVStore
(ConcurrentNavigableMap<byte[], byte[]> map) Primary constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Apply a set of mutations to this instance atomically.byte[]
get
(byte[] key) Get the value associated with the given key, if any.getAtLeast
(byte[] minKey, byte[] maxKey) Get the key/value pair having the smallest key greater than or equal to the given minimum, if any.getAtMost
(byte[] maxKey, byte[] minKey) Get the key/value pair having the largest key strictly less than the given maximum, if any.getRange
(byte[] minKey, byte[] maxKey, boolean reverse) Iterate the key/value pairs in the specified range.void
put
(byte[] key, byte[] value) Set the value associated with the given key.Create a read-only "snapshot" view of this instance equal to its current state.void
remove
(byte[] key) Remove the key/value pair with the given key, if it exists.void
removeRange
(byte[] minKey, byte[] maxKey) Remove all key/value pairs whose keys are in a given range.void
start()
Start this instance.void
stop()
Stop this instance.Methods inherited from class io.permazen.kv.util.MemoryKVStore
clone, getNavigableMap
Methods inherited from class io.permazen.kv.AbstractKVStore
adjustCounter, decodeCounter, encodeCounter
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface io.permazen.kv.mvcc.AtomicKVStore
apply
Methods inherited from interface io.permazen.kv.KVStore
adjustCounter, decodeCounter, encodeCounter, getRange, getRange, removeRange
-
Constructor Details
-
MemoryAtomicKVStore
public MemoryAtomicKVStore()Convenience constructor. Uses an internally constructedConcurrentSkipListMap
.Equivalent to:
MemoryAtomicKVStore(new ConcurrentSkipListMap<byte[], byte[]>(ByteUtil.COMPARATOR)
- See Also:
-
-
Method Details
-
get
public byte[] get(byte[] key) Description copied from interface:KVStore
Get the value associated with the given key, if any.Modifications to the returned
byte[]
array do not affect this instance.- Specified by:
get
in interfaceKVStore
- Overrides:
get
in classMemoryKVStore
- Parameters:
key
- key- Returns:
- value associated with key, or null if not found
-
getAtLeast
Description copied from interface:KVStore
Get the key/value pair having the smallest key greater than or equal to the given minimum, if any.An optional (exclusive) maximum key may also be specified; if
maxKey
is null, there is no upper bound; ifmaxKey <= minKey
, null is always returned.If keys starting with
0xff
are not supported by this instance, andminKey
starts with0xff
, then this method returns null.Modifications to the returned
byte[]
arrays do not affect this instance.- Specified by:
getAtLeast
in interfaceKVStore
- Overrides:
getAtLeast
in classMemoryKVStore
- Parameters:
minKey
- minimum key (inclusive), or null for no minimum (get the smallest key)maxKey
- maximum key (exclusive), or null for no maximum (no upper bound)- Returns:
- smallest key/value pair with
key >= minKey
andkey < maxKey
, or null if none exists
-
getAtMost
Description copied from interface:KVStore
Get the key/value pair having the largest key strictly less than the given maximum, if any.An optional (inclusive) minimum key may also be specified; if
minKey
is null, there is no lower bound (equivalent to a lower bound of the empty byte array); ifminKey >= maxKey
, null is always returned.If keys starting with
0xff
are not supported by this instance, andmaxKey
starts with0xff
, then this method behaves as ifmaxKey
were null.Modifications to the returned
byte[]
arrays do not affect this instance.- Specified by:
getAtMost
in interfaceKVStore
- Overrides:
getAtMost
in classMemoryKVStore
- Parameters:
maxKey
- maximum key (exclusive), or null for no maximum (get the largest key)minKey
- minimum key (inclusive), or null for no minimum (no lower bound)- Returns:
- largest key/value pair with
key < maxKey
andkey >= minKey
, or null if none exists
-
getRange
Description copied from interface:KVStore
Iterate the key/value pairs in the specified range. The returnedCloseableIterator
'sremove()
method must be supported and should have the same effect as invokingremove()
on the corresponding key.If keys starting with
0xff
are not supported by this instance, andminKey
starts with0xff
, then this method returns an empty iteration.If keys starting with
0xff
are not supported by this instance, andmaxKey
starts with0xff
, then this method behaves as ifmaxKey
were null.The returned
CloseableIterator
is weakly consistent (seejava.util.concurrent
). In short, the returnedCloseableIterator
must not throwConcurrentModificationException
; however, whether or not a "live"CloseableIterator
reflects any modifications made after its creation is implementation dependent. Implementations that do make post-creation updates visible in theCloseableIterator
, even if the update occurs after some delay, must preserve the order in which the modifications actually occurred.The returned
CloseableIterator
itself is not guaranteed to be thread safe; is should only be used in the thread that created it.Invokers of this method are encouraged to
close()
the returned iterators, though this is not required for correct behavior.Modifications to the returned
KVPair
key and valuebyte[]
arrays do not affect this instance.- Specified by:
getRange
in interfaceKVStore
- Overrides:
getRange
in classMemoryKVStore
- Parameters:
minKey
- minimum key (inclusive), or null for no minimum (start at the smallest key)maxKey
- maximum key (exclusive), or null for no maximum (end at the largest key)reverse
- true to return key/value pairs in reverse order (i.e., keys descending)- Returns:
- iteration of key/value pairs in the range
minKey
(inclusive) tomaxKey
(exclusive)
-
put
public void put(byte[] key, byte[] value) Description copied from interface:KVStore
Set the value associated with the given key.- Specified by:
put
in interfaceKVStore
- Overrides:
put
in classMemoryKVStore
- Parameters:
key
- keyvalue
- value
-
remove
public void remove(byte[] key) Description copied from interface:KVStore
Remove the key/value pair with the given key, if it exists.- Specified by:
remove
in interfaceKVStore
- Overrides:
remove
in classMemoryKVStore
- Parameters:
key
- key
-
removeRange
public void removeRange(byte[] minKey, byte[] maxKey) Description copied from interface:KVStore
Remove all key/value pairs whose keys are in a given range.The
minKey
must be less than or equal tomaxKey
; if they equal (and not null) then nothing happens; if they are both null then all entries are deleted.If keys starting with
0xff
are not supported by this instance, then:- If
minKey
starts with0xff
, then no change occurs - If
maxKey
starts with0xff
, then this method behaves as ifmaxKey
were null
- Specified by:
removeRange
in interfaceKVStore
- Overrides:
removeRange
in classMemoryKVStore
- Parameters:
minKey
- minimum key (inclusive), or null for no minimummaxKey
- maximum key (exclusive), or null for no maximum
- If
-
start
public void start()Description copied from interface:AtomicKVStore
Start this instance. This method must be called prior to creating any transactions.This method is idempotent: if this instance is already started, nothing happens.
Whether an instance that has been started and stopped can be restarted is implementation-dependent.
- Specified by:
start
in interfaceAtomicKVStore
-
stop
public void stop()Description copied from interface:AtomicKVStore
Stop this instance.Any open
AtomicKVStore.readOnlySnapshot()
's should beclose()
'd before invoking this method; the behavior of those that are not is undefined.This method is idempotent: if this instance has not been started, or is already stopped, nothing happens.
- Specified by:
stop
in interfaceAtomicKVStore
-
readOnlySnapshot
Description copied from interface:AtomicKVStore
Create a read-only "snapshot" view of this instance equal to its current state.The returned
CloseableKVStore
should be treated as read-only. It may not actually be read-only, but if it's not, then any changes should have no effect on this instance. The returnedCloseableKVStore
must be completely independent from this instance (subsequent changes to either one do not affect the other).The returned
CloseableKVStore
should be promplyclose()
'd when no longer needed to release any underlying resources.- Specified by:
readOnlySnapshot
in interfaceAtomicKVStore
- Returns:
- read-only, snapshot view of this instance
-
apply
Description copied from interface:AtomicKVStore
Apply a set of mutations to this instance atomically.If this method returns normally, all of the given mutations will have been applied. If this method returns abnormally, then none of the given mutations will have been applied.
In any case, other threads observing this instance will never see a partial application of the given mutations.
This method is required to apply the mutations in this order: removes, puts, adjusts.
If
sync
is true, the implementation must durably persist the changes before returning.- Specified by:
apply
in interfaceAtomicKVStore
- Parameters:
mutations
- the mutations to applysync
- if true, caller requires that the changes be durably persisted
-