Class MutableView
- All Implemented Interfaces:
KVStore,DeltaKVStore,Cloneable
- Direct Known Subclasses:
ReadWriteSpannerView
DeltaKVStore interface.
The amount of memory required scales in proportion to the number of distinct key ranges that are read or removed (if tracking reads) plus the total lengths of all keys and values written.
-
Constructor Summary
ConstructorsConstructorDescriptionMutableView(KVStore kv) Constructor.MutableView(KVStore kv, boolean trackReads) Constructor with optional read tracking.MutableView(KVStore kv, Reads reads, Writes writes) MutableView(KVStore kv, Writes writes) Constructor with no read tracking and caller-providedWrites. -
Method Summary
Modifier and TypeMethodDescriptionvoidadjustCounter(ByteData key, long amount) Adjust the counter at the given key by the given amount.voidApply all the givenMutationsto this instance.clone()Clone this instance.longdecodeCounter(ByteData bytes) Decode a counter value previously encoded byencodeCounter().voidPermanently disable read tracking and discard theReadsassociated with this instance.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.Get theKVStorethat underlies this instance.Iterate the key/value pairs in the specified range.getReads()Get theReadsassociated with this instance.Get theWritesassociated with this instance.booleanDetermine if this instance is read-only.voidSet the value associated with the given key.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.voidsetKVStore(KVStore kv) Swap out the underlyingKVStoreassociated with this instance.voidConfigure this instance as read-only.toString()voidwithoutReadTracking(boolean allowWrites, Runnable action) Temporarily disable read tracking in the current thread while performing the given action.Methods inherited from class io.permazen.kv.AbstractKVStore
getAtLeast, getAtMostMethods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface io.permazen.kv.KVStore
getAtLeast, getAtMost, getRange, getRange, removeRange
-
Constructor Details
-
MutableView
Constructor.The instance will use a new, empty
Readsinstance for read tracking.- Parameters:
kv- underlyingKVStore- Throws:
IllegalArgumentException- ifkvis null
-
MutableView
Constructor with optional read tracking.- Parameters:
kv- underlyingKVStoretrackReads- true to enable read tracking, or false for none- Throws:
IllegalArgumentException- ifkvis null
-
MutableView
Constructor with no read tracking and caller-providedWrites.- Parameters:
kv- underlyingKVStorewrites- recorded writes- Throws:
IllegalArgumentException- ifkvis nullIllegalArgumentException- ifwritesis null
-
MutableView
- Parameters:
kv- underlyingKVStorereads- recorded reads, or null for nonewrites- recorded writes- Throws:
IllegalArgumentException- ifkvis nullIllegalArgumentException- ifwritesis null
-
-
Method Details
-
getBaseKVStore
Description copied from interface:DeltaKVStoreGet theKVStorethat underlies this instance.Note that in some implementations the returned object and/or its contents may change over time, for example, if this instance gets "rebased" on a newer underlying
KVStore.- Specified by:
getBaseKVStorein interfaceDeltaKVStore- Returns:
- underlying
KVStore
-
setKVStore
Swap out the underlyingKVStoreassociated with this instance.Note: the new
KVStoreshould have a consistent encoding of counter values as the previousKVStore, otherwise a concurrent thread may read previously written counter values back incorrectly.- Parameters:
kv- new underlyingKVStore- Throws:
IllegalArgumentException- ifkvis null
-
getReads
Description copied from interface:DeltaKVStoreGet theReadsassociated with this instance.This includes all keys explicitly or implicitly read by calls to
get(),getAtLeast(),getAtMost(), andgetRange().The returned object is "live" and should only be accessed while synchronized on this instance.
The read tracking may be imprecise, as long as all actual reads are included. For example, if keys
10001,100002, and100003were read, the returnedReadsmay contain those three keys, or it may contain the entire range10001-10003, even though some keys in that range were not actually read in order to save memory. This optimization is acceptable as long as the keys that were actually read are always included.- Specified by:
getReadsin interfaceDeltaKVStore- Returns:
- reads recorded, or null if this instance is not configured to record reads or read tracking has
been permanently disabled via
DeltaKVStore.disableReadTracking()
-
getWrites
Description copied from interface:DeltaKVStoreGet theWritesassociated with this instance.The returned object is "live" and should only be accessed while synchronized on this instance.
- Specified by:
getWritesin interfaceDeltaKVStore- Returns:
- writes recorded
-
disableReadTracking
public void disableReadTracking()Description copied from interface:DeltaKVStorePermanently disable read tracking and discard theReadsassociated with this instance.This can be used to save some memory when read tracking information is no longer needed.
Does nothing if read tracking is already disabled.
- Specified by:
disableReadTrackingin interfaceDeltaKVStore
-
withoutReadTracking
Description copied from interface:DeltaKVStoreTemporarily disable read tracking in the current thread while performing the given action.If
allowWritesis false, then any write attempts byactionwill provoke anIllegalStateException.Read tracking is disabled only for the current thread, and it ends when this method returns.
- Specified by:
withoutReadTrackingin interfaceDeltaKVStore- Parameters:
allowWrites- whether to allow writes (usually this is a bad idea)action- the action to perform
-
isReadOnly
public boolean isReadOnly()Description copied from interface:DeltaKVStoreDetermine if this instance is read-only.- Specified by:
isReadOnlyin interfaceDeltaKVStore- Returns:
- true if this instance is read-only, otherwise false
-
setReadOnly
public void setReadOnly()Description copied from interface:DeltaKVStoreConfigure this instance as read-only.Any subsequent write attempts will result in an
IllegalStateException.This operation cannot be un-done.
- Specified by:
setReadOnlyin interfaceDeltaKVStore
-
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
-
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
-
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
-
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
-
apply
Description copied from interface:KVStoreApply 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. -
clone
-
toString
-