use of io.spine.server.entity.EntityRecord in project core-java by SpineEventEngine.
the class RecordStorage method read.
/**
* Reads a single item from the storage and applies a {@link FieldMask} to it.
*
* @param id ID of the item to read.
* @param fieldMask fields to read.
* @return the item with the given ID and with the {@code FieldMask} applied.
* @see #read(Object)
*/
public Optional<EntityRecord> read(I id, FieldMask fieldMask) {
final Optional<EntityRecord> rawResult = read(id);
if (!rawResult.isPresent()) {
return Optional.absent();
}
final EntityRecord.Builder builder = EntityRecord.newBuilder(rawResult.get());
final Any state = builder.getState();
final TypeUrl type = TypeUrl.parse(state.getTypeUrl());
final Message stateAsMessage = AnyPacker.unpack(state);
final Message maskedState = FieldMasks.applyMask(fieldMask, stateAsMessage, type);
final Any packedState = AnyPacker.pack(maskedState);
builder.setState(packedState);
return Optional.of(builder.build());
}
use of io.spine.server.entity.EntityRecord in project core-java by SpineEventEngine.
the class TenantRecords method findAndApplyFieldMask.
EntityRecord findAndApplyFieldMask(I givenId, FieldMask fieldMask) {
EntityRecord matchingResult = null;
for (I recordId : filtered.keySet()) {
if (recordId.equals(givenId)) {
final Optional<EntityRecordWithColumns> record = get(recordId);
if (!record.isPresent()) {
continue;
}
EntityRecord.Builder matchingRecord = record.get().getRecord().toBuilder();
final Any state = matchingRecord.getState();
final TypeUrl typeUrl = TypeUrl.parse(state.getTypeUrl());
final Message wholeState = unpack(state);
final Message maskedState = applyMask(fieldMask, wholeState, typeUrl);
final Any processed = pack(maskedState);
matchingRecord.setState(processed);
matchingResult = matchingRecord.build();
}
}
return matchingResult;
}
use of io.spine.server.entity.EntityRecord in project core-java by SpineEventEngine.
the class ProjectionStorageShould method read_all_messages_with_field_mask.
@SuppressWarnings("MethodWithMultipleLoops")
@Test
public void read_all_messages_with_field_mask() {
final List<ProjectId> ids = fillStorage(5);
final String projectDescriptor = Project.getDescriptor().getFullName();
@SuppressWarnings("DuplicateStringLiteralInspection") final FieldMask // clashes with non-related tests.
fieldMask = maskForPaths(projectDescriptor + ".id", projectDescriptor + ".name");
final Iterator<EntityRecord> read = storage.readAll(fieldMask);
final Collection<EntityRecord> readRecords = newArrayList(read);
assertSize(ids.size(), readRecords);
for (EntityRecord record : readRecords) {
final Any packedState = record.getState();
final Project state = AnyPacker.unpack(packedState);
assertMatchesMask(state, fieldMask);
}
}
use of io.spine.server.entity.EntityRecord in project core-java by SpineEventEngine.
the class AggregateQueryProcessor method process.
@Override
public ImmutableCollection<Any> process(Query query) {
final ImmutableList.Builder<Any> resultBuilder = ImmutableList.builder();
Iterator<EntityRecord> stateRecords;
final Target target = query.getTarget();
final FieldMask fieldMask = query.getFieldMask();
final boolean shouldApplyFieldMask = !fieldMask.getPathsList().isEmpty();
if (target.getIncludeAll()) {
stateRecords = shouldApplyFieldMask ? standStorage.readAllByType(type, fieldMask) : standStorage.readAllByType(type);
} else {
stateRecords = doFetchWithFilters(target, fieldMask);
}
while (stateRecords.hasNext()) {
final EntityRecord record = stateRecords.next();
final Any state = record.getState();
resultBuilder.add(state);
}
final ImmutableList<Any> result = resultBuilder.build();
return result;
}
use of io.spine.server.entity.EntityRecord in project core-java by SpineEventEngine.
the class RecordStorage method read.
/**
* Reads a record, which matches the specified {@linkplain RecordReadRequest request}
* and applies a {@link FieldMask} to it.
*
* @param request the request to read the record
* @param fieldMask fields to read.
* @return the item with the given ID and with the {@code FieldMask} applied
* or {@code Optional.absent()} if there is no record matching this request
* @see #read(RecordReadRequest)
*/
public Optional<EntityRecord> read(RecordReadRequest<I> request, FieldMask fieldMask) {
final Optional<EntityRecord> rawResult = read(request);
if (!rawResult.isPresent()) {
return Optional.absent();
}
final EntityRecord.Builder builder = EntityRecord.newBuilder(rawResult.get());
final Any state = builder.getState();
final TypeUrl type = TypeUrl.parse(state.getTypeUrl());
final Message stateAsMessage = AnyPacker.unpack(state);
final Message maskedState = FieldMasks.applyMask(fieldMask, stateAsMessage, type);
final Any packedState = AnyPacker.pack(maskedState);
builder.setState(packedState);
return Optional.of(builder.build());
}
Aggregations