Search in sources :

Example 36 with EntityRecord

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());
}
Also used : EntityRecord(io.spine.server.entity.EntityRecord) Message(com.google.protobuf.Message) TypeUrl(io.spine.type.TypeUrl) Any(com.google.protobuf.Any)

Example 37 with EntityRecord

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;
}
Also used : EntityRecord(io.spine.server.entity.EntityRecord) Message(com.google.protobuf.Message) TypeUrl(io.spine.type.TypeUrl) Any(com.google.protobuf.Any) EntityRecordWithColumns(io.spine.server.entity.storage.EntityRecordWithColumns)

Example 38 with EntityRecord

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);
    }
}
Also used : EntityRecord(io.spine.server.entity.EntityRecord) Project(io.spine.test.storage.Project) ProjectId(io.spine.test.storage.ProjectId) Any(com.google.protobuf.Any) FieldMask(com.google.protobuf.FieldMask) Test(org.junit.Test)

Example 39 with EntityRecord

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;
}
Also used : EntityRecord(io.spine.server.entity.EntityRecord) Target(io.spine.client.Target) ImmutableList(com.google.common.collect.ImmutableList) Any(com.google.protobuf.Any) FieldMask(com.google.protobuf.FieldMask)

Example 40 with EntityRecord

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());
}
Also used : EntityRecord(io.spine.server.entity.EntityRecord) Message(com.google.protobuf.Message) TypeUrl(io.spine.type.TypeUrl) Any(com.google.protobuf.Any)

Aggregations

EntityRecord (io.spine.server.entity.EntityRecord)55 Test (org.junit.Test)34 Any (com.google.protobuf.Any)24 EntityRecordWithColumns (io.spine.server.entity.storage.EntityRecordWithColumns)15 Message (com.google.protobuf.Message)10 Project (io.spine.test.storage.Project)10 TypeUrl (io.spine.type.TypeUrl)9 FieldMask (com.google.protobuf.FieldMask)7 EntityFilters (io.spine.client.EntityFilters)7 ProjectId (io.spine.test.storage.ProjectId)7 ColumnFilter (io.spine.client.ColumnFilter)5 EntityIdFilter (io.spine.client.EntityIdFilter)4 LinkedList (java.util.LinkedList)4 CompositeColumnFilter (io.spine.client.CompositeColumnFilter)3 Version (io.spine.core.Version)3 CompositeQueryParameter (io.spine.server.entity.storage.CompositeQueryParameter)3 EntityColumn (io.spine.server.entity.storage.EntityColumn)3 QueryParameters (io.spine.server.entity.storage.QueryParameters)3 StandStorage (io.spine.server.stand.StandStorage)3 ImmutableList (com.google.common.collect.ImmutableList)2