Search in sources :

Example 6 with SchemaRegistry

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));
    });
}
Also used : DataStoreConfigurationProvider(com.amplifyframework.datastore.DataStoreConfigurationProvider) Single(io.reactivex.rxjava3.core.Single) DataStoreErrorHandler(com.amplifyframework.datastore.DataStoreErrorHandler) BehaviorProcessor(io.reactivex.rxjava3.processors.BehaviorProcessor) NonNull(androidx.annotation.NonNull) ModelWithMetadata(com.amplifyframework.datastore.appsync.ModelWithMetadata) GraphQLRequest(com.amplifyframework.api.graphql.GraphQLRequest) ModelProvider(com.amplifyframework.core.model.ModelProvider) DataStoreChannelEventName(com.amplifyframework.datastore.DataStoreChannelEventName) AppSync(com.amplifyframework.datastore.appsync.AppSync) SyncQueriesStartedEvent(com.amplifyframework.datastore.events.SyncQueriesStartedEvent) AtomicReference(java.util.concurrent.atomic.AtomicReference) ApiException(com.amplifyframework.api.ApiException) ArrayList(java.util.ArrayList) SchemaRegistry(com.amplifyframework.core.model.SchemaRegistry) Time(com.amplifyframework.util.Time) Schedulers(io.reactivex.rxjava3.schedulers.Schedulers) Consumer(com.amplifyframework.core.Consumer) ModelSchema(com.amplifyframework.core.model.ModelSchema) PaginatedResult(com.amplifyframework.api.graphql.PaginatedResult) QueryPredicate(com.amplifyframework.core.model.query.predicate.QueryPredicate) HubEvent(com.amplifyframework.hub.HubEvent) Amplify(com.amplifyframework.core.Amplify) Flowable(io.reactivex.rxjava3.core.Flowable) HubChannel(com.amplifyframework.hub.HubChannel) SerializedModel(com.amplifyframework.core.model.SerializedModel) Model(com.amplifyframework.core.model.Model) Completable(io.reactivex.rxjava3.core.Completable) Logger(com.amplifyframework.logging.Logger) Objects(java.util.Objects) DataStoreException(com.amplifyframework.datastore.DataStoreException) List(java.util.List) Cancelable(com.amplifyframework.core.async.Cancelable) AmplifyDisposables(com.amplifyframework.datastore.AmplifyDisposables) ForEach(com.amplifyframework.util.ForEach) Collections(java.util.Collections) ModelSchema(com.amplifyframework.core.model.ModelSchema) Completable(io.reactivex.rxjava3.core.Completable) ArrayList(java.util.ArrayList) SyncQueriesStartedEvent(com.amplifyframework.datastore.events.SyncQueriesStartedEvent)

Example 7 with SchemaRegistry

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;
}
Also used : Context(android.content.Context) DataStoreException(com.amplifyframework.datastore.DataStoreException) AmplifyException(com.amplifyframework.AmplifyException) SynchronousStorageAdapter(com.amplifyframework.datastore.storage.SynchronousStorageAdapter) SchemaRegistry(com.amplifyframework.core.model.SchemaRegistry)

Example 8 with SchemaRegistry

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();
}
Also used : DataStoreConfigurationProvider(com.amplifyframework.datastore.DataStoreConfigurationProvider) InMemoryStorageAdapter(com.amplifyframework.datastore.storage.InMemoryStorageAdapter) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) SchemaRegistry(com.amplifyframework.core.model.SchemaRegistry)

Example 9 with SchemaRegistry

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());
}
Also used : SchemaRegistry(com.amplifyframework.core.model.SchemaRegistry) Before(org.junit.Before)

Example 10 with SchemaRegistry

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);
}
Also used : GraphQLBehavior(com.amplifyframework.api.graphql.GraphQLBehavior) ModelWithMetadata(com.amplifyframework.datastore.appsync.ModelWithMetadata) AppSyncClient(com.amplifyframework.datastore.appsync.AppSyncClient) ModelProvider(com.amplifyframework.core.model.ModelProvider) SimpleModelProvider(com.amplifyframework.datastore.model.SimpleModelProvider) BlogOwner(com.amplifyframework.testmodels.commentsblog.BlogOwner) ModelMetadata(com.amplifyframework.datastore.appsync.ModelMetadata) SchemaRegistry(com.amplifyframework.core.model.SchemaRegistry) Before(org.junit.Before)

Aggregations

SchemaRegistry (com.amplifyframework.core.model.SchemaRegistry)15 ModelSchema (com.amplifyframework.core.model.ModelSchema)8 SerializedModel (com.amplifyframework.core.model.SerializedModel)8 HashMap (java.util.HashMap)8 Test (org.junit.Test)6 ModelField (com.amplifyframework.core.model.ModelField)5 Context (android.content.Context)3 AmplifyException (com.amplifyframework.AmplifyException)3 Amplify (com.amplifyframework.core.Amplify)3 Model (com.amplifyframework.core.model.Model)3 ModelProvider (com.amplifyframework.core.model.ModelProvider)3 Logger (com.amplifyframework.logging.Logger)3 Map (java.util.Map)3 NonNull (androidx.annotation.NonNull)2 CustomTypeField (com.amplifyframework.core.model.CustomTypeField)2 CustomTypeSchema (com.amplifyframework.core.model.CustomTypeSchema)2 ModelAssociation (com.amplifyframework.core.model.ModelAssociation)2 SerializedCustomType (com.amplifyframework.core.model.SerializedCustomType)2 Where (com.amplifyframework.core.model.query.Where)2 DataStoreException (com.amplifyframework.datastore.DataStoreException)2