Search in sources :

Example 51 with RecordCoreException

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

the class FDBRecordContextTest method logWithoutSettingId.

@Test
public void logWithoutSettingId() {
    try (FDBRecordContext context = fdb.openContext()) {
        RecordCoreException err = assertThrows(RecordCoreException.class, context::logTransaction);
        assertEquals("Cannot log transaction as ID is not set", err.getMessage());
        context.ensureActive().getReadVersion().join();
        context.commit();
    }
}
Also used : RecordCoreException(com.apple.foundationdb.record.RecordCoreException) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 52 with RecordCoreException

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

the class FDBRecordStoreCountRecordsTest method addCountIndex.

@Test
public void addCountIndex() throws Exception {
    RecordMetaDataHook removeCountHook = metaData -> metaData.removeIndex(COUNT_INDEX.getName());
    try (FDBRecordContext context = openContext()) {
        openSimpleRecordStore(context, removeCountHook);
        for (int i = 0; i < 10; i++) {
            recordStore.saveRecord(makeRecord(i, 1066, i % 5));
        }
        commit(context);
    }
    try (FDBRecordContext context = openContext()) {
        openSimpleRecordStore(context, removeCountHook);
        recordStore.getSnapshotRecordCount().get();
        fail("evaluated count without index or key");
    } catch (RecordCoreException e) {
        assertThat(e.getMessage(), containsString("requires appropriate index"));
    }
    RecordMetaDataHook hook = countKeyHook(field("num_value_3_indexed"), true, 10);
    try (FDBRecordContext context = openContext()) {
        openSimpleRecordStore(context, hook);
        commit(context);
    }
    try (FDBRecordContext context = openContext()) {
        openSimpleRecordStore(context, hook);
        Index countIndex = recordStore.getRecordMetaData().getIndex("record_count");
        assertThat(recordStore.getRecordStoreState().isReadable(countIndex), is(false));
        assertThat(recordStore.getRecordStoreState().isDisabled(countIndex), is(true));
        RecordCoreException e = assertThrows(RecordCoreException.class, () -> recordStore.getSnapshotRecordCount().get());
        assertThat(e.getMessage(), containsString("requires appropriate index"));
    }
    // Build the index
    try (OnlineIndexer onlineIndexBuilder = OnlineIndexer.forRecordStoreAndIndex(recordStore, "record_count")) {
        onlineIndexBuilder.buildIndex();
    }
    try (FDBRecordContext context = openContext()) {
        openSimpleRecordStore(context, hook);
        Index countIndex = recordStore.getRecordMetaData().getIndex("record_count");
        assertThat(recordStore.getRecordStoreState().isWriteOnly(countIndex), is(false));
        assertEquals(10L, recordStore.getSnapshotRecordCount().get().longValue());
    }
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Assertions.fail(org.junit.jupiter.api.Assertions.fail) RecordMetaData(com.apple.foundationdb.record.RecordMetaData) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Key(com.apple.foundationdb.record.metadata.Key) ExecuteProperties(com.apple.foundationdb.record.ExecuteProperties) Tuple(com.apple.foundationdb.tuple.Tuple) EndpointType(com.apple.foundationdb.record.EndpointType) RecordCoreException(com.apple.foundationdb.record.RecordCoreException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ScanProperties(com.apple.foundationdb.record.ScanProperties) Expressions.concatenateFields(com.apple.foundationdb.record.metadata.Key.Expressions.concatenateFields) RecordMetaDataProto(com.apple.foundationdb.record.RecordMetaDataProto) Is.is(org.hamcrest.core.Is.is) Expressions.concat(com.apple.foundationdb.record.metadata.Key.Expressions.concat) GroupingKeyExpression(com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression) Tag(org.junit.jupiter.api.Tag) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) TestRecordsWithUnionProto(com.apple.foundationdb.record.TestRecordsWithUnionProto) Expressions.field(com.apple.foundationdb.record.metadata.Key.Expressions.field) EmptyKeyExpression(com.apple.foundationdb.record.metadata.expressions.EmptyKeyExpression) Query(com.apple.foundationdb.record.query.expressions.Query) IsolationLevel(com.apple.foundationdb.record.IsolationLevel) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) TestRecords1Proto(com.apple.foundationdb.record.TestRecords1Proto) RecordMetaDataBuilder(com.apple.foundationdb.record.RecordMetaDataBuilder) Tags(com.apple.test.Tags) IndexState(com.apple.foundationdb.record.IndexState) Test(org.junit.jupiter.api.Test) RecordMetaDataProvider(com.apple.foundationdb.record.RecordMetaDataProvider) Index(com.apple.foundationdb.record.metadata.Index) Matchers.equalTo(org.hamcrest.Matchers.equalTo) IndexTypes(com.apple.foundationdb.record.metadata.IndexTypes) TestRecordsWithHeaderProto(com.apple.foundationdb.record.TestRecordsWithHeaderProto) Matchers.containsString(org.hamcrest.Matchers.containsString) RecordCoreException(com.apple.foundationdb.record.RecordCoreException) Index(com.apple.foundationdb.record.metadata.Index) Test(org.junit.jupiter.api.Test)

