Merge lp:~chipaca/u1db/nonexistent_index_querying_is_frowned_upon into lp:u1db

Proposed by John Lenton
Status: Merged
Approved by: John Lenton
Approved revision: 302
Merged at revision: 302
Proposed branch: lp:~chipaca/u1db/nonexistent_index_querying_is_frowned_upon
Merge into: lp:u1db
Diff against target: 146 lines (+39/-3)
7 files modified
include/u1db/u1db.h (+1/-0)
src/u1db_query.c (+6/-0)
u1db/backends/inmemory.py (+8/-2)
u1db/backends/sqlite_backend.py (+7/-1)
u1db/errors.py (+4/-0)
u1db/tests/c_backend_wrapper.pyx (+3/-0)
u1db/tests/test_backends.py (+10/-0)
To merge this branch: bzr merge lp:~chipaca/u1db/nonexistent_index_querying_is_frowned_upon
Reviewer Review Type Date Requested Status
Eric Casteleijn (community) Approve
Review via email: mp+106640@code.launchpad.net

Description of the change

get_from_index and get_index_keys now consistently die with "index does not exist" if the index does not exist.

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

looks good, tests pass

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'include/u1db/u1db.h'
2--- include/u1db/u1db.h 2012-05-20 15:50:51 +0000
3+++ include/u1db/u1db.h 2012-05-21 15:08:18 +0000
4@@ -66,6 +66,7 @@
5 #define U1DB_MISSING_FIELD_SPECIFIER -15
6 #define U1DB_INVALID_FIELD_SPECIFIER -16
7 #define U1DB_DUPLICATE_INDEX_NAME -17
8+#define U1DB_INDEX_DOES_NOT_EXIST -18
9 #define U1DB_INTERNAL_ERROR -999
10
11 // Used by put_doc_if_newer
12
13=== modified file 'src/u1db_query.c'
14--- src/u1db_query.c 2012-05-15 16:55:08 +0000
15+++ src/u1db_query.c 2012-05-21 15:08:18 +0000
16@@ -440,6 +440,9 @@
17 SQLITE_TRANSIENT);
18 if (status != SQLITE_OK) { goto finish; }
19 status = sqlite3_step(statement);
20+ if (status == SQLITE_DONE) {
21+ status = U1DB_INDEX_DOES_NOT_EXIST;
22+ }
23 while (status == SQLITE_ROW) {
24 offset = sqlite3_column_int(statement, 0);
25 field = (char*)sqlite3_column_text(statement, 1);
26@@ -627,6 +630,9 @@
27 goto finish;
28 }
29 status = sqlite3_step(statement);
30+ if (status == SQLITE_DONE) {
31+ status = U1DB_INDEX_DOES_NOT_EXIST;
32+ }
33 while (status == SQLITE_ROW) {
34 key = (char*)sqlite3_column_text(statement, 0);
35 if ((status = cb(context, key)) != U1DB_OK)
36
37=== modified file 'u1db/backends/inmemory.py'
38--- u1db/backends/inmemory.py 2012-05-20 15:50:51 +0000
39+++ u1db/backends/inmemory.py 2012-05-21 15:08:18 +0000
40@@ -189,7 +189,10 @@
41 return definitions
42
43 def get_from_index(self, index_name, key_values):
44- index = self._indexes[index_name]
45+ try:
46+ index = self._indexes[index_name]
47+ except KeyError:
48+ raise errors.IndexDoesNotExist
49 doc_ids = index.lookup(key_values)
50 result = []
51 for doc_id in doc_ids:
52@@ -198,7 +201,10 @@
53 return result
54
55 def get_index_keys(self, index_name):
56- index = self._indexes[index_name]
57+ try:
58+ index = self._indexes[index_name]
59+ except KeyError:
60+ raise errors.IndexDoesNotExist
61 return list(set(index.keys()))
62
63 def whats_changed(self, old_generation=0):
64
65=== modified file 'u1db/backends/sqlite_backend.py'
66--- u1db/backends/sqlite_backend.py 2012-05-20 15:50:51 +0000
67+++ u1db/backends/sqlite_backend.py 2012-05-21 15:08:18 +0000
68@@ -535,7 +535,11 @@
69 c = self._db_handle.cursor()
70 c.execute("SELECT field FROM index_definitions"
71 " WHERE name = ? ORDER BY offset", (index_name,))
72- return [x[0] for x in c.fetchall()]
73+ fields = [x[0] for x in c.fetchall()]
74+ if not fields:
75+ raise errors.IndexDoesNotExist
76+ return fields
77+
78
79 @staticmethod
80 def _transform_glob(value, escape_char='.'):
81@@ -617,6 +621,8 @@
82 raise dbapi2.OperationalError(str(e) +
83 '\nstatement: %s\nargs: %s\n' % (SQL_INDEX_KEYS, (index,)))
84 res = c.fetchall()
85+ if not res:
86+ raise errors.IndexDoesNotExist
87 return [r[0] for r in res]
88
89 def delete_index(self, index_name):
90
91=== modified file 'u1db/errors.py'
92--- u1db/errors.py 2012-05-20 00:50:57 +0000
93+++ u1db/errors.py 2012-05-21 15:08:18 +0000
94@@ -77,6 +77,10 @@
95 """The index definition cannot be parsed."""
96
97
98+class IndexDoesNotExist(U1DBError):
99+ """No index of that name exists."""
100+
101+
102 class Unauthorized(U1DBError):
103 """Request wasn't authorized properly."""
104
105
106=== modified file 'u1db/tests/c_backend_wrapper.pyx'
107--- u1db/tests/c_backend_wrapper.pyx 2012-05-20 15:50:51 +0000
108+++ u1db/tests/c_backend_wrapper.pyx 2012-05-21 15:08:18 +0000
109@@ -118,6 +118,7 @@
110 int U1DB_INVALID_VALUE_FOR_INDEX
111 int U1DB_BROKEN_SYNC_STREAM
112 int U1DB_DUPLICATE_INDEX_NAME
113+ int U1DB_INDEX_DOES_NOT_EXIST
114 int U1DB_INTERNAL_ERROR
115
116 int U1DB_INSERTED
117@@ -567,6 +568,8 @@
118 raise errors.ConflictedDoc()
119 if status == U1DB_DUPLICATE_INDEX_NAME:
120 raise errors.IndexNameTakenError()
121+ if status == U1DB_INDEX_DOES_NOT_EXIST:
122+ raise errors.IndexDoesNotExist
123 raise RuntimeError('%s (status: %s)' % (context, status))
124
125
126
127=== modified file 'u1db/tests/test_backends.py'
128--- u1db/tests/test_backends.py 2012-05-20 15:50:51 +0000
129+++ u1db/tests/test_backends.py 2012-05-21 15:08:18 +0000
130@@ -723,6 +723,16 @@
131 self.assertEqual([doc],
132 self.db.get_from_index('test-idx', [('value', 'value2')]))
133
134+ def test_get_from_index_fails_if_no_index(self):
135+ self.assertRaises(errors.IndexDoesNotExist,
136+ self.db.get_from_index,
137+ 'foo', [])
138+
139+ def test_get_index_keys_fails_if_no_index(self):
140+ self.assertRaises(errors.IndexDoesNotExist,
141+ self.db.get_index_keys,
142+ 'foo')
143+
144 def test_put_updates_index(self):
145 doc = self.db.create_doc(simple_doc)
146 self.db.create_index('test-idx', ['key'])

Subscribers

People subscribed via source and target branches