Merge lp:~jameinel/u1db/force_doc_sync into lp:u1db

Proposed by John A Meinel
Status: Merged
Merged at revision: 123
Proposed branch: lp:~jameinel/u1db/force_doc_sync
Merge into: lp:u1db
Diff against target: 135 lines (+29/-25)
5 files modified
u1db/__init__.py (+5/-7)
u1db/backends/inmemory.py (+7/-4)
u1db/backends/sqlite_backend.py (+8/-6)
u1db/sync.py (+1/-2)
u1db/tests/test_backends.py (+8/-6)
To merge this branch: bzr merge lp:~jameinel/u1db/force_doc_sync
Reviewer Review Type Date Requested Status
Samuele Pedroni Approve
Review via email: mp+83156@code.launchpad.net

Description of the change

Updates force_doc_sync_conflict to use a Document.

The only bit I'm not fully sure about is altering the passed in document to set the has_conflicts=True attribute.

However, it makes sense to me.

To post a comment you must log in.
Revision history for this message
Samuele Pedroni (pedronis) wrote :

+1, yes I suppose it's good for consistency though the only caller in our current code doesn't care about the flag/doc afterward

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'u1db/__init__.py'
2--- u1db/__init__.py 2011-11-22 10:51:25 +0000
3+++ u1db/__init__.py 2011-11-23 13:03:59 +0000
4@@ -98,15 +98,15 @@
5 """
6 raise NotImplementedError(self.put_docs)
7
8- def force_doc_sync_conflict(self, doc_id, doc_rev, doc):
9+ def force_doc_sync_conflict(self, doc):
10 """Update documents even though they should conflict.
11
12 This is used for synchronization, and should generally not be used by
13 clients.
14
15- The content will be selected as the 'current' content for doc_id, even
16- though doc_rev may not supersede the currently stored revision. The
17- currently stored document will be added to the list of conflict
18+ The content will be selected as the 'current' content for doc.doc_id,
19+ even though doc.rev may not supersede the currently stored revision.
20+ The currently stored document will be added to the list of conflict
21 alternatives for the given doc_id.
22
23 The reason this forces the new content to be 'current' is so that we
24@@ -116,9 +116,7 @@
25 synchronize and think the data has propagated, but their local copy
26 looks fine, and the remote copy is never updated again.)
27
28- :param doc_id: The indentifier for this document
29- :param doc_rev: The document revision for this document
30- :param doc: The JSON string for the document.
31+ :param doc: A Document
32 :return: None
33 """
34 raise NotImplementedError(self.force_doc_sync_conflict)
35
36=== modified file 'u1db/backends/inmemory.py'
37--- u1db/backends/inmemory.py 2011-11-21 13:50:39 +0000
38+++ u1db/backends/inmemory.py 2011-11-23 13:03:59 +0000
39@@ -166,10 +166,13 @@
40 return (len(self._transaction_log),
41 set(self._transaction_log[old_generation:]))
42
43- def force_doc_sync_conflict(self, doc_id, doc_rev, doc):
44- my_doc_rev, my_doc = self._docs[doc_id]
45- self._conflicts.setdefault(doc_id, []).append((my_doc_rev, my_doc))
46- self._put_and_update_indexes(doc_id, my_doc, doc_rev, doc)
47+ def force_doc_sync_conflict(self, doc):
48+ my_doc_rev, my_content = self._docs[doc.doc_id]
49+ self._conflicts.setdefault(doc.doc_id, []).append(
50+ (my_doc_rev, my_content))
51+ doc.has_conflicts = True
52+ self._put_and_update_indexes(doc.doc_id, my_content, doc.rev,
53+ doc.content)
54
55
56 class InMemoryIndex(object):
57
58=== modified file 'u1db/backends/sqlite_backend.py'
59--- u1db/backends/sqlite_backend.py 2011-11-22 15:08:58 +0000
60+++ u1db/backends/sqlite_backend.py 2011-11-23 13:03:59 +0000
61@@ -339,16 +339,18 @@
62 return super(SQLiteDatabase, self).put_doc_if_newer(
63 doc_id, doc_rev, doc)
64
65- def _add_conflict(self, c, doc_id, my_doc_rev, my_doc):
66+ def _add_conflict(self, c, doc_id, my_doc_rev, my_content):
67 c.execute("INSERT INTO conflicts VALUES (?, ?, ?)",
68- (doc_id, my_doc_rev, my_doc))
69+ (doc_id, my_doc_rev, my_content))
70
71- def force_doc_sync_conflict(self, doc_id, doc_rev, doc):
72+ def force_doc_sync_conflict(self, doc):
73 with self._db_handle:
74- my_doc = self._get_doc(doc_id)
75+ my_doc = self._get_doc(doc.doc_id)
76 c = self._db_handle.cursor()
77- self._add_conflict(c, doc_id, my_doc.rev, my_doc.content)
78- self._put_and_update_indexes(doc_id, my_doc.content, doc_rev, doc)
79+ self._add_conflict(c, doc.doc_id, my_doc.rev, my_doc.content)
80+ doc.has_conflicts = True
81+ self._put_and_update_indexes(doc.doc_id, my_doc.content, doc.rev,
82+ doc.content)
83
84 def resolve_doc(self, doc_id, doc, conflicted_doc_revs):
85 with self._db_handle:
86
87=== modified file 'u1db/sync.py'
88--- u1db/sync.py 2011-11-21 14:18:51 +0000
89+++ u1db/sync.py 2011-11-23 13:03:59 +0000
90@@ -60,8 +60,7 @@
91 assert state == 'conflicted'
92 # take doc as the official value, stores the current
93 # alongside as conflict
94- self.source.force_doc_sync_conflict(doc.doc_id, doc.rev,
95- doc.content)
96+ self.source.force_doc_sync_conflict(doc)
97 self.num_inserted += 1
98
99 def _record_sync_info_with_the_target(self, start_generation):
100
101=== modified file 'u1db/tests/test_backends.py'
102--- u1db/tests/test_backends.py 2011-11-21 14:32:17 +0000
103+++ u1db/tests/test_backends.py 2011-11-23 13:03:59 +0000
104@@ -93,15 +93,15 @@
105
106 def test_get_docs_conflicted(self):
107 doc1 = self.db.create_doc(simple_doc)
108- self.db.force_doc_sync_conflict(doc1.doc_id, 'alternate:1', nested_doc)
109- self.assertEqual(
110- [Document(doc1.doc_id, 'alternate:1', nested_doc, True)],
111- self.db.get_docs([doc1.doc_id]))
112+ doc2 = Document(doc1.doc_id, 'alternate:1', nested_doc)
113+ self.db.force_doc_sync_conflict(doc2)
114+ self.assertEqual([doc2], self.db.get_docs([doc1.doc_id]))
115
116 def test_get_docs_conflicts_ignored(self):
117 doc1 = self.db.create_doc(simple_doc)
118 doc2 = self.db.create_doc(nested_doc)
119- self.db.force_doc_sync_conflict(doc1.doc_id, 'alternate:1', nested_doc)
120+ alt_doc = Document(doc1.doc_id, 'alternate:1', nested_doc)
121+ self.db.force_doc_sync_conflict(alt_doc)
122 self.assertEqual(
123 sorted([Document(doc1.doc_id, 'alternate:1', nested_doc),
124 Document(doc2.doc_id, doc2.rev, nested_doc)]),
125@@ -148,7 +148,9 @@
126
127 def test_force_doc_with_conflict(self):
128 doc1 = self.db.create_doc(simple_doc)
129- self.db.force_doc_sync_conflict(doc1.doc_id, 'alternate:1', nested_doc)
130+ doc2 = Document(doc1.doc_id, 'alternate:1', nested_doc)
131+ self.db.force_doc_sync_conflict(doc2)
132+ self.assertTrue(doc2.has_conflicts)
133 self.assertGetDoc(self.db, doc1.doc_id, 'alternate:1', nested_doc, True)
134 self.assertEqual([('alternate:1', nested_doc),
135 (doc1.rev, simple_doc)],

Subscribers

People subscribed via source and target branches