From 89479f496bfec05d16fba5bd5e986841ba3988f8 Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Fri, 23 Apr 2021 05:07:26 +0000 Subject: [PATCH] further optimize transaction storage by comparing dataHash field if the dataHash field of the transaction that needs storage matches one from the database (retrieved by profile id and ledger id), then simply update the generation of the transaction in the database (and its accounts) since this is the common case (refreshing transaction list -- most of the transactions are expected to be unchanged) this saves big time --- .../ktnx/mobileledger/dao/TransactionDAO.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/app/src/main/java/net/ktnx/mobileledger/dao/TransactionDAO.java b/app/src/main/java/net/ktnx/mobileledger/dao/TransactionDAO.java index 31319f34..8f238183 100644 --- a/app/src/main/java/net/ktnx/mobileledger/dao/TransactionDAO.java +++ b/app/src/main/java/net/ktnx/mobileledger/dao/TransactionDAO.java @@ -32,6 +32,7 @@ import net.ktnx.mobileledger.db.Transaction; import net.ktnx.mobileledger.db.TransactionAccount; import net.ktnx.mobileledger.db.TransactionWithAccounts; import net.ktnx.mobileledger.utils.Logger; +import net.ktnx.mobileledger.utils.Misc; import java.util.ArrayList; import java.util.List; @@ -128,6 +129,18 @@ public abstract class TransactionDAO extends BaseDAO { @Query("SELECT * FROM transactions where profile_id = :profileId AND ledger_id = :ledgerId") public abstract Transaction getByLedgerId(long profileId, long ledgerId); + + @Query("UPDATE transactions SET generation = :newGeneration WHERE id = :transactionId") + public abstract int updateGeneration(long transactionId, long newGeneration); + + @Query("UPDATE transaction_accounts SET generation = :newGeneration WHERE transaction_id = " + + ":transactionId") + public abstract int updateAccountsGeneration(long transactionId, long newGeneration); + @androidx.room.Transaction + public void updateGenerationWithAccounts(long transactionId, long newGeneration) { + updateGeneration(transactionId, newGeneration); + updateAccountsGeneration(transactionId, newGeneration); + } public long getGenerationSync(long profileId) { TransactionGenerationContainer result = getGenerationPOJOSync(profileId); @@ -160,8 +173,14 @@ public abstract class TransactionDAO extends BaseDAO { Transaction transaction = rec.transaction; Transaction existing = getByLedgerId(transaction.getProfileId(), transaction.getLedgerId()); if (existing != null) { + if (Misc.equalStrings(transaction.getDataHash(), existing.getDataHash())) { + updateGenerationWithAccounts(existing.getId(), rec.transaction.getGeneration()); + return; + } + existing.copyDataFrom(transaction); updateSync(existing); + transaction = existing; } else -- 2.39.5