Skip to content

Commit

Permalink
Migrate to new RecordSpec API.
Browse files Browse the repository at this point in the history
  • Loading branch information
armiol committed Nov 8, 2023
1 parent d6fde70 commit cc1501a
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public BoundedContextBuilder configureTenantIndex(BoundedContextBuilder builder)

@Override
public <I, R extends Message> RecordStorage<I, R>
createRecordStorage(ContextSpec context, RecordSpec<I, R, ?> spec) {
createRecordStorage(ContextSpec context, RecordSpec<I, R> spec) {
checkNotNull(context);
checkNotNull(spec);
var config = configurationWith(context, spec);
Expand All @@ -170,7 +170,7 @@ public BoundedContextBuilder configureTenantIndex(BoundedContextBuilder builder)
}

private <I, R extends Message>
StorageConfiguration<I, R> configurationWith(ContextSpec context, RecordSpec<I, R, ?> spec) {
StorageConfiguration<I, R> configurationWith(ContextSpec context, RecordSpec<I, R> spec) {
var wrapper = wrapperFor(context);
var recordType = spec.sourceType();
var behavior = txSettings.find(recordType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ private CustomStorages(Builder builder) {
* @return a callback to create a custom storage wrapped into {@code Optional},
* or {@code Optional.empty()} in case none was found
*/
public <I, R extends Message> Optional<CreateStorage<I, R>> find(RecordSpec<I, R, ?> spec) {
public <I, R extends Message> Optional<CreateStorage<I, R>> find(RecordSpec<I, R> spec) {
var domainType = spec.sourceType();
var optional = findValue(domainType);
Optional<CreateStorage<I, R>> result =
Expand Down Expand Up @@ -117,7 +117,7 @@ Builder self() {
* this builder.
*/
@Override
public final CustomStorages build() {
public CustomStorages build() {
return new CustomStorages(this);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public DsEntitySpec<I, R> recordSpec() {
*/
public Class<R> storedType() {
return recordSpec.recordSpec()
.storedType();
.recordType();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import io.spine.server.ContextSpec;
import io.spine.server.delivery.ShardIndex;
import io.spine.server.delivery.ShardSessionRecord;
import io.spine.server.storage.MessageRecordSpec;
import io.spine.server.storage.RecordSpec;
import io.spine.server.storage.RecordWithColumns;
import io.spine.server.storage.datastore.DatastoreStorageFactory;
import io.spine.server.storage.datastore.config.StorageConfiguration;
Expand Down Expand Up @@ -64,7 +64,7 @@ public final class DsSessionStorage
extends DsRecordStorage<ShardIndex, ShardSessionRecord> {

private static final boolean multitenant = false;
private final MessageRecordSpec<ShardIndex, ShardSessionRecord> spec;
private final RecordSpec<ShardIndex, ShardSessionRecord> spec;

/**
* Creates a new instance of this storage.
Expand Down Expand Up @@ -104,9 +104,9 @@ private static DsEntitySpec<ShardIndex, ShardSessionRecord> newRecordSpec() {
return result;
}

private static MessageRecordSpec<ShardIndex, ShardSessionRecord> messageSpec() {
private static RecordSpec<ShardIndex, ShardSessionRecord> messageSpec() {
@SuppressWarnings("ConstantConditions") /* Protobuf getters never return `nulls`. */
var spec = new MessageRecordSpec<>(
var spec = new RecordSpec<>(
ShardIndex.class,
ShardSessionRecord.class,
ShardSessionRecord::getIndex,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
*/
public final class DsEntitySpec<I, R extends Message> {

private final RecordSpec<I, R, ?> recordSpec;
private final RecordSpec<I, R> recordSpec;
private final RecordLayout<I, R> layout;

/**
Expand All @@ -63,7 +63,7 @@ public final class DsEntitySpec<I, R extends Message> {
* @param layout
* ancestor-children structure to use for Datastore Entities
*/
public DsEntitySpec(RecordSpec<I, R, ?> recordSpec, RecordLayout<I, R> layout) {
public DsEntitySpec(RecordSpec<I, R> recordSpec, RecordLayout<I, R> layout) {
this.recordSpec = checkNotNull(recordSpec);
this.layout = checkNotNull(layout);
}
Expand All @@ -75,14 +75,14 @@ public DsEntitySpec(RecordSpec<I, R, ?> recordSpec, RecordLayout<I, R> layout) {
* @param recordSpec
* specification telling which fields of Protobuf message to store in Datastore Entity
*/
public DsEntitySpec(RecordSpec<I, R, ?> recordSpec) {
public DsEntitySpec(RecordSpec<I, R> recordSpec) {
this(recordSpec, new FlatLayout<>(recordSpec.sourceType()));
}

/**
* Returns the storage specification for the persisted Protobuf message.
*/
public RecordSpec<I, R, ?> recordSpec() {
public RecordSpec<I, R> recordSpec() {
return recordSpec;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public Iterator<I> index() {
protected Iterator<I> index(RecordQuery<I, R> query) {
var spec = recordSpec();
var recordIterator = readAllRecords(query);
var result = transform(recordIterator, spec::idFromRecord);
var result = transform(recordIterator, spec::idValueIn);
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import com.google.cloud.datastore.DatastoreOptions;
import com.google.cloud.datastore.Key;
import com.google.common.testing.NullPointerTester;
import com.google.common.truth.Truth8;
import io.spine.base.Identifier;
import io.spine.core.TenantId;
import io.spine.server.BoundedContext;
Expand All @@ -55,11 +54,12 @@
import org.junit.jupiter.api.Test;

import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth8.assertThat;
import static io.spine.server.ContextSpec.multitenant;
import static io.spine.server.storage.datastore.given.DatastoreStorageFactoryTestEnv.factoryFor;
import static io.spine.server.storage.datastore.given.TestEnvironment.singleTenantSpec;
import static io.spine.server.storage.datastore.given.TestRecordSpec.projectDetailsSpec;
import static io.spine.server.storage.datastore.given.TestRecordSpec.stgProjectSpec;
import static io.spine.server.storage.given.GivenStorageProject.messageSpec;
import static io.spine.server.tenant.TenantAwareRunner.with;
import static io.spine.testing.DisplayNames.NOT_ACCEPT_NULLS;
import static io.spine.testing.server.storage.datastore.TestDatastores.defaultLocalProjectId;
Expand All @@ -85,7 +85,7 @@ final class DatastoreStorageFactoryTest {
@DisplayName(NOT_ACCEPT_NULLS)
void testNulls() {
new NullPointerTester().setDefault(ContextSpec.class, singleTenantSpec())
.setDefault(RecordSpec.class, stgProjectSpec())
.setDefault(RecordSpec.class, messageSpec())
.testAllPublicInstanceMethods(factory);
}

Expand Down Expand Up @@ -144,7 +144,7 @@ void namespaceForMultitenant() {
var datastore = options.getService();
var factory = factoryFor(datastore);
var storage =
factory.createRecordStorage(spec, stgProjectSpec());
factory.createRecordStorage(spec, messageSpec());
var key = writeForTenant(storage, tenant)
.setNamespace(namespace + ".V" + tenant.getValue())
.build();
Expand All @@ -162,7 +162,7 @@ void testDatastoreNamespaceInOptions() {
var datastore = local();
var factory = factoryFor(datastore);
var storage =
factory.createRecordStorage(spec, stgProjectSpec());
factory.createRecordStorage(spec, messageSpec());
var key = writeForTenant(storage, tenant)
.setNamespace('V' + tenant.getValue())
.build();
Expand All @@ -175,11 +175,11 @@ void testDatastoreNamespaceInOptions() {
void testProduceBcBuilder() {
DatastoreStorageFactory factory = TestDatastoreStorageFactory.local();
var builder = BoundedContext.multitenant(testName());
Truth8.assertThat(builder.tenantIndex())
assertThat(builder.tenantIndex())
.isEmpty();
var updatedIndex = factory.configureTenantIndex(builder)
.tenantIndex();
Truth8.assertThat(updatedIndex)
assertThat(updatedIndex)
.isPresent();
assertThat(updatedIndex.get()).isInstanceOf(TestNamespaceIndex.getType());
}
Expand All @@ -194,14 +194,14 @@ void plainRecord() {
var spec = ContextSpec.singleTenant(testName());
var customStorage =
InMemoryStorageFactory.newInstance()
.createRecordStorage(spec, stgProjectSpec());
.createRecordStorage(spec, messageSpec());
var factory = DatastoreStorageFactory.newBuilder()
.setDatastore(TestDatastores.local())
.useRecordStorage(StgProjectId.class, StgProject.class,
configuration -> customStorage)
.build();
var actualStorage =
factory.createRecordStorage(spec, stgProjectSpec());
factory.createRecordStorage(spec, messageSpec());
assertThat(actualStorage).isEqualTo(customStorage);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@
package io.spine.server.storage.datastore.given;

import com.google.cloud.datastore.Datastore;
import io.spine.server.entity.storage.EntityRecordSpec;
import io.spine.server.entity.EntityRecord;
import io.spine.server.entity.storage.SpecScanner;
import io.spine.server.projection.Projection;
import io.spine.server.storage.RecordSpec;
import io.spine.server.storage.datastore.DatastoreStorageFactory;
import io.spine.test.datastore.College;
import io.spine.test.datastore.CollegeId;
Expand Down Expand Up @@ -62,16 +64,16 @@ public static DatastoreStorageFactory factoryFor(Datastore datastore) {
public static class TestEntity
extends Projection<StgProjectId, StgProject, StgProject.Builder> {

public static EntityRecordSpec<StgProjectId, StgProject, TestEntity> spec() {
return EntityRecordSpec.of(TestEntity.class);
public static RecordSpec<StgProjectId, EntityRecord> spec() {
return SpecScanner.scan(TestEntity.class);
}
}

public static class DifferentTestEntity
extends Projection<CollegeId, College, College.Builder> {

public static EntityRecordSpec<CollegeId, College, DifferentTestEntity> spec() {
return EntityRecordSpec.of(DifferentTestEntity.class);
public static RecordSpec<CollegeId, EntityRecord> spec() {
return SpecScanner.scan(DifferentTestEntity.class);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@

package io.spine.server.storage.datastore.given;

import com.google.common.collect.ImmutableSet;
import io.spine.server.entity.storage.EntityRecordSpec;
import io.spine.server.entity.EntityRecord;
import io.spine.server.entity.storage.SpecScanner;
import io.spine.server.projection.Projection;
import io.spine.server.storage.MessageRecordSpec;
import io.spine.server.storage.RecordSpec;
import io.spine.test.storage.StgProject;
import io.spine.test.storage.StgProjectId;

Expand All @@ -44,24 +44,12 @@ public final class TestRecordSpec {
private TestRecordSpec() {
}

/**
* Creates a new {@code MessageRecordSpec} describing how instances of {@link StgProject}
* message are stored.
*
* <p>Defines no record columns.
*/
public static MessageRecordSpec<StgProjectId, StgProject> stgProjectSpec() {
return new MessageRecordSpec<>(
StgProjectId.class, StgProject.class, StgProject::getId, ImmutableSet.of()
);
}

/**
* Creates a new {@code EntityRecordSpec} describing how instances of {@link ProjectDetails}
* projection are stored.
*/
public static EntityRecordSpec<StgProjectId, StgProject, ProjectDetails> projectDetailsSpec() {
return EntityRecordSpec.of(ProjectDetails.class);
public static RecordSpec<StgProjectId, EntityRecord> projectDetailsSpec() {
return SpecScanner.scan(ProjectDetails.class);
}

private static class ProjectDetails
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

package io.spine.server.storage.datastore.record;

import io.spine.core.Versions;
import io.spine.environment.Tests;
import io.spine.server.ServerEnvironment;
import io.spine.server.storage.RecordStorageDelegateTest;
Expand All @@ -44,10 +43,11 @@
import org.junit.jupiter.api.Test;

import static com.google.common.truth.Truth8.assertThat;
import static io.spine.base.Identifier.newUuid;
import static io.spine.base.Time.currentTime;
import static io.spine.server.storage.given.StgColumn.due_date;
import static io.spine.server.storage.given.StgColumn.project_version;
import static io.spine.server.storage.given.StgColumn.status;
import static io.spine.server.storage.given.GivenStorageProject.StgProjectColumns.due_date;
import static io.spine.server.storage.given.GivenStorageProject.StgProjectColumns.name;
import static io.spine.server.storage.given.GivenStorageProject.StgProjectColumns.status;
import static org.junit.jupiter.api.Assertions.assertEquals;

@DisplayName("`DsRecordStorage` should")
Expand Down Expand Up @@ -107,12 +107,12 @@ public StgProject newRecord(StgProjectId id) {
void testPersistColumns() {
var id = newId();
var project = newStorageRecord(id);
var expectedVersion = Versions.newVersion(42, currentTime());
var expectedDueDate = currentTime();
var expectedStatus = StgProject.Status.STARTED;
var expectedName = newUuid();
project = project
.toBuilder()
.setProjectVersion(expectedVersion)
.setName(expectedName)
.setDueDate(expectedDueDate)
.setStatus(expectedStatus)
.build();
Expand All @@ -135,9 +135,9 @@ void testPersistColumns() {
assertEquals(expectedStatus.name(),
datastoreEntity.getString(status.name()
.value()));
assertEquals(expectedVersion.getNumber(),
datastoreEntity.getLong(project_version.name()
.value()));
assertEquals(expectedName,
datastoreEntity.getString(name.name()
.value()));
var actualDueDate =
datastoreEntity.getTimestamp(due_date.name()
.value());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import com.google.cloud.datastore.Value;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.protobuf.Any;
import com.google.protobuf.Timestamp;
import com.google.protobuf.util.Durations;
import com.google.protobuf.util.Timestamps;
Expand All @@ -43,7 +42,6 @@
import io.spine.core.Versions;
import io.spine.server.entity.EntityRecord;
import io.spine.server.entity.storage.EntityRecordStorage;
import io.spine.server.entity.storage.EntityRecordWithColumns;
import io.spine.server.storage.ColumnTypeMapping;
import io.spine.server.storage.datastore.DatastoreWrapper;
import io.spine.server.storage.datastore.Kind;
Expand Down Expand Up @@ -80,8 +78,7 @@ private DsEntityColumnsTestEnv() {
DatastoreWrapper datastore,
Key key) {
var record = toEntityRecord(college, version);
var recordWithCols = EntityRecordWithColumns.create(record, COLLEGE_CLS);
storage.write(recordWithCols);
storage.write(college.getId(), record);
var response = datastore.read(key);
checkArgument(response.isPresent());
var storedDeadline = readAdmissionDeadline(response.get());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import io.spine.server.BoundedContextBuilder;
import io.spine.server.ServerEnvironment;
import io.spine.server.entity.EntityRecord;
import io.spine.server.entity.storage.EntityRecordSpec;
import io.spine.server.entity.storage.SpecScanner;
import io.spine.server.storage.datastore.DatastoreStorageFactory;
import io.spine.server.storage.datastore.given.CollegeProjection;
import io.spine.test.datastore.College;
Expand Down Expand Up @@ -89,7 +89,7 @@ void createIndex() {
}

@AfterEach
void closeContext() throws Exception {
void closeContext() {
context.close();
}

Expand Down Expand Up @@ -170,7 +170,7 @@ void findPrefixedNamespaces() {
storageFactory.configureTenantIndex(contextBuilder);
var context = contextBuilder.build();
var storage = storageFactory
.createRecordStorage(context.spec(), EntityRecordSpec.of(CollegeProjection.class));
.createRecordStorage(context.spec(), SpecScanner.scan(CollegeProjection.class));
var id = CollegeId.newBuilder()
.setValue("Aeronautic Forgery College")
.build();
Expand Down

0 comments on commit cc1501a

Please sign in to comment.