From 2257aa6697c55b7d8e4dc3bf01a8cd5e379592da Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Mon, 3 Feb 2020 17:39:30 -0600 Subject: [PATCH] Batch fetch and push journal entries --- .../java/org/tasks/etesync/EteSyncClient.java | 25 +++++++++++++------ .../org/tasks/etesync/EteSynchronizer.java | 7 +++--- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/org/tasks/etesync/EteSyncClient.java b/app/src/main/java/org/tasks/etesync/EteSyncClient.java index c9e9c7184..53d84e462 100644 --- a/app/src/main/java/org/tasks/etesync/EteSyncClient.java +++ b/app/src/main/java/org/tasks/etesync/EteSyncClient.java @@ -1,5 +1,6 @@ package org.tasks.etesync; +import static com.google.common.collect.Lists.partition; import static com.google.common.collect.Lists.transform; import android.content.Context; @@ -37,9 +38,11 @@ import okhttp3.HttpUrl; import okhttp3.OkHttpClient; import okhttp3.OkHttpClient.Builder; import okhttp3.internal.tls.OkHostnameVerifier; +import org.tasks.Callback; import org.tasks.DebugNetworkInterceptor; import org.tasks.caldav.MemoryCookieStore; import org.tasks.data.CaldavAccount; +import org.tasks.data.CaldavCalendar; import org.tasks.injection.ForApplication; import org.tasks.preferences.Preferences; import org.tasks.security.Encryption; @@ -48,6 +51,7 @@ import timber.log.Timber; public class EteSyncClient { private static final int MAX_FETCH = 50; + private static final int MAX_PUSH = 30; private final Encryption encryption; private final Preferences preferences; @@ -155,8 +159,7 @@ public class EteSyncClient { return Pair.create(token, key); } - CryptoManager getCrypto(Journal journal) - throws VersionTooNewException, IntegrityException { + CryptoManager getCrypto(Journal journal) throws VersionTooNewException, IntegrityException { return new CryptoManager(journal.getVersion(), encryptionPassword, journal.getUid()); } @@ -185,17 +188,25 @@ public class EteSyncClient { return result; } - List> getSyncEntries(Journal journal, @Nullable String ctag) + void getSyncEntries(Journal journal, CaldavCalendar calendar, Callback>> callback) throws IntegrityException, Exceptions.HttpException, VersionTooNewException { JournalEntryManager journalEntryManager = new JournalEntryManager(httpClient, httpUrl, journal.getUid()); CryptoManager crypto = getCrypto(journal); - List journalEntries = journalEntryManager.list(crypto, ctag, MAX_FETCH); - return transform(journalEntries, e -> Pair.create(e, SyncEntry.fromJournalEntry(crypto, e))); + List journalEntries; + do { + journalEntries = journalEntryManager.list(crypto, calendar.getCtag(), MAX_FETCH); + callback.call(transform(journalEntries, e -> Pair.create(e, SyncEntry.fromJournalEntry(crypto, e)))); + } while (journalEntries.size() >= MAX_FETCH); } - void pushEntries(Journal journal, List entries, String ctag) throws HttpException { - new JournalEntryManager(httpClient, httpUrl, journal.getUid()).create(entries, ctag); + void pushEntries(Journal journal, List entries, String remoteCtag) throws HttpException { + JournalEntryManager journalEntryManager = + new JournalEntryManager(httpClient, httpUrl, journal.getUid()); + for (List partition : partition(entries, MAX_PUSH)) { + journalEntryManager.create(partition, remoteCtag); + remoteCtag = partition.get(partition.size() - 1).getUid(); + } } public EteSyncClient setForeground() { diff --git a/app/src/main/java/org/tasks/etesync/EteSynchronizer.java b/app/src/main/java/org/tasks/etesync/EteSynchronizer.java index c492245b9..aee246128 100644 --- a/app/src/main/java/org/tasks/etesync/EteSynchronizer.java +++ b/app/src/main/java/org/tasks/etesync/EteSynchronizer.java @@ -182,10 +182,11 @@ public class EteSynchronizer { localChanges.put(task.getRemoteId(), task); } - List> syncEntries = - client.getSyncEntries(journal, caldavCalendar.getCtag()); Timber.v("Applying remote changes"); - applyEntries(caldavCalendar, syncEntries, localChanges.keySet()); + client.getSyncEntries( + journal, + caldavCalendar, + syncEntries -> applyEntries(caldavCalendar, syncEntries, localChanges.keySet())); List changes = new ArrayList<>(); for (CaldavTask task : caldavDao.getDeleted(caldavCalendar.getUuid())) {