Search in sources :

Example 46 with Model

use of com.amplifyframework.core.model.Model in project amplify-android by aws-amplify.

the class SubscriptionProcessor method startSubscriptions.

/**
 * Start subscribing to model mutations.
 */
synchronized void startSubscriptions() throws DataStoreException {
    int subscriptionCount = modelProvider.modelNames().size() * SubscriptionType.values().length;
    // Create a latch with the number of subscriptions are requesting. Each of these will be
    // counted down when each subscription's onStarted event is called.
    AbortableCountDownLatch<DataStoreException> latch = new AbortableCountDownLatch<>(subscriptionCount);
    // Need to create a new buffer so we can properly handle retries and stop/start scenarios.
    // Re-using the same buffer has some unexpected results due to the replay aspect of the subject.
    buffer = ReplaySubject.create();
    Set<Observable<SubscriptionEvent<? extends Model>>> subscriptions = new HashSet<>();
    for (ModelSchema modelSchema : modelProvider.modelSchemas().values()) {
        for (SubscriptionType subscriptionType : SubscriptionType.values()) {
            subscriptions.add(subscriptionObservable(appSync, subscriptionType, latch, modelSchema));
        }
    }
    ongoingOperationsDisposable.add(Observable.merge(subscriptions).subscribeOn(Schedulers.io()).observeOn(Schedulers.io()).doOnSubscribe(disposable -> LOG.info("Starting processing subscription events.")).doOnError(failure -> LOG.warn("Reading subscription events has failed.", failure)).doOnComplete(() -> LOG.warn("Reading subscription events is completed.")).subscribe(buffer::onNext, buffer::onError, buffer::onComplete));
    boolean subscriptionsStarted;
    try {
        LOG.debug("Waiting for subscriptions to start.");
        subscriptionsStarted = latch.abortableAwait(adjustedTimeoutSeconds, TimeUnit.SECONDS);
    } catch (InterruptedException exception) {
        LOG.warn("Subscription operations were interrupted during setup.");
        return;
    }
    if (subscriptionsStarted) {
        Amplify.Hub.publish(HubChannel.DATASTORE, HubEvent.create(DataStoreChannelEventName.SUBSCRIPTIONS_ESTABLISHED));
        LOG.info(String.format(Locale.US, "Started subscription processor for models: %s of types %s.", modelProvider.modelNames(), Arrays.toString(SubscriptionType.values())));
    } else {
        throw new DataStoreException("Timed out waiting for subscription processor to start.", "Retry");
    }
}
Also used : Arrays(java.util.Arrays) AmplifyException(com.amplifyframework.AmplifyException) NonNull(androidx.annotation.NonNull) ModelWithMetadata(com.amplifyframework.datastore.appsync.ModelWithMetadata) ModelProvider(com.amplifyframework.core.model.ModelProvider) DataStoreChannelEventName(com.amplifyframework.datastore.DataStoreChannelEventName) AppSync(com.amplifyframework.datastore.appsync.AppSync) AtomicReference(java.util.concurrent.atomic.AtomicReference) Empty(com.amplifyframework.util.Empty) SchemaRegistry(com.amplifyframework.core.model.SchemaRegistry) HashSet(java.util.HashSet) Schedulers(io.reactivex.rxjava3.schedulers.Schedulers) AppSyncExtensions(com.amplifyframework.datastore.appsync.AppSyncExtensions) Consumer(com.amplifyframework.core.Consumer) CompositeDisposable(io.reactivex.rxjava3.disposables.CompositeDisposable) SubscriptionType(com.amplifyframework.api.graphql.SubscriptionType) Locale(java.util.Locale) Observable(io.reactivex.rxjava3.core.Observable) ModelSchema(com.amplifyframework.core.model.ModelSchema) GraphQLResponse(com.amplifyframework.api.graphql.GraphQLResponse) QueryPredicate(com.amplifyframework.core.model.query.predicate.QueryPredicate) HubEvent(com.amplifyframework.hub.HubEvent) Amplify(com.amplifyframework.core.Amplify) HubChannel(com.amplifyframework.hub.HubChannel) SerializedModel(com.amplifyframework.core.model.SerializedModel) AppSyncErrorType(com.amplifyframework.datastore.appsync.AppSyncExtensions.AppSyncErrorType) Model(com.amplifyframework.core.model.Model) Set(java.util.Set) Completable(io.reactivex.rxjava3.core.Completable) Action(com.amplifyframework.core.Action) Logger(com.amplifyframework.logging.Logger) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) DataStoreException(com.amplifyframework.datastore.DataStoreException) List(java.util.List) Cancelable(com.amplifyframework.core.async.Cancelable) ReplaySubject(io.reactivex.rxjava3.subjects.ReplaySubject) AmplifyDisposables(com.amplifyframework.datastore.AmplifyDisposables) GraphQLResponseException(com.amplifyframework.datastore.DataStoreException.GraphQLResponseException) VisibleForTesting(androidx.annotation.VisibleForTesting) ModelSchema(com.amplifyframework.core.model.ModelSchema) SubscriptionType(com.amplifyframework.api.graphql.SubscriptionType) DataStoreException(com.amplifyframework.datastore.DataStoreException) Observable(io.reactivex.rxjava3.core.Observable) HashSet(java.util.HashSet)

