Skip to content

Commit

Permalink
#1503 dupicates on upsync: should be fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
desperateCoder committed Nov 15, 2024
1 parent 66518e7 commit ff49d3c
Showing 1 changed file with 34 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@

import java.net.HttpURLConnection;
import java.time.Instant;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.concurrent.CountDownLatch;

import it.niedermann.nextcloud.deck.DeckLog;
Expand All @@ -28,6 +30,9 @@
import okhttp3.Headers;

public class SyncHelper {
// entity-class -> id if entity
private static final HashMap<Class<? extends IRemoteEntity>, ConcurrentSkipListSet<Long>> CURRENTLY_IN_UPSYNC = new HashMap<>();

@NonNull
private final ServerAdapter serverAdapter;
@NonNull
Expand Down Expand Up @@ -156,13 +161,20 @@ public <T extends IRemoteEntity> void doUpSyncFor(@NonNull AbstractSyncDataProvi
public <T extends IRemoteEntity> void doUpSyncFor(@NonNull AbstractSyncDataProvider<T> provider, @Nullable CountDownLatch countDownLatch) {
final List<T> allFromDB = provider.getAllChangedFromDB(dataBaseAdapter, accountId, lastSync);
if (allFromDB != null && !allFromDB.isEmpty()) {
Class<? extends IRemoteEntity> classOfEntity = allFromDB.get(0).getClass();
ConcurrentSkipListSet<Long> idsInSync = CURRENTLY_IN_UPSYNC.get(classOfEntity);
if (idsInSync == null) {
idsInSync = new ConcurrentSkipListSet<>();
CURRENTLY_IN_UPSYNC.put(classOfEntity, idsInSync);
}
for (T entity : allFromDB) {
if (idsInSync.contains(entity.getLocalId())){
continue;
}
idsInSync.add(entity.getLocalId());
if (entity.getId() != null) {
if (entity.getStatusEnum() == DBStatus.LOCAL_DELETED) {
provider.deleteOnServer(serverAdapter, accountId, getDeleteCallback(provider, entity), entity, dataBaseAdapter);
if (countDownLatch != null) {
countDownLatch.countDown();
}
provider.deleteOnServer(serverAdapter, accountId, getDeleteCallback(provider, entity, countDownLatch), entity, dataBaseAdapter);
} else {
provider.updateOnServer(serverAdapter, dataBaseAdapter, accountId, getUpdateCallback(provider, entity, countDownLatch), entity);
}
Expand All @@ -178,18 +190,20 @@ public <T extends IRemoteEntity> void doUpSyncFor(@NonNull AbstractSyncDataProvi
}
}

private <T extends IRemoteEntity> ResponseCallback<EmptyResponse> getDeleteCallback(@NonNull AbstractSyncDataProvider<T> provider, T entity) {
private <T extends IRemoteEntity> ResponseCallback<EmptyResponse> getDeleteCallback(@NonNull AbstractSyncDataProvider<T> provider, T entity, @Nullable CountDownLatch countDownLatch) {
return new ResponseCallback<>(account) {
@Override
public void onResponse(EmptyResponse response, Headers headers) {
provider.deletePhysicallyInDB(dataBaseAdapter, accountId, entity);
provider.goDeeperForUpSync(SyncHelper.this, serverAdapter, dataBaseAdapter, responseCallback);
provider.goDeeperForUpSync(SyncHelper.this, serverAdapter, dataBaseAdapter, responseCallback);;
doneWithUpsync(countDownLatch, entity);
}

@Override
public void onError(Throwable throwable) {
super.onError(throwable);
responseCallback.onError(throwable);
responseCallback.onError(throwable);;
doneWithUpsync(countDownLatch, entity);
}
};
}
Expand All @@ -204,22 +218,29 @@ public void onResponse(T response, Headers headers) {
update.setStatus(DBStatus.UP_TO_DATE.getId());
provider.updateInDB(dataBaseAdapter, accountId, update, false);
provider.goDeeperForUpSync(SyncHelper.this, serverAdapter, dataBaseAdapter, responseCallback);
if (countDownLatch != null) {
countDownLatch.countDown();
}

doneWithUpsync(countDownLatch, entity);
}

@Override
public void onError(Throwable throwable) {
super.onError(throwable);
responseCallback.onError(throwable);
if (countDownLatch != null) {
countDownLatch.countDown();
}
doneWithUpsync(countDownLatch, entity);
}
};
}

private <T extends IRemoteEntity> void doneWithUpsync(CountDownLatch countDownLatch, T entity) {
if (countDownLatch != null) {
countDownLatch.countDown();
}
ConcurrentSkipListSet<Long> idsInSync = CURRENTLY_IN_UPSYNC.get(entity.getClass());
if (idsInSync != null) {
idsInSync.remove(entity.getLocalId());
}
}

public void fixRelations(@NonNull IRelationshipProvider relationshipProvider) {
// this is OK, since the delete only affects records with status UP_TO_DATE
relationshipProvider.deleteAllExisting(dataBaseAdapter, accountId);
Expand Down

0 comments on commit ff49d3c

Please sign in to comment.