Package com.apple.foundationdb.async
Class RankedSet
java.lang.Object
com.apple.foundationdb.async.RankedSet
RankedSet
supports the efficient retrieval of elements by their rank as
defined by lexicographic order.
Elements are added or removed from the set as byte-array keys.
The fundamental operations are:
- rank: the ordinal position of an element of the set (
rank(com.apple.foundationdb.ReadTransactionContext, byte[])
). - select: the element at a given ordinal position in the set (
getNth(com.apple.foundationdb.ReadTransactionContext, long)
).
The set is implemented as a skip-list with a specified number of levels. Level zero has one entry for each element. Coarser levels have a sampling of values, determined by bits the hash code of their key.
The skip-list is stored as key-value pairs within a given subspace, where the key is a tuple of the form [level, key]
and the value is the number of elements between this key and the previous key at the same level, encoded as a little-endian long.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
Configuration settings for aRankedSet
.static class
Builder forRankedSet.Config
.protected static class
static interface
Function to compute the hash used to determine which levels a key value splits on.protected static interface
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final RankedSet.Config
static final RankedSet.HashFunction
Hash using 32-bit CRC.static final RankedSet.Config
static final RankedSet.HashFunction
The default hash function to use.static final int
protected final Executor
static final RankedSet.HashFunction
Hash using the JDK's default byte array hash.static final int
static final RankedSet.HashFunction
Hash using a random number.protected final Subspace
-
Constructor Summary
ConstructorsConstructorDescriptionInitialize a new ranked set with the default configuration.Initialize a new ranked set.RankedSet
(Subspace subspace, Executor executor, RankedSet.Config config) Initialize a new ranked set.RankedSet
(Subspace subspace, Executor executor, RankedSet.HashFunction hashFunction, int nlevels) Initialize a new ranked set. -
Method Summary
Modifier and TypeMethodDescriptionadd
(TransactionContext tc, byte[] key) Add a key to the set.protected CompletableFuture<Void>
addIncrementLevelKey
(Transaction tr, byte[] key, int level, boolean orEqual) protected CompletableFuture<Void>
addInsertLevelKey
(Transaction tr, byte[] key, int level) protected CompletableFuture<Void>
addLevelZeroKey
(Transaction tr, byte[] key, int level, boolean increment) protected RankedSet.Consistency
Clears the entire set.contains
(ReadTransactionContext tc, byte[] key) Checks for the presence of a key in the set.count
(ReadTransactionContext tc, byte[] key) Count the number of occurrences of a key in the set.Get this ranked set's configuration.Get executed used by this ranked set.protected int
getKeyHash
(byte[] key) CompletableFuture<byte[]>
getNth
(ReadTransactionContext tc, long rank) Return the Nth item in the set.AsyncIterable<byte[]>
getRange
(ReadTransaction tr, byte[] beginKey, byte[] endKey) List<byte[]>
getRangeList
(ReadTransactionContext tc, byte[] beginKey, byte[] endKey) Returns the ordered set of keys in a given range.Get the subspace used to store this ranked set.Determine whetherinit(com.apple.foundationdb.TransactionContext)
needs to be called.protected <T> AsyncIterator<T>
lookupIterator
(AsyncIterable<T> iterable) static RankedSet.ConfigBuilder
Start building aRankedSet.Config
.protected CompletableFuture<Boolean>
nextLookup
(RankedSet.Lookup lookup, ReadTransaction tr) protected void
nextLookupKey
(long duration, boolean newIter, boolean hasNext, int level, boolean rankLookup) Read the deeper, likely empty, levels to get them into the RYW cache, since individual lookups may only add pieces, requiring additional requests as keys increase.rank
(ReadTransactionContext tc, byte[] key) Return the index of a key within the set.rank
(ReadTransactionContext tc, byte[] key, boolean nullIfMissing) Return the index of a key within the set.remove
(TransactionContext tc, byte[] key) Removes a key from the set.Count the items in the set.protected String
-
Field Details
-
JDK_ARRAY_HASH
Hash using the JDK's default byte array hash. This hash does not have great distribution for certain values with low entropy. -
CRC_HASH
Hash using 32-bit CRC. Although not designed for hashing, this does often give better distribution thanJDK_ARRAY_HASH
. -
RANDOM_HASH
Hash using a random number. The result is actually independent of the key. -
DEFAULT_HASH_FUNCTION
The default hash function to use. For reasons of compatibility with existing data, this defaults to the JDK's array hash. -
MAX_LEVELS
public static final int MAX_LEVELS- See Also:
-
DEFAULT_LEVELS
public static final int DEFAULT_LEVELS- See Also:
-
DEFAULT_CONFIG
-
subspace
-
executor
-
config
-
-
Constructor Details
-
RankedSet
Initialize a new ranked set.- Parameters:
subspace
- the subspace where the ranked set is storedexecutor
- an executor to use when running asynchronous tasksconfig
- configuration to use
-
RankedSet
public RankedSet(Subspace subspace, Executor executor, RankedSet.HashFunction hashFunction, int nlevels) Initialize a new ranked set. Although not (yet) deprecated, this constructor is mainly for compatibility andRankedSet(Subspace, Executor, Config)
may be superior in new code.- Parameters:
subspace
- the subspace where the ranked set is storedexecutor
- an executor to use when running asynchronous taskshashFunction
- hash function for new ranked setnlevels
- number of skip list levels for new ranked set
-
RankedSet
Initialize a new ranked set. Although not (yet) deprecated, this constructor is mainly for compatibility andRankedSet(Subspace, Executor, Config)
may be superior in new code.- Parameters:
subspace
- the subspace where the ranked set is storedexecutor
- an executor to use when running asynchronous tasksnlevels
- number of skip list levels for new ranked set
-
RankedSet
Initialize a new ranked set with the default configuration.- Parameters:
subspace
- the subspace where the ranked set is storedexecutor
- an executor to use when running asynchronous tasks
-
-
Method Details
-
newConfigBuilder
Start building aRankedSet.Config
.- Returns:
- a new
Config
that can be altered and then built for use with aRankedSet
- See Also:
-
init
-
initNeeded
Determine whetherinit(com.apple.foundationdb.TransactionContext)
needs to be called.- Parameters:
tc
- the transaction to use to access the database- Returns:
true
if this ranked set needs to be initialized
-
getSubspace
Get the subspace used to store this ranked set.- Returns:
- ranked set subspace
-
getExecutor
Get executed used by this ranked set.- Returns:
- executor used when running asynchronous tasks
-
getConfig
Get this ranked set's configuration.- Returns:
- ranked set configuration
-
add
Add a key to the set. IfRankedSet.Config.isCountDuplicates()
isfalse
andkey
is already present, the return value isfalse
. IfRankedSet.Config.isCountDuplicates()
istrue
, the return value is neverfalse
and a duplicate will cause allrank(com.apple.foundationdb.ReadTransactionContext, byte[])
s below it to increase by one.- Parameters:
tc
- the transaction to use to access the databasekey
- the key to add- Returns:
- a future that completes to
true
if the ranked set was modified
-
getKeyHash
protected int getKeyHash(byte[] key) -
addLevelZeroKey
protected CompletableFuture<Void> addLevelZeroKey(Transaction tr, byte[] key, int level, boolean increment) -
addIncrementLevelKey
protected CompletableFuture<Void> addIncrementLevelKey(Transaction tr, byte[] key, int level, boolean orEqual) -
addInsertLevelKey
-
remove
Removes a key from the set.- Parameters:
tc
- the transaction to use to access the databasekey
- the key to remove- Returns:
- a future that completes to
true
if the set was modified, that is, if the key was present before this operation
-
clear
Clears the entire set.- Parameters:
tc
- the transaction to use to access the database- Returns:
- a future that completes when the ranked set has been cleared
-
contains
Checks for the presence of a key in the set.- Parameters:
tc
- the transaction to use to access the databasekey
- the key to check for- Returns:
- a future that completes to
true
if the key is present in the ranked set
-
count
Count the number of occurrences of a key in the set.- Parameters:
tc
- the transaction to use to access the databasekey
- the key to check for- Returns:
- a future that completes to
0
if the key is not present in the ranked set or1
if the key is present in the ranked set and duplicates are not counted or the number of occurrences if duplicated are counted separately
-
getNth
Return the Nth item in the set. This operation is also referred to as select.- Parameters:
tc
- the transaction to use to access the databaserank
- the rank index to find- Returns:
- a future that completes to the key for the
rank
th item ornull
if that index is out of bounds - See Also:
-
getRangeList
Returns the ordered set of keys in a given range.- Parameters:
tc
- the transaction to use to access the databasebeginKey
- the (inclusive) lower bound for the rangeendKey
- the (exclusive) upper bound for the range- Returns:
- a list of keys in the ranked set within the given range
-
getRange
-
preloadForLookup
Read the deeper, likely empty, levels to get them into the RYW cache, since individual lookups may only add pieces, requiring additional requests as keys increase.- Parameters:
tr
- the transaction to use to access the database- Returns:
- a future that is complete when the deeper levels have been loaded
-
nextLookup
-
lookupIterator
-
nextLookupKey
protected void nextLookupKey(long duration, boolean newIter, boolean hasNext, int level, boolean rankLookup) -
rank
Return the index of a key within the set.- Parameters:
tc
- the transaction to use to access the databasekey
- the key to find- Returns:
- a future that completes to the index of
key
in the ranked set ornull
if it is not present - See Also:
-
rank
Return the index of a key within the set.- Parameters:
tc
- the transaction to use to access the databasekey
- the key to findnullIfMissing
- whether to returnnull
whenkey
is not present in the set- Returns:
- a future that completes to the index of
key
in the ranked set, ornull
if it is not present andnullIfMissing
istrue
, or the indexkey
would have in the ranked set - See Also:
-
size
Count the items in the set.- Parameters:
tc
- the transaction to use to access the database- Returns:
- a future that completes to the number of items in the set
-
checkConsistency
-
toDebugString
-