Class TimeWindowLeaderboardIndexMaintainer
- java.lang.Object
-
- com.apple.foundationdb.record.provider.foundationdb.IndexMaintainer
-
- com.apple.foundationdb.record.provider.foundationdb.indexes.StandardIndexMaintainer
-
- com.apple.foundationdb.record.provider.foundationdb.leaderboard.TimeWindowLeaderboardIndexMaintainer
-
@API(EXPERIMENTAL) public class TimeWindowLeaderboardIndexMaintainer extends StandardIndexMaintainer
Maintainer for theTIME_WINDOW_LEADERBOARD
index type.- See Also:
for details of how this works.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected class
TimeWindowLeaderboardIndexMaintainer.UpdateState
-
Field Summary
-
Fields inherited from class com.apple.foundationdb.record.provider.foundationdb.indexes.StandardIndexMaintainer
TOO_LARGE_VALUE_MESSAGE_LIMIT
-
Fields inherited from class com.apple.foundationdb.record.provider.foundationdb.IndexMaintainer
state
-
-
Constructor Summary
Constructors Constructor Description TimeWindowLeaderboardIndexMaintainer(IndexMaintainerState state)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
canEvaluateAggregateFunction(IndexAggregateFunction function)
Get whether this index can be used to evaluate the given aggregate function.boolean
canEvaluateRecordFunction(IndexRecordFunction<?> function)
Returntrue
if this index be used to evaluate the given record function.protected List<IndexEntry>
commonKeys(List<IndexEntry> oldIndexKeys, List<IndexEntry> newIndexKeys)
CompletableFuture<Void>
deleteWhere(Transaction tr, Tuple prefix)
Clear index storage associated with the given key prefix.CompletableFuture<Tuple>
evaluateAggregateFunction(IndexAggregateFunction function, TupleRange range, IsolationLevel isolationLevel)
Evaluate an aggregate function over the given range using this index.<T,M extends Message>
CompletableFuture<T>evaluateRecordFunction(EvaluationContext context, IndexRecordFunction<T> function, FDBRecord<M> record)
Evaluate a record function on the given record.protected CompletableFuture<IndexEntry>
getIndexEntry(IndexEntry rawEntry, int groupPrefixSize, TimeWindowLeaderboardDirectory directory)
protected CompletableFuture<Map<Tuple,Collection<com.apple.foundationdb.record.provider.foundationdb.leaderboard.TimeWindowLeaderboardIndexMaintainer.OrderedScoreIndexKey>>>
groupOrderedScoreIndexKeys(Iterable<IndexEntry> indexEntries, TimeWindowLeaderboardDirectory directory, boolean includesGroup)
Group the givenindexKeys
by group of sizegroupPrefixSize
, ordering within each group by score, takinghighScoreFirst
into account from the directory or any sub-directory (ifincludesGroup
istrue
).protected CompletableFuture<Boolean>
isHighScoreFirst(TimeWindowLeaderboardDirectory directory, Tuple group)
boolean
isIdempotent()
Whether updating or removing a record on this index is idempotent.protected CompletableFuture<TimeWindowLeaderboardDirectory>
loadDirectory()
protected CompletableFuture<TimeWindowLeaderboardSubDirectory>
loadSubDirectory(TimeWindowLeaderboardDirectory directory, Tuple group)
protected static Tuple
negateScoreForHighScoreFirst(Tuple entry, int position)
Negate the score element so that it sorts in reverse order to support high score first.protected TupleRange
negateScoreRange(TupleRange range)
Negate the score part of the given range, which is after the group prefix and before the timestamp and any other tiebreakers.protected CompletableFuture<TimeWindowLeaderboard>
oldestLeaderboardMatching(int type, long timestamp)
CompletableFuture<IndexOperationResult>
performOperation(IndexOperation operation)
Perform a type-specific operation on index.protected void
saveDirectory(TimeWindowLeaderboardDirectory directory)
protected void
saveSubDirectory(TimeWindowLeaderboardSubDirectory subdirectory)
RecordCursor<IndexEntry>
scan(IndexScanType scanType, TupleRange rankRange, byte[] continuation, ScanProperties scanProperties)
Scan entries in the index.protected RecordCursor<IndexEntry>
scanLeaderboard(TimeWindowLeaderboard leaderboard, boolean highScoreFirst, TupleRange scoreRange, byte[] continuation, ScanProperties scanProperties)
protected RecordCursor<IndexEntry>
scanLeaderboard(TimeWindowLeaderboard leaderboard, TupleRange range, byte[] continuation, ScanProperties scanProperties)
<M extends Message>
CompletableFuture<org.apache.commons.lang3.tuple.Pair<Long,Tuple>>timeWindowRankAndEntry(EvaluationContext context, TimeWindowForFunction timeWindow, FDBRecord<M> record)
<M extends Message>
CompletableFuture<org.apache.commons.lang3.tuple.Pair<Long,Tuple>>timeWindowRankAndEntry(FDBRecord<M> record, int type, long timestamp)
protected CompletableFuture<Collection<Tuple>>
trimScores(TimeWindowLeaderboardDirectory directory, Collection<Tuple> scores, boolean includesGroup)
protected <M extends Message>
CompletableFuture<Void>updateIndexKeys(FDBIndexableRecord<M> savedRecord, boolean remove, List<IndexEntry> indexEntries)
Update index according to record keys.-
Methods inherited from class com.apple.foundationdb.record.provider.foundationdb.indexes.StandardIndexMaintainer
addedRangeWithKey, addUniquenessViolation, canDeleteWhere, canDeleteWhere, checkKeyValueSizes, checkUniqueness, decodeValue, evaluateIndex, filteredIndexEntries, getExecutor, getGroupedCount, getGroupingCount, getTimer, indexEntryKey, makeMutable, removeUniquenessViolationsAsync, saveIndexEntryAsKeyValue, scan, scanUniquenessViolations, skipUpdateForUnchangedKeys, trimTooLargeTuple, unpackKeyValue, unpackKeyValue, update, updateIndexKeysFunction, updateOneKeyAsync, validateEntries, validateMissingEntries, validateOrphanEntries
-
Methods inherited from class com.apple.foundationdb.record.provider.foundationdb.IndexMaintainer
getIndexSubspace, getSecondarySubspace, unsupportedAggregateFunction, unsupportedRecordFunction
-
-
-
-
Constructor Detail
-
TimeWindowLeaderboardIndexMaintainer
public TimeWindowLeaderboardIndexMaintainer(IndexMaintainerState state)
-
-
Method Detail
-
oldestLeaderboardMatching
@Nonnull protected CompletableFuture<TimeWindowLeaderboard> oldestLeaderboardMatching(int type, long timestamp)
-
loadDirectory
@Nonnull protected CompletableFuture<TimeWindowLeaderboardDirectory> loadDirectory()
-
saveDirectory
protected void saveDirectory(TimeWindowLeaderboardDirectory directory)
-
loadSubDirectory
@Nonnull protected CompletableFuture<TimeWindowLeaderboardSubDirectory> loadSubDirectory(@Nonnull TimeWindowLeaderboardDirectory directory, @Nonnull Tuple group)
-
saveSubDirectory
protected void saveSubDirectory(@Nonnull TimeWindowLeaderboardSubDirectory subdirectory)
-
scan
@Nonnull public RecordCursor<IndexEntry> scan(@Nonnull IndexScanType scanType, @Nonnull TupleRange rankRange, @Nullable byte[] continuation, @Nonnull ScanProperties scanProperties)
Description copied from class:IndexMaintainer
Scan entries in the index.- Specified by:
scan
in classIndexMaintainer
- Parameters:
scanType
- thetype
of scan to performrankRange
- the range to scancontinuation
- any continuation from a previous scan invocationscanProperties
- skip, limit and other properties of the scan- Returns:
- a cursor over index entries in the given range
-
scanLeaderboard
protected RecordCursor<IndexEntry> scanLeaderboard(@Nonnull TimeWindowLeaderboard leaderboard, boolean highScoreFirst, @Nonnull TupleRange scoreRange, @Nullable byte[] continuation, @Nonnull ScanProperties scanProperties)
-
scanLeaderboard
protected RecordCursor<IndexEntry> scanLeaderboard(@Nonnull TimeWindowLeaderboard leaderboard, @Nonnull TupleRange range, @Nullable byte[] continuation, @Nonnull ScanProperties scanProperties)
-
negateScoreRange
protected TupleRange negateScoreRange(@Nonnull TupleRange range)
Negate the score part of the given range, which is after the group prefix and before the timestamp and any other tiebreakers.- Parameters:
range
- the range of scores in normal order- Returns:
- a range with scores negated so that they sort in reverse order
-
getIndexEntry
protected CompletableFuture<IndexEntry> getIndexEntry(@Nonnull IndexEntry rawEntry, int groupPrefixSize, @Nonnull TimeWindowLeaderboardDirectory directory)
-
isHighScoreFirst
protected CompletableFuture<Boolean> isHighScoreFirst(@Nonnull TimeWindowLeaderboardDirectory directory, @Nonnull Tuple group)
-
negateScoreForHighScoreFirst
protected static Tuple negateScoreForHighScoreFirst(@Nonnull Tuple entry, int position)
Negate the score element so that it sorts in reverse order to support high score first.- Parameters:
entry
- original entryposition
- position inTuple
of the score value- Returns:
- a new entry with the score negated
-
commonKeys
@Nonnull protected List<IndexEntry> commonKeys(@Nonnull List<IndexEntry> oldIndexKeys, @Nonnull List<IndexEntry> newIndexKeys)
- Overrides:
commonKeys
in classStandardIndexMaintainer
-
updateIndexKeys
protected <M extends Message> CompletableFuture<Void> updateIndexKeys(@Nonnull FDBIndexableRecord<M> savedRecord, boolean remove, @Nonnull List<IndexEntry> indexEntries)
Description copied from class:StandardIndexMaintainer
Update index according to record keys. Often this operation returns an already completed future because there is no asynchronous work to be done.- Overrides:
updateIndexKeys
in classStandardIndexMaintainer
- Type Parameters:
M
- the message type of the record- Parameters:
savedRecord
- the record being indexedremove
-true
if removing from indexindexEntries
- the result ofStandardIndexMaintainer.evaluateIndex(FDBRecord)
- Returns:
- a future completed when update is done
-
isIdempotent
public boolean isIdempotent()
Description copied from class:IndexMaintainer
Whether updating or removing a record on this index is idempotent. In principle, all index updates in the normal case are idempotent as long as the index update and the record insertion or deletion is atomic. However, certain indexes (mostly aggregate indexes) have the property that the index update on its own are not idempotent.- Overrides:
isIdempotent
in classStandardIndexMaintainer
- Returns:
- whether updating this index is idempotent
-
canEvaluateRecordFunction
public boolean canEvaluateRecordFunction(@Nonnull IndexRecordFunction<?> function)
Description copied from class:IndexMaintainer
Returntrue
if this index be used to evaluate the given record function.- Overrides:
canEvaluateRecordFunction
in classStandardIndexMaintainer
- Parameters:
function
- requested function- Returns:
true
if this index can be used to evaluate the given function
-
evaluateRecordFunction
@Nonnull public <T,M extends Message> CompletableFuture<T> evaluateRecordFunction(@Nonnull EvaluationContext context, @Nonnull IndexRecordFunction<T> function, @Nonnull FDBRecord<M> record)
Description copied from class:IndexMaintainer
Evaluate a record function on the given record.- Overrides:
evaluateRecordFunction
in classStandardIndexMaintainer
- Type Parameters:
T
- the result type of the functionM
- the message type of the record- Parameters:
context
- context for evaluationfunction
- the record function to apply to the given recordrecord
- record against which to evaluate- Returns:
- a future that completes with the result of evaluation
-
canEvaluateAggregateFunction
public boolean canEvaluateAggregateFunction(@Nonnull IndexAggregateFunction function)
Description copied from class:IndexMaintainer
Get whether this index can be used to evaluate the given aggregate function.- Overrides:
canEvaluateAggregateFunction
in classStandardIndexMaintainer
- Parameters:
function
- the requested aggregate function- Returns:
true
if this index be used to evaluate the given aggregate function
-
evaluateAggregateFunction
@Nonnull public CompletableFuture<Tuple> evaluateAggregateFunction(@Nonnull IndexAggregateFunction function, @Nonnull TupleRange range, @Nonnull IsolationLevel isolationLevel)
Description copied from class:IndexMaintainer
Evaluate an aggregate function over the given range using this index.- Overrides:
evaluateAggregateFunction
in classStandardIndexMaintainer
- Parameters:
function
- the aggregate function to evaluaterange
- the range over which to accumulate the aggregateisolationLevel
- the isolation level at which to perform the scan- Returns:
- a future that completes with the aggregate result
-
timeWindowRankAndEntry
@Nonnull public <M extends Message> CompletableFuture<org.apache.commons.lang3.tuple.Pair<Long,Tuple>> timeWindowRankAndEntry(@Nonnull EvaluationContext context, @Nonnull TimeWindowForFunction timeWindow, @Nonnull FDBRecord<M> record)
-
timeWindowRankAndEntry
@Nonnull public <M extends Message> CompletableFuture<org.apache.commons.lang3.tuple.Pair<Long,Tuple>> timeWindowRankAndEntry(@Nonnull FDBRecord<M> record, int type, long timestamp)
-
deleteWhere
public CompletableFuture<Void> deleteWhere(Transaction tr, @Nonnull Tuple prefix)
Description copied from class:IndexMaintainer
Clear index storage associated with the given key prefix.- Overrides:
deleteWhere
in classStandardIndexMaintainer
- Parameters:
tr
- transaction in which to access the databaseprefix
- prefix of primary key to clear- Returns:
- a future that is complete when the given prefix has been cleared from this index
-
performOperation
@Nonnull public CompletableFuture<IndexOperationResult> performOperation(@Nonnull IndexOperation operation)
Description copied from class:IndexMaintainer
Perform a type-specific operation on index. Allowed operations will vary by index type.- Overrides:
performOperation
in classStandardIndexMaintainer
- Parameters:
operation
- the requested operation- Returns:
- a future that completes with the result of the operation
-
trimScores
protected CompletableFuture<Collection<Tuple>> trimScores(@Nullable TimeWindowLeaderboardDirectory directory, @Nonnull Collection<Tuple> scores, boolean includesGroup)
-
groupOrderedScoreIndexKeys
protected CompletableFuture<Map<Tuple,Collection<com.apple.foundationdb.record.provider.foundationdb.leaderboard.TimeWindowLeaderboardIndexMaintainer.OrderedScoreIndexKey>>> groupOrderedScoreIndexKeys(@Nonnull Iterable<IndexEntry> indexEntries, @Nonnull TimeWindowLeaderboardDirectory directory, boolean includesGroup)
Group the givenindexKeys
by group of sizegroupPrefixSize
, ordering within each group by score, takinghighScoreFirst
into account from the directory or any sub-directory (ifincludesGroup
istrue
).- Parameters:
indexEntries
- index entries to be added to the indexdirectory
- leaderboard directory used to decide whether higher scores are better (earlier in the list)includesGroup
- whether index entries also include the group key(s)- Returns:
- a future that completes to index keys grouped by leaderboard
-
-