Search in sources :

Example 56 with GroupingKeyExpression

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

the class BitmapValueIndexMaintainerFactory method getIndexValidator.

@Override
@Nonnull
public IndexValidator getIndexValidator(Index index) {
    return new IndexValidator(index) {

        @Override
        public void validate(@Nonnull MetaDataValidator metaDataValidator) {
            super.validate(metaDataValidator);
            validateGrouping(1);
            final GroupingKeyExpression group = (GroupingKeyExpression) index.getRootExpression();
            if (group.getGroupedCount() != 1) {
                throw new KeyExpression.InvalidExpressionException(String.format("%s index needs grouped position", index.getType()), LogMessageKeys.INDEX_NAME, index.getName(), LogMessageKeys.INDEX_KEY, index.getRootExpression());
            }
            validateNotVersion();
        }

        @Override
        @SuppressWarnings("fallthrough")
        public void validateIndexForRecordType(@Nonnull RecordType recordType, @Nonnull MetaDataValidator metaDataValidator) {
            final List<Descriptors.FieldDescriptor> fields = metaDataValidator.validateIndexForRecordType(index, recordType);
            switch(fields.get(fields.size() - 1).getType()) {
                case INT64:
                case UINT64:
                case INT32:
                case UINT32:
                case SINT32:
                case SINT64:
                case FIXED32:
                case FIXED64:
                case SFIXED32:
                case SFIXED64:
                    break;
                default:
                    throw new KeyExpression.InvalidExpressionException(String.format("%s index only supports integer position key", index.getType()), LogMessageKeys.INDEX_NAME, index.getName(), LogMessageKeys.INDEX_KEY, index.getRootExpression(), "record_type", recordType.getName());
            }
        }
    };
}
Also used : IndexValidator(com.apple.foundationdb.record.metadata.IndexValidator) RecordType(com.apple.foundationdb.record.metadata.RecordType) Nonnull(javax.annotation.Nonnull) GroupingKeyExpression(com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression) MetaDataValidator(com.apple.foundationdb.record.metadata.MetaDataValidator) Nonnull(javax.annotation.Nonnull)

Example 57 with GroupingKeyExpression

use of com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression in project lionrock by panghy.

the class RemoteRecordLayerTests method setupConnection.

