From 8f540744ade9c6c1f329239a04a7ba017b349bd5 Mon Sep 17 00:00:00 2001
From: "Brian S. O'Neill" <bronee@gmail.com>
Date: Mon, 1 Oct 2012 20:19:35 +0000
Subject: Add reload check during resync.

---
 .../repo/replicated/ReplicatedRepository.java       |  5 ++++-
 .../repo/replicated/ReplicationTrigger.java         | 21 +++++++++++++++++----
 2 files changed, 21 insertions(+), 5 deletions(-)

(limited to 'src/main/java')

diff --git a/src/main/java/com/amazon/carbonado/repo/replicated/ReplicatedRepository.java b/src/main/java/com/amazon/carbonado/repo/replicated/ReplicatedRepository.java
index 9b1a218..1b46073 100644
--- a/src/main/java/com/amazon/carbonado/repo/replicated/ReplicatedRepository.java
+++ b/src/main/java/com/amazon/carbonado/repo/replicated/ReplicatedRepository.java
@@ -694,8 +694,11 @@ class ReplicatedRepository
 
         Runnable task = new Runnable() {
             public void run() {
+                // Reload deletes and updates. Don't reload inserts, because it
+                // makes a resync from an empty replica way too expensive.
+                boolean reload = masterEntry == null || replicaEntry != null;
                 try {
-                    replicationTrigger.resyncEntries(listener, replicaEntry, masterEntry);
+                    replicationTrigger.resyncEntries(listener, replicaEntry, masterEntry, reload);
                 } catch (Exception e) {
                     LogFactory.getLog(ReplicatedRepository.class).error(null, e);
                 }
diff --git a/src/main/java/com/amazon/carbonado/repo/replicated/ReplicationTrigger.java b/src/main/java/com/amazon/carbonado/repo/replicated/ReplicationTrigger.java
index 941910d..c646f8c 100644
--- a/src/main/java/com/amazon/carbonado/repo/replicated/ReplicationTrigger.java
+++ b/src/main/java/com/amazon/carbonado/repo/replicated/ReplicationTrigger.java
@@ -217,9 +217,10 @@ class ReplicationTrigger<S extends Storable> extends Trigger<S> {
      * @param listener optional
      * @param replicaEntry current replica entry, or null if none
      * @param masterEntry current master entry, or null if none
+     * @param reload true to reload master entry
      */
     void resyncEntries(ResyncCapability.Listener<? super S> listener,
-                       S replicaEntry, S masterEntry)
+                       S replicaEntry, S masterEntry, boolean reload)
         throws FetchException, PersistException
     {
         if (replicaEntry == null && masterEntry == null) {
@@ -232,6 +233,18 @@ class ReplicationTrigger<S extends Storable> extends Trigger<S> {
         try {
             Transaction txn = mRepository.enterTransaction();
             try {
+                txn.setForUpdate(true);
+
+                if (reload) {
+                    if (masterEntry == null) {
+                        masterEntry = mMasterStorage.prepare();
+                        replicaEntry.copyAllProperties(masterEntry);
+                    }
+                    if (!masterEntry.tryLoad()) {
+                        masterEntry = null;
+                    }
+                }
+
                 final S newReplicaEntry;
                 if (replicaEntry == null) {
                     newReplicaEntry = mReplicaStorage.prepare();
@@ -419,12 +432,12 @@ class ReplicationTrigger<S extends Storable> extends Trigger<S> {
                         txn.setForUpdate(true);
                         if (finalReplica.tryLoad()) {
                             if (finalMaster.tryLoad()) {
-                                resyncEntries(null, finalReplica, finalMaster);
+                                resyncEntries(null, finalReplica, finalMaster, false);
                             } else {
-                                resyncEntries(null, finalReplica, null);
+                                resyncEntries(null, finalReplica, null, false);
                             }
                         } else if (finalMaster.tryLoad()) {
-                            resyncEntries(null, null, finalMaster);
+                            resyncEntries(null, null, finalMaster, false);
                         }
                         txn.commit();
                     } finally {
-- 
cgit v1.2.3