use of com.apple.foundationdb.record.query.expressions.QueryRecordFunction in project fdb-record-layer by FoundationDB.
the class RankIndexTest method queryWithRanks.
@Test
public void queryWithRanks() throws Exception {
RecordQuery query = RecordQuery.newBuilder().setRecordType("BasicRankedRecord").setFilter(Query.field("gender").equalsValue("M")).setSort(Key.Expressions.field("score")).build();
RecordQueryPlan plan = planner.plan(query);
QueryRecordFunction<Long> ranker = Query.rank(Key.Expressions.field("score").groupBy(Key.Expressions.field("gender")));
try (FDBRecordContext context = openContext()) {
openRecordStore(context);
try (RecordCursorIterator<? extends Pair<Message, Long>> cursor = recordStore.executeQuery(plan).mapPipelined(record -> ranker.eval(recordStore, EvaluationContext.EMPTY, record.getStoredRecord()).thenApply(rank -> new ImmutablePair<>(record.getRecord(), rank)), recordStore.getPipelineSize(PipelineOperation.RECORD_FUNCTION)).asIterator()) {
long rank = 0;
while (cursor.hasNext()) {
Pair<Message, Long> recWithRank = cursor.next();
TestRecordsRankProto.BasicRankedRecord.Builder myrec = TestRecordsRankProto.BasicRankedRecord.newBuilder();
myrec.mergeFrom(recWithRank.getLeft());
assertEquals((Long) rank++, recWithRank.getRight());
}
}
}
}
Aggregations