annotate js/browser-couch.js @ 59:f36a50a6042c

Added really basic skeletal documentation for browser-couch.js.
author Atul Varma <varmaa@toolness.com>
date Wed, 15 Apr 2009 09:10:37 -0700
parents aa013581aeca
children 7fff33e9786b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
4c91125795dc Added license block.
Atul Varma <varmaa@toolness.com>
parents: 2
diff changeset
1 /* ***** BEGIN LICENSE BLOCK *****
4c91125795dc Added license block.
Atul Varma <varmaa@toolness.com>
parents: 2
diff changeset
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4c91125795dc Added license block.
Atul Varma <varmaa@toolness.com>
parents: 2
diff changeset
3 *
4c91125795dc Added license block.
Atul Varma <varmaa@toolness.com>
parents: 2
diff changeset
4 * The contents of this file are subject to the Mozilla Public License Version
4c91125795dc Added license block.
Atul Varma <varmaa@toolness.com>
parents: 2
diff changeset
5 * 1.1 (the "License"); you may not use this file except in compliance with
4c91125795dc Added license block.
Atul Varma <varmaa@toolness.com>
parents: 2
diff changeset
6 * the License. You may obtain a copy of the License at
4c91125795dc Added license block.
Atul Varma <varmaa@toolness.com>
parents: 2
diff changeset
7 * http://www.mozilla.org/MPL/
4c91125795dc Added license block.
Atul Varma <varmaa@toolness.com>
parents: 2
diff changeset
8 *
4c91125795dc Added license block.
Atul Varma <varmaa@toolness.com>
parents: 2
diff changeset
9 * Software distributed under the License is distributed on an "AS IS" basis,
4c91125795dc Added license block.
Atul Varma <varmaa@toolness.com>
parents: 2
diff changeset
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
4c91125795dc Added license block.
Atul Varma <varmaa@toolness.com>
parents: 2
diff changeset
11 * for the specific language governing rights and limitations under the
4c91125795dc Added license block.
Atul Varma <varmaa@toolness.com>
parents: 2
diff changeset
12 * License.
4c91125795dc Added license block.
Atul Varma <varmaa@toolness.com>
parents: 2
diff changeset
13 *
4c91125795dc Added license block.
Atul Varma <varmaa@toolness.com>
parents: 2
diff changeset
14 * The Original Code is Ubiquity.
4c91125795dc Added license block.
Atul Varma <varmaa@toolness.com>
parents: 2
diff changeset
15 *
4c91125795dc Added license block.
Atul Varma <varmaa@toolness.com>
parents: 2
diff changeset
16 * The Initial Developer of the Original Code is Mozilla.
4c91125795dc Added license block.
Atul Varma <varmaa@toolness.com>
parents: 2
diff changeset
17 * Portions created by the Initial Developer are Copyright (C) 2007
4c91125795dc Added license block.
Atul Varma <varmaa@toolness.com>
parents: 2
diff changeset
18 * the Initial Developer. All Rights Reserved.
4c91125795dc Added license block.
Atul Varma <varmaa@toolness.com>
parents: 2
diff changeset
19 *
4c91125795dc Added license block.
Atul Varma <varmaa@toolness.com>
parents: 2
diff changeset
20 * Contributor(s):
4c91125795dc Added license block.
Atul Varma <varmaa@toolness.com>
parents: 2
diff changeset
21 * Atul Varma <atul@mozilla.com>
4c91125795dc Added license block.
Atul Varma <varmaa@toolness.com>
parents: 2
diff changeset
22 *
4c91125795dc Added license block.
Atul Varma <varmaa@toolness.com>
parents: 2
diff changeset
23 * Alternatively, the contents of this file may be used under the terms of
4c91125795dc Added license block.
Atul Varma <varmaa@toolness.com>
parents: 2
diff changeset
24 * either the GNU General Public License Version 2 or later (the "GPL"), or
4c91125795dc Added license block.
Atul Varma <varmaa@toolness.com>
parents: 2
diff changeset
25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
4c91125795dc Added license block.
Atul Varma <varmaa@toolness.com>
parents: 2
diff changeset
26 * in which case the provisions of the GPL or the LGPL are applicable instead
4c91125795dc Added license block.
Atul Varma <varmaa@toolness.com>
parents: 2
diff changeset
27 * of those above. If you wish to allow use of your version of this file only
4c91125795dc Added license block.
Atul Varma <varmaa@toolness.com>
parents: 2
diff changeset
28 * under the terms of either the GPL or the LGPL, and not to allow others to
4c91125795dc Added license block.
Atul Varma <varmaa@toolness.com>
parents: 2
diff changeset
29 * use your version of this file under the terms of the MPL, indicate your
4c91125795dc Added license block.
Atul Varma <varmaa@toolness.com>
parents: 2
diff changeset
30 * decision by deleting the provisions above and replace them with the notice
4c91125795dc Added license block.
Atul Varma <varmaa@toolness.com>
parents: 2
diff changeset
31 * and other provisions required by the GPL or the LGPL. If you do not delete
4c91125795dc Added license block.
Atul Varma <varmaa@toolness.com>
parents: 2
diff changeset
32 * the provisions above, a recipient may use your version of this file under
4c91125795dc Added license block.
Atul Varma <varmaa@toolness.com>
parents: 2
diff changeset
33 * the terms of any one of the MPL, the GPL or the LGPL.
4c91125795dc Added license block.
Atul Varma <varmaa@toolness.com>
parents: 2
diff changeset
34 *
4c91125795dc Added license block.
Atul Varma <varmaa@toolness.com>
parents: 2
diff changeset
35 * ***** END LICENSE BLOCK ***** */
2
3997e13ca428 We now use DOM storage to persistently store the DB, and dynamically load a JSON library when necessary to serialize/deserialize data.
Atul Varma <varmaa@toolness.com>
parents: 1
diff changeset
36
59
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
37 // = BrowserCouch =
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
38 //
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
39 // This is the primary implementation file for BrowserCouch.
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
40
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
41 // === {{{isArray()}}} ===
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
42 //
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
43 // A helper function to determine whether an object is an Array
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
44 // or not. Taken from "Remedial Javascript" by Douglas Crockford
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
45 // at http://javascript.crockford.com/remedial.html.
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
46
41
3fd3aacf33fb Test suite now passes on Safari.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
47 function isArray(value) {
3fd3aacf33fb Test suite now passes on Safari.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
48 return (typeof value.length === 'number' &&
3fd3aacf33fb Test suite now passes on Safari.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
49 !(value.propertyIsEnumerable('length')) &&
3fd3aacf33fb Test suite now passes on Safari.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
50 typeof value.splice === 'function');
3fd3aacf33fb Test suite now passes on Safari.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
51 }
3fd3aacf33fb Test suite now passes on Safari.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
52
59
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
53 // === {{{ModuleLoader}}} ===
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
54 //
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
55 // A really basic module loader that allows dependencies to be
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
56 // "lazy-loaded" when their functionality is needed.
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
57
26
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
58 var ModuleLoader = {
55
aa013581aeca Directory restructuring.
Atul Varma <varmaa@toolness.com>
parents: 54
diff changeset
59 LIBS: {JSON: "js/ext/json2.js"},
26
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
60
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
61 require: function ML_require(libs, cb) {
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
62 var self = this;
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
63 var i = 0;
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
64 var lastLib = "";
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
65
41
3fd3aacf33fb Test suite now passes on Safari.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
66 if (!isArray(libs))
26
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
67 libs = [libs];
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
68
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
69 function loadNextLib() {
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
70 if (lastLib && !window[lastLib])
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
71 throw new Error("Failed to load library: " + lastLib);
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
72 if (i == libs.length)
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
73 cb();
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
74 else {
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
75 var libName = libs[i];
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
76 i += 1;
42
2d4a0ca10783 Fixed a bug in ModuleLoader whereby modules could be loaded multiple times.
Atul Varma <varmaa@toolness.com>
parents: 41
diff changeset
77 if (window[libName])
26
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
78 loadNextLib();
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
79 else {
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
80 var libUrl = self.LIBS[libName];
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
81 if (!libUrl)
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
82 throw new Error("Unknown lib: " + libName);
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
83 lastLib = libName;
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
84 self._loadScript(libUrl, window, loadNextLib);
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
85 }
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
86 }
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
87 }
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
88
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
89 loadNextLib();
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
90 },
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
91
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
92 _loadScript: function ML__loadScript(url, window, cb) {
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
93 var doc = window.document;
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
94 var script = doc.createElement("script");
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
95 script.setAttribute("src", url);
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
96 script.addEventListener(
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
97 "load",
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
98 function onLoad() {
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
99 script.removeEventListener("load", onLoad, false);
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
100 cb();
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
101 },
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
102 false
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
103 );
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
104 doc.body.appendChild(script);
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
105 }
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
106 };
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
107
59
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
108 // == MapReducers ==
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
109 //
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
110 // //MapReducer// is a generic interface for any map-reduce
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
111 // implementation. Any object implementing this interface will need
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
112 // to be able to work asynchronously, passing back control to the
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
113 // client at a given interval, so that the client has the ability to
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
114 // pause/cancel or report progress on the calculation if needed.
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
115
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
116 // === {{{WebWorkerMapReducer}}} ===
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
117 //
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
118 // A MapReducer that uses
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
119 // [[https://developer.mozilla.org/En/Using_DOM_workers|Web Workers]]
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
120 // for its implementation, allowing the client to take advantage of
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
121 // multiple processor cores and potentially decouple the map-reduce
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
122 // calculation from the user interface.
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
123
49
a5e2db06b58d Initial proof-of-concept of a WebWorkerMapReducer. Still needs lots of work.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
124 function WebWorkerMapReducer(numWorkers, Worker) {
a5e2db06b58d Initial proof-of-concept of a WebWorkerMapReducer. Still needs lots of work.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
125 if (!Worker)
a5e2db06b58d Initial proof-of-concept of a WebWorkerMapReducer. Still needs lots of work.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
126 Worker = window.Worker;
a5e2db06b58d Initial proof-of-concept of a WebWorkerMapReducer. Still needs lots of work.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
127
a5e2db06b58d Initial proof-of-concept of a WebWorkerMapReducer. Still needs lots of work.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
128 var pool = [];
a5e2db06b58d Initial proof-of-concept of a WebWorkerMapReducer. Still needs lots of work.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
129
50
e82074d95e06 The WebWorkerMapReducer now doles out jobs to individual threads; still need to implement the reduce operation though.
Atul Varma <varmaa@toolness.com>
parents: 49
diff changeset
130 function MapWorker(id) {
55
aa013581aeca Directory restructuring.
Atul Varma <varmaa@toolness.com>
parents: 54
diff changeset
131 var worker = new Worker('js/worker-map-reducer.js');
49
a5e2db06b58d Initial proof-of-concept of a WebWorkerMapReducer. Still needs lots of work.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
132 var onDone;
a5e2db06b58d Initial proof-of-concept of a WebWorkerMapReducer. Still needs lots of work.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
133
a5e2db06b58d Initial proof-of-concept of a WebWorkerMapReducer. Still needs lots of work.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
134 worker.onmessage = function(event) {
a5e2db06b58d Initial proof-of-concept of a WebWorkerMapReducer. Still needs lots of work.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
135 onDone(event.data);
a5e2db06b58d Initial proof-of-concept of a WebWorkerMapReducer. Still needs lots of work.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
136 };
a5e2db06b58d Initial proof-of-concept of a WebWorkerMapReducer. Still needs lots of work.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
137
50
e82074d95e06 The WebWorkerMapReducer now doles out jobs to individual threads; still need to implement the reduce operation though.
Atul Varma <varmaa@toolness.com>
parents: 49
diff changeset
138 this.id = id;
49
a5e2db06b58d Initial proof-of-concept of a WebWorkerMapReducer. Still needs lots of work.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
139 this.map = function MW_map(map, dict, cb) {
a5e2db06b58d Initial proof-of-concept of a WebWorkerMapReducer. Still needs lots of work.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
140 onDone = cb;
a5e2db06b58d Initial proof-of-concept of a WebWorkerMapReducer. Still needs lots of work.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
141 worker.postMessage({map: map.toString(), dict: dict});
a5e2db06b58d Initial proof-of-concept of a WebWorkerMapReducer. Still needs lots of work.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
142 };
a5e2db06b58d Initial proof-of-concept of a WebWorkerMapReducer. Still needs lots of work.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
143 }
a5e2db06b58d Initial proof-of-concept of a WebWorkerMapReducer. Still needs lots of work.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
144
a5e2db06b58d Initial proof-of-concept of a WebWorkerMapReducer. Still needs lots of work.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
145 for (var i = 0; i < numWorkers; i++)
50
e82074d95e06 The WebWorkerMapReducer now doles out jobs to individual threads; still need to implement the reduce operation though.
Atul Varma <varmaa@toolness.com>
parents: 49
diff changeset
146 pool.push(new MapWorker(i));
49
a5e2db06b58d Initial proof-of-concept of a WebWorkerMapReducer. Still needs lots of work.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
147
a5e2db06b58d Initial proof-of-concept of a WebWorkerMapReducer. Still needs lots of work.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
148 this.map = function WWMR_map(map, dict, progress, chunkSize, finished) {
50
e82074d95e06 The WebWorkerMapReducer now doles out jobs to individual threads; still need to implement the reduce operation though.
Atul Varma <varmaa@toolness.com>
parents: 49
diff changeset
149 var keys = dict.getKeys();
e82074d95e06 The WebWorkerMapReducer now doles out jobs to individual threads; still need to implement the reduce operation though.
Atul Varma <varmaa@toolness.com>
parents: 49
diff changeset
150 var size = keys.length;
e82074d95e06 The WebWorkerMapReducer now doles out jobs to individual threads; still need to implement the reduce operation though.
Atul Varma <varmaa@toolness.com>
parents: 49
diff changeset
151 var workersDone = 0;
e82074d95e06 The WebWorkerMapReducer now doles out jobs to individual threads; still need to implement the reduce operation though.
Atul Varma <varmaa@toolness.com>
parents: 49
diff changeset
152 var mapDict = {};
e82074d95e06 The WebWorkerMapReducer now doles out jobs to individual threads; still need to implement the reduce operation though.
Atul Varma <varmaa@toolness.com>
parents: 49
diff changeset
153
e82074d95e06 The WebWorkerMapReducer now doles out jobs to individual threads; still need to implement the reduce operation though.
Atul Varma <varmaa@toolness.com>
parents: 49
diff changeset
154 function getNextChunk() {
e82074d95e06 The WebWorkerMapReducer now doles out jobs to individual threads; still need to implement the reduce operation though.
Atul Varma <varmaa@toolness.com>
parents: 49
diff changeset
155 if (keys.length) {
e82074d95e06 The WebWorkerMapReducer now doles out jobs to individual threads; still need to implement the reduce operation though.
Atul Varma <varmaa@toolness.com>
parents: 49
diff changeset
156 var chunkKeys = keys.slice(0, chunkSize);
e82074d95e06 The WebWorkerMapReducer now doles out jobs to individual threads; still need to implement the reduce operation though.
Atul Varma <varmaa@toolness.com>
parents: 49
diff changeset
157 keys = keys.slice(chunkSize);
e82074d95e06 The WebWorkerMapReducer now doles out jobs to individual threads; still need to implement the reduce operation though.
Atul Varma <varmaa@toolness.com>
parents: 49
diff changeset
158 var chunk = {};
e82074d95e06 The WebWorkerMapReducer now doles out jobs to individual threads; still need to implement the reduce operation though.
Atul Varma <varmaa@toolness.com>
parents: 49
diff changeset
159 for (var i = 0; i < chunkKeys.length; i++)
e82074d95e06 The WebWorkerMapReducer now doles out jobs to individual threads; still need to implement the reduce operation though.
Atul Varma <varmaa@toolness.com>
parents: 49
diff changeset
160 chunk[chunkKeys[i]] = dict.get(chunkKeys[i]);
e82074d95e06 The WebWorkerMapReducer now doles out jobs to individual threads; still need to implement the reduce operation though.
Atul Varma <varmaa@toolness.com>
parents: 49
diff changeset
161 return chunk;
e82074d95e06 The WebWorkerMapReducer now doles out jobs to individual threads; still need to implement the reduce operation though.
Atul Varma <varmaa@toolness.com>
parents: 49
diff changeset
162 } else
e82074d95e06 The WebWorkerMapReducer now doles out jobs to individual threads; still need to implement the reduce operation though.
Atul Varma <varmaa@toolness.com>
parents: 49
diff changeset
163 return null;
e82074d95e06 The WebWorkerMapReducer now doles out jobs to individual threads; still need to implement the reduce operation though.
Atul Varma <varmaa@toolness.com>
parents: 49
diff changeset
164 }
49
a5e2db06b58d Initial proof-of-concept of a WebWorkerMapReducer. Still needs lots of work.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
165
50
e82074d95e06 The WebWorkerMapReducer now doles out jobs to individual threads; still need to implement the reduce operation though.
Atul Varma <varmaa@toolness.com>
parents: 49
diff changeset
166 function nextJob(mapWorker) {
e82074d95e06 The WebWorkerMapReducer now doles out jobs to individual threads; still need to implement the reduce operation though.
Atul Varma <varmaa@toolness.com>
parents: 49
diff changeset
167 var chunk = getNextChunk();
e82074d95e06 The WebWorkerMapReducer now doles out jobs to individual threads; still need to implement the reduce operation though.
Atul Varma <varmaa@toolness.com>
parents: 49
diff changeset
168 if (chunk) {
e82074d95e06 The WebWorkerMapReducer now doles out jobs to individual threads; still need to implement the reduce operation though.
Atul Varma <varmaa@toolness.com>
parents: 49
diff changeset
169 mapWorker.map(
e82074d95e06 The WebWorkerMapReducer now doles out jobs to individual threads; still need to implement the reduce operation though.
Atul Varma <varmaa@toolness.com>
parents: 49
diff changeset
170 map,
e82074d95e06 The WebWorkerMapReducer now doles out jobs to individual threads; still need to implement the reduce operation though.
Atul Varma <varmaa@toolness.com>
parents: 49
diff changeset
171 chunk,
e82074d95e06 The WebWorkerMapReducer now doles out jobs to individual threads; still need to implement the reduce operation though.
Atul Varma <varmaa@toolness.com>
parents: 49
diff changeset
172 function jobDone(aMapDict) {
e82074d95e06 The WebWorkerMapReducer now doles out jobs to individual threads; still need to implement the reduce operation though.
Atul Varma <varmaa@toolness.com>
parents: 49
diff changeset
173 for (name in aMapDict)
e82074d95e06 The WebWorkerMapReducer now doles out jobs to individual threads; still need to implement the reduce operation though.
Atul Varma <varmaa@toolness.com>
parents: 49
diff changeset
174 if (name in mapDict) {
e82074d95e06 The WebWorkerMapReducer now doles out jobs to individual threads; still need to implement the reduce operation though.
Atul Varma <varmaa@toolness.com>
parents: 49
diff changeset
175 var item = mapDict[name];
e82074d95e06 The WebWorkerMapReducer now doles out jobs to individual threads; still need to implement the reduce operation though.
Atul Varma <varmaa@toolness.com>
parents: 49
diff changeset
176 item.keys = item.keys.concat(aMapDict[name].keys);
e82074d95e06 The WebWorkerMapReducer now doles out jobs to individual threads; still need to implement the reduce operation though.
Atul Varma <varmaa@toolness.com>
parents: 49
diff changeset
177 item.values = item.values.concat(aMapDict[name].values);
e82074d95e06 The WebWorkerMapReducer now doles out jobs to individual threads; still need to implement the reduce operation though.
Atul Varma <varmaa@toolness.com>
parents: 49
diff changeset
178 } else
e82074d95e06 The WebWorkerMapReducer now doles out jobs to individual threads; still need to implement the reduce operation though.
Atul Varma <varmaa@toolness.com>
parents: 49
diff changeset
179 mapDict[name] = aMapDict[name];
49
a5e2db06b58d Initial proof-of-concept of a WebWorkerMapReducer. Still needs lots of work.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
180
50
e82074d95e06 The WebWorkerMapReducer now doles out jobs to individual threads; still need to implement the reduce operation though.
Atul Varma <varmaa@toolness.com>
parents: 49
diff changeset
181 if (keys.length)
e82074d95e06 The WebWorkerMapReducer now doles out jobs to individual threads; still need to implement the reduce operation though.
Atul Varma <varmaa@toolness.com>
parents: 49
diff changeset
182 progress("map",
e82074d95e06 The WebWorkerMapReducer now doles out jobs to individual threads; still need to implement the reduce operation though.
Atul Varma <varmaa@toolness.com>
parents: 49
diff changeset
183 (size - keys.length) / size,
e82074d95e06 The WebWorkerMapReducer now doles out jobs to individual threads; still need to implement the reduce operation though.
Atul Varma <varmaa@toolness.com>
parents: 49
diff changeset
184 function() { nextJob(mapWorker); });
e82074d95e06 The WebWorkerMapReducer now doles out jobs to individual threads; still need to implement the reduce operation though.
Atul Varma <varmaa@toolness.com>
parents: 49
diff changeset
185 else
e82074d95e06 The WebWorkerMapReducer now doles out jobs to individual threads; still need to implement the reduce operation though.
Atul Varma <varmaa@toolness.com>
parents: 49
diff changeset
186 workerDone();
e82074d95e06 The WebWorkerMapReducer now doles out jobs to individual threads; still need to implement the reduce operation though.
Atul Varma <varmaa@toolness.com>
parents: 49
diff changeset
187 });
e82074d95e06 The WebWorkerMapReducer now doles out jobs to individual threads; still need to implement the reduce operation though.
Atul Varma <varmaa@toolness.com>
parents: 49
diff changeset
188 } else
e82074d95e06 The WebWorkerMapReducer now doles out jobs to individual threads; still need to implement the reduce operation though.
Atul Varma <varmaa@toolness.com>
parents: 49
diff changeset
189 workerDone();
e82074d95e06 The WebWorkerMapReducer now doles out jobs to individual threads; still need to implement the reduce operation though.
Atul Varma <varmaa@toolness.com>
parents: 49
diff changeset
190 }
49
a5e2db06b58d Initial proof-of-concept of a WebWorkerMapReducer. Still needs lots of work.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
191
50
e82074d95e06 The WebWorkerMapReducer now doles out jobs to individual threads; still need to implement the reduce operation though.
Atul Varma <varmaa@toolness.com>
parents: 49
diff changeset
192 function workerDone() {
e82074d95e06 The WebWorkerMapReducer now doles out jobs to individual threads; still need to implement the reduce operation though.
Atul Varma <varmaa@toolness.com>
parents: 49
diff changeset
193 workersDone += 1;
e82074d95e06 The WebWorkerMapReducer now doles out jobs to individual threads; still need to implement the reduce operation though.
Atul Varma <varmaa@toolness.com>
parents: 49
diff changeset
194 if (workersDone == numWorkers)
e82074d95e06 The WebWorkerMapReducer now doles out jobs to individual threads; still need to implement the reduce operation though.
Atul Varma <varmaa@toolness.com>
parents: 49
diff changeset
195 allWorkersDone();
e82074d95e06 The WebWorkerMapReducer now doles out jobs to individual threads; still need to implement the reduce operation though.
Atul Varma <varmaa@toolness.com>
parents: 49
diff changeset
196 }
e82074d95e06 The WebWorkerMapReducer now doles out jobs to individual threads; still need to implement the reduce operation though.
Atul Varma <varmaa@toolness.com>
parents: 49
diff changeset
197
e82074d95e06 The WebWorkerMapReducer now doles out jobs to individual threads; still need to implement the reduce operation though.
Atul Varma <varmaa@toolness.com>
parents: 49
diff changeset
198 function allWorkersDone() {
e82074d95e06 The WebWorkerMapReducer now doles out jobs to individual threads; still need to implement the reduce operation though.
Atul Varma <varmaa@toolness.com>
parents: 49
diff changeset
199 var mapKeys = [];
e82074d95e06 The WebWorkerMapReducer now doles out jobs to individual threads; still need to implement the reduce operation though.
Atul Varma <varmaa@toolness.com>
parents: 49
diff changeset
200 for (name in mapDict)
e82074d95e06 The WebWorkerMapReducer now doles out jobs to individual threads; still need to implement the reduce operation though.
Atul Varma <varmaa@toolness.com>
parents: 49
diff changeset
201 mapKeys.push(name);
e82074d95e06 The WebWorkerMapReducer now doles out jobs to individual threads; still need to implement the reduce operation though.
Atul Varma <varmaa@toolness.com>
parents: 49
diff changeset
202 mapKeys.sort();
e82074d95e06 The WebWorkerMapReducer now doles out jobs to individual threads; still need to implement the reduce operation though.
Atul Varma <varmaa@toolness.com>
parents: 49
diff changeset
203 finished({dict: mapDict, keys: mapKeys});
e82074d95e06 The WebWorkerMapReducer now doles out jobs to individual threads; still need to implement the reduce operation though.
Atul Varma <varmaa@toolness.com>
parents: 49
diff changeset
204 }
e82074d95e06 The WebWorkerMapReducer now doles out jobs to individual threads; still need to implement the reduce operation though.
Atul Varma <varmaa@toolness.com>
parents: 49
diff changeset
205
e82074d95e06 The WebWorkerMapReducer now doles out jobs to individual threads; still need to implement the reduce operation though.
Atul Varma <varmaa@toolness.com>
parents: 49
diff changeset
206 for (var i = 0; i < numWorkers; i++)
e82074d95e06 The WebWorkerMapReducer now doles out jobs to individual threads; still need to implement the reduce operation though.
Atul Varma <varmaa@toolness.com>
parents: 49
diff changeset
207 nextJob(pool[i]);
49
a5e2db06b58d Initial proof-of-concept of a WebWorkerMapReducer. Still needs lots of work.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
208 };
a5e2db06b58d Initial proof-of-concept of a WebWorkerMapReducer. Still needs lots of work.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
209
59
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
210 // TODO: Actually implement our own reduce() method here instead
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
211 // of delegating to the single-threaded version.
49
a5e2db06b58d Initial proof-of-concept of a WebWorkerMapReducer. Still needs lots of work.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
212 this.reduce = SingleThreadedMapReducer.reduce;
a5e2db06b58d Initial proof-of-concept of a WebWorkerMapReducer. Still needs lots of work.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
213 };
a5e2db06b58d Initial proof-of-concept of a WebWorkerMapReducer. Still needs lots of work.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
214
59
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
215 // === {{{SingleThreadedMapReducer}}} ===
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
216 //
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
217 // A MapReducer that works on the current thread.
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
218
47
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
219 var SingleThreadedMapReducer = {
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
220 map: function STMR_map(map, dict, progress,
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
221 chunkSize, finished) {
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
222 var mapDict = {};
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
223 var keys = dict.getKeys();
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
224 var currDoc;
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
225
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
226 function emit(key, value) {
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
227 // TODO: This assumes that the key will always be
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
228 // an indexable value. We may have to hash the value,
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
229 // though, if it's e.g. an Object.
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
230 var item = mapDict[key];
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
231 if (!item)
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
232 item = mapDict[key] = {keys: [], values: []};
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
233 item.keys.push(currDoc.id);
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
234 item.values.push(value);
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
235 }
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
236
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
237 var i = 0;
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
238
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
239 function continueMap() {
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
240 var iAtStart = i;
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
241
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
242 do {
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
243 currDoc = dict.get(keys[i]);
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
244 map(currDoc, emit);
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
245 i++;
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
246 } while (i - iAtStart < chunkSize &&
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
247 i < keys.length)
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
248
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
249 if (i == keys.length) {
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
250 var mapKeys = [];
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
251 for (name in mapDict)
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
252 mapKeys.push(name);
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
253 mapKeys.sort();
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
254 finished({dict: mapDict, keys: mapKeys});
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
255 } else
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
256 progress("map", i / keys.length, continueMap);
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
257 }
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
258
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
259 continueMap();
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
260 },
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
261
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
262 reduce: function STMR_reduce(reduce, mapResult, progress,
48
b0d5bc990a80 Typo fix
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
263 chunkSize, finished) {
47
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
264 var rows = [];
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
265 var mapDict = mapResult.dict;
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
266 var mapKeys = mapResult.keys;
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
267
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
268 var i = 0;
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
269
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
270 function continueReduce() {
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
271 var iAtStart = i;
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
272
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
273 do {
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
274 var key = mapKeys[i];
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
275 var item = mapDict[key];
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
276
52
96379dbafc53 Got rid of a TODO, which also incidentally improved the performance of the reduce phase.
Atul Varma <varmaa@toolness.com>
parents: 50
diff changeset
277 var keys = [];
96379dbafc53 Got rid of a TODO, which also incidentally improved the performance of the reduce phase.
Atul Varma <varmaa@toolness.com>
parents: 50
diff changeset
278 for (var j = 0; j < keys.length; j++)
96379dbafc53 Got rid of a TODO, which also incidentally improved the performance of the reduce phase.
Atul Varma <varmaa@toolness.com>
parents: 50
diff changeset
279 newKeys.push([key, item.keys[j]]);
96379dbafc53 Got rid of a TODO, which also incidentally improved the performance of the reduce phase.
Atul Varma <varmaa@toolness.com>
parents: 50
diff changeset
280
47
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
281 rows.push({key: key,
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
282 value: reduce(keys, item.values)});
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
283 i++;
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
284 } while (i - iAtStart < chunkSize &&
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
285 i < mapKeys.length)
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
286
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
287 if (i == mapKeys.length)
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
288 finished(rows);
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
289 else
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
290 progress("reduce", i / mapKeys.length, continueReduce);
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
291 }
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
292
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
293 continueReduce();
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
294 }
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
295 };
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
296
59
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
297 // == Storage ==
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
298 //
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
299 // //Storage// is a generic interface for a persistent storage
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
300 // implementation capable of storing JSON-able objects.
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
301
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
302 // === {{{FakeStorage}}} ===
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
303 //
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
304 // This Storage implementation isn't actually persistent; it's just
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
305 // a placeholder that can be used for testing purposes, or when no
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
306 // persistent storage mechanisms are available.
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
307
29
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
308 function FakeStorage() {
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
309 var db = {};
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
310
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
311 function deepCopy(obj) {
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
312 if (typeof(obj) == "object") {
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
313 var copy;
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
314
41
3fd3aacf33fb Test suite now passes on Safari.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
315 if (isArray(obj))
29
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
316 copy = new Array();
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
317 else
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
318 copy = new Object();
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
319
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
320 for (name in obj) {
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
321 if (obj.hasOwnProperty(name)) {
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
322 var property = obj[name];
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
323 if (typeof(property) == "object")
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
324 copy[name] = deepCopy(property);
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
325 else
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
326 copy[name] = property;
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
327 }
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
328 }
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
329
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
330 return copy;
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
331 } else
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
332 return obj;
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
333 }
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
334
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
335 this.get = function FS_get(name, cb) {
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
336 if (!(name in db))
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
337 cb(null);
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
338 else
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
339 cb(db[name]);
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
340 };
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
341
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
342 this.put = function FS_put(name, obj, cb) {
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
343 db[name] = deepCopy(obj);
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
344 cb();
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
345 };
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
346 };
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
347
59
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
348 // === {{{LocalStorage}}} ===
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
349 //
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
350 // This Storage implementation uses the browser's HTML5 support for
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
351 // {{{localStorage}}} or {{{globalStorage}}} for object persistence.
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
352
28
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
353 function LocalStorage(JSON) {
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
354 var storage;
2
3997e13ca428 We now use DOM storage to persistently store the DB, and dynamically load a JSON library when necessary to serialize/deserialize data.
Atul Varma <varmaa@toolness.com>
parents: 1
diff changeset
355
28
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
356 if (window.globalStorage)
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
357 storage = window.globalStorage[location.hostname];
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
358 else {
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
359 if (window.localStorage)
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
360 storage = window.localStorage;
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
361 else
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
362 throw new Error("globalStorage/localStorage not available.");
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
363 }
2
3997e13ca428 We now use DOM storage to persistently store the DB, and dynamically load a JSON library when necessary to serialize/deserialize data.
Atul Varma <varmaa@toolness.com>
parents: 1
diff changeset
364
28
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
365 function ensureJSON(cb) {
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
366 if (!JSON) {
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
367 ModuleLoader.require(
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
368 "JSON",
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
369 function() {
2
3997e13ca428 We now use DOM storage to persistently store the DB, and dynamically load a JSON library when necessary to serialize/deserialize data.
Atul Varma <varmaa@toolness.com>
parents: 1
diff changeset
370 JSON = window.JSON;
28
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
371 cb();
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
372 });
2
3997e13ca428 We now use DOM storage to persistently store the DB, and dynamically load a JSON library when necessary to serialize/deserialize data.
Atul Varma <varmaa@toolness.com>
parents: 1
diff changeset
373 } else
28
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
374 cb();
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
375 }
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
376
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
377 this.get = function LS_get(name, cb) {
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
378 if (name in storage && storage[name].value)
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
379 ensureJSON(
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
380 function() {
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
381 var obj = JSON.parse(storage[name].value);
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
382 cb(obj);
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
383 });
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
384 else
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
385 cb(null);
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
386 };
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
387
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
388 this.put = function LS_put(name, obj, cb) {
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
389 ensureJSON(
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
390 function() {
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
391 storage[name] = JSON.stringify(obj);
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
392 cb();
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
393 });
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
394 };
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
395 }
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
396
59
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
397 // == BrowserCouch ==
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
398 //
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
399 // {{{BrowserCouch}}} is the main object that clients will use. It's
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
400 // intended to be somewhat analogous to CouchDB's RESTful API.
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
401
28
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
402 var BrowserCouch = {
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
403 get: function BC_get(name, cb, storage) {
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
404 if (!storage)
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
405 storage = new LocalStorage();
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
406
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
407 new this._DB(name, storage, new this._Dictionary(), cb);
0
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
408 },
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
409
28
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
410 _Dictionary: function BC__Dictionary() {
18
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
411 var dict = {};
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
412 var keys = [];
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
413
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
414 function regenerateKeys() {
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
415 keys = [];
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
416 for (key in dict)
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
417 keys.push(key);
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
418 }
12
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
419
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
420 this.has = function Dictionary_has(key) {
18
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
421 return (key in dict);
12
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
422 };
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
423
18
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
424 this.getKeys = function Dictionary_getKeys() {
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
425 return keys;
12
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
426 };
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
427
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
428 this.get = function Dictionary_get(key) {
18
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
429 return dict[key];
12
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
430 };
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
431
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
432 this.set = function Dictionary_set(key, value) {
18
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
433 if (!(key in dict))
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
434 keys.push(key);
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
435 dict[key] = value;
12
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
436 };
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
437
41
3fd3aacf33fb Test suite now passes on Safari.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
438 this.remove = function Dictionary_delete(key) {
18
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
439 delete dict[key];
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
440
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
441 // TODO: If we're in JS 1.6 and have Array.indexOf(), we
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
442 // shouldn't have to rebuild the key index like this.
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
443 regenerateKeys();
12
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
444 };
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
445
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
446 this.clear = function Dictionary_clear() {
18
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
447 dict = {};
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
448 keys = [];
12
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
449 };
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
450
28
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
451 this.pickle = function Dictionary_pickle() {
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
452 return dict;
12
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
453 };
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
454
28
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
455 this.unpickle = function Dictionary_unpickle(obj) {
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
456 dict = obj;
18
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
457 regenerateKeys();
12
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
458 };
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
459 },
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
460
28
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
461 _DB: function BC__DB(name, storage, dict, cb) {
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
462 var self = this;
2
3997e13ca428 We now use DOM storage to persistently store the DB, and dynamically load a JSON library when necessary to serialize/deserialize data.
Atul Varma <varmaa@toolness.com>
parents: 1
diff changeset
463 var dbName = 'BrowserCouch_DB_' + name;
3997e13ca428 We now use DOM storage to persistently store the DB, and dynamically load a JSON library when necessary to serialize/deserialize data.
Atul Varma <varmaa@toolness.com>
parents: 1
diff changeset
464
28
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
465 function commitToStorage(cb) {
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
466 if (!cb)
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
467 cb = function() {};
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
468 storage.put(dbName, dict.pickle(), cb);
2
3997e13ca428 We now use DOM storage to persistently store the DB, and dynamically load a JSON library when necessary to serialize/deserialize data.
Atul Varma <varmaa@toolness.com>
parents: 1
diff changeset
469 }
3997e13ca428 We now use DOM storage to persistently store the DB, and dynamically load a JSON library when necessary to serialize/deserialize data.
Atul Varma <varmaa@toolness.com>
parents: 1
diff changeset
470
3997e13ca428 We now use DOM storage to persistently store the DB, and dynamically load a JSON library when necessary to serialize/deserialize data.
Atul Varma <varmaa@toolness.com>
parents: 1
diff changeset
471 this.wipe = function DB_wipe(cb) {
12
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
472 dict.clear();
28
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
473 commitToStorage(cb);
2
3997e13ca428 We now use DOM storage to persistently store the DB, and dynamically load a JSON library when necessary to serialize/deserialize data.
Atul Varma <varmaa@toolness.com>
parents: 1
diff changeset
474 };
1
972d973433c7 Documents is now an array, and an index mapping document IDs to their position in the array is maintained.
Atul Varma <varmaa@toolness.com>
parents: 0
diff changeset
475
0
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
476 this.get = function DB_get(id, cb) {
12
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
477 if (dict.has(id))
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
478 cb(dict.get(id));
0
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
479 else
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
480 cb(null);
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
481 };
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
482
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
483 this.put = function DB_put(document, cb) {
41
3fd3aacf33fb Test suite now passes on Safari.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
484 if (isArray(document)) {
0
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
485 for (var i = 0; i < document.length; i++)
12
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
486 dict.set(document[i].id, document[i]);
0
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
487 } else
12
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
488 dict.set(document.id, document);
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
489
28
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
490 commitToStorage(cb);
0
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
491 };
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
492
22
7b418d9d27f8 Added a getLength() method to DB.
Atul Varma <varmaa@toolness.com>
parents: 21
diff changeset
493 this.getLength = function DB_getLength() {
7b418d9d27f8 Added a getLength() method to DB.
Atul Varma <varmaa@toolness.com>
parents: 21
diff changeset
494 return dict.getKeys().length;
7b418d9d27f8 Added a getLength() method to DB.
Atul Varma <varmaa@toolness.com>
parents: 21
diff changeset
495 };
7b418d9d27f8 Added a getLength() method to DB.
Atul Varma <varmaa@toolness.com>
parents: 21
diff changeset
496
0
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
497 this.view = function DB_view(options) {
9
8842af55f7fe Refactored map-reduce into its own self-contained function that isn't part of a closure
Atul Varma <varmaa@toolness.com>
parents: 8
diff changeset
498 if (!options.map)
0
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
499 throw new Error('map function not provided');
10
0a694cb579ec map-reduce now supports 'chunking', i.e. processing some number of documents and then giving the system a chance to report progress to the user, cancel the calculation, giving the UI a chance to breathe, etc, before resuming the calculation.
Atul Varma <varmaa@toolness.com>
parents: 9
diff changeset
500 if (!options.finished)
0a694cb579ec map-reduce now supports 'chunking', i.e. processing some number of documents and then giving the system a chance to report progress to the user, cancel the calculation, giving the UI a chance to breathe, etc, before resuming the calculation.
Atul Varma <varmaa@toolness.com>
parents: 9
diff changeset
501 throw new Error('finished callback not provided');
0
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
502
37
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
503 // Maximum number of items to process before giving the UI a chance
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
504 // to breathe.
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
505 var DEFAULT_CHUNK_SIZE = 1000;
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
506
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
507 // If no progress callback is given, we'll automatically give the
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
508 // UI a chance to breathe for this many milliseconds before continuing
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
509 // processing.
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
510 var DEFAULT_UI_BREATHE_TIME = 50;
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
511
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
512 var chunkSize = options.chunkSize;
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
513 if (!chunkSize)
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
514 chunkSize = DEFAULT_CHUNK_SIZE;
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
515
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
516 var progress = options.progress;
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
517 if (!progress)
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
518 progress = function defaultProgress(phase, percent, resume) {
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
519 window.setTimeout(resume, DEFAULT_UI_BREATHE_TIME);
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
520 };
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
521
47
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
522 var mapReducer = options.mapReducer;
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
523 if (!mapReducer)
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
524 mapReducer = SingleThreadedMapReducer;
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
525
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
526 mapReducer.map(
37
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
527 options.map,
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
528 dict,
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
529 progress,
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
530 chunkSize,
47
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
531 function(mapResult) {
44
fec67509fc3d Made new View and MapView classes that encapsulate view results, and in the near future will allow searching through views.
Atul Varma <varmaa@toolness.com>
parents: 42
diff changeset
532 if (options.reduce)
47
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
533 mapReducer.reduce(
44
fec67509fc3d Made new View and MapView classes that encapsulate view results, and in the near future will allow searching through views.
Atul Varma <varmaa@toolness.com>
parents: 42
diff changeset
534 options.reduce,
47
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
535 mapResult,
44
fec67509fc3d Made new View and MapView classes that encapsulate view results, and in the near future will allow searching through views.
Atul Varma <varmaa@toolness.com>
parents: 42
diff changeset
536 progress,
fec67509fc3d Made new View and MapView classes that encapsulate view results, and in the near future will allow searching through views.
Atul Varma <varmaa@toolness.com>
parents: 42
diff changeset
537 chunkSize,
fec67509fc3d Made new View and MapView classes that encapsulate view results, and in the near future will allow searching through views.
Atul Varma <varmaa@toolness.com>
parents: 42
diff changeset
538 function(rows) {
fec67509fc3d Made new View and MapView classes that encapsulate view results, and in the near future will allow searching through views.
Atul Varma <varmaa@toolness.com>
parents: 42
diff changeset
539 options.finished(new BrowserCouch._View(rows));
fec67509fc3d Made new View and MapView classes that encapsulate view results, and in the near future will allow searching through views.
Atul Varma <varmaa@toolness.com>
parents: 42
diff changeset
540 });
fec67509fc3d Made new View and MapView classes that encapsulate view results, and in the near future will allow searching through views.
Atul Varma <varmaa@toolness.com>
parents: 42
diff changeset
541 else
47
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
542 options.finished(new BrowserCouch._MapView(mapResult));
37
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
543 });
9
8842af55f7fe Refactored map-reduce into its own self-contained function that isn't part of a closure
Atul Varma <varmaa@toolness.com>
parents: 8
diff changeset
544 };
29
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
545
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
546 storage.get(
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
547 dbName,
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
548 function(obj) {
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
549 if (obj)
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
550 dict.unpickle(obj);
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
551 cb(self);
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
552 });
9
8842af55f7fe Refactored map-reduce into its own self-contained function that isn't part of a closure
Atul Varma <varmaa@toolness.com>
parents: 8
diff changeset
553 },
8842af55f7fe Refactored map-reduce into its own self-contained function that isn't part of a closure
Atul Varma <varmaa@toolness.com>
parents: 8
diff changeset
554
44
fec67509fc3d Made new View and MapView classes that encapsulate view results, and in the near future will allow searching through views.
Atul Varma <varmaa@toolness.com>
parents: 42
diff changeset
555 _View: function BC__View(rows) {
fec67509fc3d Made new View and MapView classes that encapsulate view results, and in the near future will allow searching through views.
Atul Varma <varmaa@toolness.com>
parents: 42
diff changeset
556 this.rows = rows;
46
6da7638d056f Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
557
6da7638d056f Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
558 function findRow(key, rows) {
6da7638d056f Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
559 if (rows.length > 1) {
6da7638d056f Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
560 var midpoint = Math.floor(rows.length / 2);
6da7638d056f Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
561 var row = rows[midpoint];
6da7638d056f Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
562 if (key < row.key)
6da7638d056f Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
563 return findRow(key, rows.slice(0, midpoint));
6da7638d056f Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
564 if (key > row.key)
6da7638d056f Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
565 return midpoint + findRow(key, rows.slice(midpoint));
6da7638d056f Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
566 return midpoint;
6da7638d056f Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
567 } else
6da7638d056f Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
568 return 0;
6da7638d056f Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
569 }
6da7638d056f Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
570
6da7638d056f Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
571 this.findRow = function V_findRow(key) {
6da7638d056f Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
572 return findRow(key, rows);
6da7638d056f Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
573 };
44
fec67509fc3d Made new View and MapView classes that encapsulate view results, and in the near future will allow searching through views.
Atul Varma <varmaa@toolness.com>
parents: 42
diff changeset
574 },
37
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
575
47
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
576 _MapView: function BC__MapView(mapResult) {
44
fec67509fc3d Made new View and MapView classes that encapsulate view results, and in the near future will allow searching through views.
Atul Varma <varmaa@toolness.com>
parents: 42
diff changeset
577 var rows = [];
45
3a34b9ed3a36 Added a simple findRow() method to the MapView class that finds the first row of a given key using binary search.
Atul Varma <varmaa@toolness.com>
parents: 44
diff changeset
578 var keyRows = [];
37
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
579
47
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
580 var mapKeys = mapResult.keys;
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
581 var mapDict = mapResult.dict;
7664a4e099b5 Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
Atul Varma <varmaa@toolness.com>
parents: 46
diff changeset
582
44
fec67509fc3d Made new View and MapView classes that encapsulate view results, and in the near future will allow searching through views.
Atul Varma <varmaa@toolness.com>
parents: 42
diff changeset
583 for (var i = 0; i < mapKeys.length; i++) {
fec67509fc3d Made new View and MapView classes that encapsulate view results, and in the near future will allow searching through views.
Atul Varma <varmaa@toolness.com>
parents: 42
diff changeset
584 var key = mapKeys[i];
fec67509fc3d Made new View and MapView classes that encapsulate view results, and in the near future will allow searching through views.
Atul Varma <varmaa@toolness.com>
parents: 42
diff changeset
585 var item = mapDict[key];
45
3a34b9ed3a36 Added a simple findRow() method to the MapView class that finds the first row of a given key using binary search.
Atul Varma <varmaa@toolness.com>
parents: 44
diff changeset
586 keyRows.push({key: key, pos: rows.length});
54
3607a6844691 Rows of a map view are now sorted secondarily by document ID, which makes their row order well-defined.
Atul Varma <varmaa@toolness.com>
parents: 52
diff changeset
587 var newRows = [];
44
fec67509fc3d Made new View and MapView classes that encapsulate view results, and in the near future will allow searching through views.
Atul Varma <varmaa@toolness.com>
parents: 42
diff changeset
588 for (var j = 0; j < item.keys.length; j++) {
fec67509fc3d Made new View and MapView classes that encapsulate view results, and in the near future will allow searching through views.
Atul Varma <varmaa@toolness.com>
parents: 42
diff changeset
589 var id = item.keys[j];
fec67509fc3d Made new View and MapView classes that encapsulate view results, and in the near future will allow searching through views.
Atul Varma <varmaa@toolness.com>
parents: 42
diff changeset
590 var value = item.values[j];
54
3607a6844691 Rows of a map view are now sorted secondarily by document ID, which makes their row order well-defined.
Atul Varma <varmaa@toolness.com>
parents: 52
diff changeset
591 newRows.push({id: id,
3607a6844691 Rows of a map view are now sorted secondarily by document ID, which makes their row order well-defined.
Atul Varma <varmaa@toolness.com>
parents: 52
diff changeset
592 key: key,
3607a6844691 Rows of a map view are now sorted secondarily by document ID, which makes their row order well-defined.
Atul Varma <varmaa@toolness.com>
parents: 52
diff changeset
593 value: value});
37
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
594 }
54
3607a6844691 Rows of a map view are now sorted secondarily by document ID, which makes their row order well-defined.
Atul Varma <varmaa@toolness.com>
parents: 52
diff changeset
595 newRows.sort(function(a, b) {
3607a6844691 Rows of a map view are now sorted secondarily by document ID, which makes their row order well-defined.
Atul Varma <varmaa@toolness.com>
parents: 52
diff changeset
596 if (a.id < b.id)
3607a6844691 Rows of a map view are now sorted secondarily by document ID, which makes their row order well-defined.
Atul Varma <varmaa@toolness.com>
parents: 52
diff changeset
597 return -1;
3607a6844691 Rows of a map view are now sorted secondarily by document ID, which makes their row order well-defined.
Atul Varma <varmaa@toolness.com>
parents: 52
diff changeset
598 if (a.id > b.id)
3607a6844691 Rows of a map view are now sorted secondarily by document ID, which makes their row order well-defined.
Atul Varma <varmaa@toolness.com>
parents: 52
diff changeset
599 return 1;
3607a6844691 Rows of a map view are now sorted secondarily by document ID, which makes their row order well-defined.
Atul Varma <varmaa@toolness.com>
parents: 52
diff changeset
600 return 0;
3607a6844691 Rows of a map view are now sorted secondarily by document ID, which makes their row order well-defined.
Atul Varma <varmaa@toolness.com>
parents: 52
diff changeset
601 });
3607a6844691 Rows of a map view are now sorted secondarily by document ID, which makes their row order well-defined.
Atul Varma <varmaa@toolness.com>
parents: 52
diff changeset
602 rows = rows.concat(newRows);
9
8842af55f7fe Refactored map-reduce into its own self-contained function that isn't part of a closure
Atul Varma <varmaa@toolness.com>
parents: 8
diff changeset
603 }
45
3a34b9ed3a36 Added a simple findRow() method to the MapView class that finds the first row of a given key using binary search.
Atul Varma <varmaa@toolness.com>
parents: 44
diff changeset
604
3a34b9ed3a36 Added a simple findRow() method to the MapView class that finds the first row of a given key using binary search.
Atul Varma <varmaa@toolness.com>
parents: 44
diff changeset
605 function findRow(key, keyRows) {
3a34b9ed3a36 Added a simple findRow() method to the MapView class that finds the first row of a given key using binary search.
Atul Varma <varmaa@toolness.com>
parents: 44
diff changeset
606 if (keyRows.length > 1) {
3a34b9ed3a36 Added a simple findRow() method to the MapView class that finds the first row of a given key using binary search.
Atul Varma <varmaa@toolness.com>
parents: 44
diff changeset
607 var midpoint = Math.floor(keyRows.length / 2);
3a34b9ed3a36 Added a simple findRow() method to the MapView class that finds the first row of a given key using binary search.
Atul Varma <varmaa@toolness.com>
parents: 44
diff changeset
608 var keyRow = keyRows[midpoint];
3a34b9ed3a36 Added a simple findRow() method to the MapView class that finds the first row of a given key using binary search.
Atul Varma <varmaa@toolness.com>
parents: 44
diff changeset
609 if (key < keyRow.key)
3a34b9ed3a36 Added a simple findRow() method to the MapView class that finds the first row of a given key using binary search.
Atul Varma <varmaa@toolness.com>
parents: 44
diff changeset
610 return findRow(key, keyRows.slice(0, midpoint));
3a34b9ed3a36 Added a simple findRow() method to the MapView class that finds the first row of a given key using binary search.
Atul Varma <varmaa@toolness.com>
parents: 44
diff changeset
611 if (key > keyRow.key)
3a34b9ed3a36 Added a simple findRow() method to the MapView class that finds the first row of a given key using binary search.
Atul Varma <varmaa@toolness.com>
parents: 44
diff changeset
612 return findRow(key, keyRows.slice(midpoint));
3a34b9ed3a36 Added a simple findRow() method to the MapView class that finds the first row of a given key using binary search.
Atul Varma <varmaa@toolness.com>
parents: 44
diff changeset
613 return keyRow.pos;
3a34b9ed3a36 Added a simple findRow() method to the MapView class that finds the first row of a given key using binary search.
Atul Varma <varmaa@toolness.com>
parents: 44
diff changeset
614 } else
3a34b9ed3a36 Added a simple findRow() method to the MapView class that finds the first row of a given key using binary search.
Atul Varma <varmaa@toolness.com>
parents: 44
diff changeset
615 return keyRows[0].pos;
3a34b9ed3a36 Added a simple findRow() method to the MapView class that finds the first row of a given key using binary search.
Atul Varma <varmaa@toolness.com>
parents: 44
diff changeset
616 }
3a34b9ed3a36 Added a simple findRow() method to the MapView class that finds the first row of a given key using binary search.
Atul Varma <varmaa@toolness.com>
parents: 44
diff changeset
617
54
3607a6844691 Rows of a map view are now sorted secondarily by document ID, which makes their row order well-defined.
Atul Varma <varmaa@toolness.com>
parents: 52
diff changeset
618 this.rows = rows;
45
3a34b9ed3a36 Added a simple findRow() method to the MapView class that finds the first row of a given key using binary search.
Atul Varma <varmaa@toolness.com>
parents: 44
diff changeset
619 this.findRow = function MV_findRow(key) {
3a34b9ed3a36 Added a simple findRow() method to the MapView class that finds the first row of a given key using binary search.
Atul Varma <varmaa@toolness.com>
parents: 44
diff changeset
620 return findRow(key, keyRows);
3a34b9ed3a36 Added a simple findRow() method to the MapView class that finds the first row of a given key using binary search.
Atul Varma <varmaa@toolness.com>
parents: 44
diff changeset
621 };
0
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
622 }
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
623 };