Search in sources :

Example 6 with EntityRecord

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

the class Stand method update.

/**
     * Updates the state of an entity inside of the current instance of {@code Stand}.
     *
     * <p>In case the entity update represents the new
     * {@link io.spine.server.aggregate.Aggregate Aggregate} state,
     * store the new value for the {@code Aggregate} to each of the configured instances of
     * {@link StandStorage}.
     *
     * <p>Each {@code Aggregate} state value is stored as one-to-one to its
     * {@link TypeUrl TypeUrl} obtained via {@link Any#getTypeUrl()}.
     *
     * <p>In case {@code Stand} already contains the state for this {@code Aggregate},
     * the value will be replaced.
     *
     * <p>The state updates which are not originated from the {@code Aggregate} are not
     * stored in the {@code Stand}.
     *
     * <p>In any case, the state update is then propagated to the callbacks.
     * The set of matched callbacks is determined by filtering all the registered callbacks
     * by the entity {@code TypeUrl}.
     *
     * <p>The matching callbacks are executed with the {@link #callbackExecutor}.
     *
     * @param envelope the updated entity state,
     *                 packed as {@linkplain EntityStateEnvelope envelope}
     */
void update(final EntityStateEnvelope<?, ?> envelope) {
    final EntityUpdateOperation op = new EntityUpdateOperation(envelope) {

        @Override
        public void run() {
            final Object id = envelope.getEntityId();
            final Message entityState = envelope.getMessage();
            final Any packedState = AnyPacker.pack(entityState);
            final TypeUrl entityTypeUrl = TypeUrl.of(entityState);
            final boolean aggregateUpdate = typeRegistry.hasAggregateType(entityTypeUrl);
            if (aggregateUpdate) {
                final Optional<Version> entityVersion = envelope.getEntityVersion();
                checkState(entityVersion.isPresent(), "The aggregate version must be set in order to update Stand. " + "Actual envelope: {}", envelope);
                // checked above.
                @SuppressWarnings("OptionalGetWithoutIsPresent") final Version versionValue = entityVersion.get();
                final AggregateStateId aggregateStateId = AggregateStateId.of(id, entityTypeUrl);
                final EntityRecord record = EntityRecord.newBuilder().setState(packedState).setVersion(versionValue).build();
                storage.write(aggregateStateId, record);
            }
            notifyMatchingSubscriptions(id, packedState, entityTypeUrl);
        }
    };
    op.execute();
}
Also used : EntityRecord(io.spine.server.entity.EntityRecord) Message(com.google.protobuf.Message) Version(io.spine.base.Version) EntityUpdateOperation(io.spine.server.tenant.EntityUpdateOperation) TypeUrl(io.spine.type.TypeUrl) Any(com.google.protobuf.Any)

Example 7 with EntityRecord

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

the class AggregateQueryProcessor method doFetchWithFilters.

private ImmutableCollection<EntityRecord> doFetchWithFilters(Target target, FieldMask fieldMask) {
    final EntityFilters filters = target.getFilters();
    final boolean idsAreDefined = !filters.getIdFilter().getIdsList().isEmpty();
    if (!idsAreDefined) {
        return ImmutableList.of();
    }
    final EntityIdFilter idFilter = filters.getIdFilter();
    final Collection<AggregateStateId> stateIds = Collections2.transform(idFilter.getIdsList(), stateIdTransformer);
    final ImmutableCollection<EntityRecord> result = stateIds.size() == 1 ? readOne(stateIds.iterator().next(), fieldMask) : readMany(stateIds, fieldMask);
    return result;
}
Also used : EntityRecord(io.spine.server.entity.EntityRecord) EntityIdFilter(io.spine.client.EntityIdFilter) EntityFilters(io.spine.client.EntityFilters)

Example 8 with EntityRecord

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

the class StandShould method return_empty_list_for_aggregate_reads_with_filters_not_set.

@Test
public void return_empty_list_for_aggregate_reads_with_filters_not_set() {
    final StandStorage standStorageMock = mock(StandStorage.class);
    // Return non-empty results on any storage read call.
    final EntityRecord someRecord = EntityRecord.getDefaultInstance();
    final ImmutableList<EntityRecord> nonEmptyList = ImmutableList.<EntityRecord>builder().add(someRecord).build();
    when(standStorageMock.readAllByType(any(TypeUrl.class))).thenReturn(nonEmptyList);
    when(standStorageMock.read(any(AggregateStateId.class))).thenReturn(Optional.of(someRecord));
    when(standStorageMock.readAll()).thenReturn(Maps.<AggregateStateId, EntityRecord>newHashMap());
    when(standStorageMock.readMultiple(ArgumentMatchers.<AggregateStateId>anyIterable())).thenReturn(nonEmptyList);
    final Stand stand = prepareStandWithAggregateRepo(standStorageMock);
    final Query noneOfCustomersQuery = requestFactory.query().byIds(Customer.class, Collections.<Message>emptySet());
    final MemoizeQueryResponseObserver responseObserver = new MemoizeQueryResponseObserver();
    stand.execute(noneOfCustomersQuery, responseObserver);
    verifyObserver(responseObserver);
    final List<Any> messageList = checkAndGetMessageList(responseObserver);
    assertTrue("Query returned a non-empty response message list " + "though the filter was not set", messageList.isEmpty());
}
Also used : EntityRecord(io.spine.server.entity.EntityRecord) Query(io.spine.client.Query) StandStorage(io.spine.server.stand.StandStorage) TypeUrl(io.spine.type.TypeUrl) Any(com.google.protobuf.Any) Test(org.junit.Test) TenantAwareTest(io.spine.server.tenant.TenantAwareTest)

