Merge lp:~thisfred/u1db/documentation3 into lp:u1db

Proposed by Eric Casteleijn
Status: Merged
Approved by: Eric Casteleijn
Approved revision: 387
Merged at revision: 385
Proposed branch: lp:~thisfred/u1db/documentation3
Merge into: lp:u1db
Prerequisite: lp:~thisfred/u1db/documentation2
Diff against target: 285 lines (+124/-69)
1 file modified
html-docs/high-level-api.rst (+124/-69)
To merge this branch: bzr merge lp:~thisfred/u1db/documentation3
Reviewer Review Type Date Requested Status
John O'Brien (community) Approve
Review via email: mp+120217@code.launchpad.net

Commit message

Changed more of the index / query documentation into doctests

Description of the change

Changed more of the index / query documentation into doctests

To post a comment you must log in.
Revision history for this message
John O'Brien (jdobrien) :
review: Approve
Revision history for this message
Ubuntu One Auto Pilot (otto-pilot) wrote :

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'html-docs/high-level-api.rst'
2--- html-docs/high-level-api.rst 2012-08-17 19:02:18 +0000
3+++ html-docs/high-level-api.rst 2012-08-17 19:02:18 +0000
4@@ -148,27 +148,41 @@
5 or more fields in the document. A simple example follows: view many more
6 examples here.
7
8-Given a database with the following documents::
9-
10- {"firstname": "John", "surname", "Barnes", "position": "left wing"} ID jb
11- {"firstname": "Jan", "surname", "Molby", "position": "midfield"} ID jm
12- {"firstname": "Alan", "surname", "Hansen", "position": "defence"} ID ah
13- {"firstname": "John", "surname", "Wayne", "position": "filmstar"} ID jw
14+Given a database with the following documents:
15+
16+.. testcode ::
17+
18+ import u1db
19+ db1 = u1db.open("mydb6.u1db", create=True)
20+ jb = db1.create_doc({"firstname": "John", "surname": "Barnes", "position": "left wing"})
21+ jm = db1.create_doc({"firstname": "Jan", "surname": "Molby", "position": "midfield"})
22+ ah = db1.create_doc({"firstname": "Alan", "surname": "Hansen", "position": "defence"})
23+ jw = db1.create_doc({"firstname": "John", "surname": "Wayne", "position": "filmstar"})
24
25 an index expression of ``"firstname"`` will create an index that looks
26 (conceptually) like this
27
28- ====================== ===========
29- index expression value document id
30- ====================== ===========
31+ ====================== ========
32+ index expression value document
33+ ====================== ========
34 Alan ah
35 Jan jm
36 John jb
37 John jw
38- ====================== ===========
39-
40-and that index is created with ``create_index("by-firstname", "firstname")``
41--- that is, create an index with a name and a list of index expressions.
42+ ====================== ========
43+
44+and that index is created with:
45+
46+.. testcode ::
47+
48+ db1.create_index("by-firstname", "firstname")
49+ print(sorted(db1.get_index_keys('by-firstname')))
50+
51+.. testoutput ::
52+
53+ [(u'Alan',), (u'Jan',), (u'John',)]
54+
55+-- that is, create an index with a name and one or more index expressions.
56 (Exactly how to pass the name and the list of index expressions is something
57 specific to each implementation.)
58
59@@ -181,67 +195,87 @@
60
61 **Name a field.** A basic index expression is a dot-delimited list of nesting
62 fieldnames, so the index expression ``field.sub1.sub2`` applied to a document
63-with ID ``doc1`` and content::
64-
65- {
66- "field": {
67- "sub1": {
68- "sub2": "hello"
69- "sub3": "not selected"
70- }
71- }
72- }
73+with below content:
74+
75+.. testcode ::
76+
77+ import u1db
78+ db = u1db.open('mydb7.u1db', create=True)
79+ db.create_index('by-subfield', 'field.sub1.sub2')
80+ doc1 = db.create_doc({"field": {"sub1": {"sub2": "hello", "sub3": "not selected"}}})
81+ print(sorted(db.get_index_keys('by-subfield')))
82+
83+.. testoutput ::
84+
85+ [(u'hello',)]
86
87 gives the index key "hello", and therefore an entry in the index of
88
89- ========= ======
90- Index key doc_id
91- ========= ======
92+ ========= ====
93+ Index key doc
94+ ========= ====
95 hello doc1
96- ========= ======
97+ ========= ====
98
99 **Name a list.** If an index expression names a field whose contents is a list
100 of strings, the document will have multiple entries in the index, one per entry
101 in the list. So, the index expression ``field.tags`` applied to a document with
102-ID ``doc2`` and content::
103-
104- {
105- "field": {
106- "tags": [ "tag1", "tag2", "tag3" ]
107- }
108- }
109+content:
110+
111+.. testcode ::
112+
113+ import u1db
114+ db = u1db.open('mydb8.u1db', create=True)
115+ db.create_index('by-tags', 'field.tags')
116+ doc2 = db.create_doc({"field": {"tags": [ "tag1", "tag2", "tag3" ]}})
117+ print(sorted(db.get_index_keys('by-tags')))
118+
119+.. testoutput ::
120+
121+ [(u'tag1',), (u'tag2',), (u'tag3',)]
122
123 gives index entries
124
125- ========= ======
126- Index key doc_id
127- ========= ======
128+ ========= ====
129+ Index key doc
130+ ========= ====
131 tag1 doc2
132 tag2 doc2
133 tag3 doc2
134- ========= ======
135+ ========= ====
136
137 **Subfields of objects in a list.** If an index expression points at subfields
138 of objects in a list, the document will have multiple entries in the index, one
139 for each object in the list that specifies the denoted subfield. For instance
140 the index expression ``managers.phone_number`` applied to a document
141-with doc_id ``doc3`` and content::
142-
143- {
144- "department": "department of redundancy department",
145- "managers": [
146- {"name": "Mary", "phone_number": "12345"},
147- {"name": "Katherine"},
148- {"name": "Rob", "phone_number": "54321"}]}
149+with content:
150+
151+.. testcode ::
152+
153+ import u1db
154+ db = u1db.open('mydb9.u1db', create=True)
155+ db.create_index('by-phone-number', 'managers.phone_number')
156+ doc3 = db.create_doc(
157+ {"department": "department of redundancy department",
158+ "managers": [
159+ {"name": "Mary", "phone_number": "12345"},
160+ {"name": "Katherine"},
161+ {"name": "Rob", "phone_number": "54321"}]})
162+ print(sorted(db.get_index_keys('by-phone-number')))
163+
164+.. testoutput ::
165+
166+ [(u'12345',), (u'54321',)]
167+
168
169 would give index entries:
170
171- ========= ======
172- Index key doc_id
173- ========= ======
174- 12345 doc2
175- 54321 doc2
176- ========= ======
177+ ========= ====
178+ Index key doc
179+ ========= ====
180+ 12345 doc3
181+ 54321 doc3
182+ ========= ====
183
184 **Transformation functions.** An index expression may be wrapped in any number
185 of transformation functions. A function transforms the result of the contained
186@@ -252,7 +286,7 @@
187 Available transformation functions are:
188
189 * ``lower(index_expression)`` - lowercase the value
190- * ``splitwords(index_expression)`` - split the value on whitespace; will act
191+ * ``split_words(index_expression)`` - split the value on whitespace; will act
192 like a list and add multiple entries to the index
193 * ``number(index_expression, width)`` - takes an integer value, and turns it
194 into a string, left padded with zeroes, to make it at least as wide as
195@@ -263,23 +297,29 @@
196 of an arbitrary number of sub expressions into a single index.
197
198 So, the index expression ``splitwords(lower(field.name))`` applied to
199-a document with ID "doc3" and content::
200-
201- {
202- "field": {
203- "name": "Bruce David Grobbelaar"
204- }
205- }
206+a document with content:
207+
208+.. testcode ::
209+
210+ import u1db
211+ db = u1db.open('mydb10.u1db', create=True)
212+ db.create_index('by-split-lower', 'split_words(lower(field.name))')
213+ doc4 = db.create_doc({"field": {"name": "Bruce David Grobbelaar"}})
214+ print(sorted(db.get_index_keys('by-split-lower')))
215+
216+.. testoutput ::
217+
218+ [(u'bruce',), (u'david',), (u'grobbelaar',)]
219
220 gives index entries
221
222- ========== ======
223- Index key doc_id
224- ========== ======
225+ ========== ====
226+ Index key doc
227+ ========== ====
228 bruce doc3
229 david doc3
230 grobbelaar doc3
231- ========== ======
232+ ========== ====
233
234
235 Querying an index
236@@ -288,16 +328,26 @@
237 Pass an index key or a tuple of index keys (if the index is on multiple fields)
238 to ``get_from_index``; the last index key in each tuple (and *only* the last
239 one) can end with an asterisk, which matches initial substrings. So, querying
240-our ``by-firstname`` index from above::
241-
242- get_from_index("by-firstname", "John")
243-
244+our ``by-firstname`` index from above:
245+
246+.. testcode ::
247+
248+ johns = [d.doc_id for d in db1.get_from_index("by-firstname", "John")]
249+ assert(jw.doc_id in johns)
250+ assert(jb.doc_id in johns)
251+ assert(jm.doc_id not in johns)
252
253 will return the documents with ids: 'jw', 'jb'.
254
255 ``get_from_index("by_firstname", "J*")`` will match all index keys beginning
256 with "J", and so will return the documents with ids: 'jw', 'jb', 'jm'.
257
258+.. testcode ::
259+
260+ js = [d.doc_id for d in db1.get_from_index("by-firstname", "J*")]
261+ assert(jw.doc_id in js)
262+ assert(jb.doc_id in js)
263+ assert(jm.doc_id in js)
264
265 Index functions
266 ^^^^^^^^^^^^^^^
267@@ -306,7 +356,7 @@
268 * :py:meth:`~u1db.Database.delete_index`
269 * :py:meth:`~u1db.Database.get_from_index`
270 * :py:meth:`~u1db.Database.get_range_from_index`
271- * :py:meth:`~u1db.Database.get_keys_from_index`
272+ * :py:meth:`~u1db.Database.get_index_keys`
273 * :py:meth:`~u1db.Database.list_indexes`
274
275 Synchronising
276@@ -354,4 +404,9 @@
277 os.remove(os.path.join(tmp_dir, "mydb3.u1db"))
278 os.remove(os.path.join(tmp_dir, "mydb4.u1db"))
279 os.remove(os.path.join(tmp_dir, "mydb5.u1db"))
280+ os.remove(os.path.join(tmp_dir, "mydb6.u1db"))
281+ os.remove(os.path.join(tmp_dir, "mydb7.u1db"))
282+ os.remove(os.path.join(tmp_dir, "mydb8.u1db"))
283+ os.remove(os.path.join(tmp_dir, "mydb9.u1db"))
284+ os.remove(os.path.join(tmp_dir, "mydb10.u1db"))
285 os.rmdir(tmp_dir)

Subscribers

People subscribed via source and target branches