Skip to content

mongodb and inmemory performance improvements #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 25, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 13 additions & 21 deletions lib/databases/inmemory.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ function InMemory(options) {
Store.call(this, options);
this.store = {};
this.snapshots = {};
this.undispatchedEvents = {};
}

util.inherits(InMemory, Store);
Expand Down Expand Up @@ -56,6 +57,7 @@ _.extend(InMemory.prototype, {
clear: function (callback) {
this.store = {};
this.snapshots = {};
this.undispatchedEvents = {};
if (callback) callback(null);
},

Expand Down Expand Up @@ -87,6 +89,11 @@ _.extend(InMemory.prototype, {

this.store[context][aggregate][aggregateId] = this.store[context][aggregate][aggregateId].concat(events);

var self = this;
_.forEach(events, function(evt) {
self.undispatchedEvents[evt.id] = evt;
});

callback(null);
},

Expand Down Expand Up @@ -215,31 +222,16 @@ _.extend(InMemory.prototype, {
},

getUndispatchedEvents: function (callback) {
var res = [];

this.getEvents({}, 0, -1, function (err, evts) {
for (var ele in evts) {
var evt = evts[ele];
if (!evt.dispatched) {
res.push(evt);
}
}
callback(null, res);
var res = _.map(this.undispatchedEvents, function(value, key) {
return value;
});

callback(null, res);
},

setEventToDispatched: function (id, callback) {
var res;

this.getEvents({ id: id }, 0, -1, function (err, evts) {
if (evts && evts.length > 0) {
res = evts[0];
}

res.dispatched = true;

callback(null);
});
delete this.undispatchedEvents[id];
callback(null);
},

addSnapshot: function(snap, callback) {
Expand Down
60 changes: 10 additions & 50 deletions lib/databases/mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,44 +84,13 @@ _.extend(Mongo.prototype, {
self.client = client;

self.events = self.db.collection(options.eventsCollectionName);
self.events.ensureIndex({ streamId: 1, aggregateId: 1, aggregate: 1, context: 1, commitId: 1, commitStamp: 1, commitSequence: 1, streamRevision: 1 },
self.events.ensureIndex({ aggregateId: 1, streamRevision: 1 },
function (err) { if (err) { debug(err); } });

self.events.ensureIndex({ aggregateId: 1, aggregate: 1, context: 1 },
function (err) { if (err) { debug(err); } });

self.events.ensureIndex({ aggregateId: 1, aggregate: 1 },
function (err) { if (err) { debug(err); } });

self.events.ensureIndex({ aggregateId: 1 },
function (err) { if (err) { debug(err); } });

self.events.ensureIndex({ streamId: 1 },
function (err) { if (err) { debug(err); } });

self.events.ensureIndex({ dispatched: 1 },
function (err) { if (err) { debug(err); } });

self.events.ensureIndex({ commitStamp: 1, streamRevision: 1, commitSequence: 1 },
self.events.ensureIndex({ dispatched: 1 }, { sparse: true },
function (err) { if (err) { debug(err); } });

self.snapshots = self.db.collection(options.snapshotsCollectionName);
self.snapshots.ensureIndex({ streamId: 1, aggregateId: 1, aggregate: 1, context: 1, commitStamp: 1, revision: 1, version: 1 },
function (err) { if (err) { debug(err); } });

self.snapshots.ensureIndex({ revision: -1, version: -1, commitStamp: -1 },
function (err) { if (err) { debug(err); } });

self.snapshots.ensureIndex({ aggregateId: 1, aggregate: 1, context: 1 },
function (err) { if (err) { debug(err); } });

self.snapshots.ensureIndex({ aggregateId: 1, aggregate: 1 },
function (err) { if (err) { debug(err); } });

self.snapshots.ensureIndex({ aggregateId: 1 },
function (err) { if (err) { debug(err); } });

self.snapshots.ensureIndex({ streamId: 1 },
self.snapshots.ensureIndex({ aggregateId: 1, revision: -1 },
function (err) { if (err) { debug(err); } });

self.transactions = self.db.collection(options.transactionsCollectionName);
Expand Down Expand Up @@ -243,10 +212,7 @@ _.extend(Mongo.prototype, {
}

if (query.aggregateId) {
findStatement['$or'] = [
{ aggregateId: query.aggregateId },
{ streamId: query.aggregateId } // just for compatability of < 1.0.0
];
findStatement.aggregateId = query.aggregateId;
}

if (limit === -1) {
Expand All @@ -264,17 +230,14 @@ _.extend(Mongo.prototype, {
return;
}

var options = { '$gte': revMin, '$lt': revMax };
var streamRevOptions = { '$gte': revMin, '$lt': revMax };
if (revMax == -1) {
options = { '$gte': revMin };
streamRevOptions = { '$gte': revMin };
}

var findStatement = {
'$or': [
{ aggregateId: query.aggregateId },
{ streamId: query.aggregateId } // just for compatability of < 1.0.0
],
streamRevision: options
aggregateId: query.aggregateId,
streamRevision: streamRevOptions
};

if (query.aggregate) {
Expand Down Expand Up @@ -344,7 +307,7 @@ _.extend(Mongo.prototype, {
},

setEventToDispatched: function (id, callback) {
var updateCommand = { '$set' : { 'dispatched': true } };
var updateCommand = { '$unset' : { 'dispatched': null } };
this.events.update({'_id' : id}, updateCommand, callback);
},

Expand All @@ -369,10 +332,7 @@ _.extend(Mongo.prototype, {
}

var findStatement = {
'$or': [
{ aggregateId: query.aggregateId },
{ streamId: query.aggregateId } // just for compatability of < 1.0.0
]
aggregateId: query.aggregateId
};

if (query.aggregate) {
Expand Down
45 changes: 7 additions & 38 deletions lib/databases/tingodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,44 +37,13 @@ _.extend(Tingo.prototype, {
// self.emit('disconnect');
// });
this.events = this.db.collection(options.eventsCollectionName + '.tingo');
this.events.ensureIndex({ streamId: 1, aggregateId: 1, aggregate: 1, context: 1, commitId: 1, commitStamp: 1, commitSequence: 1, streamRevision: 1 },
this.events.ensureIndex({ aggregateId: 1, streamRevision: 1 },
function (err) { if (err) { debug(err); } });

this.events.ensureIndex({ aggregateId: 1, aggregate: 1, context: 1 },
function (err) { if (err) { debug(err); } });

this.events.ensureIndex({ aggregateId: 1, aggregate: 1 },
function (err) { if (err) { debug(err); } });

this.events.ensureIndex({ aggregateId: 1 },
function (err) { if (err) { debug(err); } });

this.events.ensureIndex({ streamId: 1 },
function (err) { if (err) { debug(err); } });

this.events.ensureIndex({ dispatched: 1 },
function (err) { if (err) { debug(err); } });

this.events.ensureIndex({ commitStamp: 1, streamRevision: 1, commitSequence: 1 },
this.events.ensureIndex({ dispatched: 1 }, { sparse: true },
function (err) { if (err) { debug(err); } });

this.snapshots = this.db.collection(options.snapshotsCollectionName + '.tingo');
this.snapshots.ensureIndex({ streamId: 1, aggregateId: 1, aggregate: 1, context: 1, commitStamp: 1, revision: 1, version: 1 },
function (err) { if (err) { debug(err); } });

this.snapshots.ensureIndex({ revision: -1, version: -1, commitStamp: -1 },
function (err) { if (err) { debug(err); } });

this.snapshots.ensureIndex({ aggregateId: 1, aggregate: 1, context: 1 },
function (err) { if (err) { debug(err); } });

this.snapshots.ensureIndex({ aggregateId: 1, aggregate: 1 },
function (err) { if (err) { debug(err); } });

this.snapshots.ensureIndex({ aggregateId: 1 },
function (err) { if (err) { debug(err); } });

this.snapshots.ensureIndex({ streamId: 1 },
this.snapshots.ensureIndex({ aggregateId: 1, revision: -1 },
function (err) { if (err) { debug(err); } });

this.transactions = this.db.collection(options.transactionsCollectionName + '.tingo');
Expand Down Expand Up @@ -205,17 +174,17 @@ _.extend(Tingo.prototype, {
return;
}

var options = { '$gte': revMin, '$lt': revMax };
var streamRevOptions = { '$gte': revMin, '$lt': revMax };
if (revMax == -1) {
options = { '$gte': revMin };
streamRevOptions = { '$gte': revMin };
}

var findStatement = {
'$or': [
{ aggregateId: query.aggregateId },
{ streamId: query.aggregateId } // just for compatability of < 1.0.0
],
streamRevision: options
streamRevision: streamRevOptions
};

if (query.aggregate) {
Expand Down Expand Up @@ -285,7 +254,7 @@ _.extend(Tingo.prototype, {
},

setEventToDispatched: function (id, callback) {
var updateCommand = { '$set' : { 'dispatched': true } };
var updateCommand = { '$unset' : { 'dispatched': null } };
this.events.update({'_id' : id}, updateCommand, callback);
},

Expand Down