Example 47 with Model

use of com.amplifyframework.core.model.Model in project amplify-android by aws-amplify.

the class Merger method merge.

/**
 * Merge an item back into the local store, using a default strategy.
 * TODO: Change this method to return a Maybe, and remove the Consumer argument.
 * @param modelWithMetadata A model, combined with metadata about it
 * @param changeTypeConsumer A callback invoked when the merge method saves or deletes the model.
 * @param <T> Type of model
 * @return A completable operation to merge the model
 */
<T extends Model> Completable merge(ModelWithMetadata<T> modelWithMetadata, Consumer<StorageItemChange.Type> changeTypeConsumer) {
    AtomicReference<Long> startTime = new AtomicReference<>();
    return Completable.defer(() -> {
        ModelMetadata metadata = modelWithMetadata.getSyncMetadata();
        boolean isDelete = Boolean.TRUE.equals(metadata.isDeleted());
        int incomingVersion = metadata.getVersion() == null ? -1 : metadata.getVersion();
        T model = modelWithMetadata.getModel();
        return versionRepository.findModelVersion(model).onErrorReturnItem(-1).filter(currentVersion -> currentVersion == -1 || incomingVersion > currentVersion).flatMapCompletable(shouldMerge -> {
            Completable firstStep;
            if (mutationOutbox.hasPendingMutation(model.getId())) {
                LOG.info("Mutation outbox has pending mutation for " + model.getId() + ". Saving the metadata, but not model itself.");
                firstStep = Completable.complete();
            } else {
                firstStep = (isDelete ? delete(model, changeTypeConsumer) : save(model, changeTypeConsumer));
            }
            return firstStep.andThen(save(metadata, NoOpConsumer.create()));
        }).doOnComplete(() -> {
            announceSuccessfulMerge(modelWithMetadata);
            LOG.debug("Remote model update was sync'd down into local storage: " + modelWithMetadata);
        }).onErrorComplete(failure -> {
            if (!ErrorInspector.contains(failure, SQLiteConstraintException.class)) {
                return false;
            }
            LOG.warn("Sync failed: foreign key constraint violation: " + modelWithMetadata, failure);
            return true;
        }).doOnError(failure -> LOG.warn("Failed to sync remote model into local storage: " + modelWithMetadata, failure));
    }).doOnSubscribe(disposable -> startTime.set(System.currentTimeMillis())).doOnTerminate(() -> {
        long duration = System.currentTimeMillis() - startTime.get();
        LOG.verbose("Merged a single item in " + duration + " ms.");
    });
}
Also used : Amplify(com.amplifyframework.core.Amplify) HubChannel(com.amplifyframework.hub.HubChannel) NonNull(androidx.annotation.NonNull) QueryPredicates(com.amplifyframework.core.model.query.predicate.QueryPredicates) ModelWithMetadata(com.amplifyframework.datastore.appsync.ModelWithMetadata) StorageItemChange(com.amplifyframework.datastore.storage.StorageItemChange) DataStoreChannelEventName(com.amplifyframework.datastore.DataStoreChannelEventName) Model(com.amplifyframework.core.model.Model) Completable(io.reactivex.rxjava3.core.Completable) AtomicReference(java.util.concurrent.atomic.AtomicReference) ErrorInspector(com.amplifyframework.datastore.utils.ErrorInspector) Logger(com.amplifyframework.logging.Logger) Objects(java.util.Objects) Consumer(com.amplifyframework.core.Consumer) LocalStorageAdapter(com.amplifyframework.datastore.storage.LocalStorageAdapter) ModelMetadata(com.amplifyframework.datastore.appsync.ModelMetadata) SQLiteConstraintException(android.database.sqlite.SQLiteConstraintException) NoOpConsumer(com.amplifyframework.core.NoOpConsumer) HubEvent(com.amplifyframework.hub.HubEvent) Completable(io.reactivex.rxjava3.core.Completable) AtomicReference(java.util.concurrent.atomic.AtomicReference) ModelMetadata(com.amplifyframework.datastore.appsync.ModelMetadata)

