nexia_event_store 0.5.0 → 0.5.1

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: da7ed1c2d85dee75190b00526a806602d2d6c71b
4
- data.tar.gz: 536ec662447be0e41c8199d19a43e732ef6f9331
3
+ metadata.gz: 84e933fac343a7fcf78be09656f655c380b4aff5
4
+ data.tar.gz: c773e4bda103b1a081cfef4deefc95de06e86833
5
5
  SHA512:
6
- metadata.gz: 73bbaeaa3c718528b35aa230b5424e59cf45a63cbd7ddee2a7087a5b4f365ea8ec3d317f8b7c5c225abee78f09255f06427a17184088940c30ce669ff625d200
7
- data.tar.gz: 67f5a022073cef94a0dc566085788ccca545c9c6cc3564485d0f5d3531d6e1457a4890a46ba85233dfc75975606cea79e2eb438dbbd2c4aa0143b0521a0cae60
6
+ metadata.gz: c7fe28929dbf33069a69d13c1def033533d79f4ed2163dd8a47b1fe1d1d1b38c0869895d5d4da7d4afc658b3509ecfc0b7f4557388b8f00876539fc86e55fcae
7
+ data.tar.gz: 3f604af91ed93a41538047947a733915479e43a86cd8a403bee9887cbb347fc3b0160603e6ff2edf4b5e8e2ad6128a528789578f1f18eaa6d0f895f8f9c5121b
@@ -4,21 +4,19 @@ module EventStore
4
4
  class Aggregate
5
5
  extend Forwardable
6
6
 
7
- attr_reader :id, :type, :event_table
7
+ attr_reader :id, :type, :event_table, :snapshot, :event_stream
8
8
 
9
- def_delegators :@snapshot,
9
+ def_delegators :snapshot,
10
10
  :last_event,
11
- :snapshot,
12
11
  :rebuild_snapshot!,
13
12
  :delete_snapshot!,
14
13
  :version,
15
14
  :version_for,
16
15
  :snapshot_version_table
17
16
 
18
- def_delegators :@event_stream,
17
+ def_delegators :event_stream,
19
18
  :events,
20
19
  :events_from,
21
- :event_stream,
22
20
  :event_stream_between,
23
21
  :event_table,
24
22
  :delete_events!
@@ -44,8 +42,8 @@ module EventStore
44
42
  end
45
43
 
46
44
  def append(events)
47
- @event_stream.append(events) do |prepared_events|
48
- @snapshot.store_snapshot(prepared_events)
45
+ event_stream.append(events) do |prepared_events|
46
+ snapshot.store_snapshot(prepared_events)
49
47
  end
50
48
  end
51
49
 
@@ -2,7 +2,17 @@ module EventStore
2
2
  class Client
3
3
  extend Forwardable
4
4
 
5
- def_delegators :aggregate, :delete_snapshot!, :snapshot_version_table, :version_for
5
+ def_delegators :aggregate,
6
+ :delete_snapshot!,
7
+ :snapshot_version_table,
8
+ :version_for,
9
+ :event_table,
10
+ :type,
11
+ :id,
12
+ :version
13
+
14
+ def_delegators :event_stream,
15
+ :count
6
16
 
7
17
  def self.count
8
18
  Aggregate.count
@@ -20,18 +30,6 @@ module EventStore
20
30
  aggregate.snapshot_exists?
21
31
  end
22
32
 
23
- def id
24
- aggregate.id
25
- end
26
-
27
- def type
28
- aggregate.type
29
- end
30
-
31
- def event_table
32
- aggregate.event_table
33
- end
34
-
35
33
  def append(event_data)
36
34
  aggregate.append(event_data)
37
35
  yield(event_data) if block_given?
@@ -70,14 +68,6 @@ module EventStore
70
68
  aggregate.events_from(version_number, max)
71
69
  end
72
70
 
73
- def version
74
- aggregate.version
75
- end
76
-
77
- def count
78
- event_stream.length
79
- end
80
-
81
71
  def destroy!
82
72
  aggregate.delete_events!
83
73
  aggregate.delete_snapshot!
@@ -1,5 +1,6 @@
1
1
  module EventStore
2
2
  class EventStream
3
+ include Enumerable
3
4
 
4
5
  attr_reader :event_table
5
6
 
@@ -43,8 +44,19 @@ module EventStore
43
44
  query.all.map {|e| e[:serialized_event] = EventStore.unescape_bytea(e[:serialized_event]); e}
44
45
  end
45
46
 
46
- def event_stream
47
- events.all.map {|e| e[:serialized_event] = EventStore.unescape_bytea(e[:serialized_event]); e}
47
+ def last
48
+ to_a.last
49
+ end
50
+
51
+ def empty?
52
+ events.empty?
53
+ end
54
+
55
+ def each
56
+ events.all.each do |e|
57
+ e[:serialized_event] = EventStore.unescape_bytea(e[:serialized_event])
58
+ yield e
59
+ end
48
60
  end
