annotate tutorial.html @ 93:af36b00306d0

Typo fix
author Atul Varma <varmaa@toolness.com>
date Tue, 21 Apr 2009 10:11:07 -0700
parents 093e51c6aa3c
children 86158b61b732
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
70
36e47760ee5b Added a work-in-progress tutorial that auto-generates parts of itself.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
36e47760ee5b Added a work-in-progress tutorial that auto-generates parts of itself.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
2 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
36e47760ee5b Added a work-in-progress tutorial that auto-generates parts of itself.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
3 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
36e47760ee5b Added a work-in-progress tutorial that auto-generates parts of itself.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
4 <head>
36e47760ee5b Added a work-in-progress tutorial that auto-generates parts of itself.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
5 <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
36e47760ee5b Added a work-in-progress tutorial that auto-generates parts of itself.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
6 <link rel="stylesheet" type="text/css" media="all"
36e47760ee5b Added a work-in-progress tutorial that auto-generates parts of itself.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
7 href="css/tutorial.css" />
89
c96ebbbe03d6 Fixed title
Atul Varma <varmaa@toolness.com>
parents: 87
diff changeset
8 <title>BrowserCouch Tutorial</title>
70
36e47760ee5b Added a work-in-progress tutorial that auto-generates parts of itself.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
9 </head>
36e47760ee5b Added a work-in-progress tutorial that auto-generates parts of itself.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
10 <body>
36e47760ee5b Added a work-in-progress tutorial that auto-generates parts of itself.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
11 <div id="content" class="documentation">
86
f65007998121 added more to tutorial intro
Atul Varma <varmaa@toolness.com>
parents: 84
diff changeset
12 <h1>BrowserCouch Tutorial</h1>
70
36e47760ee5b Added a work-in-progress tutorial that auto-generates parts of itself.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
13
81
38abd7bc4886 Added intro to tutorial text.
Atul Varma <varmaa@toolness.com>
parents: 80
diff changeset
14 <p>This is a brief introduction to using the BrowserCouch API and the
38abd7bc4886 Added intro to tutorial text.
Atul Varma <varmaa@toolness.com>
parents: 80
diff changeset
15 MapReduce mechanism. If you haven't already read it, you may want to
82
90854d908f0b Minor change to tutorial
Atul Varma <varmaa@toolness.com>
parents: 81
diff changeset
16 check out the <a class="intra-wiki" href="index.html">introduction</a>
81
38abd7bc4886 Added intro to tutorial text.
Atul Varma <varmaa@toolness.com>
parents: 80
diff changeset
17 to learn more about why this style of querying is being explored as an
38abd7bc4886 Added intro to tutorial text.
Atul Varma <varmaa@toolness.com>
parents: 80
diff changeset
18 alternative to SQL for client-side Web Storage.</p>
38abd7bc4886 Added intro to tutorial text.
Atul Varma <varmaa@toolness.com>
parents: 80
diff changeset
19
86
f65007998121 added more to tutorial intro
Atul Varma <varmaa@toolness.com>
parents: 84
diff changeset
20 <p>It should also be noted that BrowserCouch is by no means "mature"
f65007998121 added more to tutorial intro
Atul Varma <varmaa@toolness.com>
parents: 84
diff changeset
21 software. It currently lacks a lot of CouchDB's features that it ought
f65007998121 added more to tutorial intro
Atul Varma <varmaa@toolness.com>
parents: 84
diff changeset
22 to have, and its API is not stable at all.</p>
f65007998121 added more to tutorial intro
Atul Varma <varmaa@toolness.com>
parents: 84
diff changeset
23
87
ebf8936fc696 More tutorial additions.
Atul Varma <varmaa@toolness.com>
parents: 86
diff changeset
24 <p>Finally, a note about the code examples in this tutorial: they're
ebf8936fc696 More tutorial additions.
Atul Varma <varmaa@toolness.com>
parents: 86
diff changeset
25 actually being executed in your browser, and their output is sometimes
ebf8936fc696 More tutorial additions.
Atul Varma <varmaa@toolness.com>
parents: 86
diff changeset
26 being displayed in this tutorial too. While this helps ensure that the
ebf8936fc696 More tutorial additions.
Atul Varma <varmaa@toolness.com>
parents: 86
diff changeset
27 software is working as intended and also allows for some interactive
ebf8936fc696 More tutorial additions.
Atul Varma <varmaa@toolness.com>
parents: 86
diff changeset
28 learning opportunities, right now it also means that some parts of the
ebf8936fc696 More tutorial additions.
Atul Varma <varmaa@toolness.com>
parents: 86
diff changeset
29 code examples may look a bit unusual. Furthermore, if you see any
ebf8936fc696 More tutorial additions.
Atul Varma <varmaa@toolness.com>
parents: 86
diff changeset
30 conspicuously blank areas in this tutorial, it could be because the
ebf8936fc696 More tutorial additions.
Atul Varma <varmaa@toolness.com>
parents: 86
diff changeset
31 tutorial code crashed&mdash;our apologies if this occurs.</p>
ebf8936fc696 More tutorial additions.
Atul Varma <varmaa@toolness.com>
parents: 86
diff changeset
32
86
f65007998121 added more to tutorial intro
Atul Varma <varmaa@toolness.com>
parents: 84
diff changeset
33 <p>With that out of the way, let's get started.</p>
f65007998121 added more to tutorial intro
Atul Varma <varmaa@toolness.com>
parents: 84
diff changeset
34
87
ebf8936fc696 More tutorial additions.
Atul Varma <varmaa@toolness.com>
parents: 86
diff changeset
35 <h1>Getting Started</h1>
ebf8936fc696 More tutorial additions.
Atul Varma <varmaa@toolness.com>
parents: 86
diff changeset
36
70
36e47760ee5b Added a work-in-progress tutorial that auto-generates parts of itself.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
37 <p>Suppose we want to add offline support for a blog. To get a
36e47760ee5b Added a work-in-progress tutorial that auto-generates parts of itself.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
38 database called <tt>blog-posts</tt> in BrowserCouch, you can use the
36e47760ee5b Added a work-in-progress tutorial that auto-generates parts of itself.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
39 following function:</p>
36e47760ee5b Added a work-in-progress tutorial that auto-generates parts of itself.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
40
36e47760ee5b Added a work-in-progress tutorial that auto-generates parts of itself.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
41 <div class="example-code">
73
8f0b18026782 Changed around a few things in the tutorial.
Atul Varma <varmaa@toolness.com>
parents: 70
diff changeset
42 BrowserCouch.get('blog-posts', onRetrieveCb, new FakeStorage());
70
36e47760ee5b Added a work-in-progress tutorial that auto-generates parts of itself.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
43 </div>
36e47760ee5b Added a work-in-progress tutorial that auto-generates parts of itself.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
44
36e47760ee5b Added a work-in-progress tutorial that auto-generates parts of itself.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
45 <p>It's clear that the first parameter is the name of the database we
36e47760ee5b Added a work-in-progress tutorial that auto-generates parts of itself.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
46 want; the second parameter is the callback that will be passed the
80
042eb025bce5 Added more tutorial text.
Atul Varma <varmaa@toolness.com>
parents: 78
diff changeset
47 database once it's fetched.</p>
042eb025bce5 Added more tutorial text.
Atul Varma <varmaa@toolness.com>
parents: 78
diff changeset
48
042eb025bce5 Added more tutorial text.
Atul Varma <varmaa@toolness.com>
parents: 78
diff changeset
49 <p>The third parameter specifies the engine that will be used to
042eb025bce5 Added more tutorial text.
Atul Varma <varmaa@toolness.com>
parents: 78
diff changeset
50 persistently store our database across browsing sessions. In this case
042eb025bce5 Added more tutorial text.
Atul Varma <varmaa@toolness.com>
parents: 78
diff changeset
51 we're using <tt>FakeStorage</tt>, which just stores everything
042eb025bce5 Added more tutorial text.
Atul Varma <varmaa@toolness.com>
parents: 78
diff changeset
52 non-persistently in memory for the sake of example. We could just as
042eb025bce5 Added more tutorial text.
Atul Varma <varmaa@toolness.com>
parents: 78
diff changeset
53 easily leave out the third parameter to have BrowserCouch figure out
042eb025bce5 Added more tutorial text.
Atul Varma <varmaa@toolness.com>
parents: 78
diff changeset
54 the best storage backend based on our browser's capabilities.</p>
70
36e47760ee5b Added a work-in-progress tutorial that auto-generates parts of itself.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
55
36e47760ee5b Added a work-in-progress tutorial that auto-generates parts of itself.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
56 <p>If the database doesn't already exist, an empty one will be created
36e47760ee5b Added a work-in-progress tutorial that auto-generates parts of itself.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
57 for us. Putting blog posts into the database can be done via
73
8f0b18026782 Changed around a few things in the tutorial.
Atul Varma <varmaa@toolness.com>
parents: 70
diff changeset
58 an <tt>onRetrieveCb()</tt> function like this:</p>
70
36e47760ee5b Added a work-in-progress tutorial that auto-generates parts of itself.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
59
36e47760ee5b Added a work-in-progress tutorial that auto-generates parts of itself.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
60 <div class="example-code">
73
8f0b18026782 Changed around a few things in the tutorial.
Atul Varma <varmaa@toolness.com>
parents: 70
diff changeset
61 function onRetrieveCb(db) {
8f0b18026782 Changed around a few things in the tutorial.
Atul Varma <varmaa@toolness.com>
parents: 70
diff changeset
62 blogDb = db;
8f0b18026782 Changed around a few things in the tutorial.
Atul Varma <varmaa@toolness.com>
parents: 70
diff changeset
63 blogDb.put(
70
36e47760ee5b Added a work-in-progress tutorial that auto-generates parts of itself.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
64 [{id: 0, author: 'Myk', title: 'Burritos', content: 'Burritos are yum.'},
73
8f0b18026782 Changed around a few things in the tutorial.
Atul Varma <varmaa@toolness.com>
parents: 70
diff changeset
65 {id: 1, author: 'Thunder', title: 'Bacon', content: 'I like bacon.'},
8f0b18026782 Changed around a few things in the tutorial.
Atul Varma <varmaa@toolness.com>
parents: 70
diff changeset
66 {id: 2, author: 'Thunder', title: 'Beer', content: 'Beer is good too.'}],
80
042eb025bce5 Added more tutorial text.
Atul Varma <varmaa@toolness.com>
parents: 78
diff changeset
67 onPutCb
70
36e47760ee5b Added a work-in-progress tutorial that auto-generates parts of itself.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
68 );
36e47760ee5b Added a work-in-progress tutorial that auto-generates parts of itself.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
69 };
36e47760ee5b Added a work-in-progress tutorial that auto-generates parts of itself.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
70 </div>
36e47760ee5b Added a work-in-progress tutorial that auto-generates parts of itself.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
71
36e47760ee5b Added a work-in-progress tutorial that auto-generates parts of itself.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
72 <p>Every item we put into our database needs to have an <tt>id</tt>
36e47760ee5b Added a work-in-progress tutorial that auto-generates parts of itself.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
73 attribute, but aside from that, the item can contain any
36e47760ee5b Added a work-in-progress tutorial that auto-generates parts of itself.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
74 JSON-encodable data.</p>
36e47760ee5b Added a work-in-progress tutorial that auto-generates parts of itself.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
75
80
042eb025bce5 Added more tutorial text.
Atul Varma <varmaa@toolness.com>
parents: 78
diff changeset
76 <p>Now that we've put some data into our database, we can play around
042eb025bce5 Added more tutorial text.
Atul Varma <varmaa@toolness.com>
parents: 78
diff changeset
77 with generating views on the data using the <a
042eb025bce5 Added more tutorial text.
Atul Varma <varmaa@toolness.com>
parents: 78
diff changeset
78 href="http://en.wikipedia.org/wiki/MapReduce">MapReduce</a> mechanism.
042eb025bce5 Added more tutorial text.
Atul Varma <varmaa@toolness.com>
parents: 78
diff changeset
79 For instance, here's an ad-hoc view using only the map phase that
042eb025bce5 Added more tutorial text.
Atul Varma <varmaa@toolness.com>
parents: 78
diff changeset
80 organizes all the post titles by author:</p>
70
36e47760ee5b Added a work-in-progress tutorial that auto-generates parts of itself.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
81
36e47760ee5b Added a work-in-progress tutorial that auto-generates parts of itself.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
82 <div class="example-code">
80
042eb025bce5 Added more tutorial text.
Atul Varma <varmaa@toolness.com>
parents: 78
diff changeset
83 function onPutCb() {
73
8f0b18026782 Changed around a few things in the tutorial.
Atul Varma <varmaa@toolness.com>
parents: 70
diff changeset
84 blogDb.view({
70
36e47760ee5b Added a work-in-progress tutorial that auto-generates parts of itself.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
85 map: function(doc, emit) {
73
8f0b18026782 Changed around a few things in the tutorial.
Atul Varma <varmaa@toolness.com>
parents: 70
diff changeset
86 emit(doc.author, doc.title);
70
36e47760ee5b Added a work-in-progress tutorial that auto-generates parts of itself.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
87 },
36e47760ee5b Added a work-in-progress tutorial that auto-generates parts of itself.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
88 finished: function(result) {
36e47760ee5b Added a work-in-progress tutorial that auto-generates parts of itself.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
89 displayInElement(result, 'author-keyed-view');
74
4450be5d1b2f Added reduce() example to the tutorial.
Atul Varma <varmaa@toolness.com>
parents: 73
diff changeset
90 tryAnotherView();
70
36e47760ee5b Added a work-in-progress tutorial that auto-generates parts of itself.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
91 }
36e47760ee5b Added a work-in-progress tutorial that auto-generates parts of itself.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
92 });
36e47760ee5b Added a work-in-progress tutorial that auto-generates parts of itself.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
93 }
36e47760ee5b Added a work-in-progress tutorial that auto-generates parts of itself.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
94 </div>
36e47760ee5b Added a work-in-progress tutorial that auto-generates parts of itself.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
95
36e47760ee5b Added a work-in-progress tutorial that auto-generates parts of itself.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
96 <p>The output placed in the <tt>author-keyed-view</tt> element is:</p>
36e47760ee5b Added a work-in-progress tutorial that auto-generates parts of itself.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
97
83
b95fc8f8a344 Added section on using findRow().
Atul Varma <varmaa@toolness.com>
parents: 82
diff changeset
98 <div class="example-output" id="author-keyed-view"></div>
70
36e47760ee5b Added a work-in-progress tutorial that auto-generates parts of itself.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
99
80
042eb025bce5 Added more tutorial text.
Atul Varma <varmaa@toolness.com>
parents: 78
diff changeset
100 <p>As you can see, BrowserCouch essentially iterated over all of the
042eb025bce5 Added more tutorial text.
Atul Varma <varmaa@toolness.com>
parents: 78
diff changeset
101 blog posts, passing each one to <tt>map()</tt>, along with an
042eb025bce5 Added more tutorial text.
Atul Varma <varmaa@toolness.com>
parents: 78
diff changeset
102 arbitrary function called <tt>emit()</tt>. The <tt>map()</tt>
042eb025bce5 Added more tutorial text.
Atul Varma <varmaa@toolness.com>
parents: 78
diff changeset
103 function then emitted key-value pairs which show up in the view. It's
042eb025bce5 Added more tutorial text.
Atul Varma <varmaa@toolness.com>
parents: 78
diff changeset
104 worth noting that <tt>map()</tt> can call <tt>emit()</tt> as much as
042eb025bce5 Added more tutorial text.
Atul Varma <varmaa@toolness.com>
parents: 78
diff changeset
105 it wants to; each call will add a new row to the view.</p>
042eb025bce5 Added more tutorial text.
Atul Varma <varmaa@toolness.com>
parents: 78
diff changeset
106
74
4450be5d1b2f Added reduce() example to the tutorial.
Atul Varma <varmaa@toolness.com>
parents: 73
diff changeset
107 <p>We could also try creating another view that adds a
4450be5d1b2f Added reduce() example to the tutorial.
Atul Varma <varmaa@toolness.com>
parents: 73
diff changeset
108 <tt>reduce()</tt> function to group together the blog post titles with
4450be5d1b2f Added reduce() example to the tutorial.
Atul Varma <varmaa@toolness.com>
parents: 73
diff changeset
109 the authors:</p>
4450be5d1b2f Added reduce() example to the tutorial.
Atul Varma <varmaa@toolness.com>
parents: 73
diff changeset
110
4450be5d1b2f Added reduce() example to the tutorial.
Atul Varma <varmaa@toolness.com>
parents: 73
diff changeset
111 <div class="example-code">
4450be5d1b2f Added reduce() example to the tutorial.
Atul Varma <varmaa@toolness.com>
parents: 73
diff changeset
112 function tryAnotherView() {
4450be5d1b2f Added reduce() example to the tutorial.
Atul Varma <varmaa@toolness.com>
parents: 73
diff changeset
113 blogDb.view({
4450be5d1b2f Added reduce() example to the tutorial.
Atul Varma <varmaa@toolness.com>
parents: 73
diff changeset
114 map: function(doc, emit) {
4450be5d1b2f Added reduce() example to the tutorial.
Atul Varma <varmaa@toolness.com>
parents: 73
diff changeset
115 emit(doc.author, doc.title);
4450be5d1b2f Added reduce() example to the tutorial.
Atul Varma <varmaa@toolness.com>
parents: 73
diff changeset
116 },
4450be5d1b2f Added reduce() example to the tutorial.
Atul Varma <varmaa@toolness.com>
parents: 73
diff changeset
117 reduce: function(keys, values) {
4450be5d1b2f Added reduce() example to the tutorial.
Atul Varma <varmaa@toolness.com>
parents: 73
diff changeset
118 return values;
4450be5d1b2f Added reduce() example to the tutorial.
Atul Varma <varmaa@toolness.com>
parents: 73
diff changeset
119 },
4450be5d1b2f Added reduce() example to the tutorial.
Atul Varma <varmaa@toolness.com>
parents: 73
diff changeset
120 finished: function(result) {
4450be5d1b2f Added reduce() example to the tutorial.
Atul Varma <varmaa@toolness.com>
parents: 73
diff changeset
121 displayInElement(result, 'author-titles-view');
84
dbe68e5af673 Shortened tutorial code a bit.
Atul Varma <varmaa@toolness.com>
parents: 83
diff changeset
122 findRows(result);
74
4450be5d1b2f Added reduce() example to the tutorial.
Atul Varma <varmaa@toolness.com>
parents: 73
diff changeset
123 }
4450be5d1b2f Added reduce() example to the tutorial.
Atul Varma <varmaa@toolness.com>
parents: 73
diff changeset
124 });
4450be5d1b2f Added reduce() example to the tutorial.
Atul Varma <varmaa@toolness.com>
parents: 73
diff changeset
125 }
4450be5d1b2f Added reduce() example to the tutorial.
Atul Varma <varmaa@toolness.com>
parents: 73
diff changeset
126 </div>
4450be5d1b2f Added reduce() example to the tutorial.
Atul Varma <varmaa@toolness.com>
parents: 73
diff changeset
127
4450be5d1b2f Added reduce() example to the tutorial.
Atul Varma <varmaa@toolness.com>
parents: 73
diff changeset
128 <p>The output is as follows:</p>
4450be5d1b2f Added reduce() example to the tutorial.
Atul Varma <varmaa@toolness.com>
parents: 73
diff changeset
129
83
b95fc8f8a344 Added section on using findRow().
Atul Varma <varmaa@toolness.com>
parents: 82
diff changeset
130 <div class="example-output" id="author-titles-view"></div>
74
4450be5d1b2f Added reduce() example to the tutorial.
Atul Varma <varmaa@toolness.com>
parents: 73
diff changeset
131
80
042eb025bce5 Added more tutorial text.
Atul Varma <varmaa@toolness.com>
parents: 78
diff changeset
132 <p>The <tt>reduce()</tt> mechanism is a bit harder to
042eb025bce5 Added more tutorial text.
Atul Varma <varmaa@toolness.com>
parents: 78
diff changeset
133 understand. Essentially, BrowserCouch takes all the rows generated by
042eb025bce5 Added more tutorial text.
Atul Varma <varmaa@toolness.com>
parents: 78
diff changeset
134 <tt>map()</tt> and generates a new list of key-value rows, where the
042eb025bce5 Added more tutorial text.
Atul Varma <varmaa@toolness.com>
parents: 78
diff changeset
135 value of each row is the list of all values that match the row's key.
042eb025bce5 Added more tutorial text.
Atul Varma <varmaa@toolness.com>
parents: 78
diff changeset
136 This explains what the <tt>values</tt> argument passed to
042eb025bce5 Added more tutorial text.
Atul Varma <varmaa@toolness.com>
parents: 78
diff changeset
137 <tt>reduce()</tt> is.</p>
042eb025bce5 Added more tutorial text.
Atul Varma <varmaa@toolness.com>
parents: 78
diff changeset
138
87
ebf8936fc696 More tutorial additions.
Atul Varma <varmaa@toolness.com>
parents: 86
diff changeset
139 <p>The <tt>keys</tt> argument is a list of 2-tuples, the first of
80
042eb025bce5 Added more tutorial text.
Atul Varma <varmaa@toolness.com>
parents: 78
diff changeset
140 which is the key, and the second of which is the document id that
042eb025bce5 Added more tutorial text.
Atul Varma <varmaa@toolness.com>
parents: 78
diff changeset
141 emitted the key during the map phase.</p>
042eb025bce5 Added more tutorial text.
Atul Varma <varmaa@toolness.com>
parents: 78
diff changeset
142
042eb025bce5 Added more tutorial text.
Atul Varma <varmaa@toolness.com>
parents: 78
diff changeset
143 <p>The <tt>reduce()</tt> function is called for each unique key, and
042eb025bce5 Added more tutorial text.
Atul Varma <varmaa@toolness.com>
parents: 78
diff changeset
144 its return value is the value for its key in the final view.</p>
042eb025bce5 Added more tutorial text.
Atul Varma <varmaa@toolness.com>
parents: 78
diff changeset
145
83
b95fc8f8a344 Added section on using findRow().
Atul Varma <varmaa@toolness.com>
parents: 82
diff changeset
146 <p>Once you've got a view, you can use the view's <tt>findRow()</tt>
93
af36b00306d0 Typo fix
Atul Varma <varmaa@toolness.com>
parents: 91
diff changeset
147 method to find the first row whose key matches (or is closest to) the
83
b95fc8f8a344 Added section on using findRow().
Atul Varma <varmaa@toolness.com>
parents: 82
diff changeset
148 one you provide. For example:</p>
b95fc8f8a344 Added section on using findRow().
Atul Varma <varmaa@toolness.com>
parents: 82
diff changeset
149
b95fc8f8a344 Added section on using findRow().
Atul Varma <varmaa@toolness.com>
parents: 82
diff changeset
150 <div class="example-code">
84
dbe68e5af673 Shortened tutorial code a bit.
Atul Varma <varmaa@toolness.com>
parents: 83
diff changeset
151 function findRows(result) {
dbe68e5af673 Shortened tutorial code a bit.
Atul Varma <varmaa@toolness.com>
parents: 83
diff changeset
152 var rowIndex = result.findRow('Thunder');
dbe68e5af673 Shortened tutorial code a bit.
Atul Varma <varmaa@toolness.com>
parents: 83
diff changeset
153 displayInElement(result.rows[rowIndex], 'author-find-row-view');
dbe68e5af673 Shortened tutorial code a bit.
Atul Varma <varmaa@toolness.com>
parents: 83
diff changeset
154 tryMyView();
83
b95fc8f8a344 Added section on using findRow().
Atul Varma <varmaa@toolness.com>
parents: 82
diff changeset
155 }
b95fc8f8a344 Added section on using findRow().
Atul Varma <varmaa@toolness.com>
parents: 82
diff changeset
156 </div>
b95fc8f8a344 Added section on using findRow().
Atul Varma <varmaa@toolness.com>
parents: 82
diff changeset
157
b95fc8f8a344 Added section on using findRow().
Atul Varma <varmaa@toolness.com>
parents: 82
diff changeset
158 <p>The output for this one is:</p>
b95fc8f8a344 Added section on using findRow().
Atul Varma <varmaa@toolness.com>
parents: 82
diff changeset
159
b95fc8f8a344 Added section on using findRow().
Atul Varma <varmaa@toolness.com>
parents: 82
diff changeset
160 <div class="example-output" id="author-find-row-view"></div>
b95fc8f8a344 Added section on using findRow().
Atul Varma <varmaa@toolness.com>
parents: 82
diff changeset
161
78
17ce8b6be452 Added a 'now you try' section and a 'where to go from here' section to the tutorial.
Atul Varma <varmaa@toolness.com>
parents: 74
diff changeset
162 <h1>Now You Try!</h1>
17ce8b6be452 Added a 'now you try' section and a 'where to go from here' section to the tutorial.
Atul Varma <varmaa@toolness.com>
parents: 74
diff changeset
163
84
dbe68e5af673 Shortened tutorial code a bit.
Atul Varma <varmaa@toolness.com>
parents: 83
diff changeset
164 <p>If your eyes are crossed right now, no worries&mdash;most people
dbe68e5af673 Shortened tutorial code a bit.
Atul Varma <varmaa@toolness.com>
parents: 83
diff changeset
165 take a long time to understand exactly what MapReduce is doing. That
dbe68e5af673 Shortened tutorial code a bit.
Atul Varma <varmaa@toolness.com>
parents: 83
diff changeset
166 said, the easiest way to understand how MapReduce works is just to
dbe68e5af673 Shortened tutorial code a bit.
Atul Varma <varmaa@toolness.com>
parents: 83
diff changeset
167 play around with creating your own view.</p>
80
042eb025bce5 Added more tutorial text.
Atul Varma <varmaa@toolness.com>
parents: 78
diff changeset
168
91
093e51c6aa3c Minor rewording
Atul Varma <varmaa@toolness.com>
parents: 89
diff changeset
169 <p>You can use the text field below to do just that. Just press the
093e51c6aa3c Minor rewording
Atul Varma <varmaa@toolness.com>
parents: 89
diff changeset
170 tab key when you're done making changes to recompute the view.</p>
78
17ce8b6be452 Added a 'now you try' section and a 'where to go from here' section to the tutorial.
Atul Varma <varmaa@toolness.com>
parents: 74
diff changeset
171
17ce8b6be452 Added a 'now you try' section and a 'where to go from here' section to the tutorial.
Atul Varma <varmaa@toolness.com>
parents: 74
diff changeset
172 <textarea class="example-code try-code">
17ce8b6be452 Added a 'now you try' section and a 'where to go from here' section to the tutorial.
Atul Varma <varmaa@toolness.com>
parents: 74
diff changeset
173 function tryMyView() {
17ce8b6be452 Added a 'now you try' section and a 'where to go from here' section to the tutorial.
Atul Varma <varmaa@toolness.com>
parents: 74
diff changeset
174 blogDb.view({
17ce8b6be452 Added a 'now you try' section and a 'where to go from here' section to the tutorial.
Atul Varma <varmaa@toolness.com>
parents: 74
diff changeset
175 map: function(doc, emit) {
17ce8b6be452 Added a 'now you try' section and a 'where to go from here' section to the tutorial.
Atul Varma <varmaa@toolness.com>
parents: 74
diff changeset
176 emit(doc.author, doc.title);
17ce8b6be452 Added a 'now you try' section and a 'where to go from here' section to the tutorial.
Atul Varma <varmaa@toolness.com>
parents: 74
diff changeset
177 },
17ce8b6be452 Added a 'now you try' section and a 'where to go from here' section to the tutorial.
Atul Varma <varmaa@toolness.com>
parents: 74
diff changeset
178 reduce: function(keys, values) {
17ce8b6be452 Added a 'now you try' section and a 'where to go from here' section to the tutorial.
Atul Varma <varmaa@toolness.com>
parents: 74
diff changeset
179 return values;
17ce8b6be452 Added a 'now you try' section and a 'where to go from here' section to the tutorial.
Atul Varma <varmaa@toolness.com>
parents: 74
diff changeset
180 },
17ce8b6be452 Added a 'now you try' section and a 'where to go from here' section to the tutorial.
Atul Varma <varmaa@toolness.com>
parents: 74
diff changeset
181 finished: function(result) {
17ce8b6be452 Added a 'now you try' section and a 'where to go from here' section to the tutorial.
Atul Varma <varmaa@toolness.com>
parents: 74
diff changeset
182 displayInElement(result, 'try-my-view');
17ce8b6be452 Added a 'now you try' section and a 'where to go from here' section to the tutorial.
Atul Varma <varmaa@toolness.com>
parents: 74
diff changeset
183 }
17ce8b6be452 Added a 'now you try' section and a 'where to go from here' section to the tutorial.
Atul Varma <varmaa@toolness.com>
parents: 74
diff changeset
184 });
17ce8b6be452 Added a 'now you try' section and a 'where to go from here' section to the tutorial.
Atul Varma <varmaa@toolness.com>
parents: 74
diff changeset
185 }
17ce8b6be452 Added a 'now you try' section and a 'where to go from here' section to the tutorial.
Atul Varma <varmaa@toolness.com>
parents: 74
diff changeset
186 </textarea>
17ce8b6be452 Added a 'now you try' section and a 'where to go from here' section to the tutorial.
Atul Varma <varmaa@toolness.com>
parents: 74
diff changeset
187
17ce8b6be452 Added a 'now you try' section and a 'where to go from here' section to the tutorial.
Atul Varma <varmaa@toolness.com>
parents: 74
diff changeset
188 <p>Here's the output to the above view:</p>
17ce8b6be452 Added a 'now you try' section and a 'where to go from here' section to the tutorial.
Atul Varma <varmaa@toolness.com>
parents: 74
diff changeset
189
17ce8b6be452 Added a 'now you try' section and a 'where to go from here' section to the tutorial.
Atul Varma <varmaa@toolness.com>
parents: 74
diff changeset
190 <div class="example-output" id="try-my-view"></div>
17ce8b6be452 Added a 'now you try' section and a 'where to go from here' section to the tutorial.
Atul Varma <varmaa@toolness.com>
parents: 74
diff changeset
191
17ce8b6be452 Added a 'now you try' section and a 'where to go from here' section to the tutorial.
Atul Varma <varmaa@toolness.com>
parents: 74
diff changeset
192 <h1>Where To Go From Here</h1>
17ce8b6be452 Added a 'now you try' section and a 'where to go from here' section to the tutorial.
Atul Varma <varmaa@toolness.com>
parents: 74
diff changeset
193
17ce8b6be452 Added a 'now you try' section and a 'where to go from here' section to the tutorial.
Atul Varma <varmaa@toolness.com>
parents: 74
diff changeset
194 <p>There's features in the API that aren't covered here, so check out
17ce8b6be452 Added a 'now you try' section and a 'where to go from here' section to the tutorial.
Atul Varma <varmaa@toolness.com>
parents: 74
diff changeset
195 the check out the <a class="intra-wiki"
87
ebf8936fc696 More tutorial additions.
Atul Varma <varmaa@toolness.com>
parents: 86
diff changeset
196 href="index.html#js/tests.js">test suite's annotated source code</a>
ebf8936fc696 More tutorial additions.
Atul Varma <varmaa@toolness.com>
parents: 86
diff changeset
197 for more examples.</p>
78
17ce8b6be452 Added a 'now you try' section and a 'where to go from here' section to the tutorial.
Atul Varma <varmaa@toolness.com>
parents: 74
diff changeset
198
70
36e47760ee5b Added a work-in-progress tutorial that auto-generates parts of itself.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
199 <script src="js/ext/jquery.js"></script>
36e47760ee5b Added a work-in-progress tutorial that auto-generates parts of itself.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
200 <script src="js/browser-couch.js"></script>
36e47760ee5b Added a work-in-progress tutorial that auto-generates parts of itself.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
201 <script src="js/tutorial.js"></script>
36e47760ee5b Added a work-in-progress tutorial that auto-generates parts of itself.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
202 </body>
36e47760ee5b Added a work-in-progress tutorial that auto-generates parts of itself.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
203 </html>