From f0d13f6678ee1fff0f25a26d6489756efd458822 Mon Sep 17 00:00:00 2001 From: Slava Kim Date: Sun, 23 Feb 2014 17:52:17 -0800 Subject: [PATCH] Restructure and update comments for _addMatching-limits behavior The logic remains the same. Also remove `fields` - nothing uses the term `fields` except for the fields projection. --- .../mongo-livedata/oplog_observe_driver.js | 29 +++++++++++++------ 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/packages/mongo-livedata/oplog_observe_driver.js b/packages/mongo-livedata/oplog_observe_driver.js index 65e26bf1c6..c9d6ce6ed2 100644 --- a/packages/mongo-livedata/oplog_observe_driver.js +++ b/packages/mongo-livedata/oplog_observe_driver.js @@ -228,8 +228,8 @@ _.extend(OplogObserveDriver.prototype, { _addMatching: function (doc) { var self = this; var id = doc._id; - var fields = _.clone(doc); - delete fields._id; + var doc = _.clone(doc); + delete doc._id; if (self._published.has(id)) throw Error("tried to add something already published " + id); if (self._limit && self._unpublishedBuffer.has(id)) @@ -242,14 +242,25 @@ _.extend(OplogObserveDriver.prototype, { // The query is unlimited or didn't publish enough documents yet or the new // document would fit into published set pushing the maximum element out, // then we need to publish the doc. + var toPublish = ! limit || self._published.size() < limit || + comparator(maxPublished, doc) > 0; + // Otherwise we might need to buffer it (only in case of limited query). - // Buffering a new document is allowed only if it is inserted in the middle - // or the beginning of it as we cannot determine if there are documents - // outside of the buffer easily. - if (!limit || self._published.size() < limit || comparator(maxPublished, fields) > 0) { - self._addPublished(id, fields); - } else if ((self._safeAppendToBuffer && self._unpublishedBuffer.size() < limit) || (maxBuffered && comparator(maxBuffered, fields) > 0)) { - self._addBuffered(id, fields); + // Buffering is allowed if the buffer is not filled up yet and all matching + // docs are either in the published set or in the buffer. + var canAppendToBuffer = self._safeAppendToBuffer && + self._unpublishedBuffer.size() < limit; + + // Or if it is small enough to be safely inserted to the middle or the + // beginning of the buffer. + var canInsertIntoBuffer = maxBuffered && comparator(maxBuffered, doc) > 0; + + var toBuffer = canAppendToBuffer || canInsertIntoBuffer; + + if (toPublish) { + self._addPublished(id, doc); + } else if (toBuffer) { + self._addBuffered(id, doc); } }, // Called when a document leaves the "Matching" results set.