Search in sources :

Example 56 with KeyExpression

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

the class GeophileIndexMaintainer method getSpatialFunction.

// If the bottom-right child is GeophileSpatialFunctionKeyExpression, return it. Else error.
@Nonnull
static GeophileSpatialFunctionKeyExpression getSpatialFunction(@Nonnull Index index) {
    KeyExpression rootKey = index.getRootExpression();
    if (rootKey instanceof KeyWithValueExpression) {
        rootKey = ((KeyWithValueExpression) rootKey).getKeyExpression();
    }
    final List<KeyExpression> components = rootKey.normalizeKeyForPositions();
    final KeyExpression rightComponent = components.get(components.size() - 1);
    KeyExpression bottomComponent = rightComponent;
    while (true) {
        if (bottomComponent instanceof GeophileSpatialFunctionKeyExpression) {
            return (GeophileSpatialFunctionKeyExpression) bottomComponent;
        }
        if (bottomComponent instanceof KeyExpressionWithChild) {
            bottomComponent = ((KeyExpressionWithChild) bottomComponent).getChild();
            continue;
        }
        throw new KeyExpression.InvalidExpressionException(String.format("need spatial key expression for %s index", index.getType()), LogMessageKeys.INDEX_NAME, index.getName(), LogMessageKeys.INDEX_KEY, index.getRootExpression());
    }
}
Also used : KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) KeyExpressionWithChild(com.apple.foundationdb.record.metadata.expressions.KeyExpressionWithChild) KeyWithValueExpression(com.apple.foundationdb.record.metadata.expressions.KeyWithValueExpression) Nonnull(javax.annotation.Nonnull)

Example 57 with KeyExpression

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

the class FDBRecordStoreSplitRecordsTest method clearOmitUnsplitRecordSuffixTyped.

@Test
public void clearOmitUnsplitRecordSuffixTyped() {
    final RecordMetaDataBuilder metaData = RecordMetaData.newBuilder().setRecords(TestRecords1Proto.getDescriptor());
    final KeyExpression pkey = concat(Key.Expressions.recordType(), field("rec_no"));
    metaData.getRecordType("MySimpleRecord").setPrimaryKey(pkey);
    metaData.getRecordType("MyOtherRecord").setPrimaryKey(pkey);
    FDBRecordStore.Builder builder = FDBRecordStore.newBuilder().setKeySpacePath(path).setMetaDataProvider(metaData).setFormatVersion(FDBRecordStore.FORMAT_CONTROL_FORMAT_VERSION);
    final FDBStoredRecord<Message> saved;
    try (FDBRecordContext context = openContext()) {
        recordStore = builder.setContext(context).create();
        TestRecords1Proto.MyOtherRecord record = TestRecords1Proto.MyOtherRecord.newBuilder().setRecNo(1L).build();
        saved = recordStore.saveRecord(record);
        commit(context);
    }
    metaData.addIndex("MySimpleRecord", "num_value_2");
    builder.setFormatVersion(FDBRecordStore.MAX_SUPPORTED_FORMAT_VERSION);
    try (FDBRecordContext context = openContext()) {
        recordStore = builder.setContext(context).open();
        FDBStoredRecord<Message> loaded = recordStore.loadRecord(saved.getPrimaryKey());
        assertNotNull(loaded);
        assertEquals(saved.getRecord(), loaded.getRecord());
        assertTrue(recordStore.getRecordStoreState().getStoreHeader().getOmitUnsplitRecordSuffix());
    }
}
Also used : TestRecords1Proto(com.apple.foundationdb.record.TestRecords1Proto) Message(com.google.protobuf.Message) RecordMetaDataBuilder(com.apple.foundationdb.record.RecordMetaDataBuilder) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 58 with KeyExpression

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

the class FDBRecordStoreTest method testAccessUserField.

/**
 * Test that the user field can be set.
 */
