Merge lp:~pedronis/u1db/get-docs-return-generators-or-iterable into lp:u1db

Proposed by Samuele Pedroni
Status: Merged
Approved by: Samuele Pedroni
Approved revision: 364
Merged at revision: 364
Proposed branch: lp:~pedronis/u1db/get-docs-return-generators-or-iterable
Merge into: lp:u1db
Diff against target: 153 lines (+21/-19)
5 files modified
u1db/__init__.py (+2/-1)
u1db/backends/__init__.py (+1/-3)
u1db/remote/http_database.py (+2/-4)
u1db/sync.py (+3/-1)
u1db/tests/test_backends.py (+13/-10)
To merge this branch: bzr merge lp:~pedronis/u1db/get-docs-return-generators-or-iterable
Reviewer Review Type Date Requested Status
Eric Casteleijn (community) Approve
Review via email: mp+116950@code.launchpad.net

Commit message

don't promise more than an iterable as return value for get_docs, in practice use generators, relax tests

Description of the change

don't promise more than an iterable as return value for get_docs, in practice use generators, relax tests

To post a comment you must log in.
Revision history for this message
Eric Casteleijn (thisfred) wrote :

Yay! looks great. Tests pass.

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 2012-07-23 16:24:07 +0000
3+++ u1db/__init__.py 2012-07-26 19:18:18 +0000
4@@ -111,7 +111,8 @@
5 :param include_deleted: If set to True, deleted documents will be
6 returned with empty content. Otherwise deleted documents will not
7 be included in the results.
8- :return: [Document] for each document id and matching doc_ids order.
9+ :return: iterable giving the Document object for each document id
10+ in matching doc_ids order.
11 """
12 raise NotImplementedError(self.get_docs)
13
14
15=== modified file 'u1db/backends/__init__.py'
16--- u1db/backends/__init__.py 2012-07-19 19:50:58 +0000
17+++ u1db/backends/__init__.py 2012-07-26 19:18:18 +0000
18@@ -109,15 +109,13 @@
19
20 def get_docs(self, doc_ids, check_for_conflicts=True,
21 include_deleted=False):
22- result = []
23 for doc_id in doc_ids:
24 doc = self._get_doc(doc_id)
25 if doc.is_tombstone() and not include_deleted:
26 continue
27 if check_for_conflicts:
28 doc.has_conflicts = self._has_conflicts(doc_id)
29- result.append(doc)
30- return result
31+ yield doc
32
33 def _get_trans_id_for_gen(self, generation):
34 """Get the transaction id corresponding to a particular generation.
35
36=== modified file 'u1db/remote/http_database.py'
37--- u1db/remote/http_database.py 2012-07-19 19:50:58 +0000
38+++ u1db/remote/http_database.py 2012-07-26 19:18:18 +0000
39@@ -104,9 +104,8 @@
40
41 def get_docs(self, doc_ids, check_for_conflicts=True,
42 include_deleted=False):
43- docs = []
44 if not doc_ids:
45- return docs
46+ return
47 doc_ids = ','.join(doc_ids)
48 res, headers = self._request(
49 'GET', ['docs'], {
50@@ -116,8 +115,7 @@
51 doc = self._factory(
52 doc_dict['doc_id'], doc_dict['doc_rev'], doc_dict['content'])
53 doc.has_conflicts = doc_dict['has_conflicts']
54- docs.append(doc)
55- return docs
56+ yield doc
57
58 def create_doc_from_json(self, content, doc_id=None):
59 if doc_id is None:
60
61=== modified file 'u1db/sync.py'
62--- u1db/sync.py 2012-07-06 20:48:40 +0000
63+++ u1db/sync.py 2012-07-26 19:18:18 +0000
64@@ -249,11 +249,13 @@
65 docs_by_gen = izip(
66 docs, (gen for _, gen, _ in changes_to_return),
67 (trans_id for _, _, trans_id in changes_to_return))
68+ _outgoing_trace = [] # for tests
69 for doc, gen, trans_id in docs_by_gen:
70 return_doc_cb(doc, gen, trans_id)
71+ _outgoing_trace.append((doc.doc_id, doc.rev))
72 # for tests
73 self._db._last_exchange_log['return'] = {
74- 'docs': [(d.doc_id, d.rev) for d in docs],
75+ 'docs': _outgoing_trace,
76 'last_gen': self.new_gen
77 }
78
79
80=== modified file 'u1db/tests/test_backends.py'
81--- u1db/tests/test_backends.py 2012-07-24 17:54:55 +0000
82+++ u1db/tests/test_backends.py 2012-07-26 19:18:18 +0000
83@@ -228,13 +228,14 @@
84 doc1 = self.db.create_doc_from_json(simple_doc)
85 doc2 = self.db.create_doc_from_json(nested_doc)
86 self.assertEqual([doc1, doc2],
87- self.db.get_docs([doc1.doc_id, doc2.doc_id]))
88+ list(self.db.get_docs([doc1.doc_id, doc2.doc_id])))
89
90 def test_get_docs_deleted(self):
91 doc1 = self.db.create_doc_from_json(simple_doc)
92 doc2 = self.db.create_doc_from_json(nested_doc)
93 self.db.delete_doc(doc1)
94- self.assertEqual([doc2], self.db.get_docs([doc1.doc_id, doc2.doc_id]))
95+ self.assertEqual([doc2],
96+ list(self.db.get_docs([doc1.doc_id, doc2.doc_id])))
97
98 def test_get_docs_include_deleted(self):
99 doc1 = self.db.create_doc_from_json(simple_doc)
100@@ -242,18 +243,19 @@
101 self.db.delete_doc(doc1)
102 self.assertEqual(
103 [doc1, doc2],
104- self.db.get_docs([doc1.doc_id, doc2.doc_id], include_deleted=True))
105+ list(self.db.get_docs([doc1.doc_id, doc2.doc_id],
106+ include_deleted=True)))
107
108 def test_get_docs_request_ordered(self):
109 doc1 = self.db.create_doc_from_json(simple_doc)
110 doc2 = self.db.create_doc_from_json(nested_doc)
111 self.assertEqual([doc1, doc2],
112- self.db.get_docs([doc1.doc_id, doc2.doc_id]))
113+ list(self.db.get_docs([doc1.doc_id, doc2.doc_id])))
114 self.assertEqual([doc2, doc1],
115- self.db.get_docs([doc2.doc_id, doc1.doc_id]))
116+ list(self.db.get_docs([doc2.doc_id, doc1.doc_id])))
117
118 def test_get_docs_empty_list(self):
119- self.assertEqual([], self.db.get_docs([]))
120+ self.assertEqual([], list(self.db.get_docs([])))
121
122 def test_handles_nested_content(self):
123 doc = self.db.create_doc_from_json(nested_doc)
124@@ -641,7 +643,7 @@
125 self.db._put_doc_if_newer(
126 doc2, save_conflict=True, replica_uid='r', replica_gen=1,
127 replica_trans_id='foo')
128- self.assertEqual([doc2], self.db.get_docs([doc1.doc_id]))
129+ self.assertEqual([doc2], list(self.db.get_docs([doc1.doc_id])))
130
131 def test_get_docs_conflicts_ignored(self):
132 doc1 = self.db.create_doc_from_json(simple_doc)
133@@ -653,8 +655,8 @@
134 no_conflict_doc = self.make_document(doc1.doc_id, 'alternate:1',
135 nested_doc)
136 self.assertEqual([no_conflict_doc, doc2],
137- self.db.get_docs([doc1.doc_id, doc2.doc_id],
138- check_for_conflicts=False))
139+ list(self.db.get_docs([doc1.doc_id, doc2.doc_id],
140+ check_for_conflicts=False)))
141
142 def test_get_doc_conflicts(self):
143 doc = self.db.create_doc_from_json(simple_doc)
144@@ -1718,7 +1720,8 @@
145 replica_trans_id='foo')
146 self.assertTrue(
147 isinstance(
148- self.db.get_docs([doc1.doc_id])[0], TestAlternativeDocument))
149+ list(self.db.get_docs([doc1.doc_id]))[0],
150+ TestAlternativeDocument))
151
152 def test_get_from_index_with_factory(self):
153 self.db.set_document_factory(TestAlternativeDocument)

Subscribers

People subscribed via source and target branches