Example 48 with Model

use of com.amplifyframework.core.model.Model in project amplify-android by aws-amplify.

the class AppSyncClient method mutation.

private <T extends Model> Cancelable mutation(final GraphQLRequest<ModelWithMetadata<T>> request, final Consumer<GraphQLResponse<ModelWithMetadata<T>>> onResponse, final Consumer<DataStoreException> onFailure) {
    final Consumer<GraphQLResponse<ModelWithMetadata<T>>> responseConsumer = response -> {
        if (response.hasErrors()) {
            onResponse.accept(new GraphQLResponse<>(null, response.getErrors()));
        } else {
            onResponse.accept(response);
        }
    };
    final Consumer<ApiException> failureConsumer = failure -> onFailure.accept(new DataStoreException("Failure during mutation.", failure, "Check details."));
    final Cancelable cancelable = api.mutate(request, responseConsumer, failureConsumer);
    if (cancelable != null) {
        return cancelable;
    }
    return new NoOpCancelable();
}
Also used : Amplify(com.amplifyframework.core.Amplify) AmplifyException(com.amplifyframework.AmplifyException) NonNull(androidx.annotation.NonNull) QueryPredicates(com.amplifyframework.core.model.query.predicate.QueryPredicates) GraphQLRequest(com.amplifyframework.api.graphql.GraphQLRequest) Model(com.amplifyframework.core.model.Model) Action(com.amplifyframework.core.Action) ApiException(com.amplifyframework.api.ApiException) Logger(com.amplifyframework.logging.Logger) GraphQLBehavior(com.amplifyframework.api.graphql.GraphQLBehavior) DataStoreException(com.amplifyframework.datastore.DataStoreException) Consumer(com.amplifyframework.core.Consumer) Nullable(androidx.annotation.Nullable) Cancelable(com.amplifyframework.core.async.Cancelable) SubscriptionType(com.amplifyframework.api.graphql.SubscriptionType) AuthModeStrategyType(com.amplifyframework.api.aws.AuthModeStrategyType) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) ModelSchema(com.amplifyframework.core.model.ModelSchema) GraphQLResponse(com.amplifyframework.api.graphql.GraphQLResponse) ApiCategoryBehavior(com.amplifyframework.api.ApiCategoryBehavior) PaginatedResult(com.amplifyframework.api.graphql.PaginatedResult) QueryPredicate(com.amplifyframework.core.model.query.predicate.QueryPredicate) DataStoreException(com.amplifyframework.datastore.DataStoreException) GraphQLResponse(com.amplifyframework.api.graphql.GraphQLResponse) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) Cancelable(com.amplifyframework.core.async.Cancelable) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) ApiException(com.amplifyframework.api.ApiException)

