public abstract class AbstractKVStore extends Object implements KVStore
KVStore
implementations.
This class provides a partial implementation via the following methods:
get()
implementation based on getAtLeast()
getAtLeast()
and getAtMost()
implementations based on
getRange()
.remove()
implementation that delegates to removeRange()
.removeRange()
implementation that delegates to getRange()
,
iterating through the range of keys and removing them one-by-one via Iterator.remove()
.encodeCounter()
, encodeCounter()
, and
adjustCounter()
implementations using normal reads and writes
of values in big-endian encoding (does not provide any lock-free behavior).put()
implementation throwing UnsupportedOperationException
Therefore, a read-only KVStore
implementation is possible simply by implementing KVStore.getRange(byte[], byte[], boolean)
.
KVPairIterator
Modifier | Constructor and Description |
---|---|
protected |
AbstractKVStore() |
Modifier and Type | Method and Description |
---|---|
void |
adjustCounter(byte[] key,
long amount)
Adjust the counter at the given key by the given amount.
|
long |
decodeCounter(byte[] value)
Decode a counter value previously encoded by
encodeCounter() . |
byte[] |
encodeCounter(long value)
Encode a counter value into a
byte[] value suitable for use with decodeCounter()
and/or adjustCounter() . |
byte[] |
get(byte[] key)
Get the value associated with the given key, if any.
|
KVPair |
getAtLeast(byte[] minKey,
byte[] maxKey)
Get the key/value pair having the smallest key greater than or equal to the given minimum, if any.
|
KVPair |
getAtMost(byte[] maxKey,
byte[] minKey)
Get the key/value pair having the largest key strictly less than the given maximum, if any.
|
void |
put(byte[] key,
byte[] value)
Set the value associated with the given key.
|
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.
|
public byte[] get(byte[] key)
KVStore
Modifications to the returned byte[]
array do not affect this instance.
public KVPair getAtLeast(byte[] minKey, byte[] maxKey)
KVStore
An optional (exclusive) maximum key may also be specified; if maxKey
is null, there is no upper bound;
if maxKey <= minKey
, null is always returned.
If keys starting with 0xff
are not supported by this instance, and minKey
starts with 0xff
,
then this method returns null.
Modifications to the returned byte[]
arrays do not affect this instance.
getAtLeast
in interface KVStore
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)key >= minKey
and key < maxKey
, or null if none existspublic KVPair getAtMost(byte[] maxKey, byte[] minKey)
KVStore
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); if minKey >= maxKey
, null is always returned.
If keys starting with 0xff
are not supported by this instance, and maxKey
starts with 0xff
,
then this method behaves as if maxKey
were null.
Modifications to the returned byte[]
arrays do not affect this instance.
getAtMost
in interface KVStore
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)key < maxKey
and key >= minKey
, or null if none existspublic void put(byte[] key, byte[] value)
KVStore
public void remove(byte[] key)
KVStore
public void removeRange(byte[] minKey, byte[] maxKey)
KVStore
The minKey
must be less than or equal to maxKey
; 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:
minKey
starts with 0xff
, then no change occursmaxKey
starts with 0xff
, then this method behaves as if maxKey
were nullremoveRange
in interface KVStore
minKey
- minimum key (inclusive), or null for no minimummaxKey
- maximum key (exclusive), or null for no maximumpublic byte[] encodeCounter(long value)
KVStore
byte[]
value suitable for use with decodeCounter()
and/or adjustCounter()
.encodeCounter
in interface KVStore
value
- desired counter valuepublic long decodeCounter(byte[] value)
KVStore
encodeCounter()
.decodeCounter
in interface KVStore
value
- encoded counter valuepublic void adjustCounter(byte[] key, long amount)
KVStore
Ideally this operation should behave in a lock-free manner, so that concurrent transactions can invoke it without conflict. However, when lock-free behavior occurs (if at all) depends on the implementation.
If there is no value associated with key
, or key
's value is not a valid counter encoding as
would be acceptable to decodeCounter()
, then how this operation affects key
's
value is undefined.
adjustCounter
in interface KVStore
key
- keyamount
- amount to adjust counter value byCopyright © 2019. All rights reserved.