Interface KVStore
- All Known Subinterfaces:
AtomicKVStore,CloseableKVStore,DeltaKVStore,KVTransaction
- All Known Implementing Classes:
AbstractKVStore,ArrayKVStore,ArrayKVTransaction,AtomicArrayKVStore,AtomicKVDatabase,BerkeleyKVTransaction,BranchedKVTransaction,ByteArrayLMDBKVStore,ByteArrayLMDBKVTransaction,CachingKVStore,CachingKVTransaction,CloseableForwardingKVStore,FallbackKVTransaction,ForwardingKVStore,FoundationKVStore,FoundationKVTransaction,LevelDBAtomicKVStore,LevelDBKVStore,LevelDBKVTransaction,LMDBKVStore,LMDBKVTransaction,MemoryAtomicKVStore,MemoryKVStore,MutableView,MVMapKVStore,MVMapSnapshot,MVStoreAtomicKVStore,MVStoreKVTransaction,PrefixKVStore,PrefixKVTransaction,RaftKVTransaction,ReadOnlySpannerView,ReadWriteSpannerView,SimpleKVTransaction,SnapshotKVTransaction,SnapshotLevelDBKVStore,SpannerKVTransaction,SQLKVTransaction,UnmodifiableKVStore,XMLKVTransaction,XodusKVStore,XodusKVTransaction
Implementations are not required to support accessing keys that start with 0xff,
and if not may throw IllegalArgumentException if such keys are accessed.
Thread Safety
Instances must be thread safe, in the sense that multi-threaded operations never lead to a behavior
that is inconsisitent with some consistent total ordering of those operations. So for example if
thread A invokes removeRange() while thread B does a put to some
key in the range, then afterwards either the range is empty or it contains only the key, but in
any case no other outcome is possible.
With respect to thread safety, the set of possible "operations" includes accessing the CloseableIterator
returned by getRange(); see getRange() for details.
Lock-free Counters
Implementations are encouraged to include support for encoding a 64 bit counter in a key/value pair such that the counter
can be efficiently adjusted by concurrent transactions without conflict.
In practice this means no locking is required to increment or decrement the counter by some amount, as long as
it's not necessary to actually directly read or write the counter value in the same transaction.
Whether counter adjustments are actually lock-free is implementation dependent, however, the counter methods
encodeCounter(), decodeCounter(), and adjustCounter()
must function correctly as specified in all cases.
How counters are encoded is specific to the implementation. Clients needing to read or write counter values directly
should use decodeCounter() and encodeCounter(), respectively.
Counters are removed using the normal methods (i.e., remove() and removeRange()).
-
Method Summary
Modifier and TypeMethodDescriptionvoidadjustCounter(ByteData key, long amount) Adjust the counter at the given key by the given amount.default voidApply all the givenMutationsto this instance.longdecodeCounter(ByteData value) Decode a counter value previously encoded byencodeCounter().encodeCounter(long value) Encode a counter value into abyte[]value suitable for use withdecodeCounter()and/oradjustCounter().Get the value associated with the given key, if any.getAtLeast(ByteData minKey, ByteData maxKey) Get the key/value pair having the smallest key greater than or equal to the given minimum, if any.Get the key/value pair having the largest key strictly less than the given maximum, if any.default CloseableIterator<KVPair>Iterate the key/value pairs in the specified range in the forward direction.default CloseableIterator<KVPair>Iterate the key/value pairs in the specified range in the forward direction.Iterate the key/value pairs in the specified range.voidSet the value associated with the given key.voidRemove the key/value pair with the given key, if it exists.default voidremoveRange(KeyRange range) Remove all key/value pairs whose keys are in a given range.voidremoveRange(ByteData minKey, ByteData maxKey) Remove all key/value pairs whose keys are in a given range.
-
Method Details
-
get
Get the value associated with the given key, if any.- Parameters:
key- key- Returns:
- value associated with key, or null if not found
- Throws:
IllegalArgumentException- ifkeystarts with0xffand such keys are not supportedStaleKVTransactionException- if an underlying transaction is no longer usableRetryKVTransactionException- if an underlying transaction must be retried and is no longer usableNullPointerException- ifkeyis null
-
getAtLeast
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
maxKeyis null, there is no upper bound; ifmaxKey <= minKey, null is always returned.If keys starting with
0xffare not supported by this instance, andminKeystarts with0xff, then this method returns null.- 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 >= minKeyandkey < maxKey, or null if none exists - Throws:
StaleKVTransactionException- if an underlying transaction is no longer usableRetryKVTransactionException- if an underlying transaction must be retried and is no longer usable
-
getAtMost
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
minKeyis 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
0xffare not supported by this instance, andmaxKeystarts with0xff, then this method behaves as ifmaxKeywere null.- 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 < maxKeyandkey >= minKey, or null if none exists - Throws:
StaleKVTransactionException- if an underlying transaction is no longer usableRetryKVTransactionException- if an underlying transaction must be retried and is no longer usable
-
getRange
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
0xffare not supported by this instance, andminKeystarts with0xff, then this method returns an empty iteration.If keys starting with
0xffare not supported by this instance, andmaxKeystarts with0xff, then this method behaves as ifmaxKeywere null.The returned
CloseableIteratoris weakly consistent (seejava.util.concurrent). In short, the returnedCloseableIteratormust not throwConcurrentModificationException; however, whether or not a "live"CloseableIteratorreflects 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
CloseableIteratoritself 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.- 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) - Throws:
IllegalArgumentException- ifminKey > maxKeyStaleKVTransactionException- if an underlying transaction is no longer usableRetryKVTransactionException- if an underlying transaction must be retried and is no longer usable
-
getRange
Iterate the key/value pairs in the specified range in the forward direction.This is a convenience method, equivalent to:
getRange(minKey, maxKey, false).- 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)- Returns:
- iteration of key/value pairs in the range
minKey(inclusive) tomaxKey(exclusive) - Throws:
IllegalArgumentException- ifminKey > maxKeyStaleKVTransactionException- if an underlying transaction is no longer usableRetryKVTransactionException- if an underlying transaction must be retried and is no longer usable
-
getRange
Iterate the key/value pairs in the specified range in the forward direction.This is a convenience method, equivalent to:
getRange(range.getMin(), range.getMax(), false).- Parameters:
range- range of keys to iterate- Returns:
- iteration of key/value pairs in
range - Throws:
StaleKVTransactionException- if an underlying transaction is no longer usableRetryKVTransactionException- if an underlying transaction must be retried and is no longer usableIllegalArgumentException- ifrangeis null
-
put
Set the value associated with the given key.- Parameters:
key- keyvalue- value- Throws:
IllegalArgumentException- ifkeystarts with0xffand such keys are not supportedStaleKVTransactionException- if an underlying transaction is no longer usableRetryKVTransactionException- if an underlying transaction must be retried and is no longer usableNullPointerException- ifkeyorvalueis null
-
remove
Remove the key/value pair with the given key, if it exists.- Parameters:
key- key- Throws:
IllegalArgumentException- ifkeystarts with0xffand such keys are not supportedStaleKVTransactionException- if an underlying transaction is no longer usableRetryKVTransactionException- if an underlying transaction must be retried and is no longer usableNullPointerException- ifkeyis null
-
removeRange
Remove all key/value pairs whose keys are in a given range.The
minKeymust 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
0xffare not supported by this instance, then:- If
minKeystarts with0xff, then no change occurs - If
maxKeystarts with0xff, then this method behaves as ifmaxKeywere null
- Parameters:
minKey- minimum key (inclusive), or null for no minimummaxKey- maximum key (exclusive), or null for no maximum- Throws:
IllegalArgumentException- ifminKey > maxKeyStaleKVTransactionException- if an underlying transaction is no longer usableRetryKVTransactionException- if an underlying transaction must be retried and is no longer usable
- If
-
removeRange
Remove all key/value pairs whose keys are in a given range.Equivalent to:
removeRange(range.getMin(), range.getMax()).- Parameters:
range- range to remove- Throws:
IllegalArgumentException- ifrangeis nullStaleKVTransactionException- if an underlying transaction is no longer usableRetryKVTransactionException- if an underlying transaction must be retried and is no longer usable
-
encodeCounter
Encode a counter value into abyte[]value suitable for use withdecodeCounter()and/oradjustCounter().- Parameters:
value- desired counter value- Returns:
- encoded counter value
- Throws:
StaleKVTransactionException- if an underlying transaction is no longer usableRetryKVTransactionException- if an underlying transaction must be retried and is no longer usable
-
decodeCounter
Decode a counter value previously encoded byencodeCounter().- Parameters:
value- encoded counter value- Returns:
- decoded counter value
- Throws:
IllegalArgumentException- ifvalueis not a valid counter valueStaleKVTransactionException- if an underlying transaction is no longer usableRetryKVTransactionException- if an underlying transaction must be retried and is no longer usableNullPointerException- ifvalueis null
-
adjustCounter
Adjust the counter at the given key by the given amount.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, orkey's value is not a valid counter encoding as would be acceptable todecodeCounter(), then how this operation affectskey's value is undefined.- Parameters:
key- keyamount- amount to adjust counter value by- Throws:
StaleKVTransactionException- if an underlying transaction is no longer usableRetryKVTransactionException- if an underlying transaction must be retried and is no longer usableNullPointerException- ifkeyis null
-
apply
Apply all the givenMutationsto this instance.Mutations are always to be applied in this order: removes, puts, counter adjustments.
The implementation in
KVStoresimply iterates over the individual changes and applies them viaremove()(for removals of a single key),removeRange(),put(), and/oradjustCounter(). Implementations that can process batch updates more efficiently are encouraged to override this method.Unlike
AtomicKVStore.apply(), this method is not required to apply the mutations atomically.- Parameters:
mutations- mutations to apply- Throws:
IllegalArgumentException- ifmutationsis nullUnsupportedOperationException- if this instance is immutable
-