annotate browser-couch.js @ 9:8842af55f7fe

Refactored map-reduce into its own self-contained function that isn't part of a closure
author Atul Varma <varmaa@toolness.com>
date Fri, 10 Apr 2009 11:32:23 -0700
parents bd3a2f82ead2
children 0a694cb579ec
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
0
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
37 var BrowserCouch = {
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
38 get: function BC_get(name, cb, storage, JSON) {
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
39 var self = this;
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
40
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
41 function createDb() {
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
42 cb(new self._DB(name, storage, JSON));
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
43 }
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
44
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
45 if (!storage) {
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
46 if (window.globalStorage || window.localStorage) {
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
47 if (window.globalStorage)
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
48 storage = window.globalStorage[location.hostname];
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
49 else
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
50 storage = window.localStorage;
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
51 if (window.JSON) {
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
52 JSON = window.JSON;
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
53 createDb();
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
54 } else
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
55 self._loadScript(
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
56 "json2.js",
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
57 window,
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
58 function() {
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
59 if (!window.JSON)
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
60 throw new Error('JSON library failed to load');
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
61 JSON = window.JSON;
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
62 createDb();
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
63 });
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
64 } else {
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
65 /* TODO: Consider using JSPersist or something else here. */
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
66 throw new Error('unable to find persistent storage backend');
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
67 }
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
68 } else
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
69 createDb();
0
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
70 },
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
71
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
72 _loadScript: function BC__loadScript(url, window, cb) {
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
73 var doc = window.document;
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
74 var script = doc.createElement("script");
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
75 script.setAttribute("src", url);
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
76 script.addEventListener(
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
77 "load",
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
78 function onLoad() {
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
79 script.removeEventListener("load", onLoad, false);
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
80 cb();
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
81 },
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
82 false
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
83 );
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
84 doc.body.appendChild(script);
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
85 },
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
86
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
87 _DB: function BC__DB(name, storage, JSON) {
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
88 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
89
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
90 var documents = [];
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
91 var docIdIndex = {};
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
92
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
93 if (dbName in storage && storage[dbName].value) {
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
94 var db = JSON.parse(storage[dbName].value);
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
95 documents = db.documents;
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
96 docIdIndex = db.docIdIndex;
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
97 }
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
98
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
99 function commitToStorage() {
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
100 storage[dbName].value = JSON.stringify(
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
101 {documents: documents,
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
102 docIdIndex: docIdIndex}
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
103 );
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 }
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
105
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
106 this.wipe = function DB_wipe(cb) {
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 documents = [];
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
108 docIdIndex = {};
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
109 commitToStorage();
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
110 if (cb)
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
111 cb();
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
112 };
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
113
0
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
114 this.get = function DB_get(id, cb) {
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
115 if (id in docIdIndex)
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
116 cb(documents[docIdIndex[id]]);
0
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
117 else
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
118 cb(null);
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
119 };
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
120
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
121 this.put = function DB_put(document, 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
122 function putSingleDocument(doc) {
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
123 if (doc.id in docIdIndex)
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
124 documents[docIdIndex[doc.id]] = doc;
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
125 else
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
126 docIdIndex[doc.id] = documents.push(doc) - 1;
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
127 }
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
128
0
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
129 if (document.constructor.name == "Array") {
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
130 for (var i = 0; i < document.length; i++)
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
131 putSingleDocument(document[i]);
0
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
132 } else
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
133 putSingleDocument(document);
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
134 commitToStorage();
0
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
135 cb();
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
136 };
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
137
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
138 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
139 if (!options.map)
0
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
140 throw new Error('map function not provided');
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
141
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
142 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
143 options.reduce,
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
144 documents,
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
145 options.callback);
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
146 };
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
147 },
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
148
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
149 _mapReduce: function BC__mapReduce(map, reduce, documents, cb) {
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
150 var mapResult = {};
0
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
151
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
152 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
153 // 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
154 // 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
155 // though, if it's e.g. an Object.
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
156 if (!mapResult[key])
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
157 mapResult[key] = [];
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
158 mapResult[key].push(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
159 }
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
160
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
161 for (var i = 0; i < documents.length; i++)
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
162 map(documents[i], emit);
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
163
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
164 if (reduce) {
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
165 var keys = [];
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
166 var values = [];
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
167
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
168 for (key in mapResult) {
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
169 keys.push(key);
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
170 values.push(mapResult[key]);
0
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
171 }
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
172
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
173 cb(reduce(keys, values));
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
174 } else {
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
175 var result = [];
0
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
176
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
177 for (key in mapResult) {
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
178 var values = mapResult[key];
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
179 for (i = 0; i < values.length; i++)
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
180 result.push([key, values[i]]);
0
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
181 }
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
182 cb(result);
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
183 }
0
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
184 }
3c23e383cd32 Origination.
Atul Varma <varmaa@toolness.com>
parents:
diff changeset
185 };