Search in sources :

Example 96 with Index

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

the class FDBRecordStoreIndexTest method minMaxTupleRepeatConcatenated.

@Test
public void minMaxTupleRepeatConcatenated() throws Exception {
    final FieldKeyExpression fieldKey = field("repeater", FanType.Concatenate);
    final GroupingKeyExpression indexKey = fieldKey.ungrouped();
    final RecordMetaDataHook hook = md -> {
        RecordTypeBuilder type = md.getRecordType("MySimpleRecord");
        md.addIndex(type, new Index("min", indexKey, IndexTypes.MIN_EVER_TUPLE));
        md.addIndex(type, new Index("max", indexKey, IndexTypes.MAX_EVER_TUPLE));
    };
    final IndexAggregateFunction min = new IndexAggregateFunction(FunctionNames.MIN_EVER, indexKey, null);
    final IndexAggregateFunction max = new IndexAggregateFunction(FunctionNames.MAX_EVER, indexKey, null);
    List<String> types = Collections.singletonList("MySimpleRecord");
    try (FDBRecordContext context = openContext()) {
        openSimpleRecordStore(context, hook);
        assertNull(recordStore.evaluateAggregateFunction(types, min, Key.Evaluated.EMPTY, IsolationLevel.SNAPSHOT).join());
        assertNull(recordStore.evaluateAggregateFunction(types, max, Key.Evaluated.EMPTY, IsolationLevel.SNAPSHOT).join());
        TestRecords1Proto.MySimpleRecord.Builder recBuilder = TestRecords1Proto.MySimpleRecord.newBuilder();
        recBuilder.setRecNo(1);
        recordStore.saveRecord(recBuilder.build());
        assertEquals(Tuple.from(Tuple.from()), recordStore.evaluateAggregateFunction(types, min, Key.Evaluated.EMPTY, IsolationLevel.SNAPSHOT).join());
        assertEquals(Tuple.from(Tuple.from()), recordStore.evaluateAggregateFunction(types, max, Key.Evaluated.EMPTY, IsolationLevel.SNAPSHOT).join());
        recBuilder.addRepeater(1);
        recordStore.saveRecord(recBuilder.build());
        assertEquals(Tuple.from(Tuple.from()), recordStore.evaluateAggregateFunction(types, min, Key.Evaluated.EMPTY, IsolationLevel.SNAPSHOT).join());
        assertEquals(Tuple.from(Tuple.from(1L)), recordStore.evaluateAggregateFunction(types, max, Key.Evaluated.EMPTY, IsolationLevel.SNAPSHOT).join());
        recBuilder.addRepeater(1);
        recordStore.saveRecord(recBuilder.build());
        assertEquals(Tuple.from(Tuple.from()), recordStore.evaluateAggregateFunction(types, min, Key.Evaluated.EMPTY, IsolationLevel.SNAPSHOT).join());
        assertEquals(Tuple.from(Tuple.from(1L, 1L)), recordStore.evaluateAggregateFunction(types, max, Key.Evaluated.EMPTY, IsolationLevel.SNAPSHOT).join());
        recBuilder.clearRepeater();
        recBuilder.addRepeater(2);
        recordStore.saveRecord(recBuilder.build());
        assertEquals(Tuple.from(Tuple.from()), recordStore.evaluateAggregateFunction(types, min, Key.Evaluated.EMPTY, IsolationLevel.SNAPSHOT).join());
        assertEquals(Tuple.from(Tuple.from(2L)), recordStore.evaluateAggregateFunction(types, max, Key.Evaluated.EMPTY, IsolationLevel.SNAPSHOT).join());
        commit(context);
    }
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) LogMessageKeys(com.apple.foundationdb.record.logging.LogMessageKeys) MetaDataException(com.apple.foundationdb.record.metadata.MetaDataException) IndexScanType(com.apple.foundationdb.record.IndexScanType) Pair(org.apache.commons.lang3.tuple.Pair) FDBError(com.apple.foundationdb.FDBError) RecordCoreException(com.apple.foundationdb.record.RecordCoreException) RecordIndexUniquenessViolation(com.apple.foundationdb.record.RecordIndexUniquenessViolation) Expressions.concat(com.apple.foundationdb.record.metadata.Key.Expressions.concat) GroupingKeyExpression(com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression) Tag(org.junit.jupiter.api.Tag) Query(com.apple.foundationdb.record.query.expressions.Query) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) IndexOptions(com.apple.foundationdb.record.metadata.IndexOptions) Set(java.util.Set) FanType(com.apple.foundationdb.record.metadata.expressions.KeyExpression.FanType) TupleRange(com.apple.foundationdb.record.TupleRange) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) RecordMetaDataProvider(com.apple.foundationdb.record.RecordMetaDataProvider) RecordStoreState(com.apple.foundationdb.record.RecordStoreState) TupleHelpers(com.apple.foundationdb.tuple.TupleHelpers) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) InvalidIndexEntry(com.apple.foundationdb.record.provider.foundationdb.indexes.InvalidIndexEntry) AutoContinuingCursor(com.apple.foundationdb.record.cursors.AutoContinuingCursor) Matchers.is(org.hamcrest.Matchers.is) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Assertions.fail(org.junit.jupiter.api.Assertions.fail) FunctionNames(com.apple.foundationdb.record.FunctionNames) RecordMetaData(com.apple.foundationdb.record.RecordMetaData) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) IndexAggregateFunction(com.apple.foundationdb.record.metadata.IndexAggregateFunction) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) AsyncUtil(com.apple.foundationdb.async.AsyncUtil) ArrayList(java.util.ArrayList) Strings(com.google.common.base.Strings) CloseableAsyncIterator(com.apple.foundationdb.async.CloseableAsyncIterator) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) FDBRecordStoreBase.indexEntryKey(com.apple.foundationdb.record.provider.foundationdb.FDBRecordStoreBase.indexEntryKey) Nullable(javax.annotation.Nullable) Matchers.greaterThanOrEqualTo(org.hamcrest.Matchers.greaterThanOrEqualTo) IsolationLevel(com.apple.foundationdb.record.IsolationLevel) Tags(com.apple.test.Tags) TestRecords1EvolvedProto(com.apple.foundationdb.record.TestRecords1EvolvedProto) ExecutionException(java.util.concurrent.ExecutionException) Assertions.assertArrayEquals(org.junit.jupiter.api.Assertions.assertArrayEquals) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Index(com.apple.foundationdb.record.metadata.Index) FDBException(com.apple.foundationdb.FDBException) ThenKeyExpression(com.apple.foundationdb.record.metadata.expressions.ThenKeyExpression) IndexEntry(com.apple.foundationdb.record.IndexEntry) StoreTimer(com.apple.foundationdb.record.provider.common.StoreTimer) LoggerFactory(org.slf4j.LoggerFactory) Assertions.assertNotEquals(org.junit.jupiter.api.Assertions.assertNotEquals) Random(java.util.Random) Tuple(com.apple.foundationdb.tuple.Tuple) Range(com.apple.foundationdb.Range) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Expressions.concatenateFields(com.apple.foundationdb.record.metadata.Key.Expressions.concatenateFields) ImmutableSet(com.google.common.collect.ImmutableSet) TestRecords1Proto(com.apple.foundationdb.record.TestRecords1Proto) ImmutableMap(com.google.common.collect.ImmutableMap) Matchers.lessThanOrEqualTo(org.hamcrest.Matchers.lessThanOrEqualTo) Collection(java.util.Collection) CompletionException(java.util.concurrent.CompletionException) IndexQueryabilityFilter(com.apple.foundationdb.record.query.IndexQueryabilityFilter) TypeSafeMatcher(org.hamcrest.TypeSafeMatcher) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) List(java.util.List) EvaluationContext(com.apple.foundationdb.record.EvaluationContext) Matchers.equalTo(org.hamcrest.Matchers.equalTo) IndexTypes(com.apple.foundationdb.record.metadata.IndexTypes) Optional(java.util.Optional) TestNoIndexesProto(com.apple.foundationdb.record.TestNoIndexesProto) LazyCursor(com.apple.foundationdb.record.cursors.LazyCursor) EnumSource(org.junit.jupiter.params.provider.EnumSource) CompletableFuture(java.util.concurrent.CompletableFuture) Iterators(com.google.common.collect.Iterators) Key(com.apple.foundationdb.record.metadata.Key) FieldKeyExpression(com.apple.foundationdb.record.metadata.expressions.FieldKeyExpression) HashSet(java.util.HashSet) ExecuteProperties(com.apple.foundationdb.record.ExecuteProperties) ScanProperties(com.apple.foundationdb.record.ScanProperties) RecordCursorIterator(com.apple.foundationdb.record.RecordCursorIterator) BooleanSource(com.apple.test.BooleanSource) Nonnull(javax.annotation.Nonnull) Expressions.field(com.apple.foundationdb.record.metadata.Key.Expressions.field) EmptyKeyExpression(com.apple.foundationdb.record.metadata.expressions.EmptyKeyExpression) Matchers.hasEntry(org.hamcrest.Matchers.hasEntry) Description(org.hamcrest.Description) Matchers.oneOf(org.hamcrest.Matchers.oneOf) Logger(org.slf4j.Logger) RecordMetaDataBuilder(com.apple.foundationdb.record.RecordMetaDataBuilder) RecordTypeBuilder(com.apple.foundationdb.record.metadata.RecordTypeBuilder) IndexState(com.apple.foundationdb.record.IndexState) TestRecordsIndexFilteringProto(com.apple.foundationdb.record.TestRecordsIndexFilteringProto) Message(com.google.protobuf.Message) Collections(java.util.Collections) FieldKeyExpression(com.apple.foundationdb.record.metadata.expressions.FieldKeyExpression) GroupingKeyExpression(com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression) Index(com.apple.foundationdb.record.metadata.Index) IndexAggregateFunction(com.apple.foundationdb.record.metadata.IndexAggregateFunction) RecordTypeBuilder(com.apple.foundationdb.record.metadata.RecordTypeBuilder) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 97 with Index

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

