Search in sources :

Example 11 with EntityRecordWithColumns

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

the class RecordBasedRepository method store.

/**
 * {@inheritDoc}
 */
@Override
public void store(E entity) {
    final EntityRecordWithColumns record = toRecord(entity);
    final RecordStorage<I> storage = recordStorage();
    storage.write(entity.getId(), record);
}
Also used : EntityRecordWithColumns(io.spine.server.entity.storage.EntityRecordWithColumns)

Example 12 with EntityRecordWithColumns

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

the class ColumnRecordsShould method feed_entity_columns_to_database_record.

@Test
public void feed_entity_columns_to_database_record() {
    // Set up mocks and arguments
    final List<Object> destination = new ArrayList<>(MOCK_COLUMNS_COUNT);
    final Map<String, Column.MemoizedValue<?>> columns = setupMockColumnsAllowingNulls();
    final CollectAnyColumnType type = spy(CollectAnyColumnType.class);
    final ColumnTypeRegistry<CollectAnyColumnType> registry = ColumnTypeRegistry.<CollectAnyColumnType>newBuilder().put(Object.class, type).build();
    final EntityRecordWithColumns recordWithColumns = EntityRecordWithColumns.of(EntityRecord.getDefaultInstance(), columns);
    final Function<String, Object> colIdMapper = spy(new NoOpColumnIdentifierMapper());
    // Invoke the prepersistence action
    ColumnRecords.feedColumnsTo(destination, recordWithColumns, registry, colIdMapper);
    // Verify calls
    verify(colIdMapper, times(MOCK_COLUMNS_COUNT)).apply(anyString());
    verify(type, times(MOCK_NON_NULL_COLUMNS_COUNT)).setColumnValue(eq(destination), any(Object.class), anyString());
    verify(type, times(MOCK_NULL_COLUMNS_COUNT)).setNull(eq(destination), anyString());
    final int indexOfNull = destination.indexOf(null);
    assertTrue("Null value was not saved to the destination", indexOfNull >= 0);
    assertContainsAll(destination, getNonNullColumnValues().toArray());
}
Also used : ArrayList(java.util.ArrayList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) EntityRecordWithColumns(io.spine.server.entity.storage.EntityRecordWithColumns) Test(org.junit.Test)

Example 13 with EntityRecordWithColumns

use of io.spine.server.entity.storage.EntityRecordWithColumns 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 14 with EntityRecordWithColumns

use of io.spine.server.entity.storage.EntityRecordWithColumns 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) Map(java.util.Map) Maps.newConcurrentMap(com.google.common.collect.Maps.newConcurrentMap) Any(com.google.protobuf.Any) ImmutableMap(com.google.common.collect.ImmutableMap) EntityRecordWithColumns(io.spine.server.entity.storage.EntityRecordWithColumns)

Example 15 with EntityRecordWithColumns

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

the class RecordBasedRepository method toRecord.

/**
 * Converts the passed entity into the record.
 */
protected EntityRecordWithColumns toRecord(E entity) {
    final EntityRecord entityRecord = entityConverter().convert(entity);
    checkNotNull(entityRecord);
    final EntityRecordWithColumns recordWithColumns = EntityRecordWithColumns.create(entityRecord, entity, recordStorage());
    return recordWithColumns;
}
Also used : EntityRecordWithColumns(io.spine.server.entity.storage.EntityRecordWithColumns)

Aggregations

EntityRecordWithColumns (io.spine.server.entity.storage.EntityRecordWithColumns)22 EntityRecord (io.spine.server.entity.EntityRecord)14 Test (org.junit.Test)12 Any (com.google.protobuf.Any)7 ColumnFilter (io.spine.client.ColumnFilter)5 Message (com.google.protobuf.Message)3 EntityFilters (io.spine.client.EntityFilters)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 Project (io.spine.test.storage.Project)3 Int32Value (com.google.protobuf.Int32Value)2 CompositeColumnFilter (io.spine.client.CompositeColumnFilter)2 TypeUrl (io.spine.type.TypeUrl)2 HashMap (java.util.HashMap)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Function (com.google.common.base.Function)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Maps.newConcurrentMap (com.google.common.collect.Maps.newConcurrentMap)1 FieldMask (com.google.protobuf.FieldMask)1