Class AtomicKVDatabase
- All Implemented Interfaces:
KVStore,AtomicKVStore
AtomicKVStore view of a KVDatabase,
using individual transactions for each operation.
Warning: this class is only appropriate for use with KVDatabases that implement some form of MVCC;
KVDatabases that use locking will likely generate conflicts if readOnlySnapshot() is used concurrently
with other methods.
- See Also:
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidadjustCounter(ByteData key, long amount) Adjust the counter at the given key by the given amount.voidApply a set of mutations to this instance atomically.longdecodeCounter(ByteData bytes) 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.Iterate the key/value pairs in the specified range.voidSet the value associated with the given key.Create a read-only "snapshot" view of this instance equal to its current state.voidRemove the key/value pair with the given key, if it exists.voidremoveRange(ByteData minKey, ByteData maxKey) Remove all key/value pairs whose keys are in a given range.voidstart()Start this instance.voidstop()Stop this instance.toString()Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface io.permazen.kv.mvcc.AtomicKVStore
applyMethods inherited from interface io.permazen.kv.KVStore
getRange, getRange, removeRange
-
Field Details
-
kvdb
-
-
Constructor Details
-
AtomicKVDatabase
Constructor.- Parameters:
kvdb- underlying database- Throws:
IllegalArgumentException- ifkvdbis null
-
-
Method Details
-
get
Description copied from interface:KVStoreGet the value associated with the given key, if any.- Specified by:
getin interfaceKVStore- Overrides:
getin classAbstractKVStore- Parameters:
key- key- Returns:
- value associated with key, or null if not found
-
getAtLeast
Description copied from interface:KVStoreGet 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.- Specified by:
getAtLeastin interfaceKVStore- Overrides:
getAtLeastin classAbstractKVStore- 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
-
getAtMost
Description copied from interface:KVStoreGet 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.- Specified by:
getAtMostin interfaceKVStore- Overrides:
getAtMostin classAbstractKVStore- 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
-
getRange
Description copied from interface:KVStoreIterate 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.- Specified by:
getRangein interfaceKVStore- 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
Description copied from interface:KVStoreSet the value associated with the given key.- Specified by:
putin interfaceKVStore- Overrides:
putin classAbstractKVStore- Parameters:
key- keyvalue- value
-
remove
Description copied from interface:KVStoreRemove the key/value pair with the given key, if it exists.- Specified by:
removein interfaceKVStore- Overrides:
removein classAbstractKVStore- Parameters:
key- key
-
removeRange
Description copied from interface:KVStoreRemove 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
- Specified by:
removeRangein interfaceKVStore- Overrides:
removeRangein classAbstractKVStore- Parameters:
minKey- minimum key (inclusive), or null for no minimummaxKey- maximum key (exclusive), or null for no maximum
- If
-
adjustCounter
Description copied from interface:KVStoreAdjust 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.- Specified by:
adjustCounterin interfaceKVStore- Overrides:
adjustCounterin classAbstractKVStore- Parameters:
key- keyamount- amount to adjust counter value by
-
encodeCounter
Description copied from interface:KVStoreEncode a counter value into abyte[]value suitable for use withdecodeCounter()and/oradjustCounter().- Specified by:
encodeCounterin interfaceKVStore- Overrides:
encodeCounterin classAbstractKVStore- Parameters:
value- desired counter value- Returns:
- encoded counter value
-
decodeCounter
Description copied from interface:KVStoreDecode a counter value previously encoded byencodeCounter().- Specified by:
decodeCounterin interfaceKVStore- Overrides:
decodeCounterin classAbstractKVStore- Parameters:
bytes- encoded counter value- Returns:
- decoded counter value
-
start
@PostConstruct public void start()Description copied from interface:AtomicKVStoreStart 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:
startin interfaceAtomicKVStore
-
stop
@PreDestroy public void stop()Description copied from interface:AtomicKVStoreStop 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:
stopin interfaceAtomicKVStore
-
readOnlySnapshot
Description copied from interface:AtomicKVStoreCreate a read-only "snapshot" view of this instance equal to its current state.The returned
CloseableKVStoreshould 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 returnedCloseableKVStoremust be completely independent from this instance (subsequent changes to either one do not affect the other).The returned
CloseableKVStoreshould be promplyclose()'d when no longer needed to release any underlying resources.- Specified by:
readOnlySnapshotin interfaceAtomicKVStore- Returns:
- read-only, snapshot view of this instance
-
apply
Description copied from interface:AtomicKVStoreApply 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
syncis true, the implementation must durably persist the changes before returning.- Specified by:
applyin interfaceAtomicKVStore- Parameters:
mutations- the mutations to applysync- if true, caller requires that the changes be durably persisted
-
toString
-