Search in sources :

Example 76 with FDBRecordContext

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);
    }
}
Also used : FDBIndexedRecord(com.apple.foundationdb.record.provider.foundationdb.FDBIndexedRecord) KeyValueLogMessage(com.apple.foundationdb.record.logging.KeyValueLogMessage) Message(com.google.protobuf.Message) Matchers.containsString(org.hamcrest.Matchers.containsString) PlanMatchers.hasTupleString(com.apple.foundationdb.record.query.plan.match.PlanMatchers.hasTupleString) ComplexDocument(com.apple.foundationdb.record.TestRecordsTextProto.ComplexDocument) BunchedMapScanEntry(com.apple.foundationdb.map.BunchedMapScanEntry) IndexEntry(com.apple.foundationdb.record.IndexEntry) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) BunchedMap(com.apple.foundationdb.map.BunchedMap) ImmutableMap(com.google.common.collect.ImmutableMap) Tuple(com.apple.foundationdb.tuple.Tuple) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 77 with FDBRecordContext

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);
    }
}
Also used : BunchedMapScanEntry(com.apple.foundationdb.map.BunchedMapScanEntry) IndexEntry(com.apple.foundationdb.record.IndexEntry) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) SimpleDocument(com.apple.foundationdb.record.TestRecordsTextProto.SimpleDocument) Index(com.apple.foundationdb.record.metadata.Index) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 78 with FDBRecordContext

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)));
        });
    }
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Query(com.apple.foundationdb.record.query.expressions.Query) Arrays(java.util.Arrays) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) TestRecords1Proto(com.apple.foundationdb.record.TestRecords1Proto) IndexOptions(com.apple.foundationdb.record.metadata.IndexOptions) Tags(com.apple.test.Tags) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) TupleRange(com.apple.foundationdb.record.TupleRange) Key(com.apple.foundationdb.record.metadata.Key) FDBRecordStoreTestBase(com.apple.foundationdb.record.provider.foundationdb.FDBRecordStoreTestBase) Test(org.junit.jupiter.api.Test) IndexScanType(com.apple.foundationdb.record.IndexScanType) Tuple(com.apple.foundationdb.tuple.Tuple) List(java.util.List) Index(com.apple.foundationdb.record.metadata.Index) ScanProperties(com.apple.foundationdb.record.ScanProperties) TupleHelpers(com.apple.foundationdb.tuple.TupleHelpers) IndexTypes(com.apple.foundationdb.record.metadata.IndexTypes) Tag(org.junit.jupiter.api.Tag) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Collections(java.util.Collections) Query(com.apple.foundationdb.record.query.expressions.Query) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) Index(com.apple.foundationdb.record.metadata.Index) Test(org.junit.jupiter.api.Test)

Example 79 with FDBRecordContext

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);
    }
}
Also used : RecordQueryPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) Matchers.hasToString(org.hamcrest.Matchers.hasToString) PlanMatchers.hasTupleString(com.apple.foundationdb.record.query.plan.match.PlanMatchers.hasTupleString) RecordQuery(com.apple.foundationdb.record.query.RecordQuery) Test(org.junit.jupiter.api.Test)

Example 80 with FDBRecordContext

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);
    }
}
Also used : RecordQueryPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) RecordQuery(com.apple.foundationdb.record.query.RecordQuery) Test(org.junit.jupiter.api.Test)

Aggregations

FDBRecordContext (com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext)542 Test (org.junit.jupiter.api.Test)365 RecordQueryPlan (com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan)226 RecordQuery (com.apple.foundationdb.record.query.RecordQuery)215 Message (com.google.protobuf.Message)187 FDBQueriedRecord (com.apple.foundationdb.record.provider.foundationdb.FDBQueriedRecord)170 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)165 Tuple (com.apple.foundationdb.tuple.Tuple)147 Tag (org.junit.jupiter.api.Tag)136 Tags (com.apple.test.Tags)129 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)123 List (java.util.List)121 Index (com.apple.foundationdb.record.metadata.Index)119 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)114 ArrayList (java.util.ArrayList)112 Collections (java.util.Collections)100 Query (com.apple.foundationdb.record.query.expressions.Query)97 RecordQueryPlanner (com.apple.foundationdb.record.query.plan.RecordQueryPlanner)96 Arrays (java.util.Arrays)94 ExecuteProperties (com.apple.foundationdb.record.ExecuteProperties)93