49
61
 
50
62
  def delete_events!
@@ -1,5 +1,6 @@
1
1
  module EventStore
2
2
  class Snapshot
3
+ include Enumerable
3
4
 
4
5
  attr_reader :snapshot_version_table, :snapshot_table
5
6
 
@@ -15,7 +16,7 @@ module EventStore
15
16
  end
16
17
 
17
18
  def last_event
18
- snapshot.last
19
+ to_a.last
19
20
  end
20
21
 
21
22
  def version(snapshot_key =:current_version)
@@ -26,22 +27,16 @@ module EventStore
26
27
  version(snapshot_key(fully_qualified_name: fully_qualified_name, sub_key: sub_key))
27
28
  end
28
29
 
29
- def size
30
- snapshot.size
31
- end
32
-
33
- def snapshot
30
+ def each
34
31
  events_hash = auto_rebuild_snapshot(read_raw_snapshot)
35
- snap = []
36
- events_hash.each_pair do |key, value|
32
+ events_hash.inject([]) do |snapshot, (key, value)|
37
33
  fully_qualified_name, _ = key.split(EventStore::SNAPSHOT_KEY_DELIMITER)
38
34
  raw_event = value.split(EventStore::SNAPSHOT_DELIMITER)
39
35
  version = raw_event.first.to_i
40
36
  serialized_event = EventStore.unescape_bytea(raw_event[1])
41
37
  occurred_at = Time.parse(raw_event.last)
42
- snap << SerializedEvent.new(fully_qualified_name, serialized_event, version, occurred_at)
43
- end
44
- snap.sort {|a,b| a.version <=> b.version}
38
+ snapshot + [SerializedEvent.new(fully_qualified_name, serialized_event, version, occurred_at)]
39
+ end.sort_by(&:version).each { |e| yield e }
45
40
  end
46
41
 
47
42
  def rebuild_snapshot!
@@ -1,3 +1,3 @@
1
1
  module EventStore
2
- VERSION = '0.5.0'
2
+ VERSION = '0.5.1'
3
3
  end
@@ -49,7 +49,6 @@ describe EventStore::Client do
49
49
  describe '#raw_event_stream' do
50
50
  it "should be an array of hashes that represent database records, not EventStore::SerializedEvent objects" do
51
51
  raw_stream = es_client.new(AGGREGATE_ID_ONE, :device).raw_event_stream
52
- expect(raw_stream.class).to eq(Array)
53
52
  raw_event = raw_stream.first
54
53
  expect(raw_event.class).to eq(Hash)
55
54
  expect(raw_event.keys).to eq([:id, :version, :aggregate_id, :fully_qualified_name, :occurred_at, :serialized_event, :sub_key])
@@ -288,16 +287,16 @@ describe EventStore::Client do
288
287
  it "should set the snapshot version number to match that of the last event in the aggregate's event stream" do
289
288
  events = [@new_event, @really_new_event]
290
289
  initial_stream_version = @client.raw_event_stream.last[:version]
291
- expect(@client.snapshot.last.version).to eq(initial_stream_version)
290
+ expect(@client.snapshot.version).to eq(initial_stream_version)
292
291
  @client.append(events)
293
292
  updated_stream_version = @client.raw_event_stream.last[:version]
294
- expect(@client.snapshot.last.version).to eq(updated_stream_version)
293
+ expect(@client.snapshot.version).to eq(updated_stream_version)
295
294
  end
296
295
 
297
296
  it "should write-through-cache the event in a snapshot without duplicating events" do
298
297
  @client.destroy!
299
298
  @client.append([@old_event, @new_event, @really_new_event])
300
- expect(@client.snapshot).to eq(@client.event_stream)
299
+ expect(@client.snapshot.to_a).to eq(@client.event_stream)
301
300
  end
302
301
 
303
302
  it "should raise a meaningful exception when a nil event given to it to append" do
@@ -323,7 +322,7 @@ describe EventStore::Client do
323
322
  expected = []
324
323
  expected << @client.event_stream.first
325
324
  expected << @client.event_stream.last
326
- expect(@client.snapshot).to eq(expected)
325
+ expect(@client.snapshot.to_a).to eq(expected)
327
326
  end
328
327
 
329
328
  #TODO if we let the db assign version# then this can't be true anymore
@@ -341,10 +340,10 @@ describe EventStore::Client do
341
340
  it "should set the snapshot version number to match that of the last event in the aggregate's event stream" do
342
341
  events = [@old_event, @old_event]
343
342
  initial_stream_version = @client.raw_event_stream.last[:version]
344
- expect(@client.snapshot.last.version).to eq(initial_stream_version)
343
+ expect(@client.snapshot.version).to eq(initial_stream_version)
345
344
  @client.append(events)
346
345
  updated_stream_version = @client.raw_event_stream.last[:version]
