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();
}
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);
}
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());
}
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());
}
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());
}
Aggregations