annotate browser-couch.js @ 28:6b79ea22097e

Decoupled storage implementation more by creating a LocalStorage class.
author Atul Varma <varmaa@toolness.com>
date Mon, 13 Apr 2009 16:08:52 -0700
parents fd8f4c369dc4
children 7fe72409d8f1
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
26
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
37 var ModuleLoader = {
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
38 LIBS: {JSON: "json2.js"},
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
39
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
40 require: function ML_require(libs, cb) {
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
41 var self = this;
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
42 var i = 0;
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
43 var lastLib = "";
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
44
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
45 if (libs.constructor.name != "Array")
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
46 libs = [libs];
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
47
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
48 function loadNextLib() {
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
49 if (lastLib && !window[lastLib])
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
50 throw new Error("Failed to load library: " + lastLib);
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
51 if (i == libs.length)
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
52 cb();
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
53 else {
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
54 var libName = libs[i];
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
55 i += 1;
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
56 if (window.libName)
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
57 loadNextLib();
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
58 else {
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
59 var libUrl = self.LIBS[libName];
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
60 if (!libUrl)
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
61 throw new Error("Unknown lib: " + libName);
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
62 lastLib = libName;
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
63 self._loadScript(libUrl, window, loadNextLib);
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
64 }
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
65 }
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
66 }
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
67
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
68 loadNextLib();
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
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
71 _loadScript: function ML__loadScript(url, window, cb) {
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
72 var doc = window.document;
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
73 var script = doc.createElement("script");
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
74 script.setAttribute("src", url);
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
75 script.addEventListener(
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
76 "load",
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
77 function onLoad() {
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
78 script.removeEventListener("load", onLoad, false);
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
79 cb();
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
80 },
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
81 false
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
82 );
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
83 doc.body.appendChild(script);
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
84 }
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
85 };
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
86
28
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
87 function LocalStorage(JSON) {
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
88 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
89
28
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
90 if (window.globalStorage)
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
91 storage = window.globalStorage[location.hostname];
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
92 else {
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
93 if (window.localStorage)
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
94 storage = window.localStorage;
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
95 else
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
96 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
97 }
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
98
28
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
99 function ensureJSON(cb) {
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
100 if (!JSON) {
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
101 ModuleLoader.require(
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
102 "JSON",
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
103 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
104 JSON = window.JSON;
28
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
105 cb();
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
106 });
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
107 } else
28
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
108 cb();
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
109 }
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
110
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
111 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
112 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
113 ensureJSON(
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
114 function() {
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
115 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
116 cb(obj);
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
117 });
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
118 else
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
119 cb(null);
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
120 };
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
121
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
122 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
123 ensureJSON(
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
124 function() {
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
125 storage[name] = JSON.stringify(obj);
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
126 cb();
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
127 });
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
128 };
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
129 }
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
130
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
131 var BrowserCouch = {
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
132 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
133 if (!storage)
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
134 storage = new LocalStorage();
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
135
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
136 new this._DB(name, storage, new this._Dictionary(), cb);
0
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
137 },
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
138
28
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
139 _Dictionary: function BC__Dictionary() {
18
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
140 var dict = {};
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
141 var keys = [];
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
142
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
143 function regenerateKeys() {
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
144 keys = [];
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
145 for (key in dict)
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
146 keys.push(key);
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
147 }
12
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
148
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
149 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
150 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
151 };
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
152
18
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
153 this.getKeys = function Dictionary_getKeys() {
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
154 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
155 };
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
156
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
157 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
158 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
159 };
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
160
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
161 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
162 if (!(key in dict))
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
163 keys.push(key);
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
164 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
165 };
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
166
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
167 this.delete = function Dictionary_delete(key) {
18
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
168 delete dict[key];
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
169
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
170 // 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
171 // 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
172 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
173 };
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
174
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
175 this.clear = function Dictionary_clear() {
18
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
176 dict = {};
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
177 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
178 };
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
179
28
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
180 this.pickle = function Dictionary_pickle() {
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
181 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
182 };
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
183
28
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
184 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
185 dict = obj;
18
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
186 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
187 };
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
188 },
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
189
28
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
190 _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
191 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
192 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
193
28
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
194 storage.get(
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
195 dbName,
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
196 function(obj) {
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
197 if (obj)
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
198 dict.unpickle(obj);
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
199 cb(self);
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
200 });
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
201
28
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
202 function commitToStorage(cb) {
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
203 if (!cb)
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
204 cb = function() {};
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
205 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
206 }
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
207
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
208 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
209 dict.clear();
28
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
210 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
211 };
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
212
0
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
213 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
214 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
215 cb(dict.get(id));
0
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
216 else
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
217 cb(null);
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
218 };
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
219
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
220 this.put = function DB_put(document, cb) {
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
221 if (document.constructor.name == "Array") {
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
222 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
223 dict.set(document[i].id, document[i]);
0
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
224 } 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
225 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
226
28
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
227 commitToStorage(cb);
0
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
228 };
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
229
22
7b418d9d27f8 Added a getLength() method to DB.
Atul Varma <varmaa@toolness.com>
parents: 21
diff changeset
230 this.getLength = function DB_getLength() {
7b418d9d27f8 Added a getLength() method to DB.
Atul Varma <varmaa@toolness.com>
parents: 21
diff changeset
231 return dict.getKeys().length;
7b418d9d27f8 Added a getLength() method to DB.
Atul Varma <varmaa@toolness.com>
parents: 21
diff changeset
232 };
7b418d9d27f8 Added a getLength() method to DB.
Atul Varma <varmaa@toolness.com>
parents: 21
diff changeset
233
0
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
234 this.view = function DB_view(options) {
11
89c6105c2d1e Added a TODO.
Atul Varma <varmaa@toolness.com>
parents: 10
diff changeset
235 // TODO: Add support for worker threads.
89c6105c2d1e Added a TODO.
Atul Varma <varmaa@toolness.com>
parents: 10
diff changeset
236
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
237 if (!options.map)
0
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
238 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
239 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
240 throw new Error('finished callback not provided');
0
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
241
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
242 BrowserCouch._mapReduce(options.map,
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
243 options.reduce,
12
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
244 dict,
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
245 options.progress,
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
246 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
247 options.chunkSize);
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
248 };
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
249 },
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
250
12
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
251 _mapReduce: function BC__mapReduce(map, reduce, dict, progress,
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
252 finished, chunkSize) {
19
3dd04250a3d2 Made map-reduce obey couchDB's return value, simplified implementation a bit.
Atul Varma <varmaa@toolness.com>
parents: 18
diff changeset
253 var mapDict = {};
16
53b9f2e988da Refactored reduce implementation so it'll be parallelizable/chunkable like map currently is.
Atul Varma <varmaa@toolness.com>
parents: 15
diff changeset
254 var mapKeys = [];
18
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
255 var keys = dict.getKeys();
20
1d8e0d388549 reduce phase now supports progress callbacks/asynchronous behavior.
Atul Varma <varmaa@toolness.com>
parents: 19
diff changeset
256 var rows = [];
15
01237459756d Realized I had the wrong idea of what reduce was doing and reimpemented it properly.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
257 var currDoc;
0
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
258
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
259 function emit(key, value) {
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
260 // TODO: This assumes that the key will always be
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
261 // an indexable value. We may have to hash the value,
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
262 // though, if it's e.g. an Object.
19
3dd04250a3d2 Made map-reduce obey couchDB's return value, simplified implementation a bit.
Atul Varma <varmaa@toolness.com>
parents: 18
diff changeset
263 if (!(key in mapDict)) {
3dd04250a3d2 Made map-reduce obey couchDB's return value, simplified implementation a bit.
Atul Varma <varmaa@toolness.com>
parents: 18
diff changeset
264 mapKeys.push(key);
3dd04250a3d2 Made map-reduce obey couchDB's return value, simplified implementation a bit.
Atul Varma <varmaa@toolness.com>
parents: 18
diff changeset
265 mapDict[key] = {keys: [], values: []};
15
01237459756d Realized I had the wrong idea of what reduce was doing and reimpemented it properly.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
266 }
19
3dd04250a3d2 Made map-reduce obey couchDB's return value, simplified implementation a bit.
Atul Varma <varmaa@toolness.com>
parents: 18
diff changeset
267 mapDict[key].keys.push([key, currDoc.id]);
3dd04250a3d2 Made map-reduce obey couchDB's return value, simplified implementation a bit.
Atul Varma <varmaa@toolness.com>
parents: 18
diff changeset
268 mapDict[key].values.push(value);
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
269 }
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
270
12
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
271 // Maximum number of items to process before giving the UI a chance
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
272 // to breathe.
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
273 var DEFAULT_CHUNK_SIZE = 1000;
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
274
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
275 // If no progress callback is given, we'll automatically give the
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
276 // UI a chance to breathe for this many milliseconds before continuing
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
277 // processing.
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
278 var DEFAULT_UI_BREATHE_TIME = 50;
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
279
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
280 if (!chunkSize)
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
281 chunkSize = DEFAULT_CHUNK_SIZE;
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
282 var i = 0;
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
283
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
284 function continueMap() {
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
285 var iAtStart = i;
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
286
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
287 do {
18
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
288 currDoc = dict.get(keys[i]);
15
01237459756d Realized I had the wrong idea of what reduce was doing and reimpemented it properly.
Atul Varma <varmaa@toolness.com>
parents: 14
diff changeset
289 map(currDoc, emit);
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
290 i++;
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
291 } while (i - iAtStart < chunkSize &&
18
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
292 i < keys.length)
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
293
18
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
294 if (i == keys.length)
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
295 doReduce();
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
296 else {
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
297 if (progress)
20
1d8e0d388549 reduce phase now supports progress callbacks/asynchronous behavior.
Atul Varma <varmaa@toolness.com>
parents: 19
diff changeset
298 progress("map", i / keys.length, continueMap);
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
299 else
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
300 window.setTimeout(continueMap, DEFAULT_UI_BREATHE_TIME);
0
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
301 }
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
302 }
0
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
303
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
304 continueMap();
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
305
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
306 function doReduce() {
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
307 if (reduce) {
20
1d8e0d388549 reduce phase now supports progress callbacks/asynchronous behavior.
Atul Varma <varmaa@toolness.com>
parents: 19
diff changeset
308 var i = 0;
1d8e0d388549 reduce phase now supports progress callbacks/asynchronous behavior.
Atul Varma <varmaa@toolness.com>
parents: 19
diff changeset
309
1d8e0d388549 reduce phase now supports progress callbacks/asynchronous behavior.
Atul Varma <varmaa@toolness.com>
parents: 19
diff changeset
310 function continueReduce() {
1d8e0d388549 reduce phase now supports progress callbacks/asynchronous behavior.
Atul Varma <varmaa@toolness.com>
parents: 19
diff changeset
311 var iAtStart = i;
1d8e0d388549 reduce phase now supports progress callbacks/asynchronous behavior.
Atul Varma <varmaa@toolness.com>
parents: 19
diff changeset
312
1d8e0d388549 reduce phase now supports progress callbacks/asynchronous behavior.
Atul Varma <varmaa@toolness.com>
parents: 19
diff changeset
313 do {
1d8e0d388549 reduce phase now supports progress callbacks/asynchronous behavior.
Atul Varma <varmaa@toolness.com>
parents: 19
diff changeset
314 var key = mapKeys[i];
1d8e0d388549 reduce phase now supports progress callbacks/asynchronous behavior.
Atul Varma <varmaa@toolness.com>
parents: 19
diff changeset
315 var item = mapDict[key];
1d8e0d388549 reduce phase now supports progress callbacks/asynchronous behavior.
Atul Varma <varmaa@toolness.com>
parents: 19
diff changeset
316 rows.push({key: key,
1d8e0d388549 reduce phase now supports progress callbacks/asynchronous behavior.
Atul Varma <varmaa@toolness.com>
parents: 19
diff changeset
317 value: reduce(item.keys, item.values)});
1d8e0d388549 reduce phase now supports progress callbacks/asynchronous behavior.
Atul Varma <varmaa@toolness.com>
parents: 19
diff changeset
318 i++;
1d8e0d388549 reduce phase now supports progress callbacks/asynchronous behavior.
Atul Varma <varmaa@toolness.com>
parents: 19
diff changeset
319 } while (i - iAtStart < chunkSize &&
1d8e0d388549 reduce phase now supports progress callbacks/asynchronous behavior.
Atul Varma <varmaa@toolness.com>
parents: 19
diff changeset
320 i < mapKeys.length)
1d8e0d388549 reduce phase now supports progress callbacks/asynchronous behavior.
Atul Varma <varmaa@toolness.com>
parents: 19
diff changeset
321
1d8e0d388549 reduce phase now supports progress callbacks/asynchronous behavior.
Atul Varma <varmaa@toolness.com>
parents: 19
diff changeset
322 if (i == mapKeys.length)
1d8e0d388549 reduce phase now supports progress callbacks/asynchronous behavior.
Atul Varma <varmaa@toolness.com>
parents: 19
diff changeset
323 doSort();
1d8e0d388549 reduce phase now supports progress callbacks/asynchronous behavior.
Atul Varma <varmaa@toolness.com>
parents: 19
diff changeset
324 else {
1d8e0d388549 reduce phase now supports progress callbacks/asynchronous behavior.
Atul Varma <varmaa@toolness.com>
parents: 19
diff changeset
325 if (progress)
1d8e0d388549 reduce phase now supports progress callbacks/asynchronous behavior.
Atul Varma <varmaa@toolness.com>
parents: 19
diff changeset
326 progress("reduce", i / mapKeys.length, continueReduce);
1d8e0d388549 reduce phase now supports progress callbacks/asynchronous behavior.
Atul Varma <varmaa@toolness.com>
parents: 19
diff changeset
327 else
1d8e0d388549 reduce phase now supports progress callbacks/asynchronous behavior.
Atul Varma <varmaa@toolness.com>
parents: 19
diff changeset
328 window.setTimeout(continueReduce, DEFAULT_UI_BREATHE_TIME);
1d8e0d388549 reduce phase now supports progress callbacks/asynchronous behavior.
Atul Varma <varmaa@toolness.com>
parents: 19
diff changeset
329 }
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
330 }
20
1d8e0d388549 reduce phase now supports progress callbacks/asynchronous behavior.
Atul Varma <varmaa@toolness.com>
parents: 19
diff changeset
331
1d8e0d388549 reduce phase now supports progress callbacks/asynchronous behavior.
Atul Varma <varmaa@toolness.com>
parents: 19
diff changeset
332 continueReduce();
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
333 } else {
16
53b9f2e988da Refactored reduce implementation so it'll be parallelizable/chunkable like map currently is.
Atul Varma <varmaa@toolness.com>
parents: 15
diff changeset
334 for (i = 0; i < mapKeys.length; i++) {
19
3dd04250a3d2 Made map-reduce obey couchDB's return value, simplified implementation a bit.
Atul Varma <varmaa@toolness.com>
parents: 18
diff changeset
335 var key = mapKeys[i];
3dd04250a3d2 Made map-reduce obey couchDB's return value, simplified implementation a bit.
Atul Varma <varmaa@toolness.com>
parents: 18
diff changeset
336 var item = mapDict[key];
3dd04250a3d2 Made map-reduce obey couchDB's return value, simplified implementation a bit.
Atul Varma <varmaa@toolness.com>
parents: 18
diff changeset
337 for (var j = 0; j < item.keys.length; j++) {
3dd04250a3d2 Made map-reduce obey couchDB's return value, simplified implementation a bit.
Atul Varma <varmaa@toolness.com>
parents: 18
diff changeset
338 var id = item.keys[j][1];
3dd04250a3d2 Made map-reduce obey couchDB's return value, simplified implementation a bit.
Atul Varma <varmaa@toolness.com>
parents: 18
diff changeset
339 var value = item.values[j];
3dd04250a3d2 Made map-reduce obey couchDB's return value, simplified implementation a bit.
Atul Varma <varmaa@toolness.com>
parents: 18
diff changeset
340 rows.push({id: id,
3dd04250a3d2 Made map-reduce obey couchDB's return value, simplified implementation a bit.
Atul Varma <varmaa@toolness.com>
parents: 18
diff changeset
341 key: key,
3dd04250a3d2 Made map-reduce obey couchDB's return value, simplified implementation a bit.
Atul Varma <varmaa@toolness.com>
parents: 18
diff changeset
342 value: value});
3dd04250a3d2 Made map-reduce obey couchDB's return value, simplified implementation a bit.
Atul Varma <varmaa@toolness.com>
parents: 18
diff changeset
343 }
16
53b9f2e988da Refactored reduce implementation so it'll be parallelizable/chunkable like map currently is.
Atul Varma <varmaa@toolness.com>
parents: 15
diff changeset
344 }
20
1d8e0d388549 reduce phase now supports progress callbacks/asynchronous behavior.
Atul Varma <varmaa@toolness.com>
parents: 19
diff changeset
345
1d8e0d388549 reduce phase now supports progress callbacks/asynchronous behavior.
Atul Varma <varmaa@toolness.com>
parents: 19
diff changeset
346 doSort();
0
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
347 }
20
1d8e0d388549 reduce phase now supports progress callbacks/asynchronous behavior.
Atul Varma <varmaa@toolness.com>
parents: 19
diff changeset
348 }
1d8e0d388549 reduce phase now supports progress callbacks/asynchronous behavior.
Atul Varma <varmaa@toolness.com>
parents: 19
diff changeset
349
1d8e0d388549 reduce phase now supports progress callbacks/asynchronous behavior.
Atul Varma <varmaa@toolness.com>
parents: 19
diff changeset
350 function doSort() {
19
3dd04250a3d2 Made map-reduce obey couchDB's return value, simplified implementation a bit.
Atul Varma <varmaa@toolness.com>
parents: 18
diff changeset
351 rows.sort(function compare(a, b) {
3dd04250a3d2 Made map-reduce obey couchDB's return value, simplified implementation a bit.
Atul Varma <varmaa@toolness.com>
parents: 18
diff changeset
352 if (a.key < b.key)
3dd04250a3d2 Made map-reduce obey couchDB's return value, simplified implementation a bit.
Atul Varma <varmaa@toolness.com>
parents: 18
diff changeset
353 return -1;
3dd04250a3d2 Made map-reduce obey couchDB's return value, simplified implementation a bit.
Atul Varma <varmaa@toolness.com>
parents: 18
diff changeset
354 if (a.key > b.key)
3dd04250a3d2 Made map-reduce obey couchDB's return value, simplified implementation a bit.
Atul Varma <varmaa@toolness.com>
parents: 18
diff changeset
355 return 1;
3dd04250a3d2 Made map-reduce obey couchDB's return value, simplified implementation a bit.
Atul Varma <varmaa@toolness.com>
parents: 18
diff changeset
356 return 0;
3dd04250a3d2 Made map-reduce obey couchDB's return value, simplified implementation a bit.
Atul Varma <varmaa@toolness.com>
parents: 18
diff changeset
357 });
3dd04250a3d2 Made map-reduce obey couchDB's return value, simplified implementation a bit.
Atul Varma <varmaa@toolness.com>
parents: 18
diff changeset
358 finished({rows: rows});
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
359 }
0
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
360 }
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
361 };