22
22
23
23
import com .apple .foundationdb .record .EvaluationContext ;
24
24
import com .apple .foundationdb .record .ExecuteProperties ;
25
+ import com .apple .foundationdb .record .ExecuteState ;
25
26
import com .apple .foundationdb .record .RecordCursor ;
26
27
import com .apple .foundationdb .record .RecordCursorResult ;
27
28
import com .apple .foundationdb .record .RecordMetaData ;
29
+ import com .apple .foundationdb .record .RecordScanLimiterFactory ;
28
30
import com .apple .foundationdb .record .TestRecords1Proto ;
29
31
import com .apple .foundationdb .record .provider .foundationdb .FDBRecordContext ;
30
32
import com .apple .foundationdb .record .query .plan .ScanComparisons ;
@@ -292,36 +294,56 @@ void test(final boolean useNestedResult) {
292
294
final var plan =
293
295
new AggregationPlanBuilder (recordStore .getRecordMetaData (), "MySimpleRecord" )
294
296
.withAggregateValue ("num_value_2" , value -> new NumericAggregationValue .Sum (NumericAggregationValue .PhysicalOperator .SUM_I , value ))
295
- .withGroupCriterion ("num_value_3_indexed" )
296
297
.withGroupCriterion ("str_value_indexed" )
297
298
.build (useNestedResult );
298
299
299
- final var result = executePlanWithRecordScanLimit (plan , 2 );
300
- assertResults (useNestedResult ? this ::assertResultNested : this ::assertResultFlattened , result , resultOf (0 , "0" , 1 ), resultOf (1 , "0" , 2 ), resultOf (1 , "1" , 3 ), resultOf (2 , "1" , 9 ));
300
+ /*
301
+ num_value_2 str_value_indexed
302
+ 0 "0"
303
+ 1 "0"
304
+ 2 "0"
305
+ */
306
+ /*
307
+ RecordCursorResult<QueryResult> result1 = executePlanWithRecordScanLimit(plan, 1, null, useNestedResult,
308
+ List.of(resultOf("0", 0)));
309
+ RecordCursorResult<QueryResult> result2 = executePlanWithRecordScanLimit(plan, 2, null, useNestedResult,
310
+ List.of(resultOf("0", 1)));
311
+ // only scanned 2 rows?
312
+ RecordCursorResult<QueryResult> result3 = executePlanWithRecordScanLimit(plan, 3, null, useNestedResult,
313
+ List.of(resultOf("0", 1)));
314
+
315
+ */
316
+ // only scanned 3 rows?
317
+ RecordCursorResult <QueryResult > result4 = executePlanWithRecordScanLimit (plan , 4 , null , useNestedResult ,
318
+ List .of (resultOf ("0" , 3 )));
319
+ /*
320
+ RecordCursorResult<QueryResult> result5 = executePlanWithRecordScanLimit(plan, 5, null, useNestedResult,
321
+ List.of(resultOf("0", 3), resultOf("1", 3)));
322
+ RecordCursorResult<QueryResult> result6 = executePlanWithRecordScanLimit(plan, 6, null, useNestedResult,
323
+ List.of(resultOf("0", 3), resultOf("1", 7)));
324
+ RecordCursorResult<QueryResult> result7 = executePlanWithRecordScanLimit(plan, 7, null, useNestedResult,
325
+ List.of(resultOf("0", 3), resultOf("1", 12)));
326
+
327
+ */
301
328
}
302
329
}
303
330
304
- private List <QueryResult > executePlanWithRecordScanLimit (final RecordQueryPlan plan , final int recordScanLimit ) {
305
- byte [] continuation = null ;
306
- List <QueryResult > queryResults = new LinkedList <>();
331
+ private RecordCursorResult <QueryResult > executePlanWithRecordScanLimit (final RecordQueryPlan plan , final int recordScanLimit , byte [] continuation , final boolean useNestedResult , List <?> expectedResult ) {
332
+ RecordCursorResult < QueryResult > currentCursorResult ;
333
+ List <QueryResult > currentQueryResults = new LinkedList <>();
307
334
while (true ) {
308
335
RecordCursor <QueryResult > currentCursor = executePlan (plan , 0 , recordScanLimit , continuation );
309
- RecordCursorResult <QueryResult > currentCursorResult ;
310
- while (true ) {
311
- currentCursorResult = currentCursor .getNext ();
312
- continuation = currentCursorResult .getContinuation ().toBytes ();
313
- if (!currentCursorResult .hasNext ()) {
314
- break ;
315
- }
316
- queryResults .add (currentCursorResult .get ());
317
- System .out .println ("current result:" + currentCursorResult .get ().getMessage ());
318
- }
319
- System .out .println ("getNoNextReson:" + currentCursorResult .getNoNextReason ());
320
- if (currentCursorResult .getNoNextReason () == RecordCursor .NoNextReason .SOURCE_EXHAUSTED ) {
336
+ currentCursorResult = currentCursor .getNext ();
337
+ continuation = currentCursorResult .getContinuation ().toBytes ();
338
+ if (!currentCursorResult .hasNext ()) {
321
339
break ;
322
340
}
341
+ currentQueryResults .add (currentCursorResult .get ());
342
+ System .out .println ("current result:" + currentCursorResult .get ().getMessage ());
323
343
}
324
- return queryResults ;
344
+ assertResults (useNestedResult ? this ::assertResultNested : this ::assertResultFlattened , currentQueryResults , expectedResult .toArray (new List <?>[0 ]));
345
+ System .out .println ("getNoNextReson:" + currentCursorResult .getNoNextReason ());
346
+ return currentCursorResult ;
325
347
}
326
348
327
349
private void populateDB (final int numRecords ) throws Exception {
@@ -341,11 +363,19 @@ private void populateDB(final int numRecords) throws Exception {
341
363
}
342
364
343
365
@ Nonnull
344
- private List <QueryResult > executePlan (final RecordQueryPlan plan ) {
366
+ private RecordCursor <QueryResult > executePlan (final RecordQueryPlan plan , final int rowLimit , final int recordScanLimit , final byte [] continuation ) {
345
367
final var types = plan .getDynamicTypes ();
346
368
final var typeRepository = TypeRepository .newBuilder ().addAllTypes (types ).build ();
369
+ ExecuteState executeState ;
370
+ if (recordScanLimit > 0 ) {
371
+ executeState = new ExecuteState (RecordScanLimiterFactory .enforce (recordScanLimit ), null );
372
+ } else {
373
+ executeState = ExecuteState .NO_LIMITS ;
374
+ }
375
+ ExecuteProperties executeProperties = ExecuteProperties .SERIAL_EXECUTE ;
376
+ executeProperties = executeProperties .setReturnedRowLimit (rowLimit ).setState (executeState );
347
377
try {
348
- return plan .executePlan (recordStore , EvaluationContext .forTypeRepository (typeRepository ), null , ExecuteProperties . SERIAL_EXECUTE ). asList (). get ( );
378
+ return plan .executePlan (recordStore , EvaluationContext .forTypeRepository (typeRepository ), continuation , executeProperties );
349
379
} catch (final Throwable t ) {
350
380
throw Assertions .<RuntimeException >fail (t );
351
381
}
0 commit comments