use of com.apple.foundationdb.record.provider.foundationdb.FDBQueriedRecord in project fdb-record-layer by FoundationDB.
the class RankIndexTest method checkScores.
@Test
public void checkScores() throws Exception {
try (FDBRecordContext context = openContext()) {
openRecordStore(context);
FDBStoredRecord<Message> rec = recordStore.loadRecord(Tuple.from("achilles"));
assertNotNull(rec);
TestRecordsRankProto.BasicRankedRecord.Builder myrec = TestRecordsRankProto.BasicRankedRecord.newBuilder();
myrec.mergeFrom(rec.getRecord());
assertEquals(100, myrec.getScore());
commit(context);
}
RecordQuery query = RecordQuery.newBuilder().setRecordType("BasicRankedRecord").setFilter(Query.field("score").equalsValue(200)).build();
RecordQueryPlan plan = planner.plan(query);
assertEquals("Index(BasicRankedRecord$score [[200],[200]])", plan.toString());
try (FDBRecordContext context = openContext()) {
openRecordStore(context);
int i = 0;
try (RecordCursorIterator<FDBQueriedRecord<Message>> cursor = recordStore.executeQuery(plan).asIterator()) {
while (cursor.hasNext()) {
FDBQueriedRecord<Message> rec = cursor.next();
TestRecordsRankProto.BasicRankedRecord.Builder myrec = TestRecordsRankProto.BasicRankedRecord.newBuilder();
myrec.mergeFrom(rec.getRecord());
assertEquals(200, myrec.getScore());
i++;
}
}
assertEquals(2, i);
}
}
use of com.apple.foundationdb.record.provider.foundationdb.FDBQueriedRecord in project fdb-record-layer by FoundationDB.
the class RankIndexTest method checkRanks.
@Test
public void checkRanks() throws Exception {
try (FDBRecordContext context = openContext()) {
openRecordStore(context);
FDBStoredRecord<Message> rec = recordStore.loadRecord(Tuple.from("achilles"));
assertNotNull(rec);
RecordFunction<Long> rank = Query.rank("score").getFunction();
assertEquals((Long) 1L, recordStore.evaluateRecordFunction(rank, rec).get());
commit(context);
}
RecordQuery query = RecordQuery.newBuilder().setRecordType("BasicRankedRecord").setFilter(Query.rank("score").equalsValue(2L)).build();
RecordQueryPlan plan = planner.plan(query);
assertEquals("Index(BasicRankedRecord$score [[2],[2]] BY_RANK)", plan.toString());
try (FDBRecordContext context = openContext()) {
openRecordStore(context);
int i = 0;
try (RecordCursorIterator<FDBQueriedRecord<Message>> cursor = recordStore.executeQuery(plan).asIterator()) {
while (cursor.hasNext()) {
FDBQueriedRecord<Message> rec = cursor.next();
TestRecordsRankProto.BasicRankedRecord.Builder myrec = TestRecordsRankProto.BasicRankedRecord.newBuilder();
myrec.mergeFrom(rec.getRecord());
assertEquals(200, myrec.getScore());
i++;
}
}
// 2 records tied for this rank.
assertEquals(2, i);
}
}
use of com.apple.foundationdb.record.provider.foundationdb.FDBQueriedRecord in project fdb-record-layer by FoundationDB.
the class VersionIndexTest method queryOnRepeatedVersion.
@ParameterizedTest(name = "queryOnRepeatedVersions [formatVersion = {0}, splitLongRecords = {1}]")
@MethodSource("formatVersionArguments")
@SuppressWarnings("try")
public void queryOnRepeatedVersion(int testFormatVersion, boolean testSplitLongRecords) {
formatVersion = testFormatVersion;
splitLongRecords = testSplitLongRecords;
List<MySimpleRecord> simpleRecords = IntStream.range(0, 30).mapToObj(id -> {
MySimpleRecord.Builder builder = MySimpleRecord.newBuilder().setRecNo(id * 2).setNumValue2(id % 2).setNumValue3Indexed(id % 3);
for (int i = 0; i < id % 3; i++) {
builder.addRepeater(i);
}
return builder.build();
}).collect(Collectors.toList());
Iterator<MySimpleRecord> simpleIterator = simpleRecords.iterator();
while (simpleIterator.hasNext()) {
try (FDBRecordContext context = openContext(repeatedVersionHook)) {
int done = 0;
while (simpleIterator.hasNext() && done != 5) {
recordStore.saveRecord(simpleIterator.next());
done += 1;
}
context.commit();
}
}
try (FDBRecordContext context = openContext(repeatedVersionHook)) {
List<Long> expectedKeys = simpleRecords.stream().filter(rec -> rec.getRepeaterList().contains(1)).map(MySimpleRecord::getRecNo).collect(Collectors.toList());
FDBRecordVersion last = null;
List<Long> receivedKeys = new ArrayList<>();
int totalSeen = 0;
while (true) {
RecordQueryPlan plan;
if (last == null) {
RecordQuery query = RecordQuery.newBuilder().setRecordType("MySimpleRecord").setFilter(Query.field("repeater").oneOfThem().equalsValue(1)).setSort(VersionKeyExpression.VERSION).setRemoveDuplicates(false).build();
plan = planner.plan(query);
assertEquals("Index(MySimpleRecord$repeater-version [[1],[1]])", plan.toString());
} else {
RecordQuery query = RecordQuery.newBuilder().setRecordType("MySimpleRecord").setFilter(Query.and(Query.field("repeater").oneOfThem().equalsValue(1), Query.version().greaterThan(last))).setSort(VersionKeyExpression.VERSION).setRemoveDuplicates(false).build();
plan = planner.plan(query);
assertEquals("Index(MySimpleRecord$repeater-version ([1, " + last.toVersionstamp() + "],[1]])", plan.toString());
}
RecordCursorIterator<FDBQueriedRecord<Message>> cursor = recordStore.executeQuery(plan, null, ExecuteProperties.newBuilder().setReturnedRowLimit(4).build()).asIterator();
boolean hasAny = false;
while (cursor.hasNext()) {
hasAny = true;
FDBQueriedRecord<Message> record = cursor.next();
assertTrue(record.hasVersion());
if (last != null) {
assertThat(last, lessThan(record.getVersion()));
}
last = record.getVersion();
MySimpleRecord simpleRecord = MySimpleRecord.newBuilder().mergeFrom(record.getRecord()).build();
assertThat(simpleRecord.getRepeaterList(), hasItem(1));
receivedKeys.add(field("rec_no").evaluateSingleton(record.getStoredRecord()).toTuple().getLong(0));
totalSeen += 1;
}
if (!hasAny) {
break;
}
}
assertEquals(simpleRecords.size() / 3, totalSeen);
assertEquals(expectedKeys, receivedKeys);
}
}
use of com.apple.foundationdb.record.provider.foundationdb.FDBQueriedRecord in project fdb-record-layer by FoundationDB.
the class BitmapValueIndexTest method andNotQuery.
@Test
void andNotQuery() {
try (FDBRecordContext context = openContext()) {
createOrOpenRecordStore(context, metaData(REC_NO_BY_STR_NUMS_HOOK));
saveRecords(100, 200);
commit(context);
}
try (FDBRecordContext context = openContext()) {
createOrOpenRecordStore(context, metaData(REC_NO_BY_STR_NUMS_HOOK));
setupPlanner(null);
// Covering(Index(rec_no_by_str_num2 [[odd, 1],[odd, 1]] BY_GROUP) -> [rec_no: KEY[2]]) BITAND BITNOT Covering(Index(rec_no_by_str_num3 [[odd, 2],[odd, 2]] BY_GROUP) -> [rec_no: KEY[2]])
final RecordQueryPlan queryPlan = plan(BITMAP_VALUE_REC_NO_BY_STR, Query.and(Query.field("str_value").equalsValue("odd"), Query.field("num_value_2").equalsValue(1), Query.not(Query.field("num_value_3").equalsValue(2))));
assertThat(queryPlan, compositeBitmap(hasToString("[0] BITAND BITNOT [1]"), Arrays.asList(coveringIndexScan(indexScan(allOf(indexName("rec_no_by_str_num2"), indexScanType(IndexScanType.BY_GROUP), bounds(hasTupleString("[[odd, 1],[odd, 1]]"))))), coveringIndexScan(indexScan(allOf(indexName("rec_no_by_str_num3"), indexScanType(IndexScanType.BY_GROUP), bounds(hasTupleString("[[odd, 2],[odd, 2]]"))))))));
assertEquals(1339577551, queryPlan.planHash(PlanHashable.PlanHashKind.LEGACY));
assertEquals(17236339, queryPlan.planHash(PlanHashable.PlanHashKind.FOR_CONTINUATION));
assertEquals(2043204530, queryPlan.planHash(PlanHashable.PlanHashKind.STRUCTURAL_WITHOUT_LITERALS));
assertThat(collectOnBits(queryPlan.execute(recordStore).map(FDBQueriedRecord::getIndexEntry)), equalTo(IntStream.range(100, 200).boxed().filter(i -> (i & 1) == 1).filter(i -> (i % 7) == 1 && !((i % 5) == 2)).collect(Collectors.toList())));
}
}
use of com.apple.foundationdb.record.provider.foundationdb.FDBQueriedRecord in project fdb-record-layer by FoundationDB.
the class BitmapValueIndexTest method singleQuery.
@Test
void singleQuery() {
try (FDBRecordContext context = openContext()) {
createOrOpenRecordStore(context, metaData(REC_NO_BY_STR_NUMS_HOOK));
saveRecords(100, 200);
commit(context);
}
try (FDBRecordContext context = openContext()) {
createOrOpenRecordStore(context, metaData(REC_NO_BY_STR_NUMS_HOOK));
setupPlanner(null);
final RecordQueryPlan queryPlan = plan(BITMAP_VALUE_REC_NO_BY_STR, Query.and(Query.field("str_value").equalsValue("odd"), Query.field("num_value_2").equalsValue(3)));
assertThat(queryPlan, coveringIndexScan(indexScan(allOf(indexName("rec_no_by_str_num2"), indexScanType(IndexScanType.BY_GROUP), bounds(hasTupleString("[[odd, 3],[odd, 3]]"))))));
assertEquals(1188586655, queryPlan.planHash());
assertThat(collectOnBits(queryPlan.execute(recordStore).map(FDBQueriedRecord::getIndexEntry)), equalTo(IntStream.range(100, 200).boxed().filter(i -> (i & 1) == 1).filter(i -> (i % 7) == 3).collect(Collectors.toList())));
}
}
Aggregations