Example 49 with Model

use of com.amplifyframework.core.model.Model in project amplify-android by aws-amplify.

the class AppSyncClient method sync.

@NonNull
@Override
public <T extends Model> Cancelable sync(@NonNull GraphQLRequest<PaginatedResult<ModelWithMetadata<T>>> request, @NonNull Consumer<GraphQLResponse<PaginatedResult<ModelWithMetadata<T>>>> onResponse, @NonNull Consumer<DataStoreException> onFailure) {
    final Consumer<GraphQLResponse<PaginatedResult<ModelWithMetadata<T>>>> responseConsumer = apiQueryResponse -> {
        if (apiQueryResponse.hasErrors()) {
            onFailure.accept(new DataStoreException("Failure performing sync query to AppSync: " + apiQueryResponse.getErrors().toString(), AmplifyException.TODO_RECOVERY_SUGGESTION));
        } else {
            onResponse.accept(apiQueryResponse);
        }
    };
    final Consumer<ApiException> failureConsumer = failure -> onFailure.accept(new DataStoreException("Failure performing sync query to AppSync.", failure, AmplifyException.TODO_RECOVERY_SUGGESTION));
    final Cancelable cancelable = api.query(request, responseConsumer, failureConsumer);
    if (cancelable != null) {
        return cancelable;
    }
    return new NoOpCancelable();
}
Also used : Amplify(com.amplifyframework.core.Amplify) AmplifyException(com.amplifyframework.AmplifyException) NonNull(androidx.annotation.NonNull) QueryPredicates(com.amplifyframework.core.model.query.predicate.QueryPredicates) GraphQLRequest(com.amplifyframework.api.graphql.GraphQLRequest) Model(com.amplifyframework.core.model.Model) Action(com.amplifyframework.core.Action) ApiException(com.amplifyframework.api.ApiException) Logger(com.amplifyframework.logging.Logger) GraphQLBehavior(com.amplifyframework.api.graphql.GraphQLBehavior) DataStoreException(com.amplifyframework.datastore.DataStoreException) Consumer(com.amplifyframework.core.Consumer) Nullable(androidx.annotation.Nullable) Cancelable(com.amplifyframework.core.async.Cancelable) SubscriptionType(com.amplifyframework.api.graphql.SubscriptionType) AuthModeStrategyType(com.amplifyframework.api.aws.AuthModeStrategyType) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) ModelSchema(com.amplifyframework.core.model.ModelSchema) GraphQLResponse(com.amplifyframework.api.graphql.GraphQLResponse) ApiCategoryBehavior(com.amplifyframework.api.ApiCategoryBehavior) PaginatedResult(com.amplifyframework.api.graphql.PaginatedResult) QueryPredicate(com.amplifyframework.core.model.query.predicate.QueryPredicate) DataStoreException(com.amplifyframework.datastore.DataStoreException) GraphQLResponse(com.amplifyframework.api.graphql.GraphQLResponse) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) Cancelable(com.amplifyframework.core.async.Cancelable) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) ApiException(com.amplifyframework.api.ApiException) NonNull(androidx.annotation.NonNull)

Example 50 with Model

use of com.amplifyframework.core.model.Model in project amplify-android by aws-amplify.

the class AppSyncClient method subscription.

