Mercurial > 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, 14 Apr 2009 11:25:34 -0700 |
parents | eba5866b6adc |
children | 3a34b9ed3a36 |
files | browser-couch.js |
diffstat | 1 files changed, 60 insertions(+), 47 deletions(-) [+] |
line wrap: on
line diff
--- a/browser-couch.js Tue Apr 14 10:53:27 2009 -0700 +++ b/browser-couch.js Tue Apr 14 11:25:34 2009 -0700 @@ -305,11 +305,17 @@ progress, chunkSize, function(mapDict) { - BrowserCouch._reduce(options.reduce, - mapDict, - progress, - chunkSize, - options.finished); + if (options.reduce) + BrowserCouch._reduce( + options.reduce, + mapDict, + progress, + chunkSize, + function(rows) { + options.finished(new BrowserCouch._View(rows)); + }); + else + options.finished(new BrowserCouch._MapView(mapDict)); }); }; @@ -363,57 +369,64 @@ _reduce: function BC__reduce(reduce, mapDict, progress, chunkSize, finished) { var rows = []; + var mapKeys = this._makeMapKeys(mapDict); + + var i = 0; + + function continueReduce() { + var iAtStart = i; + + do { + var key = mapKeys[i]; + var item = mapDict[key]; + + // TODO: The map() method is only available on JS 1.6. + var keys = item.keys.map(function pairKeyWithDocId(docId) { + return [key, docId]; + }); + rows.push({key: key, + value: reduce(keys, item.values)}); + i++; + } while (i - iAtStart < chunkSize && + i < mapKeys.length) + + if (i == mapKeys.length) + finished(rows); + else + progress("reduce", i / mapKeys.length, continueReduce); + } + + continueReduce(); + }, + + _makeMapKeys: function BC__makeMapKeys(mapDict) { var mapKeys = []; for (name in mapDict) mapKeys.push(name); - mapKeys.sort(); - - if (reduce) { - var i = 0; - - function continueReduce() { - var iAtStart = i; + return mapKeys; + }, - do { - var key = mapKeys[i]; - var item = mapDict[key]; + _View: function BC__View(rows) { + this.rows = rows; + }, - // TODO: The map() method is only available on JS 1.6. - var keys = item.keys.map(function pairKeyWithDocId(docId) { - return [key, docId]; - }); - rows.push({key: key, - value: reduce(keys, item.values)}); - i++; - } while (i - iAtStart < chunkSize && - i < mapKeys.length) + _MapView: function BC__MapView(mapDict) { + var rows = []; - if (i == mapKeys.length) - doneWithReduce(); - else - progress("reduce", i / mapKeys.length, continueReduce); - } + this.rows = rows; - continueReduce(); - } else { - for (i = 0; i < mapKeys.length; i++) { - var key = mapKeys[i]; - var item = mapDict[key]; - for (var j = 0; j < item.keys.length; j++) { - var id = item.keys[j]; - var value = item.values[j]; - rows.push({id: id, - key: key, - value: value}); - } + var mapKeys = BrowserCouch._makeMapKeys(mapDict); + for (var i = 0; i < mapKeys.length; i++) { + var key = mapKeys[i]; + var item = mapDict[key]; + for (var j = 0; j < item.keys.length; j++) { + var id = item.keys[j]; + var value = item.values[j]; + rows.push({id: id, + key: key, + value: value}); } - - doneWithReduce(); - } - - function doneWithReduce() { - finished({rows: rows}); } } };