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[]
array keys in a KVStore.
The key sort order must be consistent with the corresponding key ByteData key encodings, i.e., unsigned lexicographical.
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 anIteratorwhoseIterator.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(io.permazen.util.ByteData) 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
FieldsModifier and TypeFieldDescriptionprotected final KeyFilterKey filter, or null if all keys in the key range should be visible.protected final KeyRangeKey range, or null for the entire range.protected final KVStoreThe underlyingKVStore.protected final booleanWhether we are in "prefix" mode.protected final booleanWhether the ordering of this instance is reversed.Fields inherited from class io.permazen.util.AbstractNavigableSet
bounds -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedAbstractKVNavigableSet(KVStore kv, boolean prefixMode) Convenience constructor for when there are no range restrictions.protectedAbstractKVNavigableSet(KVStore kv, boolean prefixMode, boolean reversed, KeyRange keyRange, KeyFilter keyFilter, Bounds<E> bounds) Internal constructor.protectedAbstractKVNavigableSet(KVStore kv, boolean prefixMode, KeyRange keyRange) Primary constructor.protectedAbstractKVNavigableSet(KVStore kv, boolean prefixMode, ByteData prefix) Convenience constructor for when the range of visibleKVStorekeys is all keys sharing a given prefix. -
Method Summary
Modifier and TypeMethodDescriptionbooleanprotected 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 Edecode(ByteData.Reader reader) Decode an element from abyte[]key.protected abstract voidencode(ByteData.Writer writer, Object obj) Encode the given object into abyte[]key.protected ByteDataencodeVisible(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()booleanisEmpty()Overridden inAbstractIterationSetto minimize the use ofAbstractIterationSet.size().protected booleanprotected booleanisWithinLowerBound(E elem) Determine if the given element is within this instance's lower bound (if any).protected booleanisWithinUpperBound(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, tailSetMethods inherited from class io.permazen.util.AbstractIterationSet
equals, hashCode, size, spliterator, stream, toArray, toArrayMethods inherited from class java.util.AbstractSet
removeAllMethods inherited from class java.util.AbstractCollection
add, addAll, clear, containsAll, retainAll, toStringMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArrayMethods inherited from interface java.util.Set
add, addAll, clear, containsAll, equals, hashCode, removeAll, retainAll, size, toArray, toArrayMethods 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- underlyingKVStoreprefixMode- whether to allow keys to have trailing garbage- Throws:
IllegalArgumentException- ifkvis null
-
AbstractKVNavigableSet
Convenience constructor for when the range of visibleKVStorekeys is all keys sharing a given prefix.- Parameters:
kv- underlyingKVStoreprefixMode- whether to allow keys to have trailing garbageprefix- prefix defining minimum and maximum keys- Throws:
IllegalArgumentException- ifkvis nullIllegalArgumentException- ifprefixis null or empty
-
AbstractKVNavigableSet
Primary constructor.- Parameters:
kv- underlyingKVStoreprefixMode- whether to allow keys to have trailing garbagekeyRange- key range restriction, or null for none- Throws:
IllegalArgumentException- ifkvis 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
boundsare set, thenkeyRangemust exclude all keys outside of those bounds.- Parameters:
kv- underlyingKVStoreprefixMode- whether to allow keys to have trailing garbagereversed- whether ordering is reversed (impliesboundsare also inverted, but notkeyRange)keyRange- key range restriction, or null for nonekeyFilter- key filter, or null for nonebounds- range restriction- Throws:
IllegalArgumentException- ifkvorboundsis null
-
-
Method Details
-
isEmpty
public boolean isEmpty()Description copied from class:AbstractIterationSetOverridden inAbstractIterationSetto minimize the use ofAbstractIterationSet.size().- Specified by:
isEmptyin interfaceCollection<E>- Specified by:
isEmptyin interfaceSet<E>- Overrides:
isEmptyin classAbstractIterationSet<E>
-
first
-
last
-
pollFirst
- Specified by:
pollFirstin interfaceNavigableSet<E>- Overrides:
pollFirstin classAbstractNavigableSet<E>
-
pollLast
- Specified by:
pollLastin interfaceNavigableSet<E>- Overrides:
pollLastin classAbstractNavigableSet<E>
-
contains
- Specified by:
containsin interfaceCollection<E>- Specified by:
containsin interfaceSet<E>- Overrides:
containsin classAbstractCollection<E>
-
iterator
- Specified by:
iteratorin interfaceCollection<E>- Specified by:
iteratorin interfaceIterable<E>- Specified by:
iteratorin interfaceNavigableSet<E>- Specified by:
iteratorin interfaceSet<E>- Specified by:
iteratorin 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 passkeyFilterwill be effectively hidden from view.The restrictions of the given
KeyFilterwill be added to any currentKeyFilterrestrictions on this instance. TheAbstractNavigableSet.boundsassociated with this instance will not change.- Parameters:
keyFilter- additional key filtering to apply- Returns:
- filtered view of this instance
- Throws:
IllegalArgumentException- ifkeyFilteris null
-
isWithinLowerBound
Description copied from class:AbstractNavigableSetDetermine if the given element is within this instance's lower bound (if any).The implementation in
AbstractNavigableSetreturnsthis.bounds.isWithinLowerBound(this.comparator(), elem).- Overrides:
isWithinLowerBoundin classAbstractNavigableSet<E>- Parameters:
elem- set element- Returns:
- true if
elemis within this instance's lower bound, or this instance has no lower bound
-
isWithinUpperBound
Description copied from class:AbstractNavigableSetDetermine if the given element is within this instance's upper bound (if any).The implementation in
AbstractNavigableSetreturnsthis.bounds.isWithinUpperBound(this.comparator(), elem).- Overrides:
isWithinUpperBoundin classAbstractNavigableSet<E>- Parameters:
elem- set element- Returns:
- true if
elemis within this instance's upper bound, or this instance has no upper bound
-
createSubSet
Description copied from class:AbstractNavigableSetCreate a (possibly reversed) view of this instance with (possibly) tighter lower and/or upper bounds. ThenewBoundsare consistent with the new ordering (i.e., reversed relative to this instance's ordering ifreverseis true) and have already been range-checked against this instance's current bounds.- Specified by:
createSubSetin 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 ifreverseis true) and have already been range-checked against this instance's bounds.- Parameters:
newReversed- whether the new set's ordering should be reversed (impliesnewBoundsare 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- ifnewBoundsis null
-
encode
Encode the given object into abyte[]key. Note that this method must throwIllegalArgumentException, notClassCastExceptionorNullPointerException, ifobjdoes not have the correct type or is an illegal null value.- Parameters:
writer- output for encodedbyte[]key corresponding toobjobj- set element- Throws:
IllegalArgumentException- ifobjis not of the required Java type supported by this setIllegalArgumentException- ifobjis null and this set does not support null elements
-
decode
Decode an element from abyte[]key.If not in prefix mode, all of
readermust 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
Determine if the givenbyte[]key is visible in this set according to the configuredKeyRangeand/orKeyFilter, if any.- Parameters:
key- key to test- Returns:
- true if key is visible
- Throws:
IllegalArgumentException- ifkeyis 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(ByteData.Writer, Object)to attempt the actual encoding.- Parameters:
obj- object to encode, possibly nullfail- whether, ifobjcan't be encoded, to throw an exception (true) or return null (false)- Returns:
- encoed key for
obj, or null iffailis false andobjhas the wrong type or is out of bounds - Throws:
IllegalArgumentException- iffailis true andobjhas the wrong typeIllegalArgumentException- iffailis true and the resulting key is not visible
-