use of com.apple.foundationdb.annotation.API in project fdb-record-layer by FoundationDB.
the class FDBMetaDataStore method updateRecordsAsync.
/**
* Update the meta-data records descriptor asynchronously.
* This adds any new record types to the meta-data and replaces all of the current descriptors with the new descriptors.
*
* <p>
* It is important to note that if a local file descriptor is set using {@link #setLocalFileDescriptor(Descriptors.FileDescriptor)},
* the local file descriptor must be at least as evolved as the records descriptor passed to this method.
* Otherwise, {@code updateRecordsAsync} will fail.
* </p>
*
* <p>
* If the given file descriptor is missing a union message, this method will add one before updating the meta-data.
* The generated union descriptor is constructed by adding any non-{@code NESTED} types in the file descriptor to the
* union descriptor from the currently stored meta-data. A new field is not added if a field of the given type already
* exists, and the order of any existing fields is preserved. Note that types are identified by name, so renaming
* top-level message types may result in validation errors when trying to update the record descriptor.
* </p>
*
* @param recordsDescriptor the new recordsDescriptor
* @return a future that completes when the records descriptor is updated
*/
@Nonnull
@API(API.Status.MAINTAINED)
public CompletableFuture<Void> updateRecordsAsync(@Nonnull Descriptors.FileDescriptor recordsDescriptor) {
return loadCurrentProto().thenCompose(metaDataProto -> {
// Update the records without using its local file descriptor. Let saveAndSetCurrent use the local file descriptor when saving the meta-data.
RecordMetaDataBuilder recordMetaDataBuilder = createMetaDataBuilder(metaDataProto, false);
// If union is missing, first add a default union.
recordMetaDataBuilder.updateRecords(MetaDataProtoEditor.addDefaultUnionIfMissing(recordsDescriptor, recordMetaDataBuilder.getUnionDescriptor()));
return saveAndSetCurrent(recordMetaDataBuilder.getRecordMetaData().toProto());
});
}
use of com.apple.foundationdb.annotation.API in project fdb-record-layer by FoundationDB.
the class RecordMetaDataBuilder method addJoinedRecordType.
/**
* Add a new joined record type.
* @param name the name of the new record type
* @return a new uninitialized joined record type
*/
@Nonnull
@API(API.Status.EXPERIMENTAL)
public JoinedRecordTypeBuilder addJoinedRecordType(@Nonnull String name) {
if (recordTypes.containsKey(name)) {
throw new MetaDataException("There is already a record type named " + name);
}
if (syntheticRecordTypes.containsKey(name)) {
throw new MetaDataException("There is already a synthetic record type named " + name);
}
JoinedRecordTypeBuilder recordType = new JoinedRecordTypeBuilder(name, getNextRecordTypeKey(), this);
syntheticRecordTypes.put(name, recordType);
return recordType;
}
use of com.apple.foundationdb.annotation.API in project fdb-record-layer by FoundationDB.
the class FDBRecordStore method loadSyntheticRecord.
/**
* Load a {@link FDBSyntheticRecord synthetic record} by loading its stored constituent records and synthesizing it from them.
* @param primaryKey the primary key of the synthetic record, which includes the primary keys of the constituents
* @return a future which completes to the synthesized record
*/
@Nonnull
@API(API.Status.EXPERIMENTAL)
public CompletableFuture<FDBSyntheticRecord> loadSyntheticRecord(@Nonnull Tuple primaryKey) {
SyntheticRecordType<?> syntheticRecordType = getRecordMetaData().getSyntheticRecordTypeFromRecordTypeKey(primaryKey.get(0));
int nconstituents = syntheticRecordType.getConstituents().size();
if (nconstituents != primaryKey.size() - 1) {
throw recordCoreException("Primary key does not have correct number of nested keys: " + primaryKey);
}
final Map<String, FDBStoredRecord<? extends Message>> constituents = new ConcurrentHashMap<>(nconstituents);
final CompletableFuture<?>[] futures = new CompletableFuture<?>[nconstituents];
for (int i = 0; i < nconstituents; i++) {
final SyntheticRecordType.Constituent constituent = syntheticRecordType.getConstituents().get(i);
final Tuple constituentKey = primaryKey.getNestedTuple(i + 1);
if (constituentKey == null) {
futures[i] = AsyncUtil.DONE;
} else {
futures[i] = loadRecordAsync(constituentKey).thenApply(rec -> {
if (rec == null) {
throw new RecordDoesNotExistException("constituent record not found: " + constituent.getName());
}
constituents.put(constituent.getName(), rec);
return null;
});
}
}
return CompletableFuture.allOf(futures).thenApply(vignore -> FDBSyntheticRecord.of(syntheticRecordType, constituents));
}
Aggregations