Example 9 with EntityRecord

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

the class StandShould method operate_with_storage_provided_through_builder.

@SuppressWarnings("OverlyCoupledMethod")
@Test
public void operate_with_storage_provided_through_builder() {
    final StandStorage standStorageMock = mock(StandStorage.class);
    final BoundedContext boundedContext = BoundedContext.newBuilder().setStand(Stand.newBuilder().setStorage(standStorageMock)).build();
    final Stand stand = boundedContext.getStand();
    assertNotNull(stand);
    final io.spine.server.Given.CustomerAggregateRepository customerAggregateRepo = new io.spine.server.Given.CustomerAggregateRepository(boundedContext);
    stand.registerTypeSupplier(customerAggregateRepo);
    final int numericIdValue = 17;
    final CustomerId customerId = customerIdFor(numericIdValue);
    final io.spine.server.Given.CustomerAggregate customerAggregate = customerAggregateRepo.create(customerId);
    final Customer customerState = customerAggregate.getState();
    final TypeUrl customerType = TypeUrl.of(Customer.class);
    final Version stateVersion = Tests.newVersionWithNumber(1);
    verify(standStorageMock, never()).write(any(AggregateStateId.class), any(EntityRecordWithColumns.class));
    stand.update(asEnvelope(customerId, customerState, stateVersion));
    final AggregateStateId expectedAggregateStateId = AggregateStateId.of(customerId, customerType);
    final Any packedState = AnyPacker.pack(customerState);
    final EntityRecord expectedRecord = EntityRecord.newBuilder().setState(packedState).build();
    verify(standStorageMock, times(1)).write(eq(expectedAggregateStateId), recordStateMatcher(expectedRecord));
}
Also used : StreamObservers.noOpObserver(io.spine.io.StreamObservers.noOpObserver) StreamObserver(io.grpc.stub.StreamObserver) MemoizingObserver(io.spine.io.StreamObservers.MemoizingObserver) StreamObservers.memoizingObserver(io.spine.io.StreamObservers.memoizingObserver) Customer(io.spine.test.commandservice.customer.Customer) TypeUrl(io.spine.type.TypeUrl) CustomerId(io.spine.test.commandservice.customer.CustomerId) Any(com.google.protobuf.Any) EntityRecordWithColumns(io.spine.server.entity.storage.EntityRecordWithColumns) EntityRecord(io.spine.server.entity.EntityRecord) Version(io.spine.base.Version) StandStorage(io.spine.server.stand.StandStorage) BoundedContext(io.spine.server.BoundedContext) Test(org.junit.Test) TenantAwareTest(io.spine.server.tenant.TenantAwareTest)

Example 10 with EntityRecord

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

the class StandShould method setupStandStorageWithCustomers.

private StandStorage setupStandStorageWithCustomers(Map<CustomerId, Customer> sampleCustomers, TypeUrl customerType) {
    final StandStorage standStorage = StorageFactorySwitch.get(isMultitenant()).createStandStorage();
    final ImmutableList.Builder<AggregateStateId> stateIdsBuilder = ImmutableList.builder();
    final ImmutableList.Builder<EntityRecord> recordsBuilder = ImmutableList.builder();
    for (CustomerId customerId : sampleCustomers.keySet()) {
        final AggregateStateId stateId = AggregateStateId.of(customerId, customerType);
        final Customer customer = sampleCustomers.get(customerId);
        final Any customerState = AnyPacker.pack(customer);
        final EntityRecord entityRecord = EntityRecord.newBuilder().setState(customerState).build();
        stateIdsBuilder.add(stateId);
        recordsBuilder.add(entityRecord);
        standStorage.write(stateId, entityRecord);
    }
    return standStorage;
}
Also used : EntityRecord(io.spine.server.entity.EntityRecord) Customer(io.spine.test.commandservice.customer.Customer) StandStorage(io.spine.server.stand.StandStorage) ImmutableList(com.google.common.collect.ImmutableList) CustomerId(io.spine.test.commandservice.customer.CustomerId) Any(com.google.protobuf.Any)

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