use of com.apple.foundationdb.record.provider.foundationdb.FDBQueriedRecord in project fdb-record-layer by FoundationDB.
the class RankIndexTest method halfIntervalGroupedRankQuery.
@Test
public void halfIntervalGroupedRankQuery() throws Exception {
RecordQuery query = RecordQuery.newBuilder().setRecordType("BasicRankedRecord").setFilter(Query.and(Query.field("gender").equalsValue("M"), Query.rank(Key.Expressions.field("score").groupBy(Key.Expressions.field("gender"))).lessThan(1L))).build();
RecordQueryPlan plan = planner.plan(query);
assertEquals("Index(rank_by_gender ([M, null],[M, 1]) BY_RANK)", plan.toString());
try (FDBRecordContext context = openContext()) {
openRecordStore(context);
try (RecordCursor<FDBQueriedRecord<Message>> cursor = recordStore.executeQuery(plan)) {
FDBQueriedRecord<Message> rec = cursor.getNext().get();
TestRecordsRankProto.BasicRankedRecord.Builder myrec = TestRecordsRankProto.BasicRankedRecord.newBuilder();
myrec.mergeFrom(rec.getRecord());
assertEquals("hector", myrec.getName());
assertEquals(75, myrec.getScore());
assertFalse(cursor.getNext().hasNext());
}
}
}
use of com.apple.foundationdb.record.provider.foundationdb.FDBQueriedRecord in project fdb-record-layer by FoundationDB.
the class RankIndexTest method rankScanContinuation.
@Test
public void rankScanContinuation() throws Exception {
RecordQuery query = RecordQuery.newBuilder().setRecordType("BasicRankedRecord").setFilter(Query.rank("score").lessThan(100)).build();
RecordQueryPlan plan = planner.plan(query);
assertTrue(plan.hasIndexScan("BasicRankedRecord$score"));
try (FDBRecordContext context = openContext()) {
openRecordStore(context);
Multiset<String> names = HashMultiset.create();
byte[] continuation = null;
do {
RecordCursor<FDBQueriedRecord<Message>> recs = recordStore.executeQuery(plan, continuation, ExecuteProperties.newBuilder().setReturnedRowLimit(2).build());
recs.forEach(rec -> names.add(TestRecordsRankProto.BasicRankedRecord.newBuilder().mergeFrom(rec.getRecord()).getName())).join();
continuation = recs.getNext().getContinuation().toBytes();
} while (continuation != null);
assertEquals(ImmutableMultiset.of("achilles", "hector", "helen", "penelope", "laodice"), names);
commit(context);
}
}
use of com.apple.foundationdb.record.provider.foundationdb.FDBQueriedRecord in project fdb-record-layer by FoundationDB.
the class RankIndexTest method rightHalfIntervalRankQuery.
@Test
public void rightHalfIntervalRankQuery() throws Exception {
RecordQuery query = RecordQuery.newBuilder().setRecordType("BasicRankedRecord").setFilter(Query.rank("score").lessThan(2L)).build();
RecordQueryPlan plan = planner.plan(query);
assertEquals("Index(BasicRankedRecord$score ([null],[2]) BY_RANK)", plan.toString());
try (FDBRecordContext context = openContext()) {
openRecordStore(context);
try (RecordCursor<FDBQueriedRecord<Message>> cursor = recordStore.executeQuery(plan)) {
RecordCursorResult<FDBQueriedRecord<Message>> result = cursor.getNext();
FDBQueriedRecord<Message> rec = result.get();
TestRecordsRankProto.BasicRankedRecord.Builder myrec = TestRecordsRankProto.BasicRankedRecord.newBuilder();
myrec.mergeFrom(rec.getRecord());
assertEquals("hector", myrec.getName());
assertEquals(75, myrec.getScore());
result = cursor.getNext();
assertTrue(result.hasNext());
rec = result.get();
myrec = TestRecordsRankProto.BasicRankedRecord.newBuilder();
myrec.mergeFrom(rec.getRecord());
assertEquals("achilles", myrec.getName());
assertEquals(100, myrec.getScore());
result = cursor.getNext();
assertFalse(result.hasNext());
}
}
}
use of com.apple.foundationdb.record.provider.foundationdb.FDBQueriedRecord in project fdb-record-layer by FoundationDB.
the class RankIndexTest method leftHalfIntervalRankQuery.
@Test
public void leftHalfIntervalRankQuery() throws Exception {
RecordQuery query = RecordQuery.newBuilder().setRecordType("BasicRankedRecord").setFilter(Query.rank("score").greaterThan(2L)).build();
RecordQueryPlan plan = planner.plan(query);
assertEquals("Index(BasicRankedRecord$score ([2],> BY_RANK)", plan.toString());
try (FDBRecordContext context = openContext()) {
openRecordStore(context);
try (RecordCursor<FDBQueriedRecord<Message>> cursor = recordStore.executeQuery(plan)) {
FDBQueriedRecord<Message> rec = cursor.getNext().get();
TestRecordsRankProto.BasicRankedRecord.Builder myrec = TestRecordsRankProto.BasicRankedRecord.newBuilder();
myrec.mergeFrom(rec.getRecord());
assertEquals("laodice", myrec.getName());
assertEquals(300, myrec.getScore());
assertFalse(cursor.getNext().hasNext());
}
}
}
use of com.apple.foundationdb.record.provider.foundationdb.FDBQueriedRecord in project fdb-record-layer by FoundationDB.
the class RankIndexTest method rankScanIntersection.
@Test
public void rankScanIntersection() throws Exception {
try (FDBRecordContext context = openContext()) {
openRecordStore(context, md -> {
md.removeIndex("rank_by_gender");
md.addIndex("BasicRankedRecord", "gender");
});
recordStore.rebuildIndex(recordStore.getRecordMetaData().getIndex("BasicRankedRecord$gender")).join();
// Laodice fails the rank test; need something that fails the gender test.
recordStore.saveRecord(TestRecordsRankProto.BasicRankedRecord.newBuilder().setName("patroclus").setScore(200).setGender("M").build());
RecordQuery query = RecordQuery.newBuilder().setRecordType("BasicRankedRecord").setFilter(Query.and(Query.rank("score").equalsValue(2), Query.field("gender").equalsValue("F"))).build();
RecordQueryPlan plan = planner.plan(query);
assertEquals("Index(BasicRankedRecord$score [[2],[2]] BY_RANK) ∩ Index(BasicRankedRecord$gender [[F],[F]])", plan.toString());
Set<String> names = new HashSet<>();
Function<FDBQueriedRecord<Message>, String> name = rec -> TestRecordsRankProto.BasicRankedRecord.newBuilder().mergeFrom(rec.getRecord()).getName();
RecordCursor<String> cursor = recordStore.executeQuery(plan, null, ExecuteProperties.newBuilder().setReturnedRowLimit(1).build()).map(name);
RecordCursorResult<String> result = cursor.getNext();
assertTrue(result.hasNext());
names.add(result.get());
cursor = recordStore.executeQuery(plan, result.getContinuation().toBytes(), ExecuteProperties.newBuilder().setReturnedRowLimit(1).build()).map(name);
result = cursor.getNext();
assertTrue(result.hasNext());
names.add(result.get());
cursor = recordStore.executeQuery(plan, result.getContinuation().toBytes(), ExecuteProperties.newBuilder().setReturnedRowLimit(1).build()).map(name);
result = cursor.getNext();
assertFalse(result.hasNext());
assertEquals(Sets.newHashSet("penelope", "helen"), names);
commit(context);
}
}
Aggregations