annotate browser-couch.js @ 47:7664a4e099b5

Factored-out map-reduce implementation into a new interface, which currently has one implementation, SingleThreadedMapReducer.
author Atul Varma <varmaa@toolness.com>
date Tue, 14 Apr 2009 12:45:44 -0700
parents 6da7638d056f
children b0d5bc990a80
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
41
3fd3aacf33fb Test suite now passes on Safari.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
37 function isArray(value) {
3fd3aacf33fb Test suite now passes on Safari.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
38 // Taken from "Remedial Javascript" by Douglas Crockford:
3fd3aacf33fb Test suite now passes on Safari.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
39 // http://javascript.crockford.com/remedial.html
3fd3aacf33fb Test suite now passes on Safari.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
40
3fd3aacf33fb Test suite now passes on Safari.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
41 return (typeof value.length === 'number' &&
3fd3aacf33fb Test suite now passes on Safari.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
42 !(value.propertyIsEnumerable('length')) &&
3fd3aacf33fb Test suite now passes on Safari.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
43 typeof value.splice === 'function');
3fd3aacf33fb Test suite now passes on Safari.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
44 }
3fd3aacf33fb Test suite now passes on Safari.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
45
26
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
46 var ModuleLoader = {
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
47 LIBS: {JSON: "json2.js"},
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
48
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
49 require: function ML_require(libs, cb) {
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
50 var self = this;
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
51 var i = 0;
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
52 var lastLib = "";
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
53
41
3fd3aacf33fb Test suite now passes on Safari.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
54 if (!isArray(libs))
26
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
55 libs = [libs];
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
56
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
57 function loadNextLib() {
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
58 if (lastLib && !window[lastLib])
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
59 throw new Error("Failed to load library: " + lastLib);
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
60 if (i == libs.length)
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
61 cb();
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
62 else {
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
63 var libName = libs[i];
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
64 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
65 if (window[libName])
26
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
66 loadNextLib();
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
67 else {
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
68 var libUrl = self.LIBS[libName];
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
69 if (!libUrl)
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
70 throw new Error("Unknown lib: " + libName);
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
71 lastLib = libName;
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
72 self._loadScript(libUrl, window, loadNextLib);
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
73 }
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
74 }
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
75 }
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
76
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
77 loadNextLib();
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
78 },
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
79
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
80 _loadScript: function ML__loadScript(url, window, cb) {
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
81 var doc = window.document;
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
82 var script = doc.createElement("script");
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
83 script.setAttribute("src", url);
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
84 script.addEventListener(
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
85 "load",
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
86 function onLoad() {
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
87 script.removeEventListener("load", onLoad, false);
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
88 cb();
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 false
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 doc.body.appendChild(script);
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
93 }
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
94 };
fd8f4c369dc4 Added a more robust module loader.
Atul Varma <varmaa@toolness.com>
parents: 24
diff changeset
95
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
96 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
97 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
98 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
99 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
100 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
101 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
102
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
103 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
104 // 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
105 // 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
106 // 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
107 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
108 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
109 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
110 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
111 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
112 }
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
113
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
114 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
115
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
116 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
117 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
118
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
119 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
120 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
121 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
122 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
123 } 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
124 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
125
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
126 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
127 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
128 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
129 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
130 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
131 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
132 } 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
133 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
134 }
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
135
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
136 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
137 },
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
138
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
139 reduce: function STMR_reduce(reduce, mapResult, 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
140 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
141 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
142 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
143 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
144
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
145 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
146
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
147 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
148 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
149
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
150 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
151 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
152 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
153
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
154 // TODO: The map() method is only available on JS 1.6.
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
155 var keys = item.keys.map(function pairKeyWithDocId(docId) {
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
156 return [key, docId];
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
157 });
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
158 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
159 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
160 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
161 } 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
162 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
163
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
164 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
165 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
166 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
167 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
168 }
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
169
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
170 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
171 }
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
172 };
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
173
29
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
174 function FakeStorage() {
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
175 var db = {};
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
176
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
177 function deepCopy(obj) {
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
178 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
179 var copy;
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
180
41
3fd3aacf33fb Test suite now passes on Safari.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
181 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
182 copy = new Array();
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
183 else
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
184 copy = new Object();
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
185
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
186 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
187 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
188 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
189 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
190 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
191 else
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
192 copy[name] = property;
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
193 }
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
194 }
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
195
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
196 return copy;
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
197 } else
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
198 return obj;
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
199 }
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
200
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
201 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
202 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
203 cb(null);
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
204 else
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
205 cb(db[name]);
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
206 };
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
207
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
208 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
209 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
210 cb();
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
211 };
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
212 };
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
213
28
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
214 function LocalStorage(JSON) {
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
215 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
216
28
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
217 if (window.globalStorage)
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
218 storage = window.globalStorage[location.hostname];
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
219 else {
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
220 if (window.localStorage)
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
221 storage = window.localStorage;
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
222 else
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
223 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
224 }
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
225
28
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
226 function ensureJSON(cb) {
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
227 if (!JSON) {
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
228 ModuleLoader.require(
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
229 "JSON",
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
230 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
231 JSON = window.JSON;
28
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
232 cb();
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
233 });
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
234 } else
28
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
235 cb();
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
236 }
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
237
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
238 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
239 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
240 ensureJSON(
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
241 function() {
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
242 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
243 cb(obj);
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
244 });
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
245 else
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
246 cb(null);
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
247 };
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
248
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
249 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
250 ensureJSON(
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
251 function() {
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
252 storage[name] = JSON.stringify(obj);
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
253 cb();
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
254 });
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
255 };
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
256 }
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
257
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
258 var BrowserCouch = {
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
259 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
260 if (!storage)
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
261 storage = new LocalStorage();
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
262
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
263 new this._DB(name, storage, new this._Dictionary(), cb);
0
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
264 },
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
265
28
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
266 _Dictionary: function BC__Dictionary() {
18
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
267 var dict = {};
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
268 var keys = [];
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
269
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
270 function regenerateKeys() {
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
271 keys = [];
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
272 for (key in dict)
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
273 keys.push(key);
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
274 }
12
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
275
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
276 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
277 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
278 };
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
279
18
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
280 this.getKeys = function Dictionary_getKeys() {
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
281 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
282 };
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
283
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
284 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
285 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
286 };
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
287
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
288 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
289 if (!(key in dict))
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
290 keys.push(key);
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
291 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
292 };
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
293
41
3fd3aacf33fb Test suite now passes on Safari.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
294 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
295 delete dict[key];
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
296
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
297 // 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
298 // 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
299 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
300 };
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
301
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
302 this.clear = function Dictionary_clear() {
18
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
303 dict = {};
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
304 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
305 };
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
306
28
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
307 this.pickle = function Dictionary_pickle() {
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
308 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
309 };
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
310
28
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
311 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
312 dict = obj;
18
75b17e0a7897 Refactoring: made implementation of dictionary a lot simpler.
Atul Varma <varmaa@toolness.com>
parents: 17
diff changeset
313 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
314 };
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
315 },
9ed45c82a731 Refactoring: made a new ordered dictionary class and made the DB use that.
Atul Varma <varmaa@toolness.com>
parents: 11
diff changeset
316
28
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
317 _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
318 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
319 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
320
28
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
321 function commitToStorage(cb) {
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
322 if (!cb)
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
323 cb = function() {};
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
324 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
325 }
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
326
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
327 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
328 dict.clear();
28
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
329 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
330 };
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
331
0
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
332 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
333 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
334 cb(dict.get(id));
0
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
335 else
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
336 cb(null);
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
337 };
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
338
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
339 this.put = function DB_put(document, cb) {
41
3fd3aacf33fb Test suite now passes on Safari.
Atul Varma <varmaa@toolness.com>
parents: 37
diff changeset
340 if (isArray(document)) {
0
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
341 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
342 dict.set(document[i].id, document[i]);
0
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
343 } 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
344 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
345
28
6b79ea22097e Decoupled storage implementation more by creating a LocalStorage class.
Atul Varma <varmaa@toolness.com>
parents: 26
diff changeset
346 commitToStorage(cb);
0
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
347 };
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
348
22
7b418d9d27f8 Added a getLength() method to DB.
Atul Varma <varmaa@toolness.com>
parents: 21
diff changeset
349 this.getLength = function DB_getLength() {
7b418d9d27f8 Added a getLength() method to DB.
Atul Varma <varmaa@toolness.com>
parents: 21
diff changeset
350 return dict.getKeys().length;
7b418d9d27f8 Added a getLength() method to DB.
Atul Varma <varmaa@toolness.com>
parents: 21
diff changeset
351 };
7b418d9d27f8 Added a getLength() method to DB.
Atul Varma <varmaa@toolness.com>
parents: 21
diff changeset
352
0
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
353 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
354 if (!options.map)
0
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
355 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
356 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
357 throw new Error('finished callback not provided');
0
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
358
37
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
359 // 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
360 // to breathe.
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
361 var DEFAULT_CHUNK_SIZE = 1000;
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
362
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
363 // 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
364 // 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
365 // processing.
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
366 var DEFAULT_UI_BREATHE_TIME = 50;
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
367
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
368 var chunkSize = options.chunkSize;
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
369 if (!chunkSize)
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
370 chunkSize = DEFAULT_CHUNK_SIZE;
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
371
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
372 var progress = options.progress;
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
373 if (!progress)
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
374 progress = function defaultProgress(phase, percent, resume) {
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
375 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
376 };
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
377
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
378 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
379 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
380 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
381
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
382 mapReducer.map(
37
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
383 options.map,
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
384 dict,
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
385 progress,
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
386 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
387 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
388 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
389 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
390 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
391 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
392 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
393 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
394 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
395 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
396 });
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
397 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
398 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
399 });
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
400 };
29
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
401
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
402 storage.get(
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
403 dbName,
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
404 function(obj) {
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
405 if (obj)
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
406 dict.unpickle(obj);
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
407 cb(self);
7fe72409d8f1 Added a FakeStorage backend and made the big test use it.
Atul Varma <varmaa@toolness.com>
parents: 28
diff changeset
408 });
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
409 },
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
410
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
411 _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
412 this.rows = rows;
46
6da7638d056f Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
413
6da7638d056f Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
414 function findRow(key, rows) {
6da7638d056f Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
415 if (rows.length > 1) {
6da7638d056f Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
416 var midpoint = Math.floor(rows.length / 2);
6da7638d056f Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
417 var row = rows[midpoint];
6da7638d056f Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
418 if (key < row.key)
6da7638d056f Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
419 return findRow(key, rows.slice(0, midpoint));
6da7638d056f Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
420 if (key > row.key)
6da7638d056f Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
421 return midpoint + findRow(key, rows.slice(midpoint));
6da7638d056f Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
422 return midpoint;
6da7638d056f Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
423 } else
6da7638d056f Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
424 return 0;
6da7638d056f Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
425 }
6da7638d056f Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
426
6da7638d056f Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
427 this.findRow = function V_findRow(key) {
6da7638d056f Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
428 return findRow(key, rows);
6da7638d056f Added findRow() method to the View class.
Atul Varma <varmaa@toolness.com>
parents: 45
diff changeset
429 };
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
430 },
37
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
431
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
432 _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
433 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
434 var keyRows = [];
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
435 this.rows = rows;
37
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
436
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
437 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
438 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
439
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
440 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
441 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
442 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
443 keyRows.push({key: key, pos: rows.length});
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
444 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
445 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
446 var value = item.values[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
447 rows.push({id: id,
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
448 key: key,
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
449 value: value});
37
cf2122f596c8 Separated mapReduce() into separate map() and reduce() functions.
Atul Varma <varmaa@toolness.com>
parents: 36
diff changeset
450 }
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
451 }
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
452
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
453 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
454 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
455 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
456 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
457 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
458 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
459 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
460 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
461 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
462 } 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
463 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
464 }
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
465
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
466 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
467 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
468 };
0
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
469 }
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
470 };