use of com.apple.foundationdb.record.provider.foundationdb.FDBQueriedRecord in project fdb-record-layer by FoundationDB.
the class SortCursorTests method memorySortContinuations.
@Test
public void memorySortContinuations() throws Exception {
final Function<byte[], RecordCursor<FDBQueriedRecord<Message>>> scanRecords = continuation -> {
final ExecuteProperties executeProperties = ExecuteProperties.newBuilder().setScannedRecordsLimit(20).build();
return recordStore.scanRecords(null, null, EndpointType.TREE_START, EndpointType.TREE_END, continuation, new ScanProperties(executeProperties)).map(FDBQueriedRecord::stored);
};
final MemoryAdapterBase adapter = new MemoryAdapterBase() {
@Override
public int getMaxRecordCountInMemory() {
return 10;
}
};
List<Integer> resultNums = new ArrayList<>();
byte[] continuation = null;
int transactionCount = 0;
do {
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context);
try (RecordCursor<FDBQueriedRecord<Message>> cursor = MemorySortCursor.create(adapter, scanRecords, timer, continuation)) {
while (true) {
RecordCursorResult<FDBQueriedRecord<Message>> result = cursor.getNext();
if (result.hasNext()) {
int num2 = TestRecords1Proto.MySimpleRecord.newBuilder().mergeFrom(result.get().getRecord()).getNumValue2();
resultNums.add(num2);
} else {
continuation = result.getContinuation().toBytes();
break;
}
}
}
transactionCount++;
}
} while (continuation != null);
assertEquals(110, transactionCount);
assertEquals(sortedNums, resultNums);
}
use of com.apple.foundationdb.record.provider.foundationdb.FDBQueriedRecord in project fdb-record-layer by FoundationDB.
the class SortCursorTests method memorySort.
@Test
public void memorySort() throws Exception {
final Function<byte[], RecordCursor<FDBQueriedRecord<Message>>> scanRecords = continuation -> recordStore.scanRecords(null, null, EndpointType.TREE_START, EndpointType.TREE_END, continuation, ScanProperties.FORWARD_SCAN).map(FDBQueriedRecord::stored);
final MemoryAdapterBase adapter = new MemoryAdapterBase() {
@Override
public int getMaxRecordCountInMemory() {
return 20;
}
};
List<Integer> resultNums;
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context);
try (RecordCursor<FDBQueriedRecord<Message>> cursor = MemorySortCursor.create(adapter, scanRecords, timer, null)) {
resultNums = cursor.map(r -> TestRecords1Proto.MySimpleRecord.newBuilder().mergeFrom(r.getRecord()).getNumValue2()).asList().get();
}
}
assertEquals(sortedNums.subList(0, 20), resultNums);
}
use of com.apple.foundationdb.record.provider.foundationdb.FDBQueriedRecord in project fdb-record-layer by FoundationDB.
the class FDBRecordStoreQueryTest method testParameterQuery1.
/**
* Verify that index lookups work with parameterized queries.
*/
@DualPlannerTest
void testParameterQuery1() throws Exception {
RecordMetaDataHook hook = complexQuerySetupHook();
complexQuerySetup(hook);
RecordQuery query = RecordQuery.newBuilder().setRecordType("MySimpleRecord").setFilter(Query.and(Query.field("str_value_indexed").equalsParameter("1"), Query.field("num_value_2").equalsParameter("2"))).build();
// Index(multi_index [EQUALS $1, EQUALS $2])
RecordQueryPlan plan = planner.plan(query);
assertMatchesExactly(plan, indexPlan().where(indexName("multi_index")).and(scanComparisons(range("[EQUALS $1, EQUALS $2]"))));
assertEquals(584809367, plan.planHash(PlanHashable.PlanHashKind.LEGACY));
assertEquals(1148926968, plan.planHash(PlanHashable.PlanHashKind.FOR_CONTINUATION));
assertEquals(1148926968, plan.planHash(PlanHashable.PlanHashKind.STRUCTURAL_WITHOUT_LITERALS));
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context, hook);
for (int attempt = 1; attempt <= 2; attempt++) {
String strValue;
int numValue2;
switch(attempt) {
case 1:
strValue = "even";
numValue2 = 1;
break;
case 2:
default:
strValue = "odd";
numValue2 = 2;
break;
}
Bindings.Builder bindings = Bindings.newBuilder();
bindings.set("1", strValue);
bindings.set("2", numValue2);
EvaluationContext evaluationContext = EvaluationContext.forBindings(bindings.build());
int i = 0;
try (RecordCursorIterator<FDBQueriedRecord<Message>> cursor = plan.execute(recordStore, evaluationContext).asIterator()) {
while (cursor.hasNext()) {
FDBQueriedRecord<Message> rec = cursor.next();
TestRecords1Proto.MySimpleRecord.Builder myrec = TestRecords1Proto.MySimpleRecord.newBuilder();
myrec.mergeFrom(rec.getRecord());
assertEquals(strValue, myrec.getStrValueIndexed());
assertTrue((myrec.getNumValue2() % 3) == numValue2);
i++;
}
}
assertEquals(16, i);
}
assertDiscardedNone(context);
}
}
use of com.apple.foundationdb.record.provider.foundationdb.FDBQueriedRecord in project fdb-record-layer by FoundationDB.
the class FDBRecordStoreQueryTestBase method querySimpleRecordStoreWithContinuation.
/**
* A query execution utility that can handle continuations. This is very similar to the above {@link #querySimpleRecordStore}
* with the additional support for {@link ExecuteProperties} and continuation.
* This method returns the last result encountered. In the case where the row limit was encountered, this would be the one
* result that contains the continuation that should be used on the next call.
* @param recordMetaDataHook Metadata hook to invoke while opening store
* @param plan the plan to execute
* @param contextSupplier provider method to get execution context
* @param continuation execution continuation
* @param executeProperties execution properties to pass into the execute method
* @param checkNumRecords Consumer that verifies correct number of records returned
* @param checkRecord Consumer that asserts every record retrieved
* @param checkDiscarded Consumer that asserts the number of discarded records
* @return the last result from the cursor
* @throws Throwable any thrown exception, or its cause if the exception is a {@link ExecutionException}
*/
protected RecordCursorResult<FDBQueriedRecord<Message>> querySimpleRecordStoreWithContinuation(@Nonnull RecordMetaDataHook recordMetaDataHook, @Nonnull RecordQueryPlan plan, @Nonnull Supplier<EvaluationContext> contextSupplier, @Nullable byte[] continuation, @Nonnull ExecuteProperties executeProperties, @Nonnull Consumer<Integer> checkNumRecords, @Nonnull Consumer<TestRecords1Proto.MySimpleRecord.Builder> checkRecord, @Nonnull Consumer<FDBRecordContext> checkDiscarded) throws Throwable {
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context, recordMetaDataHook);
AtomicInteger i = new AtomicInteger(0);
CompletableFuture<RecordCursorResult<FDBQueriedRecord<Message>>> lastResult;
RecordCursor<FDBQueriedRecord<Message>> cursor = plan.execute(recordStore, contextSupplier.get(), continuation, executeProperties);
lastResult = cursor.forEachResult(result -> {
TestRecords1Proto.MySimpleRecord.Builder myrec = TestRecords1Proto.MySimpleRecord.newBuilder();
myrec.mergeFrom(result.get().getRecord());
checkRecord.accept(myrec);
i.incrementAndGet();
});
lastResult.get();
checkNumRecords.accept(i.get());
checkDiscarded.accept(context);
// TODO a hack until this gets refactored properly
clearStoreCounter(context);
return lastResult.get();
} catch (ExecutionException ex) {
throw ex.getCause();
}
}
use of com.apple.foundationdb.record.provider.foundationdb.FDBQueriedRecord in project fdb-record-layer by FoundationDB.
the class FDBRecordStoreQueryTestBase method querySimpleRecordStore.
protected int querySimpleRecordStore(RecordMetaDataHook recordMetaDataHook, RecordQueryPlan plan, Supplier<EvaluationContext> contextSupplier, TestHelpers.DangerousConsumer<TestRecords1Proto.MySimpleRecord.Builder> checkRecord, TestHelpers.DangerousConsumer<FDBRecordContext> checkDiscarded) throws Exception {
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context, recordMetaDataHook);
int i = 0;
try (RecordCursorIterator<FDBQueriedRecord<Message>> cursor = plan.execute(recordStore, contextSupplier.get()).asIterator()) {
while (cursor.hasNext()) {
FDBQueriedRecord<Message> rec = cursor.next();
TestRecords1Proto.MySimpleRecord.Builder myrec = TestRecords1Proto.MySimpleRecord.newBuilder();
myrec.mergeFrom(rec.getRecord());
checkRecord.accept(myrec);
i++;
}
}
checkDiscarded.accept(context);
// TODO a hack until this gets refactored properly
clearStoreCounter(context);
return i;
}
}
Aggregations