347
- expect(@client.snapshot.last.version).to eq(updated_stream_version)
346
+ expect(@client.snapshot.version).to eq(updated_stream_version)
348
347
  end
349
348
  end
350
349
  end
@@ -6,12 +6,11 @@ AGGREGATE_ID_TWO = SecureRandom.uuid
6
6
 
7
7
  module EventStore
8
8
  describe "Snapshots" do
9
-
10
9
  context "when there are no events" do
11
10
  let(:client) { EventStore::Client.new(AGGREGATE_ID_ONE) }
12
11
 
13
12
  it "should build an empty snapshot for a new client" do
14
- expect(client.snapshot).to eq([])
13
+ expect(client.snapshot.any?).to eq(false)
15
14
  expect(client.version).to eq(-1)
16
15
  expect(EventStore.redis.hget(client.snapshot_version_table, :current_version)).to eq(nil)
17
16
  end
@@ -27,7 +26,7 @@ module EventStore
27
26
  let(:client) { EventStore::Client.new(AGGREGATE_ID_TWO) }
28
27
 
29
28
  before do
30
- expect(client.snapshot.length).to eq(0)
29
+ expect(client.snapshot.count).to eq(0)
31
30
  client.append events_for(AGGREGATE_ID_TWO)
32
31
  end
33
32
 
@@ -49,14 +48,14 @@ module EventStore
49
48
  expected_snapshot = serialized_events(expected_snapshot_events)
50
49
  actual_snapshot = client.snapshot
51
50
 
52
- expect(client.event_stream.length).to eq(15)
51
+ expect(client.event_stream.count).to eq(15)
53
52
  expect(actual_snapshot.map(&:fully_qualified_name)).to eq(expected_snapshot_events)
54
- expect(actual_snapshot.length).to eq(8)
53
+ expect(actual_snapshot.count).to eq(8)
55
54
  expect(actual_snapshot.map(&:serialized_event)).to eq(expected_snapshot.map(&:serialized_event))
56
55
  end
57
56
 
58
57
  it "increments the version number of the snapshot when an event is appended" do
59
- expect(client.snapshot.last.version).to eq(client.raw_event_stream.last[:version])
58
+ expect(client.snapshot.version).to eq(client.raw_event_stream.last[:version])
60
59
  end
61
60
  end
62
61
 
@@ -67,35 +67,34 @@ module EventStore
67
67
 
68
68
  it "stores a a new snapshot from the aggregate's events" do
69
69
  snapshot.rebuild_snapshot!
70
- expect(snapshot.size).to eq(2)
70
+ expect(snapshot.count).to eq(2)
71
71
  # TODO: remove #snapshot in favor of Enumerable
72
- names = snapshot.snapshot.map(&:fully_qualified_name)
72
+ names = snapshot.map(&:fully_qualified_name)
73
73
  expect(names).to eq(events.map { |e| e[:fully_qualified_name] })
74
74
  end
75
75
  end
76
76
 
77
77
  # TODO: remove this in favor of #each and include Enumerable
78
- describe "#snapshot" do
79
- let(:event_snapshot) { snapshot.snapshot }
78
+ describe "events" do
80
79
  let(:serialized_attrs) { [ :fully_qualified_name,
81
80
  :serialized_event,
82
81
  :version,
83
82
  :occurred_at ] }
84
83
 
85
84
  it "contains SerializedEvents" do
86
- event_snapshot.each { |e| expect(e).to be_a(SerializedEvent) }
85
+ snapshot.each { |e| expect(e).to be_a(SerializedEvent) }
87
86
  end
88
87
 
89
88
  it "corresponds to the events used to build the snapshot" do
90
89
  (serialized_attrs - [ :serialized_event ]).each { |attr|
91
- expect(event_snapshot.first.send(attr)).to eq(first_event[attr])
92
- expect(event_snapshot.last.send(attr)).to eq(last_event[attr])
90
+ expect(snapshot.first.send(attr)).to eq(first_event[attr])
91
+ expect(snapshot.last_event.send(attr)).to eq(last_event[attr])
93
92
  }
94
93
  end
95
94
 
96
95
  it "unescapes the serialized events" do
97
- expected_event = EventStore.unescape_bytea(last_event[:serialized_event])
98
- expect(event_snapshot.last.serialized_event).to eq(expected_event)
96
+ expected_event = EventStore.unescape_bytea(first_event[:serialized_event])
97
+ expect(snapshot.first.serialized_event).to eq(expected_event)
99
98
  end
100
99
  end
101
100
 
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.5.0
4
+ version: 0.5.1
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-01-23 00:00:00.000000000 Z
12
+ date: 2015-01-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -273,7 +273,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
273
273
  version: '0'
274
274
  requirements: []
275
275
  rubyforge_project:
276
- rubygems_version: 2.4.2
276
+ rubygems_version: 2.4.3
277
277
  signing_key:
278
278
  specification_version: 4
279
279
  summary: Ruby implementation of an EventSource (A+ES) for the Nexia Ecosystem