Skip to content

Commit

Permalink
dedup Mongo cursors
Browse files Browse the repository at this point in the history
  • Loading branch information
dgreensp authored and n1mmy committed Apr 4, 2012
1 parent 1f021cf commit df8567d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
4 changes: 4 additions & 0 deletions packages/minimongo/diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ LocalCollection._diffQuery = function (old_results, new_results, observer, deepc

var new_presence_of_id = {};
_.each(new_results, function (doc) {
if (new_presence_of_id[doc._id])
Meteor._debug("Duplicate _id in new_results");
new_presence_of_id[doc._id] = true;
});

var old_index_of_id = {};
_.each(old_results, function (doc, i) {
if (doc._id in old_index_of_id)
Meteor._debug("Duplicate _id in old_results");
old_index_of_id[doc._id] = i;
});

Expand Down
19 changes: 15 additions & 4 deletions packages/mongo-livedata/mongo_driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ _Mongo._makeCursor = function (mongo, collection_name, selector, options) {
mongo._withCollection(collection_name, function (err, collection) {
if (err) {
future.ret([false, err]);
return
return;
}
var cursor = collection.find(selector, options.fields, {
sort: options.sort, limit: options.limit, skip: options.skip});
Expand Down Expand Up @@ -288,17 +288,24 @@ _Mongo.Cursor = function (mongo, collection_name, selector, options, cursor) {
self.selector = selector;
self.options = options;
self.cursor = cursor;

self.visited_ids = {};
};

_Mongo.Cursor.prototype.forEach = function (callback) {
var self = this;
var future = new Future;

self.cursor.each(function (err, doc) {
if (err || !doc)
if (err || !doc || !doc._id) {
future.ret(err);
else
} else if (self.visited_ids[doc._id]) {
// already seen this doc; Mongo cursors can
// return duplicates
} else {
self.visited_ids[doc._id] = true;
callback(doc);
}
});

var err = future.wait();
Expand All @@ -320,6 +327,8 @@ _Mongo.Cursor.prototype.rewind = function () {

// known to be synchronous
self.cursor.rewind();

self.visited_ids = {};
};

_Mongo.Cursor.prototype.fetch = function () {
Expand All @@ -333,7 +342,9 @@ _Mongo.Cursor.prototype.fetch = function () {
var result = future.wait();
if (result[0])
throw result[0];
return result[1];
// dedup
return _.uniq(result[1], false, function(doc) {
return doc._id; });
};

_Mongo.Cursor.prototype.count = function () {
Expand Down

0 comments on commit df8567d

Please sign in to comment.