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());
}
}
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());
}
}
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);
}
}
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();
}
}
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"));
}
}
Aggregations