annotate js/browser-couch.js @ 61:c4bf1cee771d

Linked more readably to Remedial JS.
author Atul Varma <varmaa@toolness.com>
date Wed, 15 Apr 2009 09:19:29 -0700
parents 7fff33e9786b
children c6db5e897d6b
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 //
61
c4bf1cee771d Linked more readably to Remedial JS.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
43 // A helper function to determine whether an object is an Array or
c4bf1cee771d Linked more readably to Remedial JS.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
44 // not. Taken from
c4bf1cee771d Linked more readably to Remedial JS.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
45 // [[http://javascript.crockford.com/remedial.html|Remedial JavaScript]]
c4bf1cee771d Linked more readably to Remedial JS.
Atul Varma <varmaa@toolness.com>
parents: 60
diff changeset
46 // by Douglas Crockford.
59
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
47
41
3fd3aacf33fb Test suite now passes on Safari.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
48 function isArray(value) {
3fd3aacf33fb Test suite now passes on Safari.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
49 return (typeof value.length === 'number' &&
3fd3aacf33fb Test suite now passes on Safari.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
50 !(value.propertyIsEnumerable('length')) &&
3fd3aacf33fb Test suite now passes on Safari.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
51 typeof value.splice === 'function');
3fd3aacf33fb Test suite now passes on Safari.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
52 }
3fd3aacf33fb Test suite now passes on Safari.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
53
59
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
54 // === {{{ModuleLoader}}} ===
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
55 //
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
56 // 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
57 // "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
58
26
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
59 var ModuleLoader = {
55
aa013581aeca Directory restructuring.
Atul Varma <varmaa@toolness.com>
parents: 54
diff changeset
60 LIBS: {JSON: "js/ext/json2.js"},
26
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
61
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
62 require: function ML_require(libs, cb) {
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
63 var self = this;
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
64 var i = 0;
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
65 var lastLib = "";
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
66
41
3fd3aacf33fb Test suite now passes on Safari.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
67 if (!isArray(libs))
26
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
68 libs = [libs];
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
69
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
70 function loadNextLib() {
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
71 if (lastLib && !window[lastLib])
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
72 throw new Error("Failed to load library: " + lastLib);
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
73 if (i == libs.length)
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
74 cb();
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
75 else {
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
76 var libName = libs[i];
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
77 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
78 if (window[libName])
26
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
79 loadNextLib();
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
80 else {
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
81 var libUrl = self.LIBS[libName];
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
82 if (!libUrl)
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
83 throw new Error("Unknown lib: " + libName);
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
84 lastLib = libName;
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
85 self._loadScript(libUrl, window, loadNextLib);
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
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
90 loadNextLib();
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
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
93 _loadScript: function ML__loadScript(url, window, cb) {
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
94 var doc = window.document;
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
95 var script = doc.createElement("script");
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
96 script.setAttribute("src", url);
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
97 script.addEventListener(
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
98 "load",
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
99 function onLoad() {
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
100 script.removeEventListener("load", onLoad, false);
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
101 cb();
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
102 },
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
103 false
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
104 );
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
105 doc.body.appendChild(script);
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 };
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
108
59
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
109 // == MapReducers ==
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
110 //
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
111 // //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
112 // 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
113 // 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
114 // 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
115 // 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
116
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
117 // === {{{WebWorkerMapReducer}}} ===
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
118 //
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
119 // A MapReducer that uses
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
120 // [[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
121 // 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
122 // 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
123 // calculation from the user interface.
60
7fff33e9786b Linked to web worker script file in docs.
Atul Varma <varmaa@toolness.com>
parents: 59
diff changeset
124 //
7fff33e9786b Linked to web worker script file in docs.
Atul Varma <varmaa@toolness.com>
parents: 59
diff changeset
125 // The script run by spawned Web Workers is
7fff33e9786b Linked to web worker script file in docs.
Atul Varma <varmaa@toolness.com>
parents: 59
diff changeset
126 // [[#js/worker-map-reducer.js|worker-map-reducer.js]].
59
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
127
49
a5e2db06b58d Initial proof-of-concept of a WebWorkerMapReducer. Still needs lots of work.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
128 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
129 if (!Worker)
a5e2db06b58d Initial proof-of-concept of a WebWorkerMapReducer. Still needs lots of work.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
130 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
131
a5e2db06b58d Initial proof-of-concept of a WebWorkerMapReducer. Still needs lots of work.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
132 var pool = [];
a5e2db06b58d Initial proof-of-concept of a WebWorkerMapReducer. Still needs lots of work.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
133
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
134 function MapWorker(id) {
55
aa013581aeca Directory restructuring.
Atul Varma <varmaa@toolness.com>
parents: 54
diff changeset
135 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
136 var onDone;
a5e2db06b58d Initial proof-of-concept of a WebWorkerMapReducer. Still needs lots of work.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
137
a5e2db06b58d Initial proof-of-concept of a WebWorkerMapReducer. Still needs lots of work.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
138 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
139 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
140 };
a5e2db06b58d Initial proof-of-concept of a WebWorkerMapReducer. Still needs lots of work.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
141
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
142 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
143 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
144 onDone = cb;
a5e2db06b58d Initial proof-of-concept of a WebWorkerMapReducer. Still needs lots of work.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
145 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
146 };
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
a5e2db06b58d Initial proof-of-concept of a WebWorkerMapReducer. Still needs lots of work.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
149 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
150 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
151
a5e2db06b58d Initial proof-of-concept of a WebWorkerMapReducer. Still needs lots of work.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
152 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
153 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
154 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
155 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
156 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
157
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 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
159 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
160 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
161 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
162 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
163 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
164 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
165 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
166 } 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
167 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
168 }
49
a5e2db06b58d Initial proof-of-concept of a WebWorkerMapReducer. Still needs lots of work.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
169
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
170 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
171 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
172 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
173 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
174 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
175 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
176 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
177 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
178 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
179 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
180 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
181 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
182 } 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
183 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
184
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
185 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
186 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
187 (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
188 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
189 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
190 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
191 });
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 } 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
193 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
194 }
49
a5e2db06b58d Initial proof-of-concept of a WebWorkerMapReducer. Still needs lots of work.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
195
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
196 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
197 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
198 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
199 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
200 }
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
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 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
203 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
204 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
205 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
206 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
207 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
208 }
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
209
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
210 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
211 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
212 };
a5e2db06b58d Initial proof-of-concept of a WebWorkerMapReducer. Still needs lots of work.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
213
59
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
214 // 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
215 // 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
216 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
217 };
a5e2db06b58d Initial proof-of-concept of a WebWorkerMapReducer. Still needs lots of work.
Atul Varma <varmaa@toolness.com>
parents: 48
diff changeset
218
59
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
219 // === {{{SingleThreadedMapReducer}}} ===
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
220 //
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
221 // 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
222
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
223 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
224 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
225 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
226 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
227 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
228 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
229
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 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
231 // 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
232 // 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
233 // 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
234 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
235 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
236 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
237 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
238 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
239 }
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
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 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
242
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 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
244 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
245
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 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
247 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
248 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
249 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
250 } 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
251 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
252
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 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
254 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
255 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
256 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
257 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
258 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
259 } 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
260 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
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
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
263 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
264 },
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
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 reduce: function STMR_reduce(reduce, mapResult, progress,
48
b0d5bc990a80 Typo fix
Atul Varma <varmaa@toolness.com>
parents: 47
diff changeset
267 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
268 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
269 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
270 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
271
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 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
273
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 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
275 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
276
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
277 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
278 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
279 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
280
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
281 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
282 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
283 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
284
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
285 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
286 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
287 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
288 } 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
289 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
290
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 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
292 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
293 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
294 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
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
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
297 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
298 }
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
299 };
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
300
59
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
301 // == Storage ==
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
302 //
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
303 // //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
304 // 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
305
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
306 // === {{{FakeStorage}}} ===
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
307 //
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
308 // 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
309 // 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
310 // persistent storage mechanisms are available.
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
311
29
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
312 function FakeStorage() {
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
313 var db = {};
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
314
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
315 function deepCopy(obj) {
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
316 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
317 var copy;
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
318
41
3fd3aacf33fb Test suite now passes on Safari.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
319 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
320 copy = new Array();
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
321 else
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
322 copy = new Object();
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
323
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
324 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
325 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
326 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
327 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
328 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
329 else
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
330 copy[name] = property;
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
331 }
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
332 }
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 return copy;
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
335 } else
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
336 return obj;
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
337 }
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
338
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
339 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
340 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
341 cb(null);
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
342 else
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
343 cb(db[name]);
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
344 };
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 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
347 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
348 cb();
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
349 };
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
350 };
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
351
59
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
352 // === {{{LocalStorage}}} ===
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
353 //
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
354 // 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
355 // {{{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
356
28
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
357 function LocalStorage(JSON) {
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
358 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
359
28
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
360 if (window.globalStorage)
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
361 storage = window.globalStorage[location.hostname];
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
362 else {
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
363 if (window.localStorage)
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
364 storage = window.localStorage;
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
365 else
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
366 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
367 }
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
368
28
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
369 function ensureJSON(cb) {
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
370 if (!JSON) {
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
371 ModuleLoader.require(
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
372 "JSON",
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
373 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
374 JSON = window.JSON;
28
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
375 cb();
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
376 });
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
377 } else
28
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
378 cb();
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
379 }
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
380
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
381 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
382 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
383 ensureJSON(
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
384 function() {
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
385 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
386 cb(obj);
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 else
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
389 cb(null);
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
390 };
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
391
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
392 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
393 ensureJSON(
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
394 function() {
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
395 storage[name] = JSON.stringify(obj);
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
396 cb();
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
397 });
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
398 };
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
399 }
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
400
59
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
401 // == BrowserCouch ==
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
402 //
f36a50a6042c Added really basic skeletal documentation for browser-couch.js.
Atul Varma <varmaa@toolness.com>
parents: 55
diff changeset
403 // {{{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
404 // 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
405
28
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
406 var BrowserCouch = {
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
407 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
408 if (!storage)
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
409 storage = new LocalStorage();
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
410
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
411 new this._DB(name, storage, new this._Dictionary(), cb);
0
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
412 },
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
413
28
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
414 _Dictionary: function BC__Dictionary() {
18
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
415 var dict = {};
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
416 var keys = [];
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
417
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
418 function regenerateKeys() {
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
419 keys = [];
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
420 for (key in dict)
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
421 keys.push(key);
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
422 }
12
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
423
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
424 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
425 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
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
18
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
428 this.getKeys = function Dictionary_getKeys() {
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
429 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
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.get = function Dictionary_get(key) {
18
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
433 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
434 };
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
435
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
436 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
437 if (!(key in dict))
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
438 keys.push(key);
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
439 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
440 };
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
441
41
3fd3aacf33fb Test suite now passes on Safari.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
442 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
443 delete dict[key];
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
444
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
445 // 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
446 // 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
447 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
448 };
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 this.clear = function Dictionary_clear() {
18
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
451 dict = {};
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
452 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
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.pickle = function Dictionary_pickle() {
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
456 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
457 };
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
458
28
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
459 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
460 dict = obj;
18
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
461 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
462 };
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
463 },
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
464
28
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
465 _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
466 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
467 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
468
28
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
469 function commitToStorage(cb) {
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
470 if (!cb)
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
471 cb = function() {};
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
472 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
473 }
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
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
475 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
476 dict.clear();
28
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
477 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
478 };
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
479
0
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
480 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
481 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
482 cb(dict.get(id));
0
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
483 else
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
484 cb(null);
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
485 };
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
486
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
487 this.put = function DB_put(document, cb) {
41
3fd3aacf33fb Test suite now passes on Safari.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
488 if (isArray(document)) {
0
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
489 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
490 dict.set(document[i].id, document[i]);
0
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
491 } 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
492 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
493
28
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
494 commitToStorage(cb);
0
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
495 };
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
496
22
7b418d9d27f8 Added a getLength() method to DB.
Atul Varma <varmaa@toolness.com>
parents: 21
diff changeset
497 this.getLength = function DB_getLength() {
7b418d9d27f8 Added a getLength() method to DB.
Atul Varma <varmaa@toolness.com>
parents: 21
diff changeset
498 return dict.getKeys().length;
7b418d9d27f8 Added a getLength() method to DB.
Atul Varma <varmaa@toolness.com>
parents: 21
diff changeset
499 };
7b418d9d27f8 Added a getLength() method to DB.
Atul Varma <varmaa@toolness.com>
parents: 21
diff changeset
500
0
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
501 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
502 if (!options.map)
0
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
503 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
504 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
505 throw new Error('finished callback not provided');
0
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
506
37
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
507 // 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
508 // to breathe.
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
509 var DEFAULT_CHUNK_SIZE = 1000;
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
510
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
511 // 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
512 // 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
513 // processing.
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
514 var DEFAULT_UI_BREATHE_TIME = 50;
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 chunkSize = options.chunkSize;
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
517 if (!chunkSize)
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
518 chunkSize = DEFAULT_CHUNK_SIZE;
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
519
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
520 var progress = options.progress;
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
521 if (!progress)
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
522 progress = function defaultProgress(phase, percent, resume) {
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
523 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
524 };
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
525
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
526 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
527 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
528 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
529
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
530 mapReducer.map(
37
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
531 options.map,
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
532 dict,
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
533 progress,
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
534 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
535 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
536 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
537 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
538 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
539 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
540 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
541 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
542 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
543 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
544 });
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
545 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
546 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
547 });
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
548 };
29
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
549
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
550 storage.get(
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
551 dbName,
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
552 function(obj) {
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
553 if (obj)
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
554 dict.unpickle(obj);
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
555 cb(self);
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
556 });
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
557 },
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
558
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
559 _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
560 this.rows = rows;
46
6da7638d056f Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
561
6da7638d056f Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
562 function findRow(key, rows) {
6da7638d056f Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
563 if (rows.length > 1) {
6da7638d056f Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
564 var midpoint = Math.floor(rows.length / 2);
6da7638d056f Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
565 var row = rows[midpoint];
6da7638d056f Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
566 if (key < row.key)
6da7638d056f Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
567 return findRow(key, rows.slice(0, midpoint));
6da7638d056f Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
568 if (key > row.key)
6da7638d056f Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
569 return midpoint + findRow(key, rows.slice(midpoint));
6da7638d056f Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
570 return midpoint;
6da7638d056f Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
571 } else
6da7638d056f Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
572 return 0;
6da7638d056f Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
573 }
6da7638d056f Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
574
6da7638d056f Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
575 this.findRow = function V_findRow(key) {
6da7638d056f Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
576 return findRow(key, rows);
6da7638d056f Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
577 };
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
578 },
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 _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
581 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
582 var keyRows = [];
37
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
583
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
584 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
585 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
586
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
587 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
588 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
589 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
590 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
591 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
592 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
593 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
594 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
595 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
596 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
597 value: value});
37
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
598 }
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
599 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
600 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
601 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
602 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
603 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
604 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
605 });
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
606 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
607 }
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
608
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 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
610 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
611 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
612 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
613 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
614 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
615 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
616 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
617 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
618 } 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
619 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
620 }
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
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
622 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
623 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
624 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
625 };
0
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
626 }
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
627 };