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
ConstructorDescriptionKVPairIterator
(KVStore kv, byte[] prefix) Convenience constructor for forward iteration over all keys having a given prefix.KVPairIterator
(KVStore kv, byte[] prefix, boolean reverse) Convenience constructor for iteration over all keys having a given prefix.KVPairIterator
(KVStore kv, KeyRange keyRange) Convenience constructor for forward iteration over a specified range.KVPairIterator
(KVStore kv, KeyRange keyRange, KeyFilter keyFilter, boolean reverse) Primary constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Get theKeyFilter
instance used to filter visible keys, if any.Get theKeyRange
instance used to restrict the range of visible keys, if any.Get theKVStore
associated with this instance.boolean
hasNext()
boolean
Determine if this instance is going forward or backward.boolean
isVisible
(byte[] key) Determine if the given key would be visible in this instance.next()
void
remove()
void
setNextTarget
(byte[] 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, wait
Methods inherited from interface io.permazen.util.CloseableIterator
toStream
Methods 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
- underlyingKVStore
keyRange
- range restriction on visible keys, or null for none- Throws:
IllegalArgumentException
- ifkv
is null
-
KVPairIterator
Convenience constructor for forward iteration over all keys having a given prefix. Equivalent to:KVPairIterator(kv, prefix, false)
- Parameters:
kv
- underlyingKVStore
prefix
- 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
- underlyingKVStore
prefix
- 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
- underlyingKVStore
keyRange
- 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
- ifkv
is null
-
-
Method Details
-
getKVStore
Get theKVStore
associated with this instance.- Returns:
- this instance's transaction
-
getKeyRange
Get theKeyRange
instance used to restrict the range of visible keys, if any.- Returns:
KeyRange
over which this iterator iterates, or null if it iterates over all keys
-
getKeyFilter
Get theKeyFilter
instance used to filter visible keys, if any.- Returns:
KeyFilter
in 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
public boolean isVisible(byte[] key) Determine if the given key would be visible in this instance. Tests the key against the configuredKeyRange
and/orKeyFilter
, if any.- Parameters:
key
- to test- Returns:
- true if key is both in range and not filtered out
- Throws:
IllegalArgumentException
- ifkey
is null
-
setNextTarget
public void setNextTarget(byte[] targetKey) 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
targetKey
means to reposition this instance at the beginning of the iteration.This instance's configured
KeyRange
andKeyFilter
, if any, still apply: iftargetKey
is notvisible
to this instance, the next visible key aftertargetKey
will 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:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Specified by:
close
in interfaceCloseableIterator<KVPair>
-