Search in sources :

Example 21 with FieldMask

use of com.google.protobuf.FieldMask in project core-java by SpineEventEngine.

the class QueryFactory method allWithMask.

/**
     * Creates a {@link Query} to read all entity states with the {@link FieldMask}
     * applied to each of the results.
     *
     * <p>Allows to set property paths for a {@link FieldMask}, applied to each of the query
     * results. This processing is performed according to the
     * <a href="https://goo.gl/tW5wIU">FieldMask specs</a>.
     *
     * <p>In case the {@code paths} array contains entries inapplicable to the resulting entity
     * (e.g. a {@code path} references a missing field), such invalid paths
     * are silently ignored.
     *
     * @param entityClass the class of a target entity
     * @param maskPaths   the property paths for the {@code FieldMask} applied
     *                    to each of results
     * @return an instance of {@code Query} formed according to the passed parameters
     */
public Query allWithMask(Class<? extends Message> entityClass, String... maskPaths) {
    final FieldMask fieldMask = FieldMask.newBuilder().addAllPaths(Arrays.asList(maskPaths)).build();
    final Query result = composeQuery(entityClass, null, null, fieldMask);
    return result;
}
Also used : FieldMask(com.google.protobuf.FieldMask)

Example 22 with FieldMask

use of com.google.protobuf.FieldMask in project core-java by SpineEventEngine.

the class EntityQueryProcessor method process.

@Override
public ImmutableCollection<Any> process(Query query) {
    final ImmutableList.Builder<Any> resultBuilder = ImmutableList.builder();
    final Target target = query.getTarget();
    final FieldMask fieldMask = query.getFieldMask();
    final ImmutableCollection<? extends Entity> entities;
    if (target.getIncludeAll() && fieldMask.getPathsList().isEmpty()) {
        entities = repository.loadAll();
    } else {
        final EntityFilters filters = target.getFilters();
        entities = repository.find(filters, fieldMask);
    }
    for (Entity entity : entities) {
        final Message state = entity.getState();
        final Any packedState = AnyPacker.pack(state);
        resultBuilder.add(packedState);
    }
    final ImmutableList<Any> result = resultBuilder.build();
    return result;
}
Also used : Entity(io.spine.server.entity.Entity) Target(io.spine.client.Target) Message(com.google.protobuf.Message) ImmutableList(com.google.common.collect.ImmutableList) EntityFilters(io.spine.client.EntityFilters) Any(com.google.protobuf.Any) FieldMask(com.google.protobuf.FieldMask)

Example 23 with FieldMask

use of com.google.protobuf.FieldMask 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)

Example 24 with FieldMask

use of com.google.protobuf.FieldMask in project core-java by SpineEventEngine.

the class StandShould method doCheckReadingCustomersByIdAndFieldMask.

@SuppressWarnings("MethodWithMultipleLoops")
private void doCheckReadingCustomersByIdAndFieldMask(String... paths) {
    final Stand stand = prepareStandWithAggregateRepo(createStandStorage());
    final int querySize = 2;
    final Set<CustomerId> ids = new HashSet<>();
    for (int i = 0; i < querySize; i++) {
        final Customer customer = getSampleCustomer().toBuilder().setId(CustomerId.newBuilder().setNumber(i)).build();
        final Version stateVersion = Tests.newVersionWithNumber(1);
        stand.update(asEnvelope(customer.getId(), customer, stateVersion));
        ids.add(customer.getId());
    }
    final Query customerQuery = requestFactory.query().byIdsWithMask(Customer.class, ids, paths);
    final FieldMask fieldMask = FieldMask.newBuilder().addAllPaths(Arrays.asList(paths)).build();
    final MemoizeQueryResponseObserver observer = new MemoizeQueryResponseObserver() {

        @Override
        public void onNext(QueryResponse value) {
            super.onNext(value);
            final List<Any> messages = value.getMessagesList();
            Verify.assertSize(ids.size(), messages);
            for (Any message : messages) {
                final Customer customer = AnyPacker.unpack(message);
                assertNotEquals(customer, null);
                assertMatches(customer, fieldMask);
            }
        }
    };
    stand.execute(customerQuery, observer);
    verifyObserver(observer);
}
Also used : Query(io.spine.client.Query) Customer(io.spine.test.commandservice.customer.Customer) CustomerId(io.spine.test.commandservice.customer.CustomerId) Any(com.google.protobuf.Any) Version(io.spine.base.Version) QueryResponse(io.spine.client.QueryResponse) FieldMask(com.google.protobuf.FieldMask) Sets.newHashSet(com.google.common.collect.Sets.newHashSet) HashSet(java.util.HashSet)

