use of com.apple.foundationdb.record.TupleRange in project fdb-record-layer by FoundationDB.
the class LeaderboardIndexTest method omitAllTime.
@Test
public void omitAllTime() {
Leaderboards leaderboards = new GroupedNestedLeaderboards();
leaderboards.buildMetaData();
try (FDBRecordContext context = openContext()) {
leaderboards.openRecordStore(context, true);
leaderboards.recordStore.performIndexOperation("LeaderboardIndex", new TimeWindowLeaderboardWindowUpdate(System.currentTimeMillis(), true, 10100, // No allTime.
false, Arrays.asList(new TimeWindowLeaderboardWindowUpdate.TimeWindowSpec(TEN_UNITS, 10100, 5, 10, 20)), TimeWindowLeaderboardWindowUpdate.Rebuild.IF_OVERLAPPING_CHANGED));
addInitialScores(leaderboards);
context.commit();
}
try (FDBRecordContext context = openContext()) {
leaderboards.openRecordStore(context, false);
TupleRange game_1 = TupleRange.allOf(Tuple.from("game-1"));
assertEquals(Collections.emptyList(), leaderboards.scanIndexByRank(game_1).map(leaderboards::getName).asList().join());
TimeWindowScanRange ten_units = new TimeWindowScanRange(TEN_UNITS, 10100, TupleRange.allOf(Tuple.from("game-1")));
assertEquals(Arrays.asList("achilles", "hector"), leaderboards.scanIndexByTimeWindow(ten_units).map(leaderboards::getName).asList().join());
final FDBStoredRecord<Message> rec = leaderboards.findByName("achilles");
final QueryRecordFunction<Long> rank1 = leaderboards.queryRank();
assertEquals(null, leaderboards.evaluateQueryFunction(rank1, rec));
final QueryRecordFunction<Long> rank2 = leaderboards.queryTimeWindowRank(TEN_UNITS, 10100);
assertEquals((Long) 0L, leaderboards.evaluateQueryFunction(rank2, rec));
}
}
use of com.apple.foundationdb.record.TupleRange in project fdb-record-layer by FoundationDB.
the class LeaderboardIndexTest method basicUngroupedNested.
@Test
public void basicUngroupedNested() {
Leaderboards leaderboards = new UngroupedNestedLeaderboards();
basicSetup(leaderboards, false);
try (FDBRecordContext context = openContext()) {
leaderboards.openRecordStore(context, false);
assertEquals(Arrays.asList("achilles", "hector", "helen", "hecuba", "patroclus"), leaderboards.scanIndexByRank(TupleRange.ALL).map(leaderboards::getName).asList().join());
TupleRange top_2 = new TupleRange(Tuple.from(0), Tuple.from(1), EndpointType.RANGE_INCLUSIVE, EndpointType.RANGE_INCLUSIVE);
assertEquals(Arrays.asList("achilles", "hector"), leaderboards.scanIndexByRank(top_2).map(leaderboards::getName).asList().join());
TupleRange no_2 = TupleRange.allOf(Tuple.from(1));
assertEquals(Arrays.asList("hector"), leaderboards.scanIndexByRank(no_2).map(leaderboards::getName).asList().join());
TimeWindowScanRange ten_units = new TimeWindowScanRange(TEN_UNITS, 10101, TupleRange.ALL);
assertEquals(Arrays.asList("achilles", "hector"), leaderboards.scanIndexByTimeWindow(ten_units).map(leaderboards::getName).asList().join());
TimeWindowScanRange five_units = new TimeWindowScanRange(FIVE_UNITS, 10102, TupleRange.ALL);
assertEquals(Arrays.asList("achilles", "hector"), leaderboards.scanIndexByTimeWindow(five_units).map(leaderboards::getName).asList().join());
TimeWindowScanRange five_units_2 = new TimeWindowScanRange(FIVE_UNITS, 10107, TupleRange.ALL);
assertEquals(Arrays.asList("hector", "achilles"), leaderboards.scanIndexByTimeWindow(five_units_2).map(leaderboards::getName).asList().join());
final FDBStoredRecord<Message> rec = leaderboards.findByName("helen");
assertEquals((Long) 2L, leaderboards.evaluateQueryFunction(leaderboards.queryRank(), rec));
assertEquals(null, leaderboards.evaluateQueryFunction(leaderboards.queryTimeWindowRank(TEN_UNITS, 10102), rec));
}
}
use of com.apple.foundationdb.record.TupleRange in project fdb-record-layer by FoundationDB.
the class LeaderboardIndexTest method changeGrouped.
@Test
public void changeGrouped() {
Leaderboards leaderboards = new GroupedNestedLeaderboards();
basicSetup(leaderboards, true);
TupleRange game_1 = TupleRange.allOf(Tuple.from("game-1"));
try (FDBRecordContext context = openContext()) {
leaderboards.openRecordStore(context, false);
assertEquals(Arrays.asList("patroclus", "hecuba", "achilles", "hector"), leaderboards.scanIndexByRank(game_1).map(leaderboards::getName).asList().join());
final FDBStoredRecord<Message> rec1 = leaderboards.findByName("hecuba");
final FDBStoredRecord<Message> rec2 = leaderboards.findByName("achilles");
assertEquals((Long) 1L, leaderboards.evaluateQueryFunction(leaderboards.queryRank(), rec1));
assertEquals(null, leaderboards.evaluateQueryFunction(leaderboards.queryTimeWindowRank(TEN_UNITS, 10102), rec1));
assertEquals((Long) 2L, leaderboards.evaluateQueryFunction(leaderboards.queryRank(), rec2));
assertEquals((Long) 0L, leaderboards.evaluateQueryFunction(leaderboards.queryTimeWindowRank(TEN_UNITS, 10102), rec2));
}
try (FDBRecordContext context = openContext()) {
leaderboards.openRecordStore(context, false);
TestRecordsLeaderboardProto.NestedLeaderboardRecord.Builder recordBuilder = TestRecordsLeaderboardProto.NestedLeaderboardRecord.newBuilder();
recordBuilder.mergeFrom(leaderboards.findByName("achilles").getRecord());
recordBuilder.removeScores(3);
recordBuilder.removeScores(2);
leaderboards.recordStore.saveRecord(recordBuilder.build());
assertEquals(Arrays.asList("patroclus", "hecuba", "hector", "achilles"), leaderboards.scanIndexByRank(game_1).map(leaderboards::getName).asList().join());
final FDBStoredRecord<Message> rec1 = leaderboards.findByName("hecuba");
final FDBStoredRecord<Message> rec2 = leaderboards.findByName("achilles");
assertEquals((Long) 1L, leaderboards.evaluateQueryFunction(leaderboards.queryRank(), rec1));
assertEquals(null, leaderboards.evaluateQueryFunction(leaderboards.queryTimeWindowRank(TEN_UNITS, 10102), rec1));
assertEquals((Long) 3L, leaderboards.evaluateQueryFunction(leaderboards.queryRank(), rec2));
assertEquals((Long) 1L, leaderboards.evaluateQueryFunction(leaderboards.queryTimeWindowRank(TEN_UNITS, 10102), rec2));
context.commit();
}
try (FDBRecordContext context = openContext()) {
leaderboards.openRecordStore(context, false);
leaderboards.addScores("achilles", "game-1", 350, 10108, 669);
assertEquals(Arrays.asList("patroclus", "hecuba", "achilles", "hector"), leaderboards.scanIndexByRank(game_1).map(leaderboards::getName).asList().join());
final FDBStoredRecord<Message> rec1 = leaderboards.findByName("hecuba");
final FDBStoredRecord<Message> rec2 = leaderboards.findByName("achilles");
assertEquals((Long) 1L, leaderboards.evaluateQueryFunction(leaderboards.queryRank(), rec1));
assertEquals(null, leaderboards.evaluateQueryFunction(leaderboards.queryTimeWindowRank(TEN_UNITS, 10102), rec1));
assertEquals((Long) 2L, leaderboards.evaluateQueryFunction(leaderboards.queryRank(), rec2));
assertEquals((Long) 0L, leaderboards.evaluateQueryFunction(leaderboards.queryTimeWindowRank(TEN_UNITS, 10102), rec2));
context.commit();
}
try (FDBRecordContext context = openContext()) {
leaderboards.openRecordStore(context, false);
leaderboards.recordStore.deleteRecord(Tuple.from("patroclus"));
assertEquals(Arrays.asList("hecuba", "achilles", "hector"), leaderboards.scanIndexByRank(game_1).map(leaderboards::getName).asList().join());
final FDBStoredRecord<Message> rec1 = leaderboards.findByName("hecuba");
final FDBStoredRecord<Message> rec2 = leaderboards.findByName("achilles");
assertEquals((Long) 0L, leaderboards.evaluateQueryFunction(leaderboards.queryRank(), rec1));
assertEquals(null, leaderboards.evaluateQueryFunction(leaderboards.queryTimeWindowRank(TEN_UNITS, 10102), rec1));
assertEquals((Long) 1L, leaderboards.evaluateQueryFunction(leaderboards.queryRank(), rec2));
assertEquals((Long) 0L, leaderboards.evaluateQueryFunction(leaderboards.queryTimeWindowRank(TEN_UNITS, 10102), rec2));
context.commit();
}
}
use of com.apple.foundationdb.record.TupleRange in project fdb-record-layer by FoundationDB.
the class LeaderboardIndexTest method basicGrouped.
protected void basicGrouped(Leaderboards leaderboards) {
basicSetup(leaderboards, true);
try (FDBRecordContext context = openContext()) {
leaderboards.openRecordStore(context, false);
TupleRange game_1 = TupleRange.allOf(Tuple.from("game-1"));
assertEquals(Arrays.asList("patroclus", "hecuba", "achilles", "hector"), leaderboards.scanIndexByRank(game_1).map(leaderboards::getName).asList().join());
TupleRange top_2 = new TupleRange(Tuple.from("game-1", 0), Tuple.from("game-1", 1), EndpointType.RANGE_INCLUSIVE, EndpointType.RANGE_INCLUSIVE);
assertEquals(Arrays.asList("patroclus", "hecuba"), leaderboards.scanIndexByRank(top_2).map(leaderboards::getName).asList().join());
TupleRange no_2 = TupleRange.allOf(Tuple.from("game-1", 1));
assertEquals(Arrays.asList("hecuba"), leaderboards.scanIndexByRank(no_2).map(leaderboards::getName).asList().join());
TimeWindowScanRange ten_units = new TimeWindowScanRange(TEN_UNITS, 10100, TupleRange.allOf(Tuple.from("game-1")));
assertEquals(Arrays.asList("achilles", "hector"), leaderboards.scanIndexByTimeWindow(ten_units).map(leaderboards::getName).asList().join());
assertEquals(Arrays.asList("achilles"), leaderboards.scanIndexByTimeWindowWithLimit(ten_units, 1).map(leaderboards::getName).asList().join());
TimeWindowScanRange top_2_ten_units = new TimeWindowScanRange(TEN_UNITS, 10102, new TupleRange(Tuple.from("game-1", 0), Tuple.from("game-1", 1), EndpointType.RANGE_INCLUSIVE, EndpointType.RANGE_INCLUSIVE));
assertEquals(Arrays.asList("achilles", "hector"), leaderboards.scanIndexByTimeWindow(top_2_ten_units).map(leaderboards::getName).asList().join());
TimeWindowScanRange top_2_five_units = new TimeWindowScanRange(FIVE_UNITS, 10100, new TupleRange(Tuple.from("game-1", 0), Tuple.from("game-1", 1), EndpointType.RANGE_INCLUSIVE, EndpointType.RANGE_INCLUSIVE));
assertEquals(Arrays.asList("hector", "achilles"), leaderboards.scanIndexByTimeWindow(top_2_five_units).map(leaderboards::getName).asList().join());
final FDBStoredRecord<Message> rec1 = leaderboards.findByName("patroclus");
final FDBStoredRecord<Message> rec2 = leaderboards.findByName("achilles");
final FDBStoredRecord<Message> rec3 = leaderboards.findByName("hecuba");
final QueryRecordFunction<Long> rank1 = leaderboards.queryRank();
assertEquals((Long) 0L, leaderboards.evaluateQueryFunction(rank1, rec1));
assertEquals((Long) 2L, leaderboards.evaluateQueryFunction(rank1, rec2));
assertEquals((Long) 1L, leaderboards.evaluateQueryFunction(rank1, rec3));
final QueryRecordFunction<Long> rank2 = leaderboards.queryTimeWindowRank(TEN_UNITS, 10100);
assertEquals(null, leaderboards.evaluateQueryFunction(rank2, rec1));
assertEquals((Long) 0L, leaderboards.evaluateQueryFunction(rank2, rec2));
assertEquals(null, leaderboards.evaluateQueryFunction(rank2, rec3));
final QueryRecordFunction<Tuple> entry1 = leaderboards.queryTimeWindowRankAndEntry(TimeWindowLeaderboard.ALL_TIME_LEADERBOARD_TYPE, -1);
assertEquals(Tuple.from(0, 1000, 11001, 111), leaderboards.evaluateQueryFunction(entry1, rec1));
assertEquals(Tuple.from(2, 300, 10201, 668), leaderboards.evaluateQueryFunction(entry1, rec2));
assertEquals(Tuple.from(1, 750, 11201, 888), leaderboards.evaluateQueryFunction(entry1, rec3));
final QueryRecordFunction<Tuple> entry2 = leaderboards.queryTimeWindowRankAndEntry(TEN_UNITS, 10100);
assertEquals(null, leaderboards.evaluateQueryFunction(entry2, rec1));
assertEquals(Tuple.from(0, 200, 10105, 667), leaderboards.evaluateQueryFunction(entry2, rec2));
assertEquals(null, leaderboards.evaluateQueryFunction(entry2, rec3));
final IndexAggregateFunction count1 = leaderboards.timeWindowCount(TimeWindowLeaderboard.ALL_TIME_LEADERBOARD_TYPE, -1);
assertEquals((Long) 4L, leaderboards.evaluateAggregateFunction(count1, Tuple.from("game-1")).get(0));
assertEquals((Long) 1L, leaderboards.evaluateAggregateFunction(count1, Tuple.from("game-2")).get(0));
final IndexAggregateFunction count2 = leaderboards.timeWindowCount(TEN_UNITS, 10100);
assertEquals((Long) 2L, leaderboards.evaluateAggregateFunction(count2, Tuple.from("game-1")).get(0));
}
}
use of com.apple.foundationdb.record.TupleRange in project fdb-record-layer by FoundationDB.
the class FDBRecordStoreEstimateSizeTest method estimateInTwoRanges.
@Test
public void estimateInTwoRanges() throws Exception {
final int recordCount = 500;
populateStore(recordCount, recordCount * 5_000);
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context);
long estimatedRecordSize = estimateRecordsSize(recordStore);
final Tuple halfWayTuple = Tuple.from(recordCount / 2);
final TupleRange firstHalf = new TupleRange(null, halfWayTuple, EndpointType.TREE_START, EndpointType.RANGE_EXCLUSIVE);
long estimatedFirstHalf = estimateRecordsSize(recordStore, firstHalf);
final TupleRange secondHalf = new TupleRange(halfWayTuple, null, EndpointType.RANGE_INCLUSIVE, EndpointType.TREE_END);
long estimatedSecondHalf = estimateRecordsSize(recordStore, secondHalf);
LOGGER.info(KeyValueLogMessage.of("estimated both halves of records", "estimated_records_size", estimatedRecordSize, "estimated_first_half_size", estimatedFirstHalf, "estimated_second_half_size", estimatedSecondHalf));
assertEquals(estimatedFirstHalf + estimatedSecondHalf, estimatedRecordSize, "expected first half size (" + estimatedFirstHalf + ") and second half size (" + estimatedSecondHalf + ") to match full size");
commit(context);
}
}
Aggregations