Example 53 with RecordCoreException

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

the class FDBReverseDirectoryCacheTest method testPutIfNotExistsWrongValue.

@Test
public void testPutIfNotExistsWrongValue() throws Exception {
    final Random random = new Random();
    final String name = "dir_" + Math.abs(random.nextInt());
    final Long id;
    ScopedValue<String> scopedName = globalScope.wrap(name);
    try (FDBRecordContext context = openContext()) {
        Transaction tr = context.ensureActive();
        // need to use the FDB DirectoryLayer to bypass LocatableResolver which populates the reverse directory cache automatically
        id = Tuple.fromBytes(DirectoryLayer.getDefault().createOrOpen(tr, PathUtil.from(name)).get().pack()).getLong(0);
        commit(context);
    }
    FDBReverseDirectoryCache rdc = fdb.getReverseDirectoryCache();
    // Store the correct value
    try (FDBRecordContext context = openContext()) {
        rdc.putIfNotExists(context, scopedName, id).get();
        // The put should be considered a hard miss because it had to write to the cache
        assertEquals(0L, rdc.getPersistentCacheHitCount());
        assertEquals(1L, rdc.getPersistentCacheMissCount());
        assertEquals(0L, context.getTimer().getCount(FDBStoreTimer.Counts.REVERSE_DIR_PERSISTENT_CACHE_HIT_COUNT));
        assertEquals(1L, context.getTimer().getCount(FDBStoreTimer.Counts.REVERSE_DIR_PERSISTENT_CACHE_MISS_COUNT));
        commit(context);
    }
    // Try again with a different value
    try (FDBRecordContext context = openContext()) {
        rdc.putIfNotExists(context, globalScope.wrap(name + "_x"), id).get();
        commit(context);
        fail("Should have thrown an exception due to wrong value");
    } catch (ExecutionException e) {
        // This will throw if we didn't get the exception we wanted
        RecordCoreException yay = (RecordCoreException) e.getCause();
    }
}
Also used : RecordCoreException(com.apple.foundationdb.record.RecordCoreException) Random(java.util.Random) Transaction(com.apple.foundationdb.Transaction) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.jupiter.api.Test)

Example 54 with RecordCoreException

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

the class LuceneIndexQueryPlan method getToPartialRecord.

/**
 * Get the {@link IndexKeyValueToPartialRecord} instance for an {@link IndexEntry} representing a result of Lucene auto-complete suggestion.
 * The partial record contains the suggestion in the field where it is indexed from, and the grouping keys if there are any.
 */
