Skip to content
This repository has been archived by the owner on May 19, 2019. It is now read-only.

Commit

Permalink
removed useless cursor preparator
Browse files Browse the repository at this point in the history
added cursors tree tracking
code style fixes
  • Loading branch information
Artem Artemyev committed Jan 17, 2016
1 parent d053832 commit 117e4b7
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 42 deletions.
60 changes: 30 additions & 30 deletions lib/CollectionDelegate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}) {
Expand All @@ -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);
});
}

Expand All @@ -49,29 +49,29 @@ 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,
}));
});
}

find(query, options = {}) {
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);
Expand Down
13 changes: 6 additions & 7 deletions lib/Cursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down
21 changes: 17 additions & 4 deletions lib/CursorObservable.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export class CursorObservable extends Cursor {
);
this.maybeUpdate = _bind(this.maybeUpdate, this);
this._latestResult = null;
this._childrenCursors = {};
}

static defaultDebounce() {
Expand Down Expand Up @@ -107,6 +108,9 @@ export class CursorObservable extends Cursor {

const parentSetter = (cursor) => {
this._parentCursor = cursor;
if (cursor._trackChildCursor) {
cursor._trackChildCursor(cursor);
}
};

const cursorThenGenerator = (currPromise) => {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
2 changes: 1 addition & 1 deletion lib/StorageManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 117e4b7

Please sign in to comment.