use of com.apple.foundationdb.record.RecordCursor in project fdb-record-layer by FoundationDB.
the class TextIndexTest method performQueryWithRecordStoreScan.
@Nonnull
private Set<Long> performQueryWithRecordStoreScan(@Nonnull RecordMetaDataHook hook, @Nonnull QueryComponent filter) throws Exception {
final ScanProperties scanProperties = new ScanProperties(ExecuteProperties.newBuilder().setTimeLimit(3000).build());
Set<Long> results = new HashSet<>();
byte[] continuation = null;
do {
try (FDBRecordContext context = openContext()) {
openRecordStore(context);
try (RecordCursor<Long> cursor = recordStore.scanRecords(continuation, scanProperties).filter(record -> record.getRecordType().getName().equals(SIMPLE_DOC)).filter(record -> filter.eval(recordStore, EvaluationContext.EMPTY, record) == Boolean.TRUE).map(record -> record.getPrimaryKey().getLong(0))) {
cursor.forEach(results::add).get();
RecordCursorResult<Long> noNextResult = cursor.getNext();
continuation = noNextResult.getContinuation().toBytes();
}
}
} while (continuation != null);
return results;
}
use of com.apple.foundationdb.record.RecordCursor in project fdb-record-layer by FoundationDB.
the class VersionIndexTest method enableRecordVersionsAfterTheFact.
@ParameterizedTest(name = "enableRecordVersionsAfterTheFact [formatVersion = {0}, splitLongRecords = {1}]")
@MethodSource("formatVersionArguments")
@SuppressWarnings("try")
public void enableRecordVersionsAfterTheFact(int testFormatVersion, boolean testSplitLongRecords) throws ExecutionException, InterruptedException {
formatVersion = testFormatVersion;
splitLongRecords = testSplitLongRecords;
MySimpleRecord record1 = MySimpleRecord.newBuilder().setRecNo(871L).setNumValue2(871).build();
MySimpleRecord record2 = MySimpleRecord.newBuilder().setRecNo(1415L).setNumValue2(1415).build();
MySimpleRecord record3 = MySimpleRecord.newBuilder().setRecNo(3415L).setNumValue2(3415).build();
Index globalCountIndex = new Index("globalCount", new GroupingKeyExpression(EmptyKeyExpression.EMPTY, 0), IndexTypes.COUNT);
RecordMetaDataHook origHook = metaDataBuilder -> {
noVersionHook.apply(metaDataBuilder);
metaDataBuilder.addIndex((RecordTypeBuilder) null, globalCountIndex);
};
try (FDBRecordContext context = openContext(origHook)) {
assertFalse(metaData.isStoreRecordVersions());
recordStore.saveRecord(record1);
recordStore.saveRecord(record2);
recordStore.saveRecord(record3, null, FDBRecordStoreBase.VersionstampSaveBehavior.WITH_VERSION);
context.commit();
}
try (FDBRecordContext context = openContext(metaDataBuilder -> {
origHook.apply(metaDataBuilder);
metaDataBuilder.setStoreRecordVersions(true);
functionVersionHook.apply(metaDataBuilder);
})) {
assertTrue(metaData.isStoreRecordVersions());
FDBStoredRecord<Message> storedRecord1 = recordStore.loadRecord(Tuple.from(871L));
assertNotNull(storedRecord1);
assertEquals(record1, storedRecord1.getRecord());
assertFalse(storedRecord1.hasVersion());
FDBStoredRecord<Message> storedRecord2 = recordStore.loadRecord(Tuple.from(1415L));
assertNotNull(storedRecord2);
assertEquals(record2, storedRecord2.getRecord());
assertFalse(storedRecord2.hasVersion());
FDBStoredRecord<Message> storedRecord3 = recordStore.loadRecord(Tuple.from(3415L));
assertNotNull(storedRecord3);
assertEquals(record3, storedRecord3.getRecord());
assertTrue(storedRecord3.hasVersion());
RecordCursor<IndexEntry> cursor = recordStore.scanIndex(metaData.getIndex("MySimpleRecord$maybeVersion"), IndexScanType.BY_VALUE, TupleRange.ALL, null, ScanProperties.FORWARD_SCAN);
assertEquals(Arrays.asList(Tuple.from(null, 1415L), Tuple.from(FDBRecordVersion.MIN_VERSION.toVersionstamp(), 871L), Tuple.from(storedRecord3.getVersion().toVersionstamp(), 3415L)), cursor.map(IndexEntry::getKey).asList().get());
}
}
use of com.apple.foundationdb.record.RecordCursor in project fdb-record-layer by FoundationDB.
the class TextIndexTest method scanMultipleWithScanRecordLimits.
private void scanMultipleWithScanRecordLimits(@Nonnull Index index, @Nonnull List<String> tokens, int scanRecordLimit, boolean reverse) throws Exception {
try (FDBRecordContext context = openContext()) {
openRecordStore(context);
ScanProperties scanProperties = ExecuteProperties.newBuilder().setScannedRecordsLimit(scanRecordLimit).build().asScanProperties(reverse);
List<RecordCursor<IndexEntry>> cursors = tokens.stream().map(token -> recordStore.scanIndex(index, BY_TEXT_TOKEN, TupleRange.allOf(Tuple.from(token)), null, scanProperties)).collect(Collectors.toList());
int cursorIndex = 0;
int retrieved = 0;
while (!cursors.isEmpty()) {
RecordCursor<IndexEntry> cursor = cursors.get(cursorIndex);
RecordCursorResult<IndexEntry> result = cursor.getNext();
if (result.hasNext()) {
retrieved++;
cursorIndex = (cursorIndex + 1) % cursors.size();
} else {
if (!result.getNoNextReason().isSourceExhausted()) {
assertEquals(SCAN_LIMIT_REACHED, result.getNoNextReason());
}
cursors.remove(cursorIndex);
if (cursorIndex == cursors.size()) {
cursorIndex = 0;
}
}
}
// With the order that they are retrieved, the maximum value is the scanRecordLimit
// or the number of tokens.
assertThat(retrieved, lessThanOrEqualTo(Math.max(scanRecordLimit, tokens.size())));
}
}
use of com.apple.foundationdb.record.RecordCursor 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.RecordCursor 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