Mercurial > browser-couch
changeset 46:6da7638d056f
Added findRow() method to the View class.
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Tue, 14 Apr 2009 11:57:12 -0700 |
parents | 3a34b9ed3a36 |
children | 7664a4e099b5 |
files | browser-couch.js tests.js |
diffstat | 2 files changed, 34 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/browser-couch.js Tue Apr 14 11:50:46 2009 -0700 +++ b/browser-couch.js Tue Apr 14 11:57:12 2009 -0700 @@ -409,6 +409,23 @@ _View: function BC__View(rows) { this.rows = rows; + + function findRow(key, rows) { + if (rows.length > 1) { + var midpoint = Math.floor(rows.length / 2); + var row = rows[midpoint]; + if (key < row.key) + return findRow(key, rows.slice(0, midpoint)); + if (key > row.key) + return midpoint + findRow(key, rows.slice(midpoint)); + return midpoint; + } else + return 0; + } + + this.findRow = function V_findRow(key) { + return findRow(key, rows); + }; }, _MapView: function BC__MapView(mapDict) {
--- a/tests.js Tue Apr 14 11:50:46 2009 -0700 +++ b/tests.js Tue Apr 14 11:57:12 2009 -0700 @@ -150,6 +150,23 @@ }}); }); }, + testViewMapReduceFindRow_async: function(self) { + var map = this._mapWordFrequencies; + var reduce = this._reduceWordFrequencies; + this._setupTestDb( + function(db) { + db.view( + {map: map, + reduce: reduce, + finished: function(view) { + self.assertEqual(view.findRow("dogen"), 0); + self.assertEqual(view.findRow("dude"), 1); + self.assertEqual(view.findRow("hello"), 2); + self.assertEqual(view.findRow("there"), 3); + self.done(); + }}); + }); + }, testViewMapReduce_async: function(self) { var map = this._mapWordFrequencies; var reduce = this._reduceWordFrequencies;