Permalink
Browse files

implemented getEventsMatching

  • Loading branch information...
adrai committed Mar 26, 2012
1 parent 6dc19fc commit fdb22a21d66e6e8a34d12048d5ba27be0de4dbf3
Showing with 68 additions and 6 deletions.
  1. +40 −3 lib/eventStore.js
  2. +1 −1 lib/storage/inMemory/storage.js
  3. +1 −1 storage/couchDb/storage.js
  4. +1 −1 storage/redis/storage.js
  5. +25 −0 test/eventStoreSpec.js
View
@@ -422,14 +422,14 @@ Store.prototype = {
},
- // __getEvents:__ loads the events from given storage.
+ // __getEventRange:__ loads the events from given storage.
//
- // `eventStore.getEvents(index, amount, callback)`
+ // `eventStore.getEventRange(index, amount, callback)`
//
// - __index:__ entry index
// - __amount:__ amount of events
// - __callback:__ `function(err, events){}`
- getEvents: function(index, amount, callback) {
+ getEventRange: function(index, amount, callback) {
if (this.hasConfigurationErrors(callback)) return;
@@ -440,6 +440,43 @@ Store.prototype = {
}
});
+ },
+
+ // __getEventRangeMatching:__ loads the range of events from given storage.
+ //
+ // `storage.getEventRangeMatching(match, amount, callback)`
+ //
+ // - __match:__ match query in inner event (payload)
+ // - __amount:__ amount of events
+ // - __callback:__ `function(err, events){}`
+ getEventRangeMatching: function(match, amount, callback) {
+
+ if (this.hasConfigurationErrors(callback)) return;
+
+ var self = this;
+
+ this.storage.getEventRangeMatching(match, amount, function(err, events) {
+
+ events.next = function(callback) {
+
+ var lastEvt = events[events.length - 1];
+
+ for (var m in match) {
+ if (match.hasOwnProperty(m)) {
+ match[m] = lastEvt.payload[m];
+ break;
+ }
+ }
+
+ self.getEventRangeMatching(match, amount, callback);
+
+ };
+
+ if (typeof callback === 'function') {
+ callback(err, events);
+ }
+ });
+
}
};
@@ -225,7 +225,7 @@ Storage.prototype = {
if (events.length > index + 1 + amount) {
endIndex = index + 1 + amount;
} else if (events.length <= index + 1 + amount) {
- endIndex = events.length - 1;
+ endIndex = events.length;
}
events = events.slice(index + 1, endIndex);
@@ -312,7 +312,7 @@ Storage.prototype = {
if (result.length > index + 1 + amount) {
endIndex = index + 1 + amount;
} else if (result.length <= index + 1 + amount) {
- endIndex = result.length - 1;
+ endIndex = result.length;
}
result = result.slice(index + 1, endIndex);
View
@@ -315,7 +315,7 @@ Storage.prototype = {
if (arr.length > index + 1 + amount) {
endIndex = index + 1 + amount;
} else if (arr.length <= index + 1 + amount) {
- endIndex = arr.length - 1;
+ endIndex = arr.length;
}
arr = arr.slice(index + 1, endIndex);
View
@@ -101,6 +101,31 @@ vows.describe('The EventStore')
}
}
}
+ },
+
+ 'can request events by eventId': {
+ topic: function() {
+ eventstore.getEventStream('e4', 0, -1, function(err, stream) {
+ stream.addEvent({ id: '1'});
+ stream.addEvent({ id: '2'});
+ stream.addEvent({ id: '3'});
+ stream.addEvent({ id: '4'});
+ stream.addEvent({ id: '5'});
+ stream.commit(this.callback);
+ }.bind(this));
+ },
+
+ '': {
+ topic: function(err) {
+ eventstore.getEventRangeMatching({id: '2'}, 2, function(err, evts) {
+ evts.next(this.callback);
+ }.bind(this));
+ },
+
+ 'correctly': function(err, events) {
+ assert.equal(events[0].payload.id, '5');
+ }
+ }
}
}
}).export(module);

0 comments on commit fdb22a2

Please sign in to comment.