the class FDBRecordStoreIndexTest method isAbsentBuildable.

@Test
public void isAbsentBuildable() throws Exception {
    try (FDBRecordContext context = openContext()) {
        openSimpleRecordStore(context);
        assertThrows(MetaDataException.class, () -> recordStore.firstUnbuiltRange(new Index("this_index_doesn't_exist", Key.Expressions.field("str_value_indexed"))).get());
    }
}
Also used : Index(com.apple.foundationdb.record.metadata.Index) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 98 with Index

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

the class FDBRecordStoreIndexTest method markManyDisabled.

@Test
public void markManyDisabled() throws Exception {
    final Index[] indexes = new Index[100];
    for (int i = 0; i < indexes.length; i++) {
        indexes[i] = new Index(String.format("index-%d", i), "str_value_indexed");
    }
    final RecordMetaDataHook hook = metaData -> {
        final RecordTypeBuilder recordType = metaData.getRecordType("MySimpleRecord");
        for (int i = 0; i < indexes.length; i++) {
            metaData.addIndex(recordType, indexes[i]);
        }
    };
    // Such timing problems take several tries to show up.
    for (int j = 0; j < 10; j++) {
        try (FDBRecordContext context = openContext()) {
            openSimpleRecordStore(context, hook);
            List<CompletableFuture<Boolean>> futures = new ArrayList<>();
            List<Index> shouldBeDisabled = new ArrayList<>();
            // Expected contract: only use isIndexXXX and markIndexXXX and wait for all futures when done.
            for (int i = 0; i < indexes.length; i++) {
                if ((i % 2 == 0) || (i == 99 && recordStore.isIndexDisabled(indexes[i]))) {
                    futures.add(recordStore.markIndexDisabled(indexes[i]));
                    shouldBeDisabled.add(indexes[i]);
                }
            }
            AsyncUtil.whenAll(futures).join();
            for (Index index : shouldBeDisabled) {
                assertThat(index, new TypeSafeMatcher<Index>() {

                    @Override
                    protected boolean matchesSafely(Index item) {
                        return recordStore.isIndexDisabled(index);
                    }

                    @Override
                    public void describeTo(Description description) {
                        description.appendText("a disabled index");
                    }
                });
            }
        }
    }
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) LogMessageKeys(com.apple.foundationdb.record.logging.LogMessageKeys) MetaDataException(com.apple.foundationdb.record.metadata.MetaDataException) IndexScanType(com.apple.foundationdb.record.IndexScanType) Pair(org.apache.commons.lang3.tuple.Pair) FDBError(com.apple.foundationdb.FDBError) RecordCoreException(com.apple.foundationdb.record.RecordCoreException) RecordIndexUniquenessViolation(com.apple.foundationdb.record.RecordIndexUniquenessViolation) Expressions.concat(com.apple.foundationdb.record.metadata.Key.Expressions.concat) GroupingKeyExpression(com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression) Tag(org.junit.jupiter.api.Tag) Query(com.apple.foundationdb.record.query.expressions.Query) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) IndexOptions(com.apple.foundationdb.record.metadata.IndexOptions) Set(java.util.Set) FanType(com.apple.foundationdb.record.metadata.expressions.KeyExpression.FanType) TupleRange(com.apple.foundationdb.record.TupleRange) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) RecordMetaDataProvider(com.apple.foundationdb.record.RecordMetaDataProvider) RecordStoreState(com.apple.foundationdb.record.RecordStoreState) TupleHelpers(com.apple.foundationdb.tuple.TupleHelpers) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) InvalidIndexEntry(com.apple.foundationdb.record.provider.foundationdb.indexes.InvalidIndexEntry) AutoContinuingCursor(com.apple.foundationdb.record.cursors.AutoContinuingCursor) Matchers.is(org.hamcrest.Matchers.is) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Assertions.fail(org.junit.jupiter.api.Assertions.fail) FunctionNames(com.apple.foundationdb.record.FunctionNames) RecordMetaData(com.apple.foundationdb.record.RecordMetaData) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) IndexAggregateFunction(com.apple.foundationdb.record.metadata.IndexAggregateFunction) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) AsyncUtil(com.apple.foundationdb.async.AsyncUtil) ArrayList(java.util.ArrayList) Strings(com.google.common.base.Strings) CloseableAsyncIterator(com.apple.foundationdb.async.CloseableAsyncIterator) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) FDBRecordStoreBase.indexEntryKey(com.apple.foundationdb.record.provider.foundationdb.FDBRecordStoreBase.indexEntryKey) Nullable(javax.annotation.Nullable) Matchers.greaterThanOrEqualTo(org.hamcrest.Matchers.greaterThanOrEqualTo) IsolationLevel(com.apple.foundationdb.record.IsolationLevel) Tags(com.apple.test.Tags) TestRecords1EvolvedProto(com.apple.foundationdb.record.TestRecords1EvolvedProto) ExecutionException(java.util.concurrent.ExecutionException) Assertions.assertArrayEquals(org.junit.jupiter.api.Assertions.assertArrayEquals) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Index(com.apple.foundationdb.record.metadata.Index) FDBException(com.apple.foundationdb.FDBException) ThenKeyExpression(com.apple.foundationdb.record.metadata.expressions.ThenKeyExpression) IndexEntry(com.apple.foundationdb.record.IndexEntry) StoreTimer(com.apple.foundationdb.record.provider.common.StoreTimer) LoggerFactory(org.slf4j.LoggerFactory) Assertions.assertNotEquals(org.junit.jupiter.api.Assertions.assertNotEquals) Random(java.util.Random) Tuple(com.apple.foundationdb.tuple.Tuple) Range(com.apple.foundationdb.Range) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Expressions.concatenateFields(com.apple.foundationdb.record.metadata.Key.Expressions.concatenateFields) ImmutableSet(com.google.common.collect.ImmutableSet) TestRecords1Proto(com.apple.foundationdb.record.TestRecords1Proto) ImmutableMap(com.google.common.collect.ImmutableMap) Matchers.lessThanOrEqualTo(org.hamcrest.Matchers.lessThanOrEqualTo) Collection(java.util.Collection) CompletionException(java.util.concurrent.CompletionException) IndexQueryabilityFilter(com.apple.foundationdb.record.query.IndexQueryabilityFilter) TypeSafeMatcher(org.hamcrest.TypeSafeMatcher) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) List(java.util.List) EvaluationContext(com.apple.foundationdb.record.EvaluationContext) Matchers.equalTo(org.hamcrest.Matchers.equalTo) IndexTypes(com.apple.foundationdb.record.metadata.IndexTypes) Optional(java.util.Optional) TestNoIndexesProto(com.apple.foundationdb.record.TestNoIndexesProto) LazyCursor(com.apple.foundationdb.record.cursors.LazyCursor) EnumSource(org.junit.jupiter.params.provider.EnumSource) CompletableFuture(java.util.concurrent.CompletableFuture) Iterators(com.google.common.collect.Iterators) Key(com.apple.foundationdb.record.metadata.Key) FieldKeyExpression(com.apple.foundationdb.record.metadata.expressions.FieldKeyExpression) HashSet(java.util.HashSet) ExecuteProperties(com.apple.foundationdb.record.ExecuteProperties) ScanProperties(com.apple.foundationdb.record.ScanProperties) RecordCursorIterator(com.apple.foundationdb.record.RecordCursorIterator) BooleanSource(com.apple.test.BooleanSource) Nonnull(javax.annotation.Nonnull) Expressions.field(com.apple.foundationdb.record.metadata.Key.Expressions.field) EmptyKeyExpression(com.apple.foundationdb.record.metadata.expressions.EmptyKeyExpression) Matchers.hasEntry(org.hamcrest.Matchers.hasEntry) Description(org.hamcrest.Description) Matchers.oneOf(org.hamcrest.Matchers.oneOf) Logger(org.slf4j.Logger) RecordMetaDataBuilder(com.apple.foundationdb.record.RecordMetaDataBuilder) RecordTypeBuilder(com.apple.foundationdb.record.metadata.RecordTypeBuilder) IndexState(com.apple.foundationdb.record.IndexState) TestRecordsIndexFilteringProto(com.apple.foundationdb.record.TestRecordsIndexFilteringProto) Message(com.google.protobuf.Message) Collections(java.util.Collections) Description(org.hamcrest.Description) ArrayList(java.util.ArrayList) Index(com.apple.foundationdb.record.metadata.Index) RecordTypeBuilder(com.apple.foundationdb.record.metadata.RecordTypeBuilder) CompletableFuture(java.util.concurrent.CompletableFuture) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 99 with Index

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

