Search in sources :

Example 16 with EntityRecordWithColumns

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

the class RecordBasedRepository method findOrCreateRecord.

@Internal
@CheckReturnValue
public EntityRecord findOrCreateRecord(I id) {
    final E entity = findOrCreate(id);
    final EntityRecordWithColumns recordWithColumns = toRecord(entity);
    return recordWithColumns.getRecord();
}
Also used : EntityRecordWithColumns(io.spine.server.entity.storage.EntityRecordWithColumns) CheckReturnValue(javax.annotation.CheckReturnValue) Internal(io.spine.annotation.Internal)

Example 17 with EntityRecordWithColumns

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

the class RecordStorageShould method rewrite_records_in_bulk.

@Test
public void rewrite_records_in_bulk() {
    final int recordCount = 3;
    final RecordStorage<I> storage = getStorage();
    final Function<EntityRecord, EntityRecordWithColumns> recordPacker = new Function<EntityRecord, EntityRecordWithColumns>() {

        @Nullable
        @Override
        public EntityRecordWithColumns apply(@Nullable EntityRecord record) {
            if (record == null) {
                return null;
            }
            return withLifecycleColumns(record);
        }
    };
    final Map<I, EntityRecord> v1Records = new HashMap<>(recordCount);
    final Map<I, EntityRecord> v2Records = new HashMap<>(recordCount);
    for (int i = 0; i < recordCount; i++) {
        final I id = newId();
        final EntityRecord record = newStorageRecord(id);
        // Some records are changed and some are not
        final EntityRecord alternateRecord = (i % 2 == 0) ? record : newStorageRecord(id);
        v1Records.put(id, record);
        v2Records.put(id, alternateRecord);
    }
    storage.write(Maps.transformValues(v1Records, recordPacker));
    final Iterator<EntityRecord> firstRevision = storage.readAll();
    assertIteratorsEqual(v1Records.values().iterator(), firstRevision);
    storage.write(Maps.transformValues(v2Records, recordPacker));
    final Iterator<EntityRecord> secondRevision = storage.readAll();
    assertIteratorsEqual(v2Records.values().iterator(), secondRevision);
}
Also used : EntityRecord(io.spine.server.entity.EntityRecord) Function(com.google.common.base.Function) HashMap(java.util.HashMap) EntityRecordWithColumns(io.spine.server.entity.storage.EntityRecordWithColumns) Nullable(javax.annotation.Nullable) Test(org.junit.Test)

Example 18 with EntityRecordWithColumns

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

the class RecordStorageShould method write_record_with_columns.

@Test
public void write_record_with_columns() {
    final I id = newId();
    final EntityRecord record = newStorageRecord(id);
    final TestCounterEntity<I> testEntity = new TestCounterEntity<>(id);
    final RecordStorage<I> storage = getStorage();
    final EntityRecordWithColumns recordWithColumns = create(record, testEntity, storage);
    storage.write(id, recordWithColumns);
    final RecordReadRequest<I> readRequest = newReadRequest(id);
    final Optional<EntityRecord> readRecord = storage.read(readRequest);
    assertTrue(readRecord.isPresent());
    assertEquals(record, readRecord.get());
}
Also used : EntityRecord(io.spine.server.entity.EntityRecord) EntityRecordWithColumns(io.spine.server.entity.storage.EntityRecordWithColumns) Test(org.junit.Test)

Example 19 with EntityRecordWithColumns

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

the class RecordStorageShould method update_entity_column_values.

@Test
public void update_entity_column_values() {
    final Project.Status initialStatus = DONE;
    // is used for documentation purposes.
    @SuppressWarnings("UnnecessaryLocalVariable") final Project.Status statusAfterUpdate = CANCELLED;
    final Int32Value initialStatusValue = Int32Value.newBuilder().setValue(initialStatus.getNumber()).build();
    final ColumnFilter status = eq("projectStatusValue", initialStatusValue);
    final CompositeColumnFilter aggregatingFilter = CompositeColumnFilter.newBuilder().setOperator(ALL).addFilter(status).build();
    final EntityFilters filters = EntityFilters.newBuilder().addFilter(aggregatingFilter).build();
    final RecordStorage<I> storage = getStorage();
    final EntityQuery<I> query = EntityQueries.from(filters, storage);
    final I id = newId();
    final TestCounterEntity<I> entity = new TestCounterEntity<>(id);
    entity.setStatus(initialStatus);
    final EntityRecord record = newStorageRecord(id, newState(id));
    final EntityRecordWithColumns recordWithColumns = create(record, entity, storage);
    final FieldMask fieldMask = FieldMask.getDefaultInstance();
    // Create the record.
    storage.write(id, recordWithColumns);
    final Iterator<EntityRecord> recordsBefore = storage.readAll(query, fieldMask);
    assertSingleRecord(record, recordsBefore);
    // Update the entity columns of the record.
    entity.setStatus(statusAfterUpdate);
    final EntityRecordWithColumns updatedRecordWithColumns = create(record, entity, storage);
    storage.write(id, updatedRecordWithColumns);
    final Iterator<EntityRecord> recordsAfter = storage.readAll(query, fieldMask);
    assertFalse(recordsAfter.hasNext());
}
Also used : CompositeColumnFilter(io.spine.client.CompositeColumnFilter) EntityFilters(io.spine.client.EntityFilters) CompositeColumnFilter(io.spine.client.CompositeColumnFilter) ColumnFilter(io.spine.client.ColumnFilter) EntityRecordWithColumns(io.spine.server.entity.storage.EntityRecordWithColumns) EntityRecord(io.spine.server.entity.EntityRecord) Project(io.spine.test.storage.Project) Int32Value(com.google.protobuf.Int32Value) FieldMask(com.google.protobuf.FieldMask) Test(org.junit.Test)

Example 20 with EntityRecordWithColumns

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

the class RecordStorageShould method accept_records_with_empty_storage_fields.

@Test
public void accept_records_with_empty_storage_fields() {
    final I id = newId();
    final EntityRecord record = newStorageRecord(id);
    final EntityRecordWithColumns recordWithStorageFields = EntityRecordWithColumns.of(record);
    assertFalse(recordWithStorageFields.hasColumns());
    final RecordStorage<I> storage = getStorage();
    storage.write(id, recordWithStorageFields);
    final RecordReadRequest<I> readRequest = newReadRequest(id);
    final Optional<EntityRecord> actualRecord = storage.read(readRequest);
    assertTrue(actualRecord.isPresent());
    assertEquals(record, actualRecord.get());
}
Also used : EntityRecord(io.spine.server.entity.EntityRecord) EntityRecordWithColumns(io.spine.server.entity.storage.EntityRecordWithColumns) Test(org.junit.Test)

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