browser-couch
changeset 44:fec67509fc3d
Made new View and MapView classes that encapsulate view results, and in the near future will allow searching through views.
| author | Atul Varma <varmaa@toolness.com> |
|---|---|
| date | Tue Apr 14 11:25:34 2009 -0700 (3 years ago) |
| parents | eba5866b6adc |
| children | 3a34b9ed3a36 |
| files | browser-couch.js |
line diff
1.1 --- a/browser-couch.js Tue Apr 14 10:53:27 2009 -0700
1.2 +++ b/browser-couch.js Tue Apr 14 11:25:34 2009 -0700
1.3 @@ -305,11 +305,17 @@
1.4 progress,
1.5 chunkSize,
1.6 function(mapDict) {
1.7 - BrowserCouch._reduce(options.reduce,
1.8 - mapDict,
1.9 - progress,
1.10 - chunkSize,
1.11 - options.finished);
1.12 + if (options.reduce)
1.13 + BrowserCouch._reduce(
1.14 + options.reduce,
1.15 + mapDict,
1.16 + progress,
1.17 + chunkSize,
1.18 + function(rows) {
1.19 + options.finished(new BrowserCouch._View(rows));
1.20 + });
1.21 + else
1.22 + options.finished(new BrowserCouch._MapView(mapDict));
1.23 });
1.24 };
1.25
1.26 @@ -363,57 +369,64 @@
1.27 _reduce: function BC__reduce(reduce, mapDict, progress,
1.28 chunkSize, finished) {
1.29 var rows = [];
1.30 + var mapKeys = this._makeMapKeys(mapDict);
1.31 +
1.32 + var i = 0;
1.33 +
1.34 + function continueReduce() {
1.35 + var iAtStart = i;
1.36 +
1.37 + do {
1.38 + var key = mapKeys[i];
1.39 + var item = mapDict[key];
1.40 +
1.41 + // TODO: The map() method is only available on JS 1.6.
1.42 + var keys = item.keys.map(function pairKeyWithDocId(docId) {
1.43 + return [key, docId];
1.44 + });
1.45 + rows.push({key: key,
1.46 + value: reduce(keys, item.values)});
1.47 + i++;
1.48 + } while (i - iAtStart < chunkSize &&
1.49 + i < mapKeys.length)
1.50 +
1.51 + if (i == mapKeys.length)
1.52 + finished(rows);
1.53 + else
1.54 + progress("reduce", i / mapKeys.length, continueReduce);
1.55 + }
1.56 +
1.57 + continueReduce();
1.58 + },
1.59 +
1.60 + _makeMapKeys: function BC__makeMapKeys(mapDict) {
1.61 var mapKeys = [];
1.62 for (name in mapDict)
1.63 mapKeys.push(name);
1.64 + mapKeys.sort();
1.65 + return mapKeys;
1.66 + },
1.67
1.68 - mapKeys.sort();
1.69 + _View: function BC__View(rows) {
1.70 + this.rows = rows;
1.71 + },
1.72
1.73 - if (reduce) {
1.74 - var i = 0;
1.75 + _MapView: function BC__MapView(mapDict) {
1.76 + var rows = [];
1.77
1.78 - function continueReduce() {
1.79 - var iAtStart = i;
1.80 + this.rows = rows;
1.81
1.82 - do {
1.83 - var key = mapKeys[i];
1.84 - var item = mapDict[key];
1.85 -
1.86 - // TODO: The map() method is only available on JS 1.6.
1.87 - var keys = item.keys.map(function pairKeyWithDocId(docId) {
1.88 - return [key, docId];
1.89 - });
1.90 - rows.push({key: key,
1.91 - value: reduce(keys, item.values)});
1.92 - i++;
1.93 - } while (i - iAtStart < chunkSize &&
1.94 - i < mapKeys.length)
1.95 -
1.96 - if (i == mapKeys.length)
1.97 - doneWithReduce();
1.98 - else
1.99 - progress("reduce", i / mapKeys.length, continueReduce);
1.100 + var mapKeys = BrowserCouch._makeMapKeys(mapDict);
1.101 + for (var i = 0; i < mapKeys.length; i++) {
1.102 + var key = mapKeys[i];
1.103 + var item = mapDict[key];
1.104 + for (var j = 0; j < item.keys.length; j++) {
1.105 + var id = item.keys[j];
1.106 + var value = item.values[j];
1.107 + rows.push({id: id,
1.108 + key: key,
1.109 + value: value});
1.110 }
1.111 -
1.112 - continueReduce();
1.113 - } else {
1.114 - for (i = 0; i < mapKeys.length; i++) {
1.115 - var key = mapKeys[i];
1.116 - var item = mapDict[key];
1.117 - for (var j = 0; j < item.keys.length; j++) {
1.118 - var id = item.keys[j];
1.119 - var value = item.values[j];
1.120 - rows.push({id: id,
1.121 - key: key,
1.122 - value: value});
1.123 - }
1.124 - }
1.125 -
1.126 - doneWithReduce();
1.127 - }
1.128 -
1.129 - function doneWithReduce() {
1.130 - finished({rows: rows});
1.131 }
1.132 }
1.133 };
