Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

201 lines (183 sloc) 8.038 kB
<!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>
» &nbsp;<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:&nbsp;
<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>
Jump to Line
Something went wrong with that request. Please try again.