use of com.apple.foundationdb.record.TestDataTypesProto.TypesRecord in project fdb-record-layer by FoundationDB.
the class FunctionKeyIndexTest method testUniqueIndexNoViolation.
@Test
public void testUniqueIndexNoViolation() throws Exception {
// Note: the substr() function is defined by KeyExpressionTest.java
Index index = new Index("substr_unique_index", function("substr", concat(field("str_value"), value(0), value(3))), empty(), IndexTypes.VALUE, IndexOptions.UNIQUE_OPTIONS);
Records records = Records.create();
for (int i = 0; i < 10; i++) {
records.add(i, "ab" + Character.toString((char) ('c' + i)) + "_" + i);
}
saveRecords(records, index);
try (FDBRecordContext context = openContext()) {
openRecordStore(context, index);
int count = 0;
for (FDBIndexedRecord<Message> indexedRecord : getIndexRecords(recordStore, "substr_unique_index")) {
TypesRecord record = fromMessage(indexedRecord.getRecord());
assertTrue(records.contains(record), "Record does not exist");
assertEquals(record.getStrValue().substring(0, 3), indexedRecord.getIndexEntry().getKey().getString(0));
++count;
}
assertEquals(records.size(), count);
}
}
use of com.apple.foundationdb.record.TestDataTypesProto.TypesRecord in project fdb-record-layer by FoundationDB.
the class FunctionKeyIndexTest method testIndexScan.
@Test
public void testIndexScan() throws Exception {
// Note: the substr() function is defined by KeyExpressionTest.java
Index index = new Index("substr_index", function("substr", concat(field("str_value"), value(0), value(3))));
Records records = Records.create();
for (int i = 0; i < 10; i++) {
records.add(i, "ab" + Character.toString((char) ('c' + i)) + "_" + i);
}
saveRecords(records, index);
try (FDBRecordContext context = openContext()) {
openRecordStore(context, index);
TupleRange range = new TupleRange(Tuple.from("abd"), Tuple.from("abg_5"), EndpointType.RANGE_INCLUSIVE, EndpointType.RANGE_INCLUSIVE);
List<FDBIndexedRecord<Message>> results = recordStore.getRecordContext().asyncToSync(FDBStoreTimer.Waits.WAIT_SCAN_INDEX_RECORDS, recordStore.scanIndexRecords(index.getName(), IndexScanType.BY_VALUE, range, null, ScanProperties.FORWARD_SCAN).asList());
assertEquals(4, results.size());
for (FDBIndexedRecord<Message> indexedRecord : results) {
TypesRecord record = fromMessage(indexedRecord.getRecord());
assertTrue(records.contains(record), "Record does not exist");
assertEquals(record.getStrValue().substring(0, 3), indexedRecord.getIndexEntry().getKey().getString(0));
}
}
}
use of com.apple.foundationdb.record.TestDataTypesProto.TypesRecord in project fdb-record-layer by FoundationDB.
the class FunctionKeyIndexTest method testOneOfQueryFunctionIndex.
@Test
public void testOneOfQueryFunctionIndex() throws Exception {
Index funcIndex = new Index("chars_index", function("chars", field("str_value")), IndexTypes.VALUE);
Records records = Records.create();
for (int i = 0; i < 10; i++) {
String strValue = (char) ('a' + i) + "_" + (char) ('b' + i);
records.add(i, strValue);
}
saveRecords(records, funcIndex);
QueryComponent filter = Query.keyExpression(funcIndex.getRootExpression()).oneOfThem().equalsValue("c");
RecordQuery query = RecordQuery.newBuilder().setRecordType("TypesRecord").setFilter(filter).build();
// Index(chars_index [[c],[c]])
RecordQueryPlan plan = planner.plan(query);
assertThat(plan, primaryKeyDistinct(indexScan(allOf(indexName(funcIndex.getName()), bounds(hasTupleString("[[c],[c]]"))))));
assertEquals(-76945989, plan.planHash(PlanHashable.PlanHashKind.LEGACY));
assertEquals(1921601810, plan.planHash(PlanHashable.PlanHashKind.FOR_CONTINUATION));
assertEquals(-2031924515, plan.planHash(PlanHashable.PlanHashKind.STRUCTURAL_WITHOUT_LITERALS));
try (FDBRecordContext context = openContext()) {
openRecordStore(context, funcIndex);
int count = 0;
try (RecordCursorIterator<FDBQueriedRecord<Message>> cursor = recordStore.executeQuery(plan).asIterator()) {
while (cursor.hasNext()) {
FDBQueriedRecord<Message> queriedRecord = cursor.next();
TypesRecord record = fromMessage(queriedRecord.getRecord());
assertTrue(records.contains(record));
String str = record.getStrValue();
assertThat(str, containsString("c"));
++count;
}
}
assertEquals(2, count);
assertDiscardedNone(context);
}
}
use of com.apple.foundationdb.record.TestDataTypesProto.TypesRecord in project fdb-record-layer by FoundationDB.
the class FunctionKeyIndexTest method testQueryFunctionIndex.
private void testQueryFunctionIndex(boolean functionQuery) throws Exception {
Index funcIndex = new Index("substr_index", function("substr", concat(field("str_value"), value(0), value(3))), IndexTypes.VALUE);
Index normalIndex = new Index("normal_index", field("str_value"), IndexTypes.VALUE);
Records records = Records.create();
for (int i = 0; i < 10; i++) {
String strValue = "ab" + Character.toString((char) ('c' + i)) + "_" + i;
records.add(i, strValue);
}
saveRecords(records, funcIndex, normalIndex);
QueryComponent filter;
if (functionQuery) {
filter = Query.and(Query.keyExpression(funcIndex.getRootExpression()).greaterThanOrEquals("abd"), Query.keyExpression(funcIndex.getRootExpression()).lessThanOrEquals("abg"));
} else {
filter = Query.and(Query.field("str_value").greaterThanOrEquals("abd"), Query.field("str_value").lessThanOrEquals("abg"));
}
RecordQuery query = RecordQuery.newBuilder().setRecordType("TypesRecord").setFilter(filter).build();
// Index(substr_index [[abd],[abg]])
RecordQueryPlan plan = planner.plan(query);
if (functionQuery) {
assertThat(plan, indexScan(allOf(indexName(funcIndex.getName()), bounds(hasTupleString("[[abd],[abg]]")))));
assertEquals(316561162, plan.planHash(PlanHashable.PlanHashKind.LEGACY));
assertEquals(1660925199, plan.planHash(PlanHashable.PlanHashKind.FOR_CONTINUATION));
assertEquals(33212272, plan.planHash(PlanHashable.PlanHashKind.STRUCTURAL_WITHOUT_LITERALS));
} else {
// Here I'm really just making sure that (a) the substr_index is not selected, because the
// function call doesn't appear in the query anyway and (b) that the planner doesn't throw
// an exception or do something wonky as a result of the presence of this index.
assertThat(plan, indexScan(allOf(indexName(normalIndex.getName()), bounds(hasTupleString("[[abd],[abg]]")))));
assertEquals(1189784448, plan.planHash(PlanHashable.PlanHashKind.LEGACY));
assertEquals(1463869061, plan.planHash(PlanHashable.PlanHashKind.FOR_CONTINUATION));
assertEquals(-163843866, plan.planHash(PlanHashable.PlanHashKind.STRUCTURAL_WITHOUT_LITERALS));
}
try (FDBRecordContext context = openContext()) {
openRecordStore(context, funcIndex, normalIndex);
int count = 0;
try (RecordCursorIterator<FDBQueriedRecord<Message>> cursor = recordStore.executeQuery(plan).asIterator()) {
while (cursor.hasNext()) {
FDBQueriedRecord<Message> queriedRecord = cursor.next();
TypesRecord record = fromMessage(queriedRecord.getRecord());
assertTrue(records.contains(record));
String str = functionQuery ? record.getStrValue().substring(0, 3) : record.getStrValue();
assertThat(str, greaterThanOrEqualTo("abd"));
assertThat(str, lessThanOrEqualTo("abg"));
++count;
}
}
assertEquals(functionQuery ? 4 : 3, count);
assertDiscardedNone(context);
}
}
Aggregations