Search in sources :

Example 21 with RecordMetaDataBuilder

use of com.apple.foundationdb.record.RecordMetaDataBuilder 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);
}
Also used : RecordMetaData(com.apple.foundationdb.record.RecordMetaData) RecordMetaDataBuilder(com.apple.foundationdb.record.RecordMetaDataBuilder) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 22 with RecordMetaDataBuilder

use of com.apple.foundationdb.record.RecordMetaDataBuilder 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());
}
Also used : RecordMetaData(com.apple.foundationdb.record.RecordMetaData) RecordMetaDataBuilder(com.apple.foundationdb.record.RecordMetaDataBuilder) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 23 with RecordMetaDataBuilder

use of com.apple.foundationdb.record.RecordMetaDataBuilder 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());
}
Also used : RecordMetaData(com.apple.foundationdb.record.RecordMetaData) RecordMetaDataBuilder(com.apple.foundationdb.record.RecordMetaDataBuilder) RecordMetaData(com.apple.foundationdb.record.RecordMetaData) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 24 with RecordMetaDataBuilder

use of com.apple.foundationdb.record.RecordMetaDataBuilder 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());
    }
}
Also used : RecordMetaData(com.apple.foundationdb.record.RecordMetaData) RecordMetaDataBuilder(com.apple.foundationdb.record.RecordMetaDataBuilder) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) BooleanSource(com.apple.test.BooleanSource)

Example 25 with RecordMetaDataBuilder

use of com.apple.foundationdb.record.RecordMetaDataBuilder 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());
    }
}
Also used : RecordMetaData(com.apple.foundationdb.record.RecordMetaData) RecordMetaDataBuilder(com.apple.foundationdb.record.RecordMetaDataBuilder) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) BooleanSource(com.apple.test.BooleanSource)

Aggregations

RecordMetaDataBuilder (com.apple.foundationdb.record.RecordMetaDataBuilder)150 Test (org.junit.jupiter.api.Test)91 RecordMetaData (com.apple.foundationdb.record.RecordMetaData)69 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)43 Nonnull (javax.annotation.Nonnull)22 Index (com.apple.foundationdb.record.metadata.Index)21 Descriptors (com.google.protobuf.Descriptors)15 TestRecords1Proto (com.apple.foundationdb.record.TestRecords1Proto)14 KeyExpression (com.apple.foundationdb.record.metadata.expressions.KeyExpression)14 RecordMetaDataProto (com.apple.foundationdb.record.RecordMetaDataProto)13 ByteString (com.google.protobuf.ByteString)13 Tuple (com.apple.foundationdb.tuple.Tuple)12 Collections (java.util.Collections)12 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)11 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)11 Assertions.assertNotNull (org.junit.jupiter.api.Assertions.assertNotNull)11 Assertions.assertNull (org.junit.jupiter.api.Assertions.assertNull)11 Assertions.assertThrows (org.junit.jupiter.api.Assertions.assertThrows)11 RecordCoreException (com.apple.foundationdb.record.RecordCoreException)10 BooleanSource (com.apple.test.BooleanSource)10