Class KVPairIterator
- All Implemented Interfaces:
CloseableIterator<KVPair>,Closeable,AutoCloseable,Iterator<KVPair>
Iterator that iterates over all key/value pairs in a KVStore within a range of keys,
without using the KVStore.getRange() method.
This class can be used to implement KVStore.getRange() in KVStore implementations that
don't natively support iteration. Instances support forward and reverse iteration and
java.util.Iterator.remove().
The iteration is instead implemented using KVStore.getAtLeast(),
KVStore.getAtMost(), and KVStore.remove().
Repositioning
Instances support arbitrary repositioning via setNextTarget().
Key Restrictions
Instances are configured with an (optional) KeyRange that restricts the iteration to the specified key range.
Instances also support filtering visible values using a KeyFilter.
To appear in the iteration, keys must both be in the KeyRange and pass the KeyFilter, if any.
Concurrent Modification
Instances are thread safe, and always reflect the current state of the underlying KVStore,
even if it is mutated concurrently.
-
Constructor Summary
ConstructorsConstructorDescriptionKVPairIterator(KVStore kv, KeyRange keyRange) Convenience constructor for forward iteration over a specified range.KVPairIterator(KVStore kv, KeyRange keyRange, KeyFilter keyFilter, boolean reverse) Primary constructor.KVPairIterator(KVStore kv, ByteData prefix) Convenience constructor for forward iteration over all keys having a given prefix.KVPairIterator(KVStore kv, ByteData prefix, boolean reverse) Convenience constructor for iteration over all keys having a given prefix. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Get theKeyFilterinstance used to filter visible keys, if any.Get theKeyRangeinstance used to restrict the range of visible keys, if any.Get theKVStoreassociated with this instance.booleanhasNext()booleanDetermine if this instance is going forward or backward.booleanDetermine if the given key would be visible in this instance.next()voidremove()voidsetNextTarget(ByteData targetKey) Reposition this instance by setting the next "target" key.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface io.permazen.util.CloseableIterator
toStreamMethods inherited from interface java.util.Iterator
forEachRemaining
-
Constructor Details
-
KVPairIterator
Convenience constructor for forward iteration over a specified range. Equivalent to:KVPairIterator(kv, keyRange, null, false)
- Parameters:
kv- underlyingKVStorekeyRange- range restriction on visible keys, or null for none- Throws:
IllegalArgumentException- ifkvis null
-
KVPairIterator
Convenience constructor for forward iteration over all keys having a given prefix. Equivalent to:KVPairIterator(kv, prefix, false)
- Parameters:
kv- underlyingKVStoreprefix- range prefix- Throws:
IllegalArgumentException- if any parameter is null
-
KVPairIterator
Convenience constructor for iteration over all keys having a given prefix. Equivalent to:KVPairIterator(kv, KeyRange.forPrefix(prefix), null, reverse)
- Parameters:
kv- underlyingKVStoreprefix- range prefixreverse- true to iterate in a reverse direction, false to iterate in a forward direction- Throws:
IllegalArgumentException- if any parameter is null
-
KVPairIterator
Primary constructor.- Parameters:
kv- underlyingKVStorekeyRange- range restriction on visible keys, or null for nonekeyFilter- filter restriction on visible keys, or null for nonereverse- true to iterate in a reverse direction, false to iterate in a forward direction- Throws:
IllegalArgumentException- ifkvis null
-
-
Method Details
-
getKVStore
Get theKVStoreassociated with this instance.- Returns:
- this instance's transaction
-
getKeyRange
Get theKeyRangeinstance used to restrict the range of visible keys, if any.- Returns:
KeyRangeover which this iterator iterates, or null if it iterates over all keys
-
getKeyFilter
Get theKeyFilterinstance used to filter visible keys, if any.- Returns:
KeyFilterin which all keys returned by this iterator must be contained, or null if keys are not filtered
-
isReverse
public boolean isReverse()Determine if this instance is going forward or backward.- Returns:
- true if this instance is reversed
-
isVisible
Determine if the given key would be visible in this instance. Tests the key against the configuredKeyRangeand/orKeyFilter, if any.- Parameters:
key- to test- Returns:
- true if key is both in range and not filtered out
- Throws:
IllegalArgumentException- ifkeyis null
-
setNextTarget
Reposition this instance by setting the next "target" key.The target key is the key we will use to find the next element via
KVStore.getAtLeast(), orKVStore.getAtMost()if this is a reverse iterator. In the forward case, the target key is an inclusive lower bound on the next key, while in the reverse case it is an exclusive upper bound on the next key.This method may be used to reposition an interator during iteration or restart an iterator that has been exhausted. Invoking this method does not affect the behavior of
remove(), i.e., you can stillremove()the previously returned element even if you have invoked this method since invokingnext().A null
targetKeymeans to reposition this instance at the beginning of the iteration.This instance's configured
KeyRangeandKeyFilter, if any, still apply: iftargetKeyis notvisibleto this instance, the next visible key aftertargetKeywill be next in the iteration.- Parameters:
targetKey- next lower bound (exclusive) if going forward, or upper bound (exclusive) if going backward; or null to restart this instance at the beginning of its iteration
-
hasNext
public boolean hasNext() -
next
-
remove
public void remove() -
close
public void close()- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein interfaceCloseableIterator<KVPair>
-