Class AbstractKVNavigableSet<E>
- Type Parameters:
E
- element type
- All Implemented Interfaces:
Iterable<E>
,Collection<E>
,NavigableSet<E>
,Set<E>
,SortedSet<E>
- Direct Known Subclasses:
KVNavigableSet
NavigableSet
support superclass for sets backed by elements encoded as byte[]
keys in a KVStore
and whose sort order is consistent with their byte[]
key encoding.
There must be an equivalence between elements and byte[]
key encodings (i.e., there must be
only one valid encoding per set element). The values in the KVStore
are ignored.
Subclass Methods
Subclasses must implement the encode() and decode()
methods to convert elements to/from byte[]
keys (associated values are ignored), and
createSubSet()
to allow creating reversed and restricted range sub-sets.
Subclasses must also implement comparator()
, and the resulting sort order must be consistent with
the sort order of the encoded byte[]
keys (possibly reversed
).
This class provides a read-only implementation; for a mutable implementation, subclasses should also implement
add()
(if appropriate), remove()
, and AbstractCollection.clear()
; note, these methods must verify
the key isVisible()
before making any changes.
Additional subclass notes:
iterator()
returns anIterator
whoseIterator.remove()
method delegates to this instance'sremove()
method.
Prefix Mode
Instances support a "prefix mode" where the byte[]
keys may have arbitrary trailing garbage, which is ignored,
and so by definition no key can be a prefix of any other key. The length of the prefix is determined implicitly by the
number of bytes produced by encode()
or consumed by decode()
.
When not in prefix mode, decode()
must consume the entire key to preserve correct semantics.
Key Restrictions
Instances are configured with an (optional) KeyRange
; when range restriction is in
effect, this key range corresponds to the bounds.
Instances also support filtering visible values using a KeyFilter
; see filterKeys()
.
To be isVisible(byte[]) in the set, keys must both be in the KeyRange
and pass the KeyFilter
.
Concurrent Modifications
This implementation never throws ConcurrentModificationException
; instead, iterators always
see the most up-to-date state of the associated KVStore
.
- See Also:
-
Field Summary
Modifier and TypeFieldDescriptionprotected final KeyFilter
Key filter, or null if all keys in the key range should be visible.protected final KeyRange
Key range, or null for the entire range.protected final KVStore
The underlyingKVStore
.protected final boolean
Whether we are in "prefix" mode.protected final boolean
Whether the ordering of this instance is reversed.Fields inherited from class io.permazen.util.AbstractNavigableSet
bounds
-
Constructor Summary
ModifierConstructorDescriptionprotected
AbstractKVNavigableSet
(KVStore kv, boolean prefixMode) Convenience constructor for when there are no range restrictions.protected
AbstractKVNavigableSet
(KVStore kv, boolean prefixMode, boolean reversed, KeyRange keyRange, KeyFilter keyFilter, Bounds<E> bounds) Internal constructor.protected
AbstractKVNavigableSet
(KVStore kv, boolean prefixMode, byte[] prefix) Convenience constructor for when the range of visibleKVStore
keys is all keys sharing a givenbyte[]
prefix.protected
AbstractKVNavigableSet
(KVStore kv, boolean prefixMode, KeyRange keyRange) Primary constructor. -
Method Summary
Modifier and TypeMethodDescriptionboolean
protected abstract NavigableSet<E>
createSubSet
(boolean newReversed, KeyRange newKeyRange, KeyFilter newKeyFilter, Bounds<E> newBounds) Create a (possibly reversed) view of this instance with (possibly) tighter lower and/or upper bounds and the givenKeyFilter
, if any.protected final NavigableSet<E>
createSubSet
(boolean reverse, Bounds<E> newBounds) Create a (possibly reversed) view of this instance with (possibly) tighter lower and/or upper bounds.protected abstract E
decode
(ByteReader reader) Decode an element from abyte[]
key.protected abstract void
encode
(ByteWriter writer, Object obj) Encode the given object into abyte[]
key.protected byte[]
encodeVisible
(Object obj, boolean fail) Encode the given object, if possible, and verify correspondingbyte[]
key is visible, otherwise return null or throw an exception.filterKeys
(KeyFilter keyFilter) Create a view of this instance with additional filtering applied to the underlyingbyte[]
encoded keys.first()
boolean
isEmpty()
Overridden inAbstractIterationSet
to minimize the use ofAbstractIterationSet.size()
.protected boolean
isVisible
(byte[] key) protected boolean
isWithinLowerBound
(E elem) Determine if the given element is within this instance's lower bound (if any).protected boolean
isWithinUpperBound
(E elem) Determine if the given element is within this instance's upper bound (if any).iterator()
last()
pollLast()
Methods inherited from class io.permazen.util.AbstractNavigableSet
buildSpliterator, ceiling, descendingIterator, descendingSet, floor, getBounds, getComparator, headSet, headSet, higher, lower, remove, searchAbove, searchBelow, subSet, subSet, tailSet, tailSet
Methods inherited from class io.permazen.util.AbstractIterationSet
equals, hashCode, size, spliterator, stream, toArray, toArray
Methods inherited from class java.util.AbstractSet
removeAll
Methods inherited from class java.util.AbstractCollection
add, addAll, clear, containsAll, retainAll, toString
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArray
Methods inherited from interface java.util.Set
add, addAll, clear, containsAll, equals, hashCode, removeAll, retainAll, size, toArray, toArray
Methods inherited from interface java.util.SortedSet
comparator, spliterator
-
Field Details
-
kv
The underlyingKVStore
. -
prefixMode
protected final boolean prefixModeWhether we are in "prefix" mode. -
reversed
protected final boolean reversedWhether the ordering of this instance is reversed. -
keyRange
Key range, or null for the entire range. -
keyFilter
Key filter, or null if all keys in the key range should be visible.
-
-
Constructor Details
-
AbstractKVNavigableSet
Convenience constructor for when there are no range restrictions.- Parameters:
kv
- underlyingKVStore
prefixMode
- whether to allow keys to have trailing garbage- Throws:
IllegalArgumentException
- ifkv
is null
-
AbstractKVNavigableSet
Convenience constructor for when the range of visibleKVStore
keys is all keys sharing a givenbyte[]
prefix.- Parameters:
kv
- underlyingKVStore
prefixMode
- whether to allow keys to have trailing garbageprefix
- prefix defining minimum and maximum keys- Throws:
IllegalArgumentException
- ifkv
is nullIllegalArgumentException
- ifprefix
is null or empty
-
AbstractKVNavigableSet
Primary constructor.- Parameters:
kv
- underlyingKVStore
prefixMode
- whether to allow keys to have trailing garbagekeyRange
- key range restriction, or null for none- Throws:
IllegalArgumentException
- ifkv
is null
-
AbstractKVNavigableSet
protected AbstractKVNavigableSet(KVStore kv, boolean prefixMode, boolean reversed, KeyRange keyRange, KeyFilter keyFilter, Bounds<E> bounds) Internal constructor. Used for creating sub-sets and reversed views.Note: if
bounds
are set, thenkeyRange
must exclude all keys outside of those bounds.- Parameters:
kv
- underlyingKVStore
prefixMode
- whether to allow keys to have trailing garbagereversed
- whether ordering is reversed (impliesbounds
are also inverted, but notkeyRange
)keyRange
- key range restriction, or null for nonekeyFilter
- key filter, or null for nonebounds
- range restriction- Throws:
IllegalArgumentException
- ifkv
orbounds
is null
-
-
Method Details
-
isEmpty
public boolean isEmpty()Description copied from class:AbstractIterationSet
Overridden inAbstractIterationSet
to minimize the use ofAbstractIterationSet.size()
.- Specified by:
isEmpty
in interfaceCollection<E>
- Specified by:
isEmpty
in interfaceSet<E>
- Overrides:
isEmpty
in classAbstractIterationSet<E>
-
first
-
last
-
pollFirst
- Specified by:
pollFirst
in interfaceNavigableSet<E>
- Overrides:
pollFirst
in classAbstractNavigableSet<E>
-
pollLast
- Specified by:
pollLast
in interfaceNavigableSet<E>
- Overrides:
pollLast
in classAbstractNavigableSet<E>
-
contains
- Specified by:
contains
in interfaceCollection<E>
- Specified by:
contains
in interfaceSet<E>
- Overrides:
contains
in classAbstractCollection<E>
-
iterator
- Specified by:
iterator
in interfaceCollection<E>
- Specified by:
iterator
in interfaceIterable<E>
- Specified by:
iterator
in interfaceNavigableSet<E>
- Specified by:
iterator
in interfaceSet<E>
- Specified by:
iterator
in classAbstractIterationSet<E>
-
filterKeys
Create a view of this instance with additional filtering applied to the underlyingbyte[]
encoded keys. Any set element for which the corresponding key does not passkeyFilter
will be effectively hidden from view.The restrictions of the given
KeyFilter
will be added to any currentKeyFilter
restrictions on this instance. TheAbstractNavigableSet.bounds
associated with this instance will not change.- Parameters:
keyFilter
- additional key filtering to apply- Returns:
- filtered view of this instance
- Throws:
IllegalArgumentException
- ifkeyFilter
is null
-
isWithinLowerBound
Description copied from class:AbstractNavigableSet
Determine if the given element is within this instance's lower bound (if any).The implementation in
AbstractNavigableSet
returnsthis.bounds.isWithinLowerBound(this.comparator(), elem)
.- Overrides:
isWithinLowerBound
in classAbstractNavigableSet<E>
- Parameters:
elem
- set element- Returns:
- true if
elem
is within this instance's lower bound, or this instance has no lower bound
-
isWithinUpperBound
Description copied from class:AbstractNavigableSet
Determine if the given element is within this instance's upper bound (if any).The implementation in
AbstractNavigableSet
returnsthis.bounds.isWithinUpperBound(this.comparator(), elem)
.- Overrides:
isWithinUpperBound
in classAbstractNavigableSet<E>
- Parameters:
elem
- set element- Returns:
- true if
elem
is within this instance's upper bound, or this instance has no upper bound
-
createSubSet
Description copied from class:AbstractNavigableSet
Create a (possibly reversed) view of this instance with (possibly) tighter lower and/or upper bounds. ThenewBounds
are consistent with the new ordering (i.e., reversed relative to this instance's ordering ifreverse
is true) and have already been range-checked against this instance's current bounds.- Specified by:
createSubSet
in classAbstractNavigableSet<E>
- Parameters:
reverse
- whether the new set's ordering should be reversed relative to this instance's orderingnewBounds
- new bounds- Returns:
- restricted and/or reversed view of this instance
-
createSubSet
protected abstract NavigableSet<E> createSubSet(boolean newReversed, KeyRange newKeyRange, KeyFilter newKeyFilter, Bounds<E> newBounds) Create a (possibly reversed) view of this instance with (possibly) tighter lower and/or upper bounds and the givenKeyFilter
, if any. The bounds are consistent with the reversed ordering (i.e., reversed ifreverse
is true) and have already been range-checked against this instance's bounds.- Parameters:
newReversed
- whether the new set's ordering should be reversed (impliesnewBounds
are also inverted, but notkeyRange
); note: means "absolutely" reversed, not relative to this instancenewKeyRange
- new key range, or null for none; will be consistent withbounds
, if anynewKeyFilter
- new key filter, or null for nonenewBounds
- new bounds- Returns:
- restricted and/or filtered view of this instance
- Throws:
IllegalArgumentException
- ifnewBounds
is null
-
encode
Encode the given object into abyte[]
key. Note that this method must throwIllegalArgumentException
, notClassCastException
orNullPointerException
, ifobj
does not have the correct type or is an illegal null value.- Parameters:
writer
- output for encodedbyte[]
key corresponding toobj
obj
- set element- Throws:
IllegalArgumentException
- ifobj
is not of the required Java type supported by this setIllegalArgumentException
- ifobj
is null and this set does not support null elements
-
decode
Decode an element from abyte[]
key.If not in prefix mode, all of
reader
must be consumed; otherwise, the consumed portion is the prefix and any following keys with the same prefix are ignored.- Parameters:
reader
- input for encoded bytes- Returns:
- decoded set element
-
isVisible
protected boolean isVisible(byte[] key) Determine if the givenbyte[]
key is visible in this set according to the configuredKeyRange
and/orKeyFilter
, if any.- Parameters:
key
- key to test- Returns:
- true if key is visible
- Throws:
IllegalArgumentException
- ifkey
is null- See Also:
-
encodeVisible
Encode the given object, if possible, and verify correspondingbyte[]
key is visible, otherwise return null or throw an exception. Delegates toencode(ByteWriter, Object)
to attempt the actual encoding.- Parameters:
obj
- object to encode, possibly nullfail
- whether, ifobj
can't be encoded, to throw an exception (true) or return null (false)- Returns:
- encoed key for
obj
, or null iffail
is false andobj
has the wrong type or is out of bounds - Throws:
IllegalArgumentException
- iffail
is true andobj
has the wrong typeIllegalArgumentException
- iffail
is true and the resulting key is not visible
-