Search in sources :

Example 26 with EntityRecord

use of io.spine.server.entity.EntityRecord in project core-java by SpineEventEngine.

the class InMemoryRecordStorage method readMultipleRecords.

@Override
protected Iterable<EntityRecord> readMultipleRecords(final Iterable<I> givenIds, FieldMask fieldMask) {
    final TenantRecords<I> storage = getStorage();
    // It is not possible to return an immutable collection,
    // since null may be present in it.
    final Collection<EntityRecord> result = new LinkedList<>();
    for (I givenId : givenIds) {
        final EntityRecord matchingResult = storage.findAndApplyFieldMask(givenId, fieldMask);
        result.add(matchingResult);
    }
    return result;
}
Also used : EntityRecord(io.spine.server.entity.EntityRecord) LinkedList(java.util.LinkedList)

Example 27 with EntityRecord

use of io.spine.server.entity.EntityRecord in project core-java by SpineEventEngine.

the class TenantRecords method readAllRecords.

Map<I, EntityRecord> readAllRecords(FieldMask fieldMask) {
    if (fieldMask.getPathsList().isEmpty()) {
        return readAllRecords();
    }
    if (isEmpty()) {
        return ImmutableMap.of();
    }
    final ImmutableMap.Builder<I, EntityRecord> result = ImmutableMap.builder();
    for (Map.Entry<I, EntityRecordWithColumns> storageEntry : filtered.entrySet()) {
        final I id = storageEntry.getKey();
        final EntityRecord rawRecord = storageEntry.getValue().getRecord();
        final TypeUrl type = TypeUrl.parse(rawRecord.getState().getTypeUrl());
        final Any recordState = rawRecord.getState();
        final Message stateAsMessage = unpack(recordState);
        final Message processedState = applyMask(fieldMask, stateAsMessage, type);
        final Any packedState = pack(processedState);
        final EntityRecord resultingRecord = EntityRecord.newBuilder().setState(packedState).build();
        result.put(id, resultingRecord);
    }
    return result.build();
}
Also used : EntityRecord(io.spine.server.entity.EntityRecord) Message(com.google.protobuf.Message) TypeUrl(io.spine.type.TypeUrl) ImmutableMap(com.google.common.collect.ImmutableMap) Maps.newHashMap(com.google.common.collect.Maps.newHashMap) Map(java.util.Map) Any(com.google.protobuf.Any) ImmutableMap(com.google.common.collect.ImmutableMap) EntityRecordWithColumns(io.spine.server.entity.storage.EntityRecordWithColumns)

Example 28 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 29 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<I> ids = fillStorage(5);
    final String projectDescriptor = Project.getDescriptor().getFullName();
    // clashes with non-related tests.
    @SuppressWarnings("DuplicateStringLiteralInspection") final FieldMask fieldMask = maskForPaths(projectDescriptor + ".id", projectDescriptor + ".name");
    final Map<I, EntityRecord> read = storage.readAll(fieldMask);
    assertSize(ids.size(), read);
    for (Map.Entry<I, EntityRecord> record : read.entrySet()) {
        assertContains(record.getKey(), ids);
        final Any packedState = record.getValue().getState();
        final Project state = AnyPacker.unpack(packedState);
        assertMatchesMask(state, fieldMask);
    }
}
Also used : EntityRecord(io.spine.server.entity.EntityRecord) Project(io.spine.test.projection.Project) Map(java.util.Map) Any(com.google.protobuf.Any) FieldMask(com.google.protobuf.FieldMask) Test(org.junit.Test)

Example 30 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();
    ImmutableCollection<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);
    }
    for (EntityRecord record : stateRecords) {
        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)

Aggregations

EntityRecord (io.spine.server.entity.EntityRecord)41 Test (org.junit.Test)24 Any (com.google.protobuf.Any)19 EntityRecordWithColumns (io.spine.server.entity.storage.EntityRecordWithColumns)9 Message (com.google.protobuf.Message)8 TypeUrl (io.spine.type.TypeUrl)8 StandStorage (io.spine.server.stand.StandStorage)7 FieldMask (com.google.protobuf.FieldMask)6 Identifiers.idToAny (io.spine.base.Identifiers.idToAny)4 Project (io.spine.test.storage.Project)4 LinkedList (java.util.LinkedList)4 Version (io.spine.base.Version)3 EntityFilters (io.spine.client.EntityFilters)3 Project (io.spine.test.projection.Project)3 ImmutableList (com.google.common.collect.ImmutableList)2 Descriptors (com.google.protobuf.Descriptors)2 EntityIdFilter (io.spine.client.EntityIdFilter)2 LifecycleFlags (io.spine.server.entity.LifecycleFlags)2 TenantAwareTest (io.spine.server.tenant.TenantAwareTest)2 Customer (io.spine.test.commandservice.customer.Customer)2