the class FDBRecordStoreIndexTest method getIndexStates.

@Test
public void getIndexStates() throws Exception {
    final String indexName = "MySimpleRecord$str_value_indexed";
    try (FDBRecordContext context = openContext()) {
        openSimpleRecordStore(context);
        final Index index = recordStore.getRecordMetaData().getIndex(indexName);
        assertThat(recordStore.getAllIndexStates(), hasEntry(index, IndexState.READABLE));
        recordStore.markIndexWriteOnly(indexName).get();
        assertThat(recordStore.getAllIndexStates(), hasEntry(index, IndexState.WRITE_ONLY));
        context.commit();
    }
    try (FDBRecordContext context = openContext()) {
        openSimpleRecordStore(context);
        final Index index = recordStore.getRecordMetaData().getIndex(indexName);
        assertThat(recordStore.getAllIndexStates(), hasEntry(index, IndexState.WRITE_ONLY));
        recordStore.markIndexDisabled(indexName).get();
        assertThat(recordStore.getAllIndexStates(), hasEntry(index, IndexState.DISABLED));
    // does not commit
    }
    try (FDBRecordContext context = openContext()) {
        openSimpleRecordStore(context);
        final Index index = recordStore.getRecordMetaData().getIndex(indexName);
        assertThat(recordStore.getAllIndexStates(), hasEntry(index, IndexState.WRITE_ONLY));
        recordStore.saveRecord(TestRecords1Proto.MyOtherRecord.newBuilder().setRecNo(2).build());
        try (FDBRecordContext context2 = openContext()) {
            openSimpleRecordStore(context2);
            recordStore.markIndexReadable(indexName).get();
            context2.commit();
        }
        assertThrows(FDBExceptions.FDBStoreTransactionConflictException.class, context::commit);
    }
}
Also used : Index(com.apple.foundationdb.record.metadata.Index) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 100 with Index

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

