Search in sources :

Example 81 with Index

use of com.apple.foundationdb.record.metadata.Index in project fdb-record-layer by FoundationDB.

the class TextIndexTest method indexArguments.

@Nonnull
public static Stream<Arguments> indexArguments() {
    RecordMetaDataBuilder metaDataBuilder = RecordMetaData.newBuilder().setRecords(TestRecordsTextProto.getDescriptor());
    Index simpleIndex = metaDataBuilder.getIndex(TextIndexTestUtils.SIMPLE_DEFAULT_NAME);
    simpleIndex.setSubspaceKey(TextIndexTestUtils.SIMPLE_DEFAULT_NAME + "-2");
    return Stream.of(simpleIndex, SIMPLE_TEXT_FILTERING, SIMPLE_TEXT_PREFIX, SIMPLE_TEXT_SUFFIXES).map(Arguments::of);
}
Also used : RecordMetaDataBuilder(com.apple.foundationdb.record.RecordMetaDataBuilder) Arguments(org.junit.jupiter.params.provider.Arguments) Index(com.apple.foundationdb.record.metadata.Index) Nonnull(javax.annotation.Nonnull)

Example 82 with Index

use of com.apple.foundationdb.record.metadata.Index in project fdb-record-layer by FoundationDB.

the class TextIndexTest method invalidScans.

@Test
public void invalidScans() throws Exception {
    try (FDBRecordContext context = openContext()) {
        openRecordStore(context);
        final Index index = recordStore.getRecordMetaData().getIndex(TextIndexTestUtils.SIMPLE_DEFAULT_NAME);
        assertThrows(RecordCoreException.class, () -> recordStore.scanIndex(index, BY_VALUE, TupleRange.ALL, null, ScanProperties.REVERSE_SCAN));
        assertThrows(RecordCoreException.class, () -> recordStore.scanIndex(index, BY_GROUP, TupleRange.ALL, null, ScanProperties.REVERSE_SCAN));
        assertThrows(RecordCoreException.class, () -> recordStore.scanIndex(index, BY_RANK, TupleRange.ALL, null, ScanProperties.REVERSE_SCAN));
        assertThrows(RecordCoreException.class, () -> recordStore.scanIndex(index, BY_TIME_WINDOW, TupleRange.ALL, null, ScanProperties.REVERSE_SCAN));
    }
}
Also used : FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) Index(com.apple.foundationdb.record.metadata.Index) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 83 with Index

use of com.apple.foundationdb.record.metadata.Index 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 84 with Index

use of com.apple.foundationdb.record.metadata.Index 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 85 with Index

use of com.apple.foundationdb.record.metadata.Index in project fdb-record-layer by FoundationDB.

the class RankIndexTest method uniquenessViolationWithTies.

@Test
public void uniquenessViolationWithTies() throws Exception {
    // Undo loadRecords.
    clearAndInitialize();
    assertThrows(RecordIndexUniquenessViolation.class, () -> {
        try (FDBRecordContext context = openContext()) {
            openRecordStore(context, md -> {
                md.addUniversalIndex(FDBRecordStoreTestBase.COUNT_INDEX);
                md.removeIndex("rank_by_gender");
                md.addIndex("BasicRankedRecord", new Index("unique_rank_by_gender", Key.Expressions.field("score").groupBy(Key.Expressions.field("gender")), EmptyKeyExpression.EMPTY, IndexTypes.RANK, IndexOptions.UNIQUE_OPTIONS));
            });
            for (Object[] rec : RECORDS) {
                recordStore.saveRecord(TestRecordsRankProto.BasicRankedRecord.newBuilder().setName((String) rec[0]).setScore((Integer) rec[1]).setGender((String) rec[2]).build());
            }
            commit(context);
        }
    });
}
Also used : FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) Index(com.apple.foundationdb.record.metadata.Index) Test(org.junit.jupiter.api.Test)

Aggregations

Index (com.apple.foundationdb.record.metadata.Index)347 Test (org.junit.jupiter.api.Test)262 List (java.util.List)183 IndexTypes (com.apple.foundationdb.record.metadata.IndexTypes)178 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)171 Collections (java.util.Collections)168 Message (com.google.protobuf.Message)166 Tags (com.apple.test.Tags)156 Tag (org.junit.jupiter.api.Tag)156 Expressions.field (com.apple.foundationdb.record.metadata.Key.Expressions.field)155 Query (com.apple.foundationdb.record.query.expressions.Query)151 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)150 Nonnull (javax.annotation.Nonnull)147 Key (com.apple.foundationdb.record.metadata.Key)143 Tuple (com.apple.foundationdb.tuple.Tuple)142 Arrays (java.util.Arrays)139 RecordCoreException (com.apple.foundationdb.record.RecordCoreException)138 RecordQuery (com.apple.foundationdb.record.query.RecordQuery)138 ArrayList (java.util.ArrayList)135 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)134