public class KVPairIterator extends Object implements CloseableIterator<KVPair>
Iterator
that iterates over all key/value pairs in a KVStore
within a range of keys,
without using the KVStore.getRange()
method. Therefore, it can be used to implement
KVStore.getRange()
in KVStore
implementations that don't natively support iteration.
Instances support forward or 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 and Description |
---|
KVPairIterator(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.
|
Modifier and Type | Method and Description |
---|---|
void |
close() |
KeyFilter |
getKeyFilter()
Get the
KeyFilter instance used to filter visible keys, if any. |
KeyRange |
getKeyRange()
Get the
KeyRange instance used to restrict the range of visible keys, if any. |
KVStore |
getKVStore()
Get the
KVStore associated with this instance. |
boolean |
hasNext() |
boolean |
isReverse()
Determine if this instance is going forward or backward.
|
boolean |
isVisible(byte[] key)
Determine if the given key would be visible in this instance.
|
KVPair |
next() |
void |
remove() |
void |
setNextTarget(byte[] targetKey)
Reposition this instance by setting the next "target" key.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
wrap, wrap
forEachRemaining
public KVPairIterator(KVStore kv, KeyRange keyRange)
KVPairIterator(kv, keyRange, null, false)
kv
- underlying KVStore
keyRange
- range restriction on visible keys, or null for noneIllegalArgumentException
- if kv
is nullpublic KVPairIterator(KVStore kv, byte[] prefix)
KVPairIterator(kv, prefix, false)
kv
- underlying KVStore
prefix
- range prefixIllegalArgumentException
- if any parameter is nullpublic KVPairIterator(KVStore kv, byte[] prefix, boolean reverse)
KVPairIterator(kv, KeyRange.forPrefix(prefix), null, reverse)
kv
- underlying KVStore
prefix
- range prefixreverse
- true to iterate in a reverse direction, false to iterate in a forward directionIllegalArgumentException
- if any parameter is nullpublic KVPairIterator(KVStore kv, KeyRange keyRange, KeyFilter keyFilter, boolean reverse)
kv
- underlying KVStore
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 directionIllegalArgumentException
- if kv
is nullpublic KVStore getKVStore()
KVStore
associated with this instance.public KeyRange getKeyRange()
KeyRange
instance used to restrict the range of visible keys, if any.KeyRange
over which this iterator iterates, or null if it iterates over all keyspublic KeyFilter getKeyFilter()
KeyFilter
instance used to filter visible keys, if any.KeyFilter
in which all keys returned by this iterator must be contained, or null if keys are not filteredpublic boolean isReverse()
public boolean isVisible(byte[] key)
KeyRange
and/or KeyFilter
, if any.key
- to testIllegalArgumentException
- if key
is nullpublic void setNextTarget(byte[] targetKey)
The target key is the key we will use to find the next element via KVStore.getAtLeast()
or KVStore.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 still remove()
the previously
returned element even if you have invoked this method since invoking next()
.
A null targetKey
means to reposition this instance at the beginning of the iteration.
This instance's configured KeyRange
and KeyFilter
, if any, still apply: if targetKey
is not
visible
to this instance, the next visible key after targetKey
will be next in the iteration.
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 iterationpublic void close()
close
in interface CloseableIterator<KVPair>
close
in interface Closeable
close
in interface AutoCloseable
Copyright © 2022. All rights reserved.