From 117e4b754e96c43657799ce89174558b8d674ce0 Mon Sep 17 00:00:00 2001 From: Artem Artemyev Date: Mon, 18 Jan 2016 02:02:45 +0500 Subject: [PATCH] removed useless cursor preparator added cursors tree tracking code style fixes --- lib/CollectionDelegate.js | 60 +++++++++++++++++++-------------------- lib/Cursor.js | 13 ++++----- lib/CursorObservable.js | 21 +++++++++++--- lib/StorageManager.js | 2 +- 4 files changed, 54 insertions(+), 42 deletions(-) diff --git a/lib/CollectionDelegate.js b/lib/CollectionDelegate.js index 5800548..77aff05 100644 --- a/lib/CollectionDelegate.js +++ b/lib/CollectionDelegate.js @@ -13,11 +13,11 @@ export class CollectionDelegate { } insert(doc, options = {}) { - return this.db.indexManager.indexDocument(doc).then(() => { - return this.db.storageManager.persist(doc._id, doc).then(() => { - return doc._id; - }); - }); + return this.db.indexManager.indexDocument(doc).then(() => + this.db.storageManager.persist(doc._id, doc).then(() => + doc._id + ) + ); } remove(query, options = {}) { @@ -27,17 +27,17 @@ export class CollectionDelegate { 'remove(..): multi removing is not enabled by options.multi' ); - const removeStorgePromises = _map(docs, d => { - return this.db.storageManager.delete(d._id); - }); - const removeIndexPromises = _map(docs, d => { - return this.db.indexManager.deindexDocument(d); - }); - const allPromises = removeStorgePromises.concat(removeIndexPromises); + const removeStorgePromises = _map(docs, d => + this.db.storageManager.delete(d._id) + ); + const removeIndexPromises = _map(docs, d => + this.db.indexManager.deindexDocument(d) + ); - return Promise.all(allPromises).then(() => { - return docs; - }); + return Promise.all([ + ...removeStorgePromises, + ...removeIndexPromises, + ]).then(() => docs); }); } @@ -49,21 +49,21 @@ export class CollectionDelegate { var {original, updated} = result; updated = _map(updated, x => this.db.create(x)); - const updateStorgePromises = _map(updated, d => { - return this.db.storageManager.persist(d._id, d); - }); - const updateIndexPromises = _map(updated, (d, i) => { - return this.db.indexManager.reindexDocument(original[i], d); - }); - const allPromises = updateStorgePromises.concat(updateIndexPromises); + const updateStorgePromises = _map(updated, d => + this.db.storageManager.persist(d._id, d) + ); + const updateIndexPromises = _map(updated, (d, i) => + this.db.indexManager.reindexDocument(original[i], d) + ); - return Promise.all(allPromises).then(() => { - return { - modified: updated.length, - original: original, - updated: updated, - }; - }); + return Promise.all([ + ...updateStorgePromises, + ...updateIndexPromises, + ]).then(() => ({ + modified: updated.length, + original: original, + updated: updated, + })); }); } @@ -71,7 +71,7 @@ export class CollectionDelegate { return new (this.db.cursorClass)(this.db, query, options); } - findOne(query, sortObj, options = {}) { + findOne(query, options = {}) { return this.find(query, options) .aggregate(docs => docs[0]) .limit(1); diff --git a/lib/Cursor.js b/lib/Cursor.js index a99e657..b0b6aee 100644 --- a/lib/Cursor.js +++ b/lib/Cursor.js @@ -10,6 +10,9 @@ import DocumentSorter from './DocumentSorter'; import EJSON from './EJSON'; +// UUID counter for all cursors +let _currentCursorId = 0; + // Maker used for stopping pipeline processing const PIPLEINE_STOP_MARKER = {}; @@ -99,6 +102,7 @@ export class Cursor extends EventEmitter { super(); this.db = db; this.options = options; + this._id = _currentCursorId++; this._query = query; this._pipeline = []; this._executing = null; @@ -274,10 +278,9 @@ export class Cursor extends EventEmitter { return docs.slice(skip, limit + skip); } - exec(options = {}) { + exec() { if (!this._executing) { - this._executing = this._prepareCursor(options) - .then(() => this._matchObjects()) + this._executing = this._matchObjects() .then(docs => { let clonned; if (this.options.noClone) { @@ -305,10 +308,6 @@ export class Cursor extends EventEmitter { return Promise.resolve(this._executing); } - _prepareCursor(options = {}) { - return Promise.resolve(); - } - _matchObjects() { return new DocumentRetriver(this.db) .retriveForQeury(this._query) diff --git a/lib/CursorObservable.js b/lib/CursorObservable.js index 268a474..2f46ab4 100644 --- a/lib/CursorObservable.js +++ b/lib/CursorObservable.js @@ -25,6 +25,7 @@ export class CursorObservable extends Cursor { ); this.maybeUpdate = _bind(this.maybeUpdate, this); this._latestResult = null; + this._childrenCursors = {}; } static defaultDebounce() { @@ -107,6 +108,9 @@ export class CursorObservable extends Cursor { const parentSetter = (cursor) => { this._parentCursor = cursor; + if (cursor._trackChildCursor) { + cursor._trackChildCursor(cursor); + } }; const cursorThenGenerator = (currPromise) => { @@ -157,10 +161,7 @@ export class CursorObservable extends Cursor { * @return {Promise} */ update(firstRun = false) { - return this.exec({ - observable: true, - firstRun: firstRun, - }).then((result) => { + return this.exec().then((result) => { this._latestResult = result; this._updateLatestIds(); this._propagateUpdate(firstRun); @@ -252,6 +253,18 @@ export class CursorObservable extends Cursor { this._latestIds = new Set([this._latestResult._id]); } } + + /** + * Tracks a child cursor for analysing all cursors + * in the query (cursors tree) + * @param {Cursor} cursor + */ + _trackChildCursor(cursor) { + this._childrenCursors[cursor._id] = cursor; + cursor.once('stopped', () => + delete this._childrenCursors[cursor._id] + ) + } } export default CursorObservable; diff --git a/lib/StorageManager.js b/lib/StorageManager.js index 7224031..69281e9 100644 --- a/lib/StorageManager.js +++ b/lib/StorageManager.js @@ -11,7 +11,7 @@ import EJSON from './EJSON'; * and use another storage (with levelup, for example) */ export class StorageManager { - constructor(db, options) { + constructor(db, options = {}) { this.db = db; this.options = options; this._queue = new PromiseQueue(1);