Search in sources :

Example 81 with KeyExpression

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

the class KeyExpressionTest method testNestedThenRepeatsConcatentated.

@Test
public void testNestedThenRepeatsConcatentated() throws Exception {
    final KeyExpression expression = field("nesty").nest("repeated_field", FanType.Concatenate);
    expression.validate(NestedField.getDescriptor());
    assertFalse(expression.createsDuplicates());
    assertEquals(Collections.singletonList(scalar(Arrays.asList("lily", "rose"))), evaluate(expression, matryoshkaDolls));
    assertEquals(Collections.singletonList(scalar(Collections.emptyList())), evaluate(expression, emptyNested));
    assertEquals(Collections.singletonList(scalar(Collections.emptyList())), evaluate(expression, lonelyDoll));
    assertEquals(Collections.singletonList(scalar(Collections.emptyList())), evaluate(expression, null));
}
Also used : ListKeyExpression(com.apple.foundationdb.record.metadata.expressions.ListKeyExpression) GroupingKeyExpression(com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) QueryableKeyExpression(com.apple.foundationdb.record.metadata.expressions.QueryableKeyExpression) FunctionKeyExpression(com.apple.foundationdb.record.metadata.expressions.FunctionKeyExpression) FieldKeyExpression(com.apple.foundationdb.record.metadata.expressions.FieldKeyExpression) NestingKeyExpression(com.apple.foundationdb.record.metadata.expressions.NestingKeyExpression) SplitKeyExpression(com.apple.foundationdb.record.metadata.expressions.SplitKeyExpression) EmptyKeyExpression(com.apple.foundationdb.record.metadata.expressions.EmptyKeyExpression) ThenKeyExpression(com.apple.foundationdb.record.metadata.expressions.ThenKeyExpression) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 82 with KeyExpression

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

the class KeyExpressionTest method testNestedThenConatenatedFields.

@Test
public void testNestedThenConatenatedFields() throws Exception {
    final KeyExpression expression = field("nesty").nest(concatenateFields("regular_old_field", "regular_int_field"));
    expression.validate(NestedField.getDescriptor());
    assertFalse(expression.createsDuplicates());
    assertEquals(Collections.singletonList(Key.Evaluated.concatenate("Mother", 1066)), evaluate(expression, matryoshkaDolls));
    assertEquals(Collections.singletonList(Key.Evaluated.concatenate(NULL, NULL)), evaluate(expression, emptyNested));
    assertEquals(Collections.singletonList(Key.Evaluated.concatenate(NULL, NULL)), evaluate(expression, lonelyDoll));
    assertEquals(Collections.singletonList(Key.Evaluated.concatenate(NULL, NULL)), evaluate(expression, null));
}
Also used : ListKeyExpression(com.apple.foundationdb.record.metadata.expressions.ListKeyExpression) GroupingKeyExpression(com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) QueryableKeyExpression(com.apple.foundationdb.record.metadata.expressions.QueryableKeyExpression) FunctionKeyExpression(com.apple.foundationdb.record.metadata.expressions.FunctionKeyExpression) FieldKeyExpression(com.apple.foundationdb.record.metadata.expressions.FieldKeyExpression) NestingKeyExpression(com.apple.foundationdb.record.metadata.expressions.NestingKeyExpression) SplitKeyExpression(com.apple.foundationdb.record.metadata.expressions.SplitKeyExpression) EmptyKeyExpression(com.apple.foundationdb.record.metadata.expressions.EmptyKeyExpression) ThenKeyExpression(com.apple.foundationdb.record.metadata.expressions.ThenKeyExpression) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 83 with KeyExpression

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

the class FDBRecordStoreReplaceIndexTest method replacementMoreComplicatedGraphFails.

