Search in sources :

Example 16 with TupleRange

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));
    }
}
Also used : KeyValueLogMessage(com.apple.foundationdb.record.logging.KeyValueLogMessage) Message(com.google.protobuf.Message) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) TupleRange(com.apple.foundationdb.record.TupleRange) Test(org.junit.jupiter.api.Test)

Example 17 with TupleRange

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));
    }
}
Also used : KeyValueLogMessage(com.apple.foundationdb.record.logging.KeyValueLogMessage) Message(com.google.protobuf.Message) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) TupleRange(com.apple.foundationdb.record.TupleRange) Test(org.junit.jupiter.api.Test)

Example 18 with TupleRange

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();
    }
}
Also used : KeyValueLogMessage(com.apple.foundationdb.record.logging.KeyValueLogMessage) Message(com.google.protobuf.Message) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) TupleRange(com.apple.foundationdb.record.TupleRange) Test(org.junit.jupiter.api.Test)

Example 19 with TupleRange

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));
    }
}
Also used : KeyValueLogMessage(com.apple.foundationdb.record.logging.KeyValueLogMessage) Message(com.google.protobuf.Message) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) TupleRange(com.apple.foundationdb.record.TupleRange) IndexAggregateFunction(com.apple.foundationdb.record.metadata.IndexAggregateFunction) Tuple(com.apple.foundationdb.tuple.Tuple)

Example 20 with TupleRange

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);
    }
}
Also used : TupleRange(com.apple.foundationdb.record.TupleRange) Tuple(com.apple.foundationdb.tuple.Tuple) Test(org.junit.jupiter.api.Test)

Aggregations

TupleRange (com.apple.foundationdb.record.TupleRange)48 Tuple (com.apple.foundationdb.tuple.Tuple)29 Nonnull (javax.annotation.Nonnull)28 Message (com.google.protobuf.Message)21 Index (com.apple.foundationdb.record.metadata.Index)16 Test (org.junit.jupiter.api.Test)16 ScanProperties (com.apple.foundationdb.record.ScanProperties)15 FDBRecordContext (com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext)14 Nullable (javax.annotation.Nullable)14 ExecuteProperties (com.apple.foundationdb.record.ExecuteProperties)11 IndexEntry (com.apple.foundationdb.record.IndexEntry)11 List (java.util.List)11 IsolationLevel (com.apple.foundationdb.record.IsolationLevel)10 RecordCoreException (com.apple.foundationdb.record.RecordCoreException)10 RecordCursor (com.apple.foundationdb.record.RecordCursor)10 IndexScanType (com.apple.foundationdb.record.IndexScanType)9 CompletableFuture (java.util.concurrent.CompletableFuture)9 Range (com.apple.foundationdb.Range)8 API (com.apple.foundationdb.annotation.API)8 AsyncUtil (com.apple.foundationdb.async.AsyncUtil)8