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());
}
}
};
}
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();
}
Aggregations