@Test
public void replacementMoreComplicatedGraphFails() {
    try (FDBRecordContext context = openContext()) {
        MetaDataException err = assertThrows(MetaDataException.class, () -> openSimpleRecordStore(context, metaDataBuilder -> {
            final KeyExpression expr = Key.Expressions.field("num_value_2");
            final Index indexA = new Index("indexA", expr, IndexTypes.VALUE, ImmutableMap.of(IndexOptions.REPLACED_BY_OPTION_PREFIX + "_0", "indexB", IndexOptions.REPLACED_BY_OPTION_PREFIX + "_1", "indexC"));
            final Index indexB = new Index("indexB", expr, IndexTypes.VALUE, Collections.singletonMap(IndexOptions.REPLACED_BY_OPTION_PREFIX, "indexD"));
            final Index indexC = new Index("indexC", expr, IndexTypes.VALUE, Collections.singletonMap(IndexOptions.REPLACED_BY_OPTION_PREFIX, "indexD"));
            final Index indexD = new Index("indexD", expr);
            metaDataBuilder.addIndex("MySimpleRecord", indexA);
            metaDataBuilder.addIndex("MySimpleRecord", indexB);
            metaDataBuilder.addIndex("MySimpleRecord", indexC);
            metaDataBuilder.addIndex("MySimpleRecord", indexD);
        }));
        assertThat(err.getMessage(), containsString("has replacement indexes"));
    }
}
Also used : IndexEntry(com.apple.foundationdb.record.IndexEntry) IntStream(java.util.stream.IntStream) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) MetaDataException(com.apple.foundationdb.record.metadata.MetaDataException) Key(com.apple.foundationdb.record.metadata.Key) IndexScanType(com.apple.foundationdb.record.IndexScanType) Tuple(com.apple.foundationdb.tuple.Tuple) ScanProperties(com.apple.foundationdb.record.ScanProperties) BiConsumer(java.util.function.BiConsumer) Matchers.hasSize(org.hamcrest.Matchers.hasSize) Tag(org.junit.jupiter.api.Tag) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Nonnull(javax.annotation.Nonnull) Matchers.empty(org.hamcrest.Matchers.empty) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) TestRecords1Proto(com.apple.foundationdb.record.TestRecords1Proto) IndexOptions(com.apple.foundationdb.record.metadata.IndexOptions) ImmutableMap(com.google.common.collect.ImmutableMap) Tags(com.apple.test.Tags) Collectors(java.util.stream.Collectors) TupleRange(com.apple.foundationdb.record.TupleRange) Test(org.junit.jupiter.api.Test) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) KeySpacePath(com.apple.foundationdb.record.provider.foundationdb.keyspace.KeySpacePath) List(java.util.List) Index(com.apple.foundationdb.record.metadata.Index) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Message(com.google.protobuf.Message) IndexTypes(com.apple.foundationdb.record.metadata.IndexTypes) Collections(java.util.Collections) Matchers.containsString(org.hamcrest.Matchers.containsString) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) Index(com.apple.foundationdb.record.metadata.Index) MetaDataException(com.apple.foundationdb.record.metadata.MetaDataException) Test(org.junit.jupiter.api.Test)

Example 84 with KeyExpression

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

the class FDBRecordStoreCountRecordsTest method countRecordsKeyed.

private void countRecordsKeyed(boolean useIndex) {
    final KeyExpression key = field("num_value_3_indexed");
    final RecordMetaDataHook hook = countKeyHook(key, useIndex, 0);
    try (FDBRecordContext context = openContext()) {
        openSimpleRecordStore(context, hook);
        for (int i = 0; i < 100; i++) {
            recordStore.saveRecord(makeRecord(i, 0, i % 5));
        }
        commit(context);
    }
    try (FDBRecordContext context = openContext()) {
        openSimpleRecordStore(context, hook);
        assertEquals(100, recordStore.getSnapshotRecordCount().join().longValue());
        assertEquals(20, recordStore.getSnapshotRecordCount(key, Key.Evaluated.scalar(1)).join().longValue());
        commit(context);
    }
}
Also used : GroupingKeyExpression(com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression) EmptyKeyExpression(com.apple.foundationdb.record.metadata.expressions.EmptyKeyExpression) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression)

