Skip to content

Commit f409ad3

Browse files
author
Edison Zhang
committed
use & save syncExpression (MVP)
1. use serializedSyncExpression in lookupLastSyncTime 2. save serializedSyncExpression along with lastSyncTime
1 parent 1fc5f09 commit f409ad3

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

aws-datastore/src/main/java/com/amplifyframework/datastore/syncengine/SyncProcessor.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@
4646
import com.amplifyframework.hub.HubChannel;
4747
import com.amplifyframework.hub.HubEvent;
4848
import com.amplifyframework.logging.Logger;
49+
import com.amplifyframework.util.GsonFactory;
4950
import com.amplifyframework.util.Time;
51+
import com.google.gson.Gson;
5052

5153
import java.util.ArrayList;
5254
import java.util.Collections;
@@ -80,6 +82,7 @@ final class SyncProcessor {
8082
private final DataStoreConfigurationProvider dataStoreConfigurationProvider;
8183
private final QueryPredicateProvider queryPredicateProvider;
8284
private final RetryHandler requestRetry;
85+
private final Gson gson; //TODO: for pre-deserialization of QueryPredicate, will remove shortly
8386

8487
/**
8588
* The `isSyncRetryEnabled` value is being passed down all the way from the `AWSDataStorePlugin` or the
@@ -97,7 +100,7 @@ private SyncProcessor(Builder builder) {
97100
this.queryPredicateProvider = builder.queryPredicateProvider;
98101
this.requestRetry = builder.requestRetry;
99102
this.isSyncRetryEnabled = builder.isSyncRetryEnabled;
100-
103+
this.gson = GsonFactory.instance(); //TODO: for pre-deserialization of QueryPredicate, will remove shortly
101104
if (!this.isSyncRetryEnabled) {
102105
LOG.warn("Disabling sync retries will be deprecated in a future version.");
103106
}
@@ -182,10 +185,9 @@ Completable hydrate() {
182185

183186
private Completable createHydrationTask(ModelSchema schema) {
184187
ModelSyncMetricsAccumulator metricsAccumulator = new ModelSyncMetricsAccumulator(schema.getName());
185-
//TODO: QueryPredicate currentPredicate = this.queryPredicateProvider.getPredicate(schema.getName());
186-
//TODO deserialize current predicate to deserializedCurrentPredicate
187-
//TODO: pass deserializedCurrentPredicate to lookupLastSyncTime as the second parameter
188-
return syncTimeRegistry.lookupLastSyncTime(schema.getName())
188+
QueryPredicate currentPredicate = this.queryPredicateProvider.getPredicate(schema.getName());
189+
String serializedSyncExpression = gson.toJson(currentPredicate); //TODO: for pre-deserialization of QueryPredicate, will remove shortly
190+
return syncTimeRegistry.lookupLastSyncTime(schema.getName(), serializedSyncExpression)
189191
.map(this::filterOutOldSyncTimes)
190192
// And for each, perform a sync. The network response will contain an Iterable<ModelWithMetadata<T>>
191193
.flatMap(lastSyncTime -> {
@@ -204,10 +206,9 @@ private Completable createHydrationTask(ModelSchema schema) {
204206
.toSingle(() -> lastSyncTime.exists() ? SyncType.DELTA : SyncType.BASE);
205207
})
206208
.flatMapCompletable(syncType -> {
207-
//TODO: pass the deserializedCurrentPredicate to saveLastDelta/BaseSync as the third parameter
208209
Completable syncTimeSaveCompletable = SyncType.DELTA.equals(syncType) ?
209-
syncTimeRegistry.saveLastDeltaSyncTime(schema.getName(), SyncTime.now(), null) :
210-
syncTimeRegistry.saveLastBaseSyncTime(schema.getName(), SyncTime.now(), null);
210+
syncTimeRegistry.saveLastDeltaSync(schema.getName(), SyncTime.now(), serializedSyncExpression) :
211+
syncTimeRegistry.saveLastBaseSync(schema.getName(), SyncTime.now(), serializedSyncExpression);
211212
return syncTimeSaveCompletable.andThen(Completable.fromAction(() ->
212213
Amplify.Hub.publish(
213214
HubChannel.DATASTORE, metricsAccumulator.toModelSyncedEvent(syncType).toHubEvent()

aws-datastore/src/main/java/com/amplifyframework/datastore/syncengine/SyncTimeRegistry.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import androidx.annotation.NonNull;
1919
import androidx.annotation.Nullable;
20+
import androidx.core.util.ObjectsCompat;
2021

2122
import com.amplifyframework.core.model.query.Where;
2223
import com.amplifyframework.core.model.query.predicate.QueryField;
@@ -40,27 +41,27 @@ final class SyncTimeRegistry {
4041
this.localStorageAdapter = localStorageAdapter;
4142
}
4243

43-
//TODO: add the second argument: current sync expression here
44-
Single<SyncTime> lookupLastSyncTime(@NonNull String modelClassName) {
44+
Single<SyncTime> lookupLastSyncTime(@NonNull String modelClassName, @NonNull String syncExpression) {
4545
return Single.create(emitter -> {
4646
QueryPredicate hasMatchingModelClassName = QueryField.field("modelClassName").eq(modelClassName);
4747

4848
localStorageAdapter.query(LastSyncMetadata.class, Where.matches(hasMatchingModelClassName), results -> {
4949
try {
5050
LastSyncMetadata syncMetadata = extractSingleResult(modelClassName, results);
51-
//TODO: syncMetadata should contain the previous serialized sync expression
52-
//TODO: compare the previous sync expression with the current one
53-
//TODO: emmit SyncTime.never() if different
54-
emitter.onSuccess(SyncTime.from(syncMetadata.getLastSyncTime()));
51+
SyncTime lastSyncTime = SyncTime.from(syncMetadata.getLastSyncTime());
52+
String lastSyncExpression = syncMetadata.getSyncExpression();
53+
if(!ObjectsCompat.equals(lastSyncExpression, syncExpression)){
54+
lastSyncTime = SyncTime.never();
55+
}
56+
emitter.onSuccess(lastSyncTime);
5557
} catch (DataStoreException queryResultFailure) {
5658
emitter.onError(queryResultFailure);
5759
}
5860
}, emitter::onError);
5961
});
6062
}
6163

62-
//TODO: change the name to saveLastDeltaSync, and add the second argument for sync expression
63-
Completable saveLastDeltaSyncTime(@NonNull String modelClassName, @Nullable SyncTime syncTime, @Nullable String syncExpression) {
64+
Completable saveLastDeltaSync(@NonNull String modelClassName, @Nullable SyncTime syncTime, @Nullable String syncExpression) {
6465
LastSyncMetadata metadata = syncTime != null && syncTime.exists() ?
6566
LastSyncMetadata.deltaSyncedAt(modelClassName, syncTime.toLong(), syncExpression) :
6667
LastSyncMetadata.neverSynced(modelClassName);
@@ -76,8 +77,7 @@ Completable saveLastDeltaSyncTime(@NonNull String modelClassName, @Nullable Sync
7677
);
7778
}
7879

79-
//TODO: change the name to saveLastDeltaSync, and add the second argument for sync expression
80-
Completable saveLastBaseSyncTime(@NonNull String modelClassName, @Nullable SyncTime syncTime, @Nullable String syncExpression) {
80+
Completable saveLastBaseSync(@NonNull String modelClassName, @Nullable SyncTime syncTime, @Nullable String syncExpression) {
8181
LastSyncMetadata metadata = syncTime != null && syncTime.exists() ?
8282
LastSyncMetadata.baseSyncedAt(modelClassName, syncTime.toLong(), syncExpression) :
8383
LastSyncMetadata.neverSynced(modelClassName);

0 commit comments

Comments
 (0)