the class FDBRecordStoreOpeningTest method storeExistenceChecksWithNoRecords.

@Test
public void storeExistenceChecksWithNoRecords() throws Exception {
    RecordMetaData metaData = RecordMetaData.build(TestRecords1Proto.getDescriptor());
    FDBRecordStore.Builder storeBuilder;
    try (FDBRecordContext context = openContext()) {
        storeBuilder = storeBuilder(context, metaData);
        FDBRecordStore store = storeBuilder.create();
        // delete the header
        store.ensureContextActive().clear(getStoreInfoKey(store));
        commit(context);
    }
    // Should be able to recover from a completely empty record store
    try (FDBRecordContext context = openContext()) {
        storeBuilder.setContext(context);
        FDBRecordStore store = storeBuilder.createOrOpen();
        // put a range subspace in for an index so that a future store opening can see it
        store.ensureContextActive().clear(getStoreInfoKey(store));
        Index foundIndex = metaData.getAllIndexes().stream().findAny().orElseGet(() -> fail("no indexes defined in meta-data"));
        new RangeSet(store.indexRangeSubspace(foundIndex)).insertRange(context.ensureActive(), null, null).get();
        // re-delete the header
        store.ensureContextActive().clear(getStoreInfoKey(store));
        commit(context);
    }
    // should be recoverable using ERROR_IF_NO_INFO_AND_HAS_RECORDS_OR_INDEXES
    try (FDBRecordContext context = openContext()) {
        storeBuilder.setContext(context);
        assertThrows(RecordStoreNoInfoAndNotEmptyException.class, storeBuilder::createOrOpen);
        commit(context);
    }
    try (FDBRecordContext context = openContext()) {
        storeBuilder.setContext(context);
        // do not perform checkVersion yet
        FDBRecordStore store = storeBuilder.build();
        assertNull(context.ensureActive().get(getStoreInfoKey(store)).get());
        assertTrue(store.checkVersion(null, FDBRecordStoreBase.StoreExistenceCheck.ERROR_IF_NO_INFO_AND_HAS_RECORDS_OR_INDEXES).get());
        commit(context);
    }
    // Delete everything except a value in the index build space
    try (FDBRecordContext context = openContext()) {
        FDBRecordStore store = storeBuilder.setContext(context).open();
        final Subspace subspace = OnlineIndexer.indexBuildScannedRecordsSubspace(store, metaData.getIndex("MySimpleRecord$str_value_indexed"));
        // set a key in the INDEX_BUILD_SPACE
        context.ensureActive().set(subspace.getKey(), FDBRecordStore.encodeRecordCount(1215));
        context.ensureActive().clear(store.getSubspace().getKey(), subspace.getKey());
        commit(context);
    }
    try (FDBRecordContext context = openContext()) {
        storeBuilder.setContext(context);
        assertThrows(RecordStoreNoInfoAndNotEmptyException.class, storeBuilder::createOrOpen);
    }
    try (FDBRecordContext context = openContext()) {
        storeBuilder.setContext(context).createOrOpen(FDBRecordStoreBase.StoreExistenceCheck.ERROR_IF_NO_INFO_AND_HAS_RECORDS_OR_INDEXES);
        commit(context);
    }
    // Insert a record, then delete the store header
    try (FDBRecordContext context = openContext()) {
        // open as the previous open with the relaxed existence check should have fixed the store header
        FDBRecordStore store = storeBuilder.setContext(context).open();
        store.saveRecord(TestRecords1Proto.MySimpleRecord.newBuilder().setRecNo(1066L).build());
        store.ensureContextActive().clear(getStoreInfoKey(store));
        commit(context);
    }
    try (FDBRecordContext context = openContext()) {
        storeBuilder.setContext(context);
        assertThrows(RecordStoreNoInfoAndNotEmptyException.class, storeBuilder::createOrOpen);
        assertThrows(RecordStoreNoInfoAndNotEmptyException.class, () -> storeBuilder.createOrOpen(FDBRecordStoreBase.StoreExistenceCheck.ERROR_IF_NO_INFO_AND_HAS_RECORDS_OR_INDEXES));
        commit(context);
    }
    // Delete the record store, then insert a key at an unknown keyspace
    try (FDBRecordContext context = openContext()) {
        FDBRecordStore.deleteStore(context, path);
        Subspace subspace = path.toSubspace(context);
        context.ensureActive().set(subspace.pack("unknown_keyspace"), Tuple.from("doesn't matter").pack());
        commit(context);
    }
    try (FDBRecordContext context = openContext()) {
        storeBuilder.setContext(context);
        assertThrows(RecordStoreNoInfoAndNotEmptyException.class, storeBuilder::createOrOpen);
        RecordCoreException err = assertThrows(RecordCoreException.class, () -> storeBuilder.createOrOpen(FDBRecordStoreBase.StoreExistenceCheck.ERROR_IF_NO_INFO_AND_HAS_RECORDS_OR_INDEXES));
        assertEquals("Unrecognized keyspace: unknown_keyspace", err.getMessage());
        commit(context);
    }
}
Also used : RecordMetaData(com.apple.foundationdb.record.RecordMetaData) RecordCoreException(com.apple.foundationdb.record.RecordCoreException) Subspace(com.apple.foundationdb.subspace.Subspace) RangeSet(com.apple.foundationdb.async.RangeSet) Index(com.apple.foundationdb.record.metadata.Index) Test(org.junit.jupiter.api.Test)

Aggregations

Index (com.apple.foundationdb.record.metadata.Index)347 Test (org.junit.jupiter.api.Test)262 List (java.util.List)183 IndexTypes (com.apple.foundationdb.record.metadata.IndexTypes)178 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)171 Collections (java.util.Collections)168 Message (com.google.protobuf.Message)166 Tags (com.apple.test.Tags)156 Tag (org.junit.jupiter.api.Tag)156 Expressions.field (com.apple.foundationdb.record.metadata.Key.Expressions.field)155 Query (com.apple.foundationdb.record.query.expressions.Query)151 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)150 Nonnull (javax.annotation.Nonnull)147 Key (com.apple.foundationdb.record.metadata.Key)143 Tuple (com.apple.foundationdb.tuple.Tuple)142 Arrays (java.util.Arrays)139 RecordCoreException (com.apple.foundationdb.record.RecordCoreException)138 RecordQuery (com.apple.foundationdb.record.query.RecordQuery)138 ArrayList (java.util.ArrayList)135 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)134