Example 85 with KeyExpression

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

the class FDBRecordStoreCountRecordsTest method recountAndClearRecords.

private void recountAndClearRecords(boolean useIndex) throws Exception {
    final CountMetaDataHook countMetaDataHook = new CountMetaDataHook();
    countMetaDataHook.baseHook = metaData -> metaData.removeIndex(COUNT_INDEX.getName());
    final int startingPoint = 7890;
    final int value1 = 12345;
    final int value2 = 54321;
    final int value3 = 24567;
    try (FDBRecordContext context = openContext()) {
        // Simulate the state the store would be in if this were done before counting was added.
        recordStore = getStoreBuilder(context, simpleMetaData(countMetaDataHook)).setFormatVersion(FDBRecordStore.INFO_ADDED_FORMAT_VERSION).uncheckedOpen();
        recordStore.checkVersion(null, FDBRecordStoreBase.StoreExistenceCheck.ERROR_IF_EXISTS).join();
        for (int i = 0; i < 90; i++) {
            recordStore.saveRecord(makeRecord(i + startingPoint, value1, i % 5));
        }
        commit(context);
    }
    KeyExpression key3 = field("num_value_3_indexed");
    countMetaDataHook.metaDataVersion++;
    countMetaDataHook.baseHook = countKeyHook(key3, useIndex, countMetaDataHook.metaDataVersion);
    try (FDBRecordContext context = openContext()) {
        recordStore = getStoreBuilder(context, simpleMetaData(countMetaDataHook)).setFormatVersion(FDBRecordStore.RECORD_COUNT_ADDED_FORMAT_VERSION).uncheckedOpen();
        for (int i = 90; i < 100; i++) {
            recordStore.saveRecord(makeRecord(i + startingPoint, value2, i % 5));
        }
        commit(context);
    }
    try (FDBRecordContext context = openContext()) {
        recordStore = getStoreBuilder(context, simpleMetaData(countMetaDataHook)).setFormatVersion(FDBRecordStore.RECORD_COUNT_ADDED_FORMAT_VERSION).uncheckedOpen();
        assertEquals(10, recordStore.getSnapshotRecordCount().join().longValue(), "should only see new records");
        commit(context);
    }
    // Need to allow immediate rebuild of new count index.
    final FDBRecordStoreBase.UserVersionChecker alwaysEnabled = new FDBRecordStoreBase.UserVersionChecker() {

        @Override
        public CompletableFuture<Integer> checkUserVersion(int oldUserVersion, int oldMetaDataVersion, RecordMetaDataProvider metaData) {
            return CompletableFuture.completedFuture(1);
        }

        @Override
        public IndexState needRebuildIndex(Index index, long recordCount, boolean indexOnNewRecordTypes) {
            return IndexState.READABLE;
        }
    };
    try (FDBRecordContext context = openContext()) {
        uncheckedOpenSimpleRecordStore(context, countMetaDataHook);
        // Index is rebuilt here automatically in useIndex case
        recordStore.checkVersion(alwaysEnabled, FDBRecordStoreBase.StoreExistenceCheck.ERROR_IF_NOT_EXISTS).join();
        assertEquals(100, recordStore.getSnapshotRecordCount().join().longValue(), "should see all records");
        assertEquals(20, recordStore.getSnapshotRecordCount(key3, Key.Evaluated.scalar(2)).join().longValue());
        commit(context);
    }
    KeyExpression key2 = field("num_value_2");
    countMetaDataHook.metaDataVersion++;
    countMetaDataHook.baseHook = countKeyHook(key2, useIndex, countMetaDataHook.metaDataVersion);
    try (FDBRecordContext context = openContext()) {
        uncheckedOpenSimpleRecordStore(context, countMetaDataHook);
        recordStore.checkVersion(null, FDBRecordStoreBase.StoreExistenceCheck.ERROR_IF_NOT_EXISTS).join();
        if (useIndex) {
            // Need to manually rebuild index in this case.
            Index index = recordStore.getRecordMetaData().getIndex("record_count");
            recordStore.rebuildIndex(index).get();
            assertThat(recordStore.isIndexReadable(index), is(true));
        }
        assertEquals(100, recordStore.getSnapshotRecordCount().join().longValue(), "should see all records");
        for (int i = 0; i < 32; i++) {
            recordStore.saveRecord(makeRecord(i + startingPoint + 1000, value3, 0));
        }
        commit(context);
    }
    try (FDBRecordContext context = openContext()) {
        uncheckedOpenSimpleRecordStore(context, countMetaDataHook);
        assertEquals(90, recordStore.getSnapshotRecordCount(key2, Key.Evaluated.scalar(value1)).join().longValue());
        assertEquals(10, recordStore.getSnapshotRecordCount(key2, Key.Evaluated.scalar(value2)).join().longValue());
        assertEquals(32, recordStore.getSnapshotRecordCount(key2, Key.Evaluated.scalar(value3)).join().longValue());
    }
    KeyExpression pkey = field("rec_no");
    countMetaDataHook.metaDataVersion++;
    countMetaDataHook.baseHook = countKeyHook(pkey, useIndex, countMetaDataHook.metaDataVersion);
    try (FDBRecordContext context = openContext()) {
        uncheckedOpenSimpleRecordStore(context, countMetaDataHook);
        recordStore.checkVersion(null, FDBRecordStoreBase.StoreExistenceCheck.NONE).join();
        if (useIndex) {
            // Need to manually rebuild index in this case.
            Index index = recordStore.getRecordMetaData().getIndex("record_count");
            recordStore.rebuildIndex(index).get();
            assertThat(recordStore.isIndexReadable(index), is(true));
        }
        assertEquals(132, recordStore.getSnapshotRecordCount().join().longValue());
        for (int i = 0; i < 100; i++) {
            assertEquals(1, recordStore.getSnapshotRecordCount(pkey, Key.Evaluated.scalar(i + startingPoint)).join().longValue(), "Incorrect when i is " + i);
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RecordMetaDataProvider(com.apple.foundationdb.record.RecordMetaDataProvider) GroupingKeyExpression(com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression) EmptyKeyExpression(com.apple.foundationdb.record.metadata.expressions.EmptyKeyExpression) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) Index(com.apple.foundationdb.record.metadata.Index)

Aggregations

KeyExpression (com.apple.foundationdb.record.metadata.expressions.KeyExpression)152 GroupingKeyExpression (com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression)85 Test (org.junit.jupiter.api.Test)63 FieldKeyExpression (com.apple.foundationdb.record.metadata.expressions.FieldKeyExpression)60 EmptyKeyExpression (com.apple.foundationdb.record.metadata.expressions.EmptyKeyExpression)56 NestingKeyExpression (com.apple.foundationdb.record.metadata.expressions.NestingKeyExpression)56 ThenKeyExpression (com.apple.foundationdb.record.metadata.expressions.ThenKeyExpression)52 Nonnull (javax.annotation.Nonnull)52 Index (com.apple.foundationdb.record.metadata.Index)37 List (java.util.List)36 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)35 Nullable (javax.annotation.Nullable)33 RecordQueryPlan (com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan)30 FunctionKeyExpression (com.apple.foundationdb.record.metadata.expressions.FunctionKeyExpression)28 Message (com.google.protobuf.Message)26 ArrayList (java.util.ArrayList)26 RecordMetaData (com.apple.foundationdb.record.RecordMetaData)25 QueryableKeyExpression (com.apple.foundationdb.record.metadata.expressions.QueryableKeyExpression)25 RecordCoreException (com.apple.foundationdb.record.RecordCoreException)24 ListKeyExpression (com.apple.foundationdb.record.metadata.expressions.ListKeyExpression)23