private <T extends Model> Cancelable subscription(SubscriptionType subscriptionType, ModelSchema modelSchema, Consumer<String> onSubscriptionStarted, Consumer<GraphQLResponse<ModelWithMetadata<T>>> onNextResponse, Consumer<DataStoreException> onSubscriptionFailure, Action onSubscriptionCompleted) {
    final GraphQLRequest<ModelWithMetadata<T>> request;
    try {
        request = AppSyncRequestFactory.buildSubscriptionRequest(modelSchema, subscriptionType, authModeStrategyType);
    } catch (DataStoreException requestGenerationException) {
        onSubscriptionFailure.accept(requestGenerationException);
        return new NoOpCancelable();
    }
    final Consumer<GraphQLResponse<ModelWithMetadata<T>>> responseConsumer = response -> {
        if (response.hasErrors()) {
            onSubscriptionFailure.accept(new DataStoreException.GraphQLResponseException("Subscription error for " + modelSchema.getName() + ": " + response.getErrors(), response.getErrors()));
        } else {
            onNextResponse.accept(response);
        }
    };
    final Consumer<ApiException> failureConsumer = failure -> onSubscriptionFailure.accept(new DataStoreException("Error during subscription.", failure, "Evaluate details."));
    final Cancelable cancelable = api.subscribe(request, onSubscriptionStarted, responseConsumer, failureConsumer, onSubscriptionCompleted);
    if (cancelable != null) {
        return cancelable;
    }
    return new NoOpCancelable();
}
Also used : Amplify(com.amplifyframework.core.Amplify) AmplifyException(com.amplifyframework.AmplifyException) NonNull(androidx.annotation.NonNull) QueryPredicates(com.amplifyframework.core.model.query.predicate.QueryPredicates) GraphQLRequest(com.amplifyframework.api.graphql.GraphQLRequest) Model(com.amplifyframework.core.model.Model) Action(com.amplifyframework.core.Action) ApiException(com.amplifyframework.api.ApiException) Logger(com.amplifyframework.logging.Logger) GraphQLBehavior(com.amplifyframework.api.graphql.GraphQLBehavior) DataStoreException(com.amplifyframework.datastore.DataStoreException) Consumer(com.amplifyframework.core.Consumer) Nullable(androidx.annotation.Nullable) Cancelable(com.amplifyframework.core.async.Cancelable) SubscriptionType(com.amplifyframework.api.graphql.SubscriptionType) AuthModeStrategyType(com.amplifyframework.api.aws.AuthModeStrategyType) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) ModelSchema(com.amplifyframework.core.model.ModelSchema) GraphQLResponse(com.amplifyframework.api.graphql.GraphQLResponse) ApiCategoryBehavior(com.amplifyframework.api.ApiCategoryBehavior) PaginatedResult(com.amplifyframework.api.graphql.PaginatedResult) QueryPredicate(com.amplifyframework.core.model.query.predicate.QueryPredicate) DataStoreException(com.amplifyframework.datastore.DataStoreException) GraphQLResponse(com.amplifyframework.api.graphql.GraphQLResponse) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) Cancelable(com.amplifyframework.core.async.Cancelable) NoOpCancelable(com.amplifyframework.core.async.NoOpCancelable) ApiException(com.amplifyframework.api.ApiException)

Aggregations

Model (com.amplifyframework.core.model.Model)62 Test (org.junit.Test)37 DataStoreException (com.amplifyframework.datastore.DataStoreException)34 ModelSchema (com.amplifyframework.core.model.ModelSchema)32 SerializedModel (com.amplifyframework.core.model.SerializedModel)30 AmplifyException (com.amplifyframework.AmplifyException)23 Cancelable (com.amplifyframework.core.async.Cancelable)22 Action (com.amplifyframework.core.Action)19 Consumer (com.amplifyframework.core.Consumer)19 StorageItemChange (com.amplifyframework.datastore.storage.StorageItemChange)19 BlogOwner (com.amplifyframework.testmodels.commentsblog.BlogOwner)19 ArrayList (java.util.ArrayList)19 List (java.util.List)19 SchemaRegistry (com.amplifyframework.core.model.SchemaRegistry)18 RandomModel (com.amplifyframework.testutils.random.RandomModel)17 GraphQLResponse (com.amplifyframework.api.graphql.GraphQLResponse)16 QueryPredicates (com.amplifyframework.core.model.query.predicate.QueryPredicates)16 TimeUnit (java.util.concurrent.TimeUnit)16 Collections (java.util.Collections)15 ObserveQueryOptions (com.amplifyframework.core.model.query.ObserveQueryOptions)13