use of com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext 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.FDBRecordContext in project fdb-record-layer by FoundationDB.
the class RankIndexTest method rankPlusRankIn1.
@Test
public void rankPlusRankIn1() throws Exception {
// Different rank predicates: at most one can be used in the scan.
RecordQuery query = RecordQuery.newBuilder().setRecordType("BasicRankedRecord").setFilter(Query.and(Query.field("gender").equalsValue("M"), Query.rank(Key.Expressions.field("score").ungrouped()).in(Arrays.asList(0L, 2L)), Query.rank(Key.Expressions.field("score").groupBy(Key.Expressions.field("gender"))).lessThanOrEquals(1L))).build();
RecordQueryPlan plan = planner.plan(query);
assertThat(plan, inValues(equalTo(Arrays.asList(0L, 2L)), scoreForRank(containsInAnyOrder(hasToString("__rank_0 = BasicRankedRecord$score.score_for_rank($__in_rank(Field { 'score' None} group 1)__0)")), fetch(filter(rankComparisonFor("score", Comparisons.Type.EQUALS, "__rank_0"), coveringIndexScan(indexScan(allOf(indexName("rank_by_gender"), bounds(hasTupleString("([M, null],[M, 1]]"))))))))));
try (FDBRecordContext context = openContext()) {
openRecordStore(context);
List<String> names = recordStore.executeQuery(plan).map(rec -> TestRecordsRankProto.BasicRankedRecord.newBuilder().mergeFrom(rec.getRecord()).getName()).asList().join();
assertEquals(Arrays.asList("hector"), names);
commit(context);
}
}
use of com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext 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.FDBRecordContext 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.FDBRecordContext in project fdb-record-layer by FoundationDB.
the class RankIndexTest method uniquenessViolationWithTies.
@Test
public void uniquenessViolationWithTies() throws Exception {
// Undo loadRecords.
clearAndInitialize();
assertThrows(RecordIndexUniquenessViolation.class, () -> {
try (FDBRecordContext context = openContext()) {
openRecordStore(context, md -> {
md.addUniversalIndex(FDBRecordStoreTestBase.COUNT_INDEX);
md.removeIndex("rank_by_gender");
md.addIndex("BasicRankedRecord", new Index("unique_rank_by_gender", Key.Expressions.field("score").groupBy(Key.Expressions.field("gender")), EmptyKeyExpression.EMPTY, IndexTypes.RANK, IndexOptions.UNIQUE_OPTIONS));
});
for (Object[] rec : RECORDS) {
recordStore.saveRecord(TestRecordsRankProto.BasicRankedRecord.newBuilder().setName((String) rec[0]).setScore((Integer) rec[1]).setGender((String) rec[2]).build());
}
commit(context);
}
});
}
Aggregations