Skip to content
This repository
Browse code

add support for conflict resolution

  • Loading branch information...
commit 3694d52f3fcab7e240b0f0be3fb3b6dfa3a9cd9a 1 parent f509f5a
Marty Schoch authored
50  src/main/java/org/elasticsearch/transport/couchbase/capi/ElasticSearchCAPIBehavior.java
@@ -17,6 +17,7 @@
17 17
 import java.io.InputStream;
18 18
 import java.util.ArrayList;
19 19
 import java.util.HashMap;
  20
+import java.util.Iterator;
20 21
 import java.util.List;
21 22
 import java.util.Map;
22 23
 import java.util.Map.Entry;
@@ -30,6 +31,8 @@
30 31
 import org.elasticsearch.action.bulk.BulkResponse;
31 32
 import org.elasticsearch.action.delete.DeleteRequest;
32 33
 import org.elasticsearch.action.get.GetResponse;
  34
+import org.elasticsearch.action.get.MultiGetItemResponse;
  35
+import org.elasticsearch.action.get.MultiGetResponse;
33 36
 import org.elasticsearch.action.index.IndexRequest;
34 37
 import org.elasticsearch.action.index.IndexRequestBuilder;
35 38
 import org.elasticsearch.action.index.IndexResponse;
@@ -48,13 +51,15 @@
48 51
     protected String defaultDocumentType;
49 52
     protected String checkpointDocumentType;
50 53
     protected String dynamicTypePath;
  54
+    protected boolean resolveConflicts;
51 55
 
52  
-    public ElasticSearchCAPIBehavior(Client client, ESLogger logger, String defaultDocumentType, String checkpointDocumentType, String dynamicTypePath) {
  56
+    public ElasticSearchCAPIBehavior(Client client, ESLogger logger, String defaultDocumentType, String checkpointDocumentType, String dynamicTypePath, boolean resolveConflicts) {
53 57
         this.client = client;
54 58
         this.logger = logger;
55 59
         this.defaultDocumentType = defaultDocumentType;
56 60
         this.checkpointDocumentType = checkpointDocumentType;
57 61
         this.dynamicTypePath = dynamicTypePath;
  62
+        this.resolveConflicts = resolveConflicts;
58 63
     }
59 64
 
60 65
     @Override
@@ -97,14 +102,53 @@ public boolean ensureFullCommit(String database) {
97 102
     public Map<String, Object> revsDiff(String database,
98 103
             Map<String, Object> revsMap) {
99 104
 
  105
+        logger.trace("_revs_diff request for: {}", revsMap);
  106
+
  107
+        // start with all entries in the response map
100 108
         Map<String, Object> responseMap = new HashMap<String, Object>();
101 109
         for (Entry<String, Object> entry : revsMap.entrySet()) {
102 110
             String id = entry.getKey();
103  
-            Object revs = entry.getValue();
104  
-            Map<String, Object> rev = new HashMap<String, Object>();
  111
+            String revs = (String)entry.getValue();
  112
+            Map<String, String> rev = new HashMap<String, String>();
105 113
             rev.put("missing", revs);
106 114
             responseMap.put(id, rev);
107 115
         }
  116
+        logger.trace("_revs_diff response is: {}", responseMap);
  117
+
  118
+        // if resolve conflicts mode is enabled
  119
+        // perform a multi-get query to find information
  120
+        // about revisions we already have
  121
+        if (resolveConflicts) {
  122
+            String index = getElasticSearchIndexNameFromDatabase(database);
  123
+            MultiGetResponse response = client.prepareMultiGet().add(index, defaultDocumentType, responseMap.keySet()).execute().actionGet();
  124
+            if(response != null) {
  125
+                Iterator<MultiGetItemResponse> iterator = response.iterator();
  126
+                while(iterator.hasNext()) {
  127
+                    MultiGetItemResponse item = iterator.next();
  128
+                    if(item.response().exists()) {
  129
+                        String itemId = item.id();
  130
+                        Map<String, Object> source = item.response().sourceAsMap();
  131
+                        if(source != null) {
  132
+                            Map<String, Object> meta = (Map<String, Object>)source.get("meta");
  133
+                            if(meta != null) {
  134
+                                String rev = (String)meta.get("rev");
  135
+                                //retrieve the revision passed in from Couchbase
  136
+                                Map<String, String> sourceRevMap = (Map<String, String>)responseMap.get(itemId);
  137
+                                String sourceRev = sourceRevMap.get("missing");
  138
+                                if(rev.equals(sourceRev)) {
  139
+                                    // if our revision is the same as the source rev
  140
+                                    // remove it from the response map
  141
+                                    responseMap.remove(itemId);
  142
+                                    logger.trace("_revs_diff already have id: {} rev: {}", itemId, rev);
  143
+                                }
  144
+                            }
  145
+                        }
  146
+                    }
  147
+                }
  148
+            }
  149
+            logger.trace("_revs_diff response AFTER conflict resolution {}", responseMap);
  150
+        }
  151
+
108 152
         return responseMap;
109 153
     }
110 154
 

0 notes on commit 3694d52

Please sign in to comment.
Something went wrong with that request. Please try again.