use of com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext in project fdb-record-layer by FoundationDB.
the class TextIndexTest method saveComplexDocuments.
@Test
public void saveComplexDocuments() throws Exception {
ComplexDocument complexDocument = ComplexDocument.newBuilder().setGroup(0).setDocId(1066L).setText("Very complex. Not to be trifled with.").build();
ComplexDocument shakespeareDocument = ComplexDocument.newBuilder().setGroup(0).setDocId(1623L).setText(TextSamples.ROMEO_AND_JULIET_PROLOGUE).addTag("a").addTag("b").build();
ComplexDocument yiddishDocument = ComplexDocument.newBuilder().setGroup(1).setDocId(1944L).setText(TextSamples.YIDDISH).addTag("c").build();
try (FDBRecordContext context = openContext()) {
openRecordStore(context, metaDataBuilder -> metaDataBuilder.addIndex(COMPLEX_DOC, COMPLEX_TEXT_BY_GROUP));
recordStore.saveRecord(complexDocument);
int firstKeys = getSaveIndexKeyCount(recordStore);
assertEquals(complexDocument.getText().split(" ").length, firstKeys);
recordStore.saveRecord(shakespeareDocument);
int secondKeys = getSaveIndexKeyCount(recordStore) - firstKeys;
assertEquals(82, secondKeys);
recordStore.saveRecord(yiddishDocument);
int thirdKeys = getSaveIndexKeyCount(recordStore) - secondKeys - firstKeys;
assertEquals(9, thirdKeys);
List<Map.Entry<Tuple, List<Integer>>> entryList = scanMapEntries(recordStore, COMPLEX_TEXT_BY_GROUP, Tuple.from(0L, "to"));
assertEquals(Arrays.asList(entryOf(Tuple.from(1066L), Collections.singletonList(3)), entryOf(Tuple.from(1623L), Arrays.asList(18, 108))), entryList);
List<Message> recordList = recordStore.scanIndexRecords(COMPLEX_TEXT_BY_GROUP.getName(), BY_TEXT_TOKEN, TupleRange.allOf(Tuple.from(0L, "to")), null, ScanProperties.FORWARD_SCAN).map(FDBIndexedRecord::getRecord).asList().get();
assertEquals(Arrays.asList(complexDocument, shakespeareDocument), recordList);
entryList = toMapEntries(scanIndex(recordStore, COMPLEX_TEXT_BY_GROUP, TupleRange.prefixedBy("א").prepend(Tuple.from(1L))), null);
assertEquals(Arrays.asList(entryOf(Tuple.from(1L, "א", 1944L), Arrays.asList(0, 3)), entryOf(Tuple.from(1L, "און", 1944L), Collections.singletonList(8)), entryOf(Tuple.from(1L, "איז", 1944L), Collections.singletonList(2)), entryOf(Tuple.from(1L, "אן", 1944L), Collections.singletonList(6)), entryOf(Tuple.from(1L, "ארמיי", 1944L), Collections.singletonList(7))), entryList);
// Read the whole store and make sure the values come back in a somewhat sensible way
entryList = toMapEntries(scanIndex(recordStore, COMPLEX_TEXT_BY_GROUP, TupleRange.ALL), null);
assertEquals(firstKeys + secondKeys + thirdKeys, entryList.size());
int i = 0;
String last = null;
for (Map.Entry<Tuple, List<Integer>> entry : entryList) {
assertEquals(3, entry.getKey().size());
if (i < firstKeys + secondKeys) {
assertEquals(0L, entry.getKey().getLong(0));
assertThat(entry.getKey().getLong(2), anyOf(is(1066L), is(1623L)));
} else {
assertEquals(1L, entry.getKey().getLong(0));
assertEquals(1944L, entry.getKey().getLong(2));
if (i == firstKeys + secondKeys) {
last = null;
}
}
if (last != null) {
assertThat(entry.getKey().getString(1), greaterThanOrEqualTo(last));
}
last = entry.getKey().getString(1);
i++;
}
commit(context);
}
}
use of com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext in project fdb-record-layer by FoundationDB.
the class TextIndexTest method saveSimpleDocumentsWithPositionsOptionChange.
@Test
public void saveSimpleDocumentsWithPositionsOptionChange() throws Exception {
final SimpleDocument shakespeareDocument = SimpleDocument.newBuilder().setDocId(1623L).setText(TextSamples.ROMEO_AND_JULIET_PROLOGUE).build();
final SimpleDocument yiddishDocument = SimpleDocument.newBuilder().setDocId(1945L).setText(TextSamples.YIDDISH).build();
final SimpleDocument frenchDocument = SimpleDocument.newBuilder().setDocId(1871L).setText(TextSamples.FRENCH).build();
// Save one document *with* positions
try (FDBRecordContext context = openContext()) {
openRecordStore(context, metaDataBuilder -> {
metaDataBuilder.addIndex(SIMPLE_DOC, new Index(SIMPLE_TEXT_NO_POSITIONS.getName(), SIMPLE_TEXT_NO_POSITIONS.getRootExpression(), IndexTypes.TEXT));
});
recordStore.saveRecord(shakespeareDocument);
commit(context);
}
// Save one document *without* positions
try (FDBRecordContext context = openContext()) {
openRecordStore(context, metaDataBuilder -> {
metaDataBuilder.addIndex(SIMPLE_DOC, SIMPLE_TEXT_NO_POSITIONS);
});
recordStore.saveRecord(yiddishDocument);
commit(context);
}
// Save one more document *with* positions
try (FDBRecordContext context = openContext()) {
openRecordStore(context, metaDataBuilder -> {
metaDataBuilder.addIndex(SIMPLE_DOC, new Index(SIMPLE_TEXT_NO_POSITIONS.getName(), SIMPLE_TEXT_NO_POSITIONS.getRootExpression(), IndexTypes.TEXT));
});
recordStore.saveRecord(frenchDocument);
List<Map.Entry<Tuple, List<Integer>>> entryList = scanMapEntries(recordStore, SIMPLE_TEXT_NO_POSITIONS, Tuple.from("civil"));
assertEquals(Collections.singletonList(entryOf(Tuple.from(1623L), Arrays.asList(22, 25))), entryList);
entryList = scanMapEntries(recordStore, SIMPLE_TEXT_NO_POSITIONS, Tuple.from("דיאלעקט"));
assertEquals(Collections.singletonList(entryOf(Tuple.from(1945L), Collections.emptyList())), entryList);
entryList = scanMapEntries(recordStore, SIMPLE_TEXT_NO_POSITIONS, Tuple.from("recu"));
assertEquals(Collections.singletonList(entryOf(Tuple.from(1871L), Collections.singletonList(5))), entryList);
commit(context);
}
}
use of com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext in project fdb-record-layer by FoundationDB.
the class PermutedMinMaxIndexTest method deleteWhere.
@Test
public void deleteWhere() {
final RecordMetaDataHook hook = md -> {
final KeyExpression pkey = Key.Expressions.concatenateFields("num_value_2", "num_value_3_indexed", "rec_no");
md.getRecordType("MySimpleRecord").setPrimaryKey(pkey);
md.getRecordType("MyOtherRecord").setPrimaryKey(pkey);
md.removeIndex("MySimpleRecord$str_value_indexed");
md.removeIndex("MySimpleRecord$num_value_3_indexed");
md.removeIndex("MySimpleRecord$num_value_unique");
md.removeIndex(COUNT_INDEX.getName());
md.removeIndex(COUNT_UPDATES_INDEX.getName());
md.addIndex("MySimpleRecord", new Index(INDEX_NAME, Key.Expressions.concatenateFields("num_value_2", "num_value_3_indexed", "str_value_indexed", "num_value_unique").group(1), IndexTypes.PERMUTED_MAX, Collections.singletonMap(IndexOptions.PERMUTED_SIZE_OPTION, "2")));
};
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context, hook);
saveRecord(100, "yes", 1, 1);
saveRecord(150, "yes", 1, 1);
saveRecord(200, "no", 1, 1);
saveRecord(300, "yes", 1, 2);
saveRecord(400, "no", 1, 2);
saveRecord(500, "maybe", 2, 1);
assertEquals(Arrays.asList(Tuple.from(1, 150, 1, "yes"), Tuple.from(1, 200, 1, "no"), Tuple.from(1, 300, 2, "yes"), Tuple.from(1, 400, 2, "no"), Tuple.from(2, 500, 1, "maybe")), scanGroup(Tuple.from(), false));
commit(context);
}
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context, hook);
recordStore.deleteRecordsWhere(Query.field("num_value_2").equalsValue(2));
assertEquals(Arrays.asList(Tuple.from(1, 150, 1, "yes"), Tuple.from(1, 200, 1, "no"), Tuple.from(1, 300, 2, "yes"), Tuple.from(1, 400, 2, "no")), scanGroup(Tuple.from(), false));
commit(context);
}
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context, hook);
assertThrows(Query.InvalidExpressionException.class, () -> {
recordStore.deleteRecordsWhere(Query.and(Query.field("num_value_2").equalsValue(2), Query.field("num_value_3_indexed").equalsValue(1)));
});
}
}
use of com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext in project fdb-record-layer by FoundationDB.
the class RankIndexTest method rankWithoutGroupRestriction.
@Test
public void rankWithoutGroupRestriction() throws Exception {
// Grouped rank in filter but query results include all groups.
RecordQuery query = RecordQuery.newBuilder().setRecordType("BasicRankedRecord").setFilter(Query.rank(Key.Expressions.field("score").groupBy(Key.Expressions.field("gender"))).equalsValue(1L)).build();
RecordQueryPlan plan = planner.plan(query);
assertFalse(plan.hasIndexScan("rank_by_gender"));
assertTrue(plan.hasRecordScan());
try (FDBRecordContext context = openContext()) {
openRecordStore(context);
List<String> names = recordStore.executeQuery(plan).map(rec -> TestRecordsRankProto.BasicRankedRecord.newBuilder().mergeFrom(rec.getRecord()).getName()).asList().join();
assertEquals(Arrays.asList("achilles", "laodice"), names);
commit(context);
}
}
use of com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext in project fdb-record-layer by FoundationDB.
the class RankIndexTest method coveringScoreValues.
@Test
public void coveringScoreValues() throws Exception {
RecordQuery query = RecordQuery.newBuilder().setRecordType("BasicRankedRecord").setFilter(Query.field("gender").equalsValue("M")).setRequiredResults(Arrays.asList(Key.Expressions.field("gender"), Key.Expressions.field("score"))).build();
RecordQueryPlan plan = planner.plan(query);
assertThat(plan, coveringIndexScan(indexScan(allOf(indexName("rank_by_gender"), indexScanType(IndexScanType.BY_VALUE), bounds(hasTupleString("[[M],[M]]"))))));
try (FDBRecordContext context = openContext()) {
openRecordStore(context);
List<Integer> scores = recordStore.executeQuery(plan).map(rec -> TestRecordsRankProto.BasicRankedRecord.newBuilder().mergeFrom(rec.getRecord()).getScore()).asList().join();
assertEquals(Arrays.asList(75, 100), scores);
commit(context);
}
}
Aggregations