nexia_event_store 0.7.4 → 0.7.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 59f8ee711cea78125f17a42ceb0114e811c51b51
4
- data.tar.gz: 05b12845f42fd4d14c7972bd938785f6b23e12f4
3
+ metadata.gz: 47f3dda99420a603ac4a0289321ac8b7697068c5
4
+ data.tar.gz: ea93e049eaa9bf2bad597e0ba032bcbd5a51f077
5
5
  SHA512:
6
- metadata.gz: bfa0ab38b2a24f177090ae4c8c8675c66a70b4d701947aa21946ab274017bebf4940e9a629f173dd6ba9239a73f726694237ea3ad703dcd4ada24ce57c9b7d02
7
- data.tar.gz: 68b3545fa8762fd3a2eb83a1189eba23be8bc496234cd708ea2896be4f13d9b15af5fbde8b19bd8e6cfbcb7cdded18796fc482e1c971842f3b1206a3aecb2589
6
+ metadata.gz: 0a453586af4426200b85a14b9b35c9f4d612b99d8db0cecc0203438591e02b9a22824b3b2d812c78b6f5a8c99b6e540e5c40df1a0d9cde357cbb3ee7e8d55511
7
+ data.tar.gz: f049dd32391ab38e6b45a055298214b60d03b4e197c653b999b7b343867fdf61f82f50e01580ce2403c90fef286932b1866a03f2857a57a122c73317adb6ab70
@@ -4,7 +4,7 @@ module EventStore
4
4
  class Aggregate
5
5
  extend Forwardable
6
6
 
7
- attr_reader :id, :type, :event_table, :snapshot, :event_stream
7
+ attr_reader :id, :type, :event_table, :snapshot, :event_stream, :checkpoint_event
8
8
 
9
9
  def_delegators :snapshot,
10
10
  :last_event,
@@ -16,6 +16,7 @@ module EventStore
16
16
 
17
17
  def_delegators :event_stream,
18
18
  :events,
19
+ :snapshot_events,
19
20
  :events_from,
20
21
  :event_stream_between,
21
22
  :event_table,
@@ -34,12 +35,13 @@ module EventStore
34
35
  EventStore.db.from( EventStore.fully_qualified_table).distinct(:aggregate_id).select(:aggregate_id).order(:aggregate_id).limit(limit, offset).all.map{|item| item[:aggregate_id]}
35
36
  end
36
37
 
37
- def initialize(id, type = EventStore.table_name)
38
+ def initialize(id, type = EventStore.table_name, checkpoint_event = nil)
38
39
  @id = id
39
40
  @type = type
40
41
 
41
- @snapshot = Snapshot.new(self)
42
- @event_stream = EventStream.new(self)
42
+ @checkpoint_event = checkpoint_event
43
+ @snapshot = Snapshot.new(self)
44
+ @event_stream = EventStream.new(self)
43
45
  end
44
46
 
45
47
  def append(events, logger)
@@ -22,8 +22,8 @@ module EventStore
22
22
  Aggregate.ids(offset, limit)
23
23
  end
24
24
 
25
- def initialize(aggregate_id, aggregate_type = EventStore.table_name)
26
- @aggregate = Aggregate.new(aggregate_id, aggregate_type)
25
+ def initialize(aggregate_id, aggregate_type = EventStore.table_name, checkpoint_event = nil)
26
+ @aggregate = Aggregate.new(aggregate_id, aggregate_type, checkpoint_event)
27
27
  end
28
28
 
29
29
  def exists?
@@ -2,11 +2,12 @@ module EventStore
2
2
  class EventStream
3
3
  include Enumerable
4
4
 
5
- attr_reader :event_table
5
+ attr_reader :event_table, :checkpoint_event
6
6
 
7
7
  def initialize aggregate
8
8
  @aggregate = aggregate
9
9
  @id = @aggregate.id
10
+ @checkpoint_event = aggregate.checkpoint_event
10
11
  @event_table_alias = "events"
11
12
  @event_table = "#{EventStore.schema}__#{EventStore.table_name}".to_sym
12
13
  @aliased_event_table = "#{event_table}___#{@event_table_alias}".to_sym
