use of com.amplifyframework.core.model.SchemaRegistry in project amplify-android by aws-amplify.
the class SyncProcessor method hydrate.
/**
* The task of hydrating the DataStore either succeeds (with no return value),
* or it fails, with an explanation.
* @return An Rx {@link Completable} which can be used to perform the operation.
*/
Completable hydrate() {
final List<Completable> hydrationTasks = new ArrayList<>();
List<ModelSchema> modelSchemas = new ArrayList<>(modelProvider.modelSchemas().values());
// And sort them all, according to their model's topological order,
// So that when we save them, the references will exist.
TopologicalOrdering ordering = TopologicalOrdering.forRegisteredModels(schemaRegistry, modelProvider);
Collections.sort(modelSchemas, ordering::compare);
for (ModelSchema schema : modelSchemas) {
hydrationTasks.add(createHydrationTask(schema));
}
return Completable.concat(hydrationTasks).doOnSubscribe(ignore -> {
// This is where we trigger the syncQueriesStarted event since
// doOnSubscribe means that all upstream hydration tasks
// have started.
Amplify.Hub.publish(HubChannel.DATASTORE, HubEvent.create(DataStoreChannelEventName.SYNC_QUERIES_STARTED, new SyncQueriesStartedEvent(modelNames)));
}).doOnComplete(() -> {
// When the Completable completes, then emit syncQueriesReady.
Amplify.Hub.publish(HubChannel.DATASTORE, HubEvent.create(DataStoreChannelEventName.SYNC_QUERIES_READY));
});
}
use of com.amplifyframework.core.model.SchemaRegistry in project amplify-android by aws-amplify.
the class TestStorageAdapter method create.
/**
* Creates an instance of the {@link SynchronousStorageAdapter}, which has been initialized
* so that it can be used with the given models. The {@link SynchronousStorageAdapter}
* is backed by an {@link SQLiteStorageAdapter}. The caller of this method
* should do due diligence to ensure that any resources created by
* {@link SQLiteStorageAdapter#initialize(Context, Consumer, Consumer)} have been cleaned up.
* @return An initialized instance of the {@link SynchronousStorageAdapter}
*/
static SynchronousStorageAdapter create(ModelProvider modelProvider) {
SchemaRegistry schemaRegistry = SchemaRegistry.instance();
schemaRegistry.clear();
try {
schemaRegistry.register(modelProvider.models());
} catch (AmplifyException modelSchemaLoadingFailure) {
throw new RuntimeException(modelSchemaLoadingFailure);
}
SQLiteStorageAdapter sqLiteStorageAdapter = SQLiteStorageAdapter.forModels(schemaRegistry, modelProvider);
SynchronousStorageAdapter synchronousStorageAdapter = SynchronousStorageAdapter.delegatingTo(sqLiteStorageAdapter);
Context context = ApplicationProvider.getApplicationContext();
try {
synchronousStorageAdapter.initialize(context);
} catch (DataStoreException initializationFailure) {
throw new RuntimeException(initializationFailure);
}
return synchronousStorageAdapter;
}
use of com.amplifyframework.core.model.SchemaRegistry in project amplify-android by aws-amplify.
the class SyncProcessorTest method initSyncProcessor.
private void initSyncProcessor(int syncMaxRecords) throws AmplifyException {
SchemaRegistry schemaRegistry = SchemaRegistry.instance();
schemaRegistry.clear();
schemaRegistry.register(modelProvider.models());
InMemoryStorageAdapter inMemoryStorageAdapter = InMemoryStorageAdapter.create();
this.storageAdapter = SynchronousStorageAdapter.delegatingTo(inMemoryStorageAdapter);
final SyncTimeRegistry syncTimeRegistry = new SyncTimeRegistry(inMemoryStorageAdapter);
final MutationOutbox mutationOutbox = new PersistentMutationOutbox(inMemoryStorageAdapter);
final VersionRepository versionRepository = new VersionRepository(inMemoryStorageAdapter);
final Merger merger = new Merger(mutationOutbox, versionRepository, inMemoryStorageAdapter);
DataStoreConfigurationProvider dataStoreConfigurationProvider = () -> DataStoreConfiguration.builder().syncInterval(BASE_SYNC_INTERVAL_MINUTES, TimeUnit.MINUTES).syncMaxRecords(syncMaxRecords).syncPageSize(1_000).errorHandler(dataStoreException -> errorHandlerCallCount++).syncExpression(BlogOwner.class, () -> BlogOwner.NAME.beginsWith("J")).build();
QueryPredicateProvider queryPredicateProvider = new QueryPredicateProvider(dataStoreConfigurationProvider);
queryPredicateProvider.resolvePredicates();
this.syncProcessor = SyncProcessor.builder().modelProvider(modelProvider).schemaRegistry(schemaRegistry).syncTimeRegistry(syncTimeRegistry).appSync(appSync).merger(merger).dataStoreConfigurationProvider(dataStoreConfigurationProvider).queryPredicateProvider(queryPredicateProvider).retryHandler(requestRetry).isSyncRetryEnabled(isSyncRetryEnabled).build();
}
use of com.amplifyframework.core.model.SchemaRegistry in project amplify-android by aws-amplify.
the class SqlCommandTest method createSqlCommandFactory.
/**
* Setup before each test.
*/
@Before
public void createSqlCommandFactory() {
SchemaRegistry schemaRegistry = SchemaRegistry.instance();
sqlCommandFactory = new SQLiteCommandFactory(schemaRegistry, GsonFactory.instance());
}
use of com.amplifyframework.core.model.SchemaRegistry in project amplify-android by aws-amplify.
the class OrchestratorTest method setup.
/**
* Setup mocks and other common elements.
* @throws AmplifyException Not expected.
*/
@SuppressWarnings("unchecked")
@Before
public void setup() throws AmplifyException {
ShadowLog.stream = System.out;
// Arrange: create a BlogOwner
susan = BlogOwner.builder().name("Susan Quimby").build();
// SYNC_QUERIES_READY indicates that the sync queries have completed.
orchestratorInitObserver = HubAccumulator.create(HubChannel.DATASTORE, DataStoreChannelEventName.SYNC_QUERIES_READY, 1).start();
ModelMetadata metadata = new ModelMetadata(susan.getId(), false, 1, Temporal.Timestamp.now());
ModelWithMetadata<BlogOwner> modelWithMetadata = new ModelWithMetadata<>(susan, metadata);
// Mock behaviors from for the API category
mockApi = mock(GraphQLBehavior.class);
ApiMocking.mockSubscriptionStart(mockApi);
ApiMocking.mockSuccessfulMutation(mockApi, susan.getId(), modelWithMetadata);
ApiMocking.mockSuccessfulQuery(mockApi, modelWithMetadata);
AppSyncClient appSync = AppSyncClient.via(mockApi);
localStorageAdapter = InMemoryStorageAdapter.create();
ModelProvider modelProvider = SimpleModelProvider.withRandomVersion(BlogOwner.class);
SchemaRegistry schemaRegistry = SchemaRegistry.instance();
schemaRegistry.clear();
schemaRegistry.register(modelProvider.models());
orchestrator = new Orchestrator(modelProvider, schemaRegistry, localStorageAdapter, appSync, DataStoreConfiguration::defaults, () -> Orchestrator.State.SYNC_VIA_API, true);
}
Aggregations