forked from janl/jscouch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jscouch.html
200 lines (183 loc) · 7.85 KB
/
jscouch.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<!--
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy
of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
License for the specific language governing permissions and limitations under
the License.
Initial implementation:
* http://www.mudynamics.com
* http://labs.mudynamics.com
* http://www.pcapr.net
//-->
<title>Interactive CouchDB | Mu Dynamics</title>
<meta name="robots" content="index, follow">
<meta name="description" content="A JavaScript-based CouchDB emulator that implements key CouchDB concepts like collation, map/reduce and incremental reduce. This also serves as a 2 minute CouchDB tutorial.">
<meta http-equiv="content-type" content="text/xhtml; charset=utf-8" />
<link rel="stylesheet" href="jscouch.css" type="text/css" media="screen" charset="utf-8">
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="json2.js"></script>
<script type="text/javascript" src="jscouch.couchdb.js"></script>
<script type="text/javascript" src="jscouch.documents.js"></script>
<script type="text/javascript" src="jscouch.collator.js"></script>
<script type="text/javascript" src="jscouch.js"></script>
</head>
<body>
<h2>Interactive CouchDB</h2>
<div class="credits">
created by
<img class="icon" src="http://www.pcapr.net/static/image/favicon.png">
<a href="http://www.mudynamics.com">Mu Dynamics</a>
for this <a href="http://labs.mudynamics.com/2009/04/03/interactive-couchdb/">blog</a>.
</div>
<div class="intro">
This is a CouchDB emulator/visualizer written in 100% JavaScript, which
implements key concepts like collation, map/reduce and incremental reduce. It
also acts as a 2-minute CouchDB tutorial. No documents were harmed in this
process.
</div>
<div class="icouch">
<fieldset id="tabs">
<legend>
<a id="documents" href="javascript:void(0)" class="active">documents</a>
<a id="map" href="javascript:void(0)" class="">map/reduce</a>
</legend>
<div id="pane">
<div id="documents" style="">
<div>
<p>
<a href="http://couchdb.apache.org/">CouchDB</a> stores
schema-less JSON documents in a flat namespace.
Each document has an <em>id</em> that's either auto generated
or provided by the user. The JSON documents can be as simple
or as complex as it can be. You can think of each document as
an instance of a particular <strong>class</strong>. CouchDB
doesn't know or track relationships between documents. It's up
to you to keep track of that.</p> <p>The sample DB below
contains information about a bunch of pictures uploaded and
tagged by different users with some meta data about each
picture.
</p>
</div>
<table id="docs" width="100%">
<colgroup>
<col span="1">
<col width="100%">
</colgroup>
<thead>
<tr>
<td>id</td>
<td>doc</td>
</tr>
</thead>
<tbody>
</tbody>
</table>
» <a id="add" href="javascript:void(0)">add</a>
<div id="doc" style="display: none">
<textarea id="doc" rows="4" cols="60"></textarea>
<div>
<input type="button" value="apply">
<span class="warn" style="color: red"></span>
</div>
</div>
</div>
<div id="map" style="display: none; ">
<div>
<p>
Now that we have the documents in place, we can use the
<strong>map</strong> function to generate complex key
value pairs (sorted by key) and then
<strong>reduce</strong> the values corresponding to each
unique key into either a simple or complex value.
</p>
<p>
This is a very key concept so it's worth reiterating. The
key/value pairs generated by the <strong>map</strong>
function are stored in a BTree and are updated incrementally
as documents are added or updated. The query against a
view is simply slicing this sorted key space, potentially
<strong>reducing</strong> the slice and returning the
values. CouchDB will cache the reduced values in the BTree
as well and will try and reuse these cached values during
the key-space slicing, a.k.a. query.
</p>
<p>
Try and learn:
<select id="examples">
<option>choose one</option>
</select>
</p>
<div id="blurb"></div>
</div>
<table style="margin-top: 10px" width="100%">
<tbody>
<tr>
<td width="50%">
<div>
<div>function(<strong>doc</strong>) {</div>
<div>
<textarea id="map" rows="6">emit(null, null);</textarea>
</div>
<div>}</div>
</div>
</td>
<td width="50%">
<div>
<div>function(keys, values, <strong>rereduce</strong>) {</div>
<div>
<textarea id="reduce" rows="6"></textarea>
</div>
<div>}</div>
</div>
</td>
</tr>
</tbody>
</table>
<div>
»
<a id="execute" href="javascript:void(0)">execute</a>
<span class="warn" style="color: red"></span>
</div>
<table id="bottom" width="100%" style="display:none">
<tbody>
<tr>
<td valign="top" width="50%">
<table id="results"></table>
</td>
<td valign="top" width="50%">
<div>
query:
<select id="view">
<option>all</option>
<option>key</option>
<option>range</option>
</select>
<span id="views">
<span id="all">
</span>
<span id="key" style="display:none">
<input id="key" type="text" size="12">
</span>
<span id="range" style="display:none">
between <input id="startkey" type="text" size="12">
and <input id="endkey" type="text" size="12">
</span>
</span></div>
<div id="response"></div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</fieldset>
</div>
</body>
</html>