Example 25 with FieldMask

use of com.google.protobuf.FieldMask in project core-java by SpineEventEngine.

the class StandShould method select_entity_singleton_by_id_and_apply_field_masks.

@SuppressWarnings("MethodWithMultipleLoops")
@Test
public void select_entity_singleton_by_id_and_apply_field_masks() {
    final Stand stand = prepareStandWithAggregateRepo(createStandStorage());
    final String customerDescriptor = Customer.getDescriptor().getFullName();
    // clashes with non-related tests.
    @SuppressWarnings("DuplicateStringLiteralInspection") final String[] paths = { customerDescriptor + ".id", customerDescriptor + ".name" };
    final FieldMask fieldMask = FieldMask.newBuilder().addAllPaths(Arrays.asList(paths)).build();
    final List<Customer> customers = new LinkedList<>();
    final int count = 10;
    for (int i = 0; i < count; i++) {
        // Has new ID each time
        final Customer customer = getSampleCustomer();
        customers.add(customer);
        final Version stateVersion = Tests.newVersionWithNumber(1);
        stand.update(asEnvelope(customer.getId(), customer, stateVersion));
    }
    final Set<CustomerId> ids = Collections.singleton(customers.get(0).getId());
    final Query customerQuery = requestFactory.query().byIdsWithMask(Customer.class, ids, paths);
    final MemoizeQueryResponseObserver observer = new MemoizeQueryResponseObserver();
    stand.execute(customerQuery, observer);
    final List<Any> read = observer.responseHandled.getMessagesList();
    Verify.assertSize(1, read);
    final Customer customer = AnyPacker.unpack(read.get(0));
    assertMatches(customer, fieldMask);
    assertTrue(ids.contains(customer.getId()));
    verifyObserver(observer);
}
Also used : Query(io.spine.client.Query) Customer(io.spine.test.commandservice.customer.Customer) CustomerId(io.spine.test.commandservice.customer.CustomerId) Any(com.google.protobuf.Any) LinkedList(java.util.LinkedList) Version(io.spine.base.Version) FieldMask(com.google.protobuf.FieldMask) Test(org.junit.Test) TenantAwareTest(io.spine.server.tenant.TenantAwareTest)

Aggregations

FieldMask (com.google.protobuf.FieldMask)29 Test (org.junit.Test)20 Any (com.google.protobuf.Any)7 EntityRecord (io.spine.server.entity.EntityRecord)6 Descriptors (com.google.protobuf.Descriptors)5 Message (com.google.protobuf.Message)5 Query (io.spine.client.Query)5 Project (io.spine.test.aggregate.Project)5 LinkedList (java.util.LinkedList)4 EntityFilters (io.spine.client.EntityFilters)3 Target (io.spine.client.Target)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3 ImmutableList (com.google.common.collect.ImmutableList)2 Version (io.spine.base.Version)2 EntityId (io.spine.client.EntityId)2 EntityIdFilter (io.spine.client.EntityIdFilter)2 TenantAwareTest (io.spine.server.tenant.TenantAwareTest)2 TestEntity (io.spine.test.client.TestEntity)2 Customer (io.spine.test.commandservice.customer.Customer)2 CustomerId (io.spine.test.commandservice.customer.CustomerId)2