Permalink
Browse files

bulk fetch of the revisions

  • Loading branch information...
1 parent 211ee3b commit b1d3c6d50b86f6257f4fcf996527f33999898226 @azul azul committed Jan 20, 2012
Showing with 48 additions and 22 deletions.
  1. +48 −22 couch_db.js
View
@@ -90,35 +90,61 @@ exports.database.prototype.remove = function (key, callback)
exports.database.prototype.doBulk = function (bulk, callback)
{
var _this = this;
+ var keys = [];
+ var revs = {};
var setters = [];
- async.forEach(bulk, function(item, callback)
+ for(var i in bulk)
{
- var set = {_id: item.key};
-
- // TODO: optimize me. we can get all documents in one go.
- _this.db.getDoc(item.key, function(er, doc)
+ keys.push(bulk[i].key);
+ }
+ async.series([
+ function(callback)
{
- if(doc != null)
- {
- set._rev = doc._rev;
- }
- if(item.type == "set")
+ _this.db.request({
+ method: 'POST',
+ path: '/_all_docs',
+ data: {keys: keys},
+ }, function(er, r)
{
- set.value = item.value;
- setters.push(set);
- }
- else if(item.type == "remove")
+ if (er) throw new Error(JSON.stringify(er));
+ rows = r.rows;
+ for(var j in r.rows)
+ {
+ // couchDB will return error instead of value if key does not exist
+ if(rows[j].value!=null)
+ {
+ revs[rows[j].key] = rows[j].value.rev;
+ }
+ }
+ callback();
+ });
+ },
+ function(callback)
+ {
+ for(var i in bulk)
{
- set._deleted = true;
- setters.push(set);
+ var item = bulk[i];
+ var set = {_id: item.key};
+ if(revs[item.key] != null)
+ {
+ set._rev = revs[item.key];
+ }
+ if(item.type == "set")
+ {
+ set.value = item.value;
+ setters.push(set);
+ }
+ else if(item.type == "remove")
+ {
+ set._deleted = true;
+ setters.push(set);
+ }
}
callback();
- });
- }, function(err) {
- if(err!=null)
- console.error(err);
- _this.db.bulkDocs({docs: setters}, callback);
- });
+ }], function(err) {
+ _this.db.bulkDocs({docs: setters}, callback);
+ }
+ );
}
exports.database.prototype.close = function(callback)

0 comments on commit b1d3c6d

Please sign in to comment.