@Test
public void testAccessUserField() throws Exception {
    try (FDBRecordContext context = openContext()) {
        openSimpleRecordStore(context);
        assertNull(recordStore.getHeaderUserField("foo"));
        recordStore.setHeaderUserField("foo", "bar".getBytes(Charsets.UTF_8));
        commit(context);
    }
    try (FDBRecordContext context = openContext()) {
        openSimpleRecordStore(context);
        assertEquals("bar", recordStore.getHeaderUserField("foo").toStringUtf8());
        RecordMetaDataProto.DataStoreInfo storeHeader = recordStore.getRecordStoreState().getStoreHeader();
        assertEquals(1, storeHeader.getUserFieldCount());
        // Validate that one can overwrite an existing value
        recordStore.setHeaderUserField("foo", "µs".getBytes(Charsets.UTF_8));
        storeHeader = recordStore.getRecordStoreState().getStoreHeader();
        assertEquals(1, storeHeader.getUserFieldCount());
        // Validate that one can add a new value
        recordStore.setHeaderUserField("baz", field("baz").toKeyExpression().toByteString());
        storeHeader = recordStore.getRecordStoreState().getStoreHeader();
        assertEquals(2, storeHeader.getUserFieldCount());
        commit(context);
    }
    try (FDBRecordContext context = openContext()) {
        openSimpleRecordStore(context);
        // Read back the stored values
        assertEquals("µs", recordStore.getHeaderUserField("foo").toStringUtf8());
        ByteString bazValue = recordStore.getHeaderUserField("baz");
        assertNotNull(bazValue);
        KeyExpression expr = KeyExpression.fromProto(RecordMetaDataProto.KeyExpression.parseFrom(bazValue));
        assertEquals(field("baz"), expr);
        // Add in a new field
        recordStore.setHeaderUserField("qwop", Tuple.from(1066L).pack());
        // Delete the middle field
        recordStore.clearHeaderUserField("baz");
        assertNull(recordStore.getHeaderUserField("baz"));
        commit(context);
    }
    try (FDBRecordContext context = openContext()) {
        openSimpleRecordStore(context);
        assertEquals("µs", recordStore.getHeaderUserField("foo").toStringUtf8());
        assertNull(recordStore.getHeaderUserField("baz"));
        assertEquals(Tuple.from(1066L), Tuple.fromBytes(recordStore.getHeaderUserField("qwop").toByteArray()));
        RecordMetaDataProto.DataStoreInfo storeHeader = recordStore.getRecordStoreState().getStoreHeader();
        assertEquals(2, storeHeader.getUserFieldCount());
        commit(context);
    }
}
Also used : ByteString(com.google.protobuf.ByteString) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) RecordMetaDataProto(com.apple.foundationdb.record.RecordMetaDataProto) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 59 with KeyExpression

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

the class FDBMetaDataStoreTest method recordTypesWithOneOfUnion.

@Test
public void recordTypesWithOneOfUnion() {
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        RecordMetaDataBuilder metaData = RecordMetaData.newBuilder().setRecords(TestRecordsOneOfProto.getDescriptor());
        final KeyExpression pkey = Key.Expressions.field("rec_no");
        metaData.getRecordType("MySimpleRecord").setPrimaryKey(pkey);
        metaData.getRecordType("MyOtherRecord").setPrimaryKey(pkey);
        metaDataStore.saveRecordMetaData(metaData);
        context.commit();
    }
    // Add a record type to oneOf. It should fail.
    try (FDBRecordContext context = fdb.openContext()) {
        openMetaDataStore(context);
        assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MySimpleRecord"));
        DescriptorProtos.DescriptorProto newRecordType = DescriptorProtos.DescriptorProto.newBuilder().setName("MyNewRecord").addField(DescriptorProtos.FieldDescriptorProto.newBuilder().setLabel(DescriptorProtos.FieldDescriptorProto.Label.LABEL_OPTIONAL).setType(DescriptorProtos.FieldDescriptorProto.Type.TYPE_INT32).setName("rec_no").setNumber(1)).build();
        MetaDataException e = assertThrows(MetaDataException.class, () -> addRecordType(newRecordType, Key.Expressions.field("rec_no")));
        assertEquals(e.getMessage(), "Adding record type to oneof is not allowed");
        context.commit();
    }
}
Also used : RecordMetaDataBuilder(com.apple.foundationdb.record.RecordMetaDataBuilder) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) DescriptorProtos(com.google.protobuf.DescriptorProtos) MetaDataException(com.apple.foundationdb.record.metadata.MetaDataException) Test(org.junit.jupiter.api.Test) MetaDataProtoTest(com.apple.foundationdb.record.metadata.MetaDataProtoTest) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 60 with KeyExpression

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

the class FDBRecordStoreReplaceIndexTest method replacementLineFails.

@Test
public void replacementLineFails() {
    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, Collections.singletonMap(IndexOptions.REPLACED_BY_OPTION_PREFIX, "indexB"));
            final Index indexB = new Index("indexB", expr, IndexTypes.VALUE, Collections.singletonMap(IndexOptions.REPLACED_BY_OPTION_PREFIX, "indexC"));
            final Index indexC = new Index("indexC", expr);
            metaDataBuilder.addIndex("MySimpleRecord", indexA);
            metaDataBuilder.addIndex("MySimpleRecord", indexB);
            metaDataBuilder.addIndex("MySimpleRecord", indexC);
        }));
        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)

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