@VisibleForTesting
public static IndexKeyValueToPartialRecord getToPartialRecord(@Nonnull Index index, @Nonnull RecordType recordType, @Nonnull IndexScanType scanType) {
    final IndexKeyValueToPartialRecord.Builder builder = IndexKeyValueToPartialRecord.newBuilder(recordType);
    KeyExpression root = index.getRootExpression();
    if (root instanceof GroupingKeyExpression) {
        KeyExpression groupingKey = ((GroupingKeyExpression) root).getGroupingSubKey();
        for (int i = 0; i < groupingKey.getColumnSize(); i++) {
            AvailableFields.addCoveringField(groupingKey, AvailableFields.FieldData.of(IndexKeyValueToPartialRecord.TupleSource.KEY, i), builder);
        }
    }
    builder.addRequiredMessageFields();
    if (!builder.isValid(true)) {
        throw new RecordCoreException("Missing required field for result record").addLogInfo(LogMessageKeys.INDEX_NAME, index.getName()).addLogInfo(LogMessageKeys.RECORD_TYPE, recordType.getName()).addLogInfo(LogMessageKeys.SCAN_TYPE, scanType);
    }
    builder.addRegularCopier(new LuceneIndexKeyValueToPartialRecordUtils.LuceneAutoCompleteCopier());
    return builder.build();
}
Also used : RecordCoreException(com.apple.foundationdb.record.RecordCoreException) IndexKeyValueToPartialRecord(com.apple.foundationdb.record.query.plan.IndexKeyValueToPartialRecord) GroupingKeyExpression(com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression) GroupingKeyExpression(com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 55 with RecordCoreException

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

the class LuceneIndexKeyValueToPartialRecordUtils method buildPartialRecord.

public static void buildPartialRecord(@Nonnull KeyExpression root, @Nonnull Descriptors.Descriptor descriptor, @Nonnull Message.Builder builder, @Nonnull String luceneField, @Nonnull String suggestion, @Nonnull Tuple groupingKey) {
    final KeyExpression expression = root instanceof GroupingKeyExpression ? ((GroupingKeyExpression) root).getWholeKey() : root;
    LuceneIndexExpressions.getFieldsRecursively(expression, new PartialRecordBuildSource(null, descriptor, builder), (source, fieldName, value, type, stored, overriddenKeyRanges, groupingKeyIndex) -> {
        if (groupingKeyIndex > -1) {
            if (groupingKeyIndex > groupingKey.size() - 1) {
                throw new RecordCoreException("Invalid grouping value tuple given a grouping key").addLogInfo(LogMessageKeys.VALUE, groupingKey.toString());
            }
            source.buildMessage(groupingKey.get(groupingKeyIndex), (String) value, null, null, false);
        } else if (type.equals(LuceneIndexExpressions.DocumentFieldType.TEXT)) {
            buildIfFieldNameMatch(source, fieldName, luceneField, overriddenKeyRanges, suggestion, (String) value);
        }
    }, null, 0, root instanceof GroupingKeyExpression ? ((GroupingKeyExpression) root).getGroupingCount() : 0, new ArrayList<>());
}
Also used : RecordCoreException(com.apple.foundationdb.record.RecordCoreException) GroupingKeyExpression(com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) FieldKeyExpression(com.apple.foundationdb.record.metadata.expressions.FieldKeyExpression) GroupingKeyExpression(com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression)

Aggregations

RecordCoreException (com.apple.foundationdb.record.RecordCoreException)121 Nonnull (javax.annotation.Nonnull)58 Test (org.junit.jupiter.api.Test)42 Index (com.apple.foundationdb.record.metadata.Index)37 List (java.util.List)35 Nullable (javax.annotation.Nullable)31 Tuple (com.apple.foundationdb.tuple.Tuple)29 ArrayList (java.util.ArrayList)27 KeyExpression (com.apple.foundationdb.record.metadata.expressions.KeyExpression)26 CompletableFuture (java.util.concurrent.CompletableFuture)25 Collectors (java.util.stream.Collectors)24 Collections (java.util.Collections)22 GroupingKeyExpression (com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression)19 Set (java.util.Set)19 Function (java.util.function.Function)19 IndexEntry (com.apple.foundationdb.record.IndexEntry)17 TupleRange (com.apple.foundationdb.record.TupleRange)17 IndexTypes (com.apple.foundationdb.record.metadata.IndexTypes)17 RecordCursor (com.apple.foundationdb.record.RecordCursor)16 AsyncUtil (com.apple.foundationdb.async.AsyncUtil)15