use of com.apple.foundationdb.record.RecordFunction in project fdb-record-layer by FoundationDB.
the class RankIndexTest method testForRankUpdateTimingError.
@Test
@Tag(Tags.Slow)
public void testForRankUpdateTimingError() throws Exception {
fdb = FDBDatabaseFactory.instance().getDatabase();
// The NPE happens every so often, so I'm doing it 20 times as that seems to be "enough".
for (int i = 0; i < 20; i++) {
int score = 100 * (i + 1) + 1000;
TestRecordsRankProto.BasicRankedRecord record1 = TestRecordsRankProto.BasicRankedRecord.newBuilder().setName("patroclus").setScore(score).setGender("M").build();
score += 50;
TestRecordsRankProto.BasicRankedRecord record2 = TestRecordsRankProto.BasicRankedRecord.newBuilder().setName("patroclus").setScore(score).setGender("M").build();
try (FDBRecordContext context = openContext()) {
openRecordStore(context);
recordStore.saveRecord(record1);
FDBStoredRecord<Message> storedRecord2 = recordStore.saveRecord(record2);
RecordFunction<Long> rankFunction = Query.rank("score").getFunction();
Long rank = recordStore.evaluateRecordFunction(rankFunction, storedRecord2).get();
assertNotNull(rank);
RecordQuery query = RecordQuery.newBuilder().setRecordType("BasicRankedRecord").setFilter(Query.rank("score").equalsValue(rank)).build();
RecordQueryPlan plan = planner.plan(query);
assertEquals("Index(BasicRankedRecord$score [[" + rank + "],[" + rank + "]] BY_RANK)", plan.toString());
recordStore.executeQuery(plan).map(rec -> TestRecordsRankProto.BasicRankedRecord.newBuilder().mergeFrom(rec.getRecord()).getName()).asList().thenAccept(list -> assertTrue(list.contains(record2.getName()))).get();
recordStore.deleteRecord(Tuple.from("patroclus"));
commit(context);
} catch (NullPointerException e) {
e.printStackTrace();
fail("Null pointer exception: " + e.getMessage());
}
}
}
use of com.apple.foundationdb.record.RecordFunction in project fdb-record-layer by FoundationDB.
the class RankComparisons method findComparison.
private void findComparison(@Nonnull QueryRecordFunctionWithComparison comparison, @Nonnull List<Index> indexes, @Nonnull List<QueryComponent> potentialGroupFilters, @Nonnull AtomicInteger counter) {
RecordFunction<?> recordFunction = comparison.getFunction();
// TODO: Should share with indexMaintainerForAggregateFunction
// TODO: Move index-specific query planning behavior outside of planner (https://github.com/FoundationDB/fdb-record-layer/issues/17)
List<String> requiredIndexTypes;
if (recordFunction.getName().equals(FunctionNames.RANK)) {
requiredIndexTypes = Arrays.asList(IndexTypes.RANK, IndexTypes.TIME_WINDOW_LEADERBOARD);
} else if (recordFunction.getName().equals(FunctionNames.TIME_WINDOW_RANK)) {
requiredIndexTypes = Collections.singletonList(IndexTypes.TIME_WINDOW_LEADERBOARD);
} else {
requiredIndexTypes = null;
}
if (requiredIndexTypes != null) {
final GroupingKeyExpression operand = ((IndexRecordFunction) recordFunction).getOperand();
Optional<Index> matchingIndex = indexes.stream().filter(index -> requiredIndexTypes.contains(index.getType()) && index.getRootExpression().equals(operand)).min(Comparator.comparing(Index::getColumnSize));
if (matchingIndex.isPresent()) {
final KeyExpression groupBy = operand.getGroupingSubKey();
final List<QueryComponent> groupFilters = new ArrayList<>();
final List<Comparisons.Comparison> groupComparisons = new ArrayList<>();
if (!GroupingValidator.findGroupKeyFilters(potentialGroupFilters, groupBy, groupFilters, groupComparisons)) {
return;
}
QueryComponent substitute = null;
String bindingName = null;
final Comparisons.Type comparisonType = comparison.getComparison().getType();
if (!operand.createsDuplicates() && !comparisonType.isUnary()) {
bindingName = Bindings.Internal.RANK.bindingName(Integer.toString(counter.getAndIncrement()));
Comparisons.Comparison substituteComparison = new Comparisons.ParameterComparison(comparisonType, bindingName, Bindings.Internal.RANK);
final KeyExpression grouped = operand.getGroupedSubKey();
if (grouped instanceof FieldKeyExpression) {
substitute = new FieldWithComparison(((FieldKeyExpression) grouped).getFieldName(), substituteComparison);
} else if (grouped instanceof NestingKeyExpression) {
NestingKeyExpression nesting = (NestingKeyExpression) grouped;
if (nesting.getChild() instanceof FieldKeyExpression) {
substitute = new NestedField(nesting.getParent().getFieldName(), new FieldWithComparison(((FieldKeyExpression) nesting.getChild()).getFieldName(), substituteComparison));
}
}
if (substitute == null) {
bindingName = null;
}
}
comparisons.put(comparison, new RankComparison(comparison, matchingIndex.get(), groupFilters, groupComparisons, substitute, bindingName));
}
}
}
Aggregations