@@ -51,6 +52,16 @@ module EventStore
51
52
  .select_all(:events).select_append(:fully_qualified_name)
52
53
  end
53
54
 
55
+ def snapshot_events
56
+ last_checkpoint = last_event_before(Time.now.utc, [checkpoint_event]).first if checkpoint_event
57
+
58
+ if last_checkpoint
59
+ events.where{ events__id >= last_checkpoint[:id].to_i }
60
+ else
61
+ events
62
+ end
63
+ end
64
+
54
65
  def events_from(event_id, max = nil)
55
66
  # note: this depends on the events table being aliased to "events" above.
56
67
  events.limit(max).where{events__id >= event_id.to_i }.all.map do |event|
@@ -58,7 +58,8 @@ module EventStore
58
58
  delete_snapshot!
59
59
  logger.debug { "Deleting snapshot took #{Time.now - t} seconds" }
60
60
  t = Time.now
61
- all_events = @aggregate.events.all
61
+ all_events = @aggregate.snapshot_events.all
62
+ logger.debug { "getting #{all_events.count} events" }
62
63
  logger.debug { "getting all events took #{Time.now - t} seconds" }
63
64
  t = Time.now
64
65
  corrected_events = all_events.map{|e| e[:occurred_at] = TimeHacker.translate_occurred_at_from_local_to_gmt(e[:occurred_at]); e}
@@ -1,3 +1,3 @@
1
1
  module EventStore
2
- VERSION = '0.7.4'
2
+ VERSION = '0.7.5'
3
3
  end
@@ -3,11 +3,20 @@ require "mock_redis"
3
3
 
4
4
  module EventStore
5
5
  describe Snapshot do
6
- let(:redis) { EventStore.redis }
7
- let(:aggregate_type) { "awesome" }
8
- let(:aggregate_id) { "superman" }
9
- let(:events) { [] }
10
- let(:aggregate) { double("Aggregate", type: aggregate_type, id: aggregate_id, events: double(all: events)) }
6
+ let(:redis) { EventStore.redis }
7
+ let(:aggregate_type) { "awesome" }
8
+ let(:aggregate_id) { "superman" }
9
+ let(:events) { [] }
10
+ let(:snapshot_events) { events }
11
+ let(:checkpoint_event) { nil }
12
+ let(:aggregate) {
13
+ double("Aggregate",
14
+ type: aggregate_type,
15
+ id: aggregate_id,
16
+ events: double(all: events),
17
+ checkpoint_event: checkpoint_event,
18
+ snapshot_events: double(all: snapshot_events))
19
+ }
11
20
 
12
21
  subject(:snapshot) { EventStore::Snapshot.new(aggregate) }
13
22
 
@@ -72,6 +81,19 @@ module EventStore
72
81
  names = snapshot.map(&:fully_qualified_name)
73
82
  expect(names).to eq(events.map { |e| e[:fully_qualified_name] })
74
83
  end
84
+
85
+ context "with a checkpoint event" do
86
+ let(:snapshot_events){ [ last_event ] }
87
+ let(:checkpoint_event) { "the_big_event" }
88
+
89
+ it "stores a a new snapshot from the aggregate's events" do
90
+ snapshot.rebuild_snapshot!
91
+ expect(snapshot.count).to eq(1)
92
+ # TODO: remove #snapshot in favor of Enumerable
93
+ names = snapshot.map(&:fully_qualified_name)
94
+ expect(names).to eq(snapshot_events.map { |e| e[:fully_qualified_name] })
95
+ end
96
+ end
75
97
  end
76
98
 
77
99
  # TODO: remove this in favor of #each and include Enumerable
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nexia_event_store
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.4
4
+ version: 0.7.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Saieg, John Colvin
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-06-29 00:00:00.000000000 Z
12
+ date: 2015-07-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -233,7 +233,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
233
233
  version: '0'
234
234
  requirements: []
235
235
  rubyforge_project:
236
- rubygems_version: 2.4.4
236
+ rubygems_version: 2.4.2
237
237
  signing_key:
238
238
  specification_version: 4
239
239
  summary: Ruby implementation of an EventSource (A+ES) for the Nexia Ecosystem