diff options
author | Victor Shyba <victor.shyba@gmail.com> | 2015-08-14 22:08:27 -0300 |
---|---|---|
committer | Victor Shyba <victor.shyba@gmail.com> | 2015-08-26 17:20:21 -0300 |
commit | fd6c054bf11deba4ca5680cc406db7d4ce98d58d (patch) | |
tree | 328dfbc2bd5b5ed34ee75c7f85983ca3d2b82c72 /common/src/leap | |
parent | 8b95942a2be4a65222b1758f2cb63b9dd86ea69d (diff) |
[refactor] split put_doc_if_newer in two operations
Those two operations were mixed on put_doc_if_newer, extracting should
make it more clear.
Diffstat (limited to 'common/src/leap')
-rw-r--r-- | common/src/leap/soledad/common/couch.py | 37 |
1 files changed, 27 insertions, 10 deletions
diff --git a/common/src/leap/soledad/common/couch.py b/common/src/leap/soledad/common/couch.py index 6c28e0be..52fc2169 100644 --- a/common/src/leap/soledad/common/couch.py +++ b/common/src/leap/soledad/common/couch.py @@ -1140,9 +1140,11 @@ class CouchDatabase(CommonBackend): :param sync_id: The id of the current sync session. :type sync_id: str """ - self._do_set_replica_gen_and_trans_id( - other_replica_uid, other_generation, other_transaction_id, - number_of_docs=number_of_docs, doc_idx=doc_idx, sync_id=sync_id) + if other_replica_uid is not None and other_generation is not None: + self._do_set_replica_gen_and_trans_id( + other_replica_uid, other_generation, other_transaction_id, + number_of_docs=number_of_docs, doc_idx=doc_idx, + sync_id=sync_id) def _do_set_replica_gen_and_trans_id( self, other_replica_uid, other_generation, other_transaction_id, @@ -1392,6 +1394,27 @@ class CouchDatabase(CommonBackend): 'converged', at_gen is the insertion/current generation. :rtype: (str, int) """ + self._save_source_info(replica_uid, replica_gen, + replica_trans_id, number_of_docs, + doc_idx, sync_id) + state = self._process_incoming_doc(doc, save_conflict) + return state, self._get_generation() + + def _save_source_info(self, replica_uid, replica_gen, replica_trans_id, + number_of_docs, doc_idx, sync_id): + """ + Validate and save source information. + """ + self._validate_source(replica_uid, replica_gen, replica_trans_id) + self._set_replica_gen_and_trans_id( + replica_uid, replica_gen, replica_trans_id, + number_of_docs=number_of_docs, doc_idx=doc_idx, + sync_id=sync_id) + + def _process_incoming_doc(self, doc, save_conflict): + """ + Check document, save and return state. + """ cur_doc = self._get_doc(doc.doc_id, check_for_conflicts=True) # at this point, `doc` has arrived from the other syncing party, and # we will decide what to do with it. @@ -1408,7 +1431,6 @@ class CouchDatabase(CommonBackend): cur_vcr = vectorclock.VectorClockRev(None) else: cur_vcr = vectorclock.VectorClockRev(cur_doc.rev) - self._validate_source(replica_uid, replica_gen, replica_trans_id) if doc_vcr.is_newer(cur_vcr): rev = doc.rev self._prune_conflicts(doc, doc_vcr) @@ -1437,11 +1459,6 @@ class CouchDatabase(CommonBackend): state = 'conflicted' if save_conflict: self._force_doc_sync_conflict(doc) - if replica_uid is not None and replica_gen is not None: - self._set_replica_gen_and_trans_id( - replica_uid, replica_gen, replica_trans_id, - number_of_docs=number_of_docs, doc_idx=doc_idx, - sync_id=sync_id) # update info old_doc.rev = doc.rev if doc.is_tombstone(): @@ -1449,7 +1466,7 @@ class CouchDatabase(CommonBackend): else: old_doc.content = doc.content old_doc.has_conflicts = doc.has_conflicts - return state, self._get_generation() + return state def get_docs(self, doc_ids, check_for_conflicts=True, include_deleted=False): |