@BeforeEach
public void setupConnection() {
    logger.info("setupConnection()");
    RemoteFDBDatabaseFactory factory = new RemoteFDBDatabaseFactory(channel, "RemoteRecordLayerTests");
    this.fdb = factory.getDatabase();
    this.fdb.setAsyncToSyncExceptionMapper((ex, event) -> {
        if (ex instanceof ExecutionException || ex instanceof CompletionException) {
            ex = ex.getCause();
        }
        if (ex instanceof FDBException) {
            FDBException fdbex = (FDBException) ex;
            switch(FDBError.fromCode(fdbex.getCode())) {
                case TRANSACTION_TOO_OLD:
                    return (new FDBExceptions.FDBStoreTransactionIsTooOldException(fdbex));
                case NOT_COMMITTED:
                    return (new FDBExceptions.FDBStoreTransactionConflictException(fdbex));
                case TRANSACTION_TIMED_OUT:
                    return (new FDBExceptions.FDBStoreTransactionTimeoutException(fdbex));
                case TRANSACTION_TOO_LARGE:
                    return (new FDBExceptions.FDBStoreTransactionSizeException(fdbex));
                case KEY_TOO_LARGE:
                    return (new FDBExceptions.FDBStoreKeySizeException(fdbex));
                case VALUE_TOO_LARGE:
                    return (new FDBExceptions.FDBStoreValueSizeException(fdbex));
                default:
                    return fdbex.isRetryable() ? (new FDBExceptions.FDBStoreRetriableException(fdbex)) : (new FDBExceptions.FDBStoreException(fdbex));
            }
        } else if (ex instanceof RuntimeException) {
            return (RuntimeException) ex;
        } else {
            return ex instanceof InterruptedException ? (new RecordCoreInterruptedException(ex.getMessage(), new Object[] { ex })) : (new RecordCoreException(ex.getMessage(), ex));
        }
    });
    final KeySpace keySpace = new KeySpace(new DirectoryLayerDirectory("application").addSubdirectory(new KeySpaceDirectory("environment", KeySpaceDirectory.KeyType.STRING)));
    this.path = keySpace.path("application", "record-layer-sample").add("environment", "demo");
    this.fdb.runAsync(path::deleteAllDataAsync).join();
    RecordMetaDataBuilder rmdBuilder = RecordMetaData.newBuilder().setRecords(SampleProto.getDescriptor());
    rmdBuilder.getRecordType("Customer").setPrimaryKey(concatenateFields("last_name", "first_name", "customer_id"));
    rmdBuilder.addUniversalIndex(new Index("globalCount", new GroupingKeyExpression(EmptyKeyExpression.EMPTY, 0), IndexTypes.COUNT));
    rmdBuilder.addIndex("Customer", new Index("first_name", field("first_name"), IndexTypes.VALUE));
    rmdBuilder.addIndex("Customer", new Index("email_address", field("email_address", KeyExpression.FanType.FanOut), IndexTypes.VALUE));
    rmdBuilder.addIndex("Customer", new Index("preference_tag", field("preference_tag", KeyExpression.FanType.Concatenate), IndexTypes.VALUE));
    rmdBuilder.addIndex("Customer", new Index("preference_tag_count", new GroupingKeyExpression(field("preference_tag", KeyExpression.FanType.FanOut), 0), IndexTypes.COUNT));
    rmdBuilder.addIndex("Customer", new Index("order", field("order", KeyExpression.FanType.FanOut).nest("quantity"), IndexTypes.VALUE));
    rmdBuilder.addIndex("Customer", new Index("item_quantity_sum", new GroupingKeyExpression(field("order", KeyExpression.FanType.FanOut).nest(concatenateFields("item_id", "quantity")), 1), IndexTypes.SUM));
    RecordMetaData rmd = rmdBuilder.getRecordMetaData();
    this.recordStoreBuilder = FDBRecordStore.newBuilder().setMetaDataProvider(rmd).setKeySpacePath(path);
    RecordStoreState storeState = fdb.run(cx -> {
        FDBRecordStore store = recordStoreBuilder.copyBuilder().setContext(cx).createOrOpen();
        return store.getRecordStoreState();
    });
    logger.info("storeState: " + storeState.toString());
    Set<String> disabledIndexNames = storeState.getDisabledIndexNames();
    logger.info("disabledIndexNames: " + Joiner.on(",").join(disabledIndexNames));
    AsyncUtil.whenAll(storeState.getDisabledIndexNames().stream().map(indexName -> {
        // Build this index. It will begin the background job and return a future
        // that will complete when the index is ready for querying.
        OnlineIndexer indexBuilder = OnlineIndexer.newBuilder().setDatabase(fdb).setRecordStoreBuilder(recordStoreBuilder).setIndex(indexName).build();
        return indexBuilder.buildIndexAsync().whenComplete((vignore, eignore) -> {
            logger.info("Finished Rebuilding Index: " + indexName);
            indexBuilder.close();
        });
    }).collect(Collectors.toList())).join();
    writeRecords();
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) java.util(java.util) IndexAggregateFunction(com.apple.foundationdb.record.metadata.IndexAggregateFunction) KeySpace(com.apple.foundationdb.record.provider.foundationdb.keyspace.KeySpace) LoggerFactory(org.slf4j.LoggerFactory) AsyncUtil(com.apple.foundationdb.async.AsyncUtil) RecordQuery(com.apple.foundationdb.record.query.RecordQuery) RecordQueryPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan) Key(com.apple.foundationdb.record.metadata.Key) Pair(org.apache.commons.lang3.tuple.Pair) FDBError(com.apple.foundationdb.FDBError) Expressions.concatenateFields(com.apple.foundationdb.record.metadata.Key.Expressions.concatenateFields) GroupingKeyExpression(com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Expressions.field(com.apple.foundationdb.record.metadata.Key.Expressions.field) EmptyKeyExpression(com.apple.foundationdb.record.metadata.expressions.EmptyKeyExpression) DirectoryLayerDirectory(com.apple.foundationdb.record.provider.foundationdb.keyspace.DirectoryLayerDirectory) Query(com.apple.foundationdb.record.query.expressions.Query) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) Logger(org.slf4j.Logger) KeySpaceDirectory(com.apple.foundationdb.record.provider.foundationdb.keyspace.KeySpaceDirectory) CompletionException(java.util.concurrent.CompletionException) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) ExecutionException(java.util.concurrent.ExecutionException) com.apple.foundationdb.record.provider.foundationdb(com.apple.foundationdb.record.provider.foundationdb) KeySpacePath(com.apple.foundationdb.record.provider.foundationdb.keyspace.KeySpacePath) com.apple.foundationdb.record(com.apple.foundationdb.record) Index(com.apple.foundationdb.record.metadata.Index) FDBException(com.apple.foundationdb.FDBException) SampleProto(com.apple.foundationdb.record.sample.SampleProto) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Message(com.google.protobuf.Message) IndexTypes(com.apple.foundationdb.record.metadata.IndexTypes) Joiner(com.google.common.base.Joiner) AbstractGrpcTest(io.github.panghy.lionrock.foundationdb.AbstractGrpcTest) KeySpace(com.apple.foundationdb.record.provider.foundationdb.keyspace.KeySpace) Index(com.apple.foundationdb.record.metadata.Index) DirectoryLayerDirectory(com.apple.foundationdb.record.provider.foundationdb.keyspace.DirectoryLayerDirectory) ExecutionException(java.util.concurrent.ExecutionException) GroupingKeyExpression(com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression) FDBException(com.apple.foundationdb.FDBException) KeySpaceDirectory(com.apple.foundationdb.record.provider.foundationdb.keyspace.KeySpaceDirectory) CompletionException(java.util.concurrent.CompletionException) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

GroupingKeyExpression (com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression)57 Index (com.apple.foundationdb.record.metadata.Index)41 Test (org.junit.jupiter.api.Test)39 KeyExpression (com.apple.foundationdb.record.metadata.expressions.KeyExpression)35 ArrayList (java.util.ArrayList)29 Nonnull (javax.annotation.Nonnull)25 IndexTypes (com.apple.foundationdb.record.metadata.IndexTypes)23 RecordCoreException (com.apple.foundationdb.record.RecordCoreException)22 List (java.util.List)22 RecordMetaData (com.apple.foundationdb.record.RecordMetaData)21 Message (com.google.protobuf.Message)21 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)21 RecordMetaDataBuilder (com.apple.foundationdb.record.RecordMetaDataBuilder)20 IndexAggregateFunction (com.apple.foundationdb.record.metadata.IndexAggregateFunction)20 Expressions.field (com.apple.foundationdb.record.metadata.Key.Expressions.field)20 EmptyKeyExpression (com.apple.foundationdb.record.metadata.expressions.EmptyKeyExpression)20 Query (com.apple.foundationdb.record.query.expressions.Query)20 Tuple (com.apple.foundationdb.tuple.Tuple)20 Tags (com.apple.test.Tags)20 Tag (org.junit.jupiter.api.Tag)20