use of com.apple.foundationdb.tuple.Tuple in project fdb-record-layer by FoundationDB.
the class FDBSimpleJoinQueryTest method joinParentToChild.
/**
* Verify that simple binding joins in parent/child relationships work.
*/
@Test
public void joinParentToChild() throws Exception {
createJoinRecords(true);
RecordQuery parentQuery = RecordQuery.newBuilder().setRecordType("MyParentRecord").setFilter(Query.field("str_value_indexed").equalsValue("even")).build();
RecordQueryPlan parentPlan = planner.plan(parentQuery);
RecordQueryPlan childPlan = new RecordQueryLoadByKeysPlan("children");
try (FDBRecordContext context = openContext()) {
openJoinRecordStore(context);
RecordCursor<FDBQueriedRecord<Message>> parentCursor = recordStore.executeQuery(parentPlan);
RecordCursor<FDBQueriedRecord<Message>> childCursor = RecordCursor.flatMapPipelined(ignore -> recordStore.executeQuery(parentPlan), (rec, ignore) -> {
TestRecordsParentChildRelationshipProto.MyParentRecord.Builder parentRec = TestRecordsParentChildRelationshipProto.MyParentRecord.newBuilder();
parentRec.mergeFrom(rec.getRecord());
EvaluationContext childContext = EvaluationContext.forBinding("children", parentRec.getChildRecNosList().stream().map(Tuple::from).collect(Collectors.toList()));
return childPlan.execute(recordStore, childContext);
}, null, 10);
RecordCursor<String> resultsCursor = childCursor.map(rec -> {
TestRecordsParentChildRelationshipProto.MyChildRecord.Builder childRec = TestRecordsParentChildRelationshipProto.MyChildRecord.newBuilder();
childRec.mergeFrom(rec.getRecord());
return childRec.getStrValue();
});
assertEquals(Arrays.asList("2.1", "2.2", "2.3", "4.1", "4.2", "4.3"), resultsCursor.asList().join());
assertDiscardedNone(context);
}
}
use of com.apple.foundationdb.tuple.Tuple in project fdb-record-layer by FoundationDB.
the class KeyEvaluatedTest method testScalarBytesToTuple.
@Test
public void testScalarBytesToTuple() {
Key.Evaluated eval = new Key.Evaluated(Arrays.asList(ByteString.copyFrom(deadcode), ByteString.copyFrom(baseball), ByteString.copyFrom(scalable)));
Tuple tuple = eval.toTuple();
assertEquals(3, tuple.size());
assertArrayEquals(deadcode, (byte[]) tuple.get(0));
assertArrayEquals(baseball, (byte[]) tuple.get(1));
assertArrayEquals(scalable, (byte[]) tuple.get(2));
// This should not throw an error.
tuple.pack();
}
use of com.apple.foundationdb.tuple.Tuple in project fdb-record-layer by FoundationDB.
the class KeyEvaluatedTest method testConcatenatedBytesToTuple.
@Test
public void testConcatenatedBytesToTuple() {
Key.Evaluated eval = Key.Evaluated.scalar(Arrays.asList(ByteString.copyFrom(deadcode), ByteString.copyFrom(baseball), ByteString.copyFrom(scalable)));
Tuple tuple = eval.toTuple();
assertEquals(1, tuple.size());
@SuppressWarnings("unchecked") List<byte[]> list = (List<byte[]>) tuple.get(0);
assertEquals(3, list.size());
assertArrayEquals(deadcode, list.get(0));
assertArrayEquals(baseball, list.get(1));
assertArrayEquals(scalable, list.get(2));
// This should also not throw an error.
tuple.pack();
}
use of com.apple.foundationdb.tuple.Tuple in project fdb-record-layer by FoundationDB.
the class FDBRecordStoreScanLimitTest method assertScansUntilTimeLimit.
private static void assertScansUntilTimeLimit(@Nonnull FDBRecordStore recordStore, boolean reverse) {
try (RecordCursor<FDBStoredRecord<Message>> cursor = veryShortTimeLimitedScan(recordStore, reverse)) {
RecordCursorResult<FDBStoredRecord<Message>> result;
Tuple lastPrimaryKey = null;
do {
result = cursor.getNext();
if (result.hasNext()) {
FDBStoredRecord<Message> storedRecord = result.get();
assertNotNull(storedRecord);
assertTrue(storedRecord.hasVersion());
assertNotNull(storedRecord.getVersion());
Tuple primaryKey = storedRecord.getPrimaryKey();
if (lastPrimaryKey != null) {
if (reverse) {
assertThat(primaryKey, lessThan(lastPrimaryKey));
} else {
assertThat(primaryKey, greaterThan(lastPrimaryKey));
}
}
lastPrimaryKey = primaryKey;
}
} while (result.hasNext());
assertEquals(RecordCursor.NoNextReason.TIME_LIMIT_REACHED, result.getNoNextReason());
}
}
use of com.apple.foundationdb.tuple.Tuple 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));
}
}
Aggregations