use of com.apple.foundationdb.record.RecordMetaData in project fdb-record-layer by FoundationDB.
the class RecordMetaDataBuilderTest method caching.
@Test
public void caching() {
RecordMetaDataBuilder builder = createBuilder(TestRecords1Proto.getDescriptor(), true);
RecordMetaData metaData1 = builder.getRecordMetaData();
assertSame(metaData1, builder.getRecordMetaData());
builder.addIndex("MySimpleRecord", "MySimpleRecord$PRIMARY", "rec_no");
RecordMetaData metaData2 = builder.getRecordMetaData();
assertNotSame(metaData1, metaData2);
}
use of com.apple.foundationdb.record.RecordMetaData in project fdb-record-layer by FoundationDB.
the class RecordMetaDataBuilderTest method explicitSubspaceKeys.
@Test
public void explicitSubspaceKeys() {
// Add an explicit subspace key and make sure the explicit subspace key is not overridden.
Index indexWithExplicitSubspaceKey = new Index("indexWithExplicitSubspaceKey", concatenateFields("str_value_indexed", "num_value_3_indexed"));
indexWithExplicitSubspaceKey.setSubspaceKey("explicitSubspaceKey");
RecordMetaDataBuilder builder = RecordMetaData.newBuilder().enableCounterBasedSubspaceKeys().setRecords(TestRecords1Proto.getDescriptor());
builder.addIndex("MySimpleRecord", indexWithExplicitSubspaceKey);
RecordMetaData metaData = builder.build(true);
assertNotNull(metaData.getIndex("MySimpleRecord$str_value_indexed"));
assertNotNull(metaData.getIndex("MySimpleRecord$num_value_unique"));
assertNotNull(metaData.getIndex("MySimpleRecord$num_value_3_indexed"));
assertNotNull(metaData.getIndex("indexWithExplicitSubspaceKey"));
assertEquals(1L, metaData.getIndex("MySimpleRecord$str_value_indexed").getSubspaceKey());
assertEquals(2L, metaData.getIndex("MySimpleRecord$num_value_unique").getSubspaceKey());
assertEquals(3L, metaData.getIndex("MySimpleRecord$num_value_3_indexed").getSubspaceKey());
assertEquals("explicitSubspaceKey", metaData.getIndex("indexWithExplicitSubspaceKey").getSubspaceKey());
// Setting explicit subspace key to something that can clash.
Index clashingIndex = new Index("clashingIndex", concatenateFields("str_value_indexed", "num_value_3_indexed"));
clashingIndex.setSubspaceKey(1L);
final RecordMetaDataBuilder clashingBuilder = RecordMetaData.newBuilder().enableCounterBasedSubspaceKeys().setRecords(TestRecords1Proto.getDescriptor());
clashingBuilder.addIndex("MySimpleRecord", clashingIndex);
MetaDataException e = assertThrows(MetaDataException.class, () -> clashingBuilder.build(true));
assertEquals("Same subspace key 1 used by both MySimpleRecord$str_value_indexed and clashingIndex", e.getMessage());
}
use of com.apple.foundationdb.record.RecordMetaData in project fdb-record-layer by FoundationDB.
the class RecordMetaDataBuilderTest method testSetSubspaceKeyCounter.
@Test
public void testSetSubspaceKeyCounter() {
// Test setting the counter without enabling it
MetaDataException e = assertThrows(MetaDataException.class, () -> RecordMetaData.newBuilder().setRecords(TestRecords1Proto.getDescriptor()).setSubspaceKeyCounter(4L));
assertEquals("Counter-based subspace keys not enabled", e.getMessage());
// Test setting the counter to a value not greater than the current value
e = assertThrows(MetaDataException.class, () -> RecordMetaData.newBuilder().enableCounterBasedSubspaceKeys().setRecords(TestRecords1Proto.getDescriptor()).setSubspaceKeyCounter(3L));
assertEquals("Subspace key counter must be set to a value greater than its current value (3)", e.getMessage());
// Set to a random number
long randomCounter = ThreadLocalRandom.current().nextLong(1, Long.MAX_VALUE - 10);
RecordMetaDataBuilder builder = RecordMetaData.newBuilder().enableCounterBasedSubspaceKeys().setSubspaceKeyCounter(randomCounter).setRecords(TestRecords1Proto.getDescriptor());
RecordMetaData metaData = builder.build(true);
assertNotNull(metaData.getIndex("MySimpleRecord$str_value_indexed"));
assertNotNull(metaData.getIndex("MySimpleRecord$num_value_unique"));
assertNotNull(metaData.getIndex("MySimpleRecord$num_value_3_indexed"));
assertEquals(randomCounter + 1, metaData.getIndex("MySimpleRecord$str_value_indexed").getSubspaceKey());
assertEquals(randomCounter + 2, metaData.getIndex("MySimpleRecord$num_value_unique").getSubspaceKey());
assertEquals(randomCounter + 3, metaData.getIndex("MySimpleRecord$num_value_3_indexed").getSubspaceKey());
// enable and set counter in the proto.
RecordMetaDataProto.MetaData.Builder protoBuilder = RecordMetaDataProto.MetaData.newBuilder().setRecords(TestRecords1Proto.getDescriptor().toProto());
protoBuilder.setUsesSubspaceKeyCounter(true).setSubspaceKeyCounter(randomCounter);
builder = RecordMetaData.newBuilder().setRecords(protoBuilder.build(), true);
RecordMetaData metaDataFromProto = builder.build(true);
assertNotNull(metaDataFromProto.getIndex("MySimpleRecord$str_value_indexed"));
assertNotNull(metaDataFromProto.getIndex("MySimpleRecord$num_value_unique"));
assertNotNull(metaDataFromProto.getIndex("MySimpleRecord$num_value_3_indexed"));
assertEquals(randomCounter + 1, metaDataFromProto.getIndex("MySimpleRecord$str_value_indexed").getSubspaceKey());
assertEquals(randomCounter + 2, metaDataFromProto.getIndex("MySimpleRecord$num_value_unique").getSubspaceKey());
assertEquals(randomCounter + 3, metaDataFromProto.getIndex("MySimpleRecord$num_value_3_indexed").getSubspaceKey());
}
use of com.apple.foundationdb.record.RecordMetaData in project fdb-record-layer by FoundationDB.
the class RecordMetaDataBuilderTest method normalIndexDoesNotOverlapPrimaryKey.
@ParameterizedTest(name = "normalIndexDoesNotOverlapPrimaryKey [indexCounterBasedSubspaceKey = {0}]")
@BooleanSource
public void normalIndexDoesNotOverlapPrimaryKey(final boolean indexCounterBasedSubspaceKey) {
RecordMetaDataBuilder builder = RecordMetaData.newBuilder();
if (indexCounterBasedSubspaceKey) {
builder.enableCounterBasedSubspaceKeys();
}
RecordMetaData metaData = builder.setRecords(TestRecords1Proto.getDescriptor()).getRecordMetaData();
Index index = metaData.getIndex("MySimpleRecord$str_value_indexed");
assertNotNull(index);
assertNull(index.getPrimaryKeyComponentPositions());
if (!indexCounterBasedSubspaceKey) {
assertEquals(index.getName(), index.getSubspaceKey());
} else {
assertEquals(1L, index.getSubspaceKey());
}
}
use of com.apple.foundationdb.record.RecordMetaData in project fdb-record-layer by FoundationDB.
the class RecordMetaDataBuilderTest method uniqueIndexOverlappingWithNestedPrimaryKey.
@ParameterizedTest(name = "uniqueIndexOverlappingWithNestedPrimaryKey [indexCounterBasedSubspaceKey = {0}]")
@BooleanSource
public void uniqueIndexOverlappingWithNestedPrimaryKey(final boolean indexCounterBasedSubspaceKey) {
RecordMetaDataBuilder builder = createBuilder(TestRecordsWithHeaderProto.getDescriptor(), indexCounterBasedSubspaceKey);
builder.getRecordType("MyRecord").setPrimaryKey(field("header").nest(concat(field("num"), field("rec_no"))));
builder.addIndex("MyRecord", new Index("MyRecord$num-str-unique-1", concat(field("header").nest(field("num")), field("str_value")), IndexTypes.VALUE, IndexOptions.UNIQUE_OPTIONS));
builder.addIndex("MyRecord", new Index("MyRecord$num-str-unique-2", concat(field("header").nest(field("num", KeyExpression.FanType.None, Key.Evaluated.NullStandin.NULL_UNIQUE)), field("str_value")), IndexTypes.VALUE, IndexOptions.UNIQUE_OPTIONS));
RecordMetaData metaData = builder.getRecordMetaData();
Index index1 = metaData.getIndex("MyRecord$num-str-unique-1");
assertArrayEquals(new int[] { 0, -1 }, index1.getPrimaryKeyComponentPositions());
Index index2 = metaData.getIndex("MyRecord$num-str-unique-2");
assertArrayEquals(new int[] { 0, -1 }, index2.getPrimaryKeyComponentPositions());
if (!indexCounterBasedSubspaceKey) {
assertEquals(index1.getName(), index1.getSubspaceKey());
assertEquals(index2.getName(), index2.getSubspaceKey());
} else {
assertEquals(1L, index1.getSubspaceKey());
assertEquals(2L, index2.getSubspaceKey());
}
}
Aggregations