sandthorn_driver_sequel 1.0.4 → 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 906dc7ee0774059ef789a16481e505f1a8d8bceb
4
- data.tar.gz: 40e2cbffb0b5538137ee0eaec2eed54b869a64fd
3
+ metadata.gz: 32fcaa013235d94d983f889c5f3ccc7784bc8e13
4
+ data.tar.gz: aaf942165819a2b0d5257f3e2aa4288064e5fa6b
5
5
  SHA512:
6
- metadata.gz: 78b32d22e9a0cbbd7f96c273f8d3dd99c37f288311e3fdefbc16a3685e5fb48095386440b1db94dc94c39375071cba4cd01ee0ef74c576ffb36980638ae8f461
7
- data.tar.gz: a4585c75b94eaab75ce465b9996527f68b30a5504ffcda81cf96c4bcb963759ad02f61200d5746845e4ce38bc9f072b615501b7b1d1daed81934b3f94bece93d
6
+ metadata.gz: 2348c4fc6cd403f535c13b22b158431d39c8f297a2f9c32108188941de496fd250b8983b11c6e8519499c48e6630ebe68929d27895e4b09a56c2d487b0f41158
7
+ data.tar.gz: 8d83bbc8aef2d009ecbfc8e49d8da795f969d131cb2778318b96fb629c53d1d197a95bdc3522015316942b015d2e0f98f474843cd8d8f51a6c68153f052d1c46
data/Rakefile CHANGED
@@ -3,5 +3,5 @@ require "bundler/gem_tasks"
3
3
  require 'rspec/core/rake_task'
4
4
 
5
5
  RSpec::Core::RakeTask.new(:spec)
6
-
6
+ sh "ulimit -n 8192"
7
7
  task default: :spec
@@ -32,8 +32,7 @@ module SandthornDriverSequel
32
32
  error_message = "#{aggregate_type} with id #{aggregate_id}: expected event with version #{current_aggregate_version}, but got #{event[:aggregate_version]}"
33
33
  raise SandthornDriverSequel::Errors::ConcurrencyError.new(error_message)
34
34
  end
35
- blob = event[:event_data].nil? ? nil : Sequel.blob(event[:event_data])
36
- to_insert = {aggregate_table_id: pk_id, aggregate_version: event[:aggregate_version], event_name: event[:event_name], event_data: blob, timestamp: timestamp}
35
+ to_insert = ({aggregate_table_id: pk_id, aggregate_version: event[:aggregate_version], event_name: event[:event_name], event_data: event[:event_data], timestamp: timestamp})
37
36
  db[events_table_name].insert(to_insert)
38
37
  end
39
38
  db[aggregates_table_name].where(id: pk_id).update(aggregate_version: current_aggregate_version)
@@ -56,8 +55,7 @@ module SandthornDriverSequel
56
55
  raise SandthornDriverSequel::Errors::WrongAggregateVersionError.new error_message
57
56
  end
58
57
  if current_snapshot.nil?
59
- blob = aggregate_snapshot[:event_data].nil? ? nil : Sequel.blob(aggregate_snapshot[:event_data])
60
- to_insert = {aggregate_version: aggregate_version, snapshot_data: blob, aggregate_table_id: pk_id}
58
+ to_insert = {aggregate_version: aggregate_version, snapshot_data: aggregate_snapshot[:event_data], aggregate_table_id: pk_id }
61
59
  db[snapshots_table_name].insert(to_insert)
62
60
  else
63
61
  to_update = {aggregate_version: aggregate_version, snapshot_data: aggregate_snapshot[:event_data] }
@@ -162,14 +160,31 @@ module SandthornDriverSequel
162
160
  end
163
161
  end
164
162
  private
163
+
165
164
  def aggregate_events after_aggregate_version: 0, aggregate_id: nil
165
+
166
166
  rel = "#{events_table_name}__aggregate_version".to_sym
167
167
  where_proc = eval("lambda{ #{rel} > after_aggregate_version }")
168
168
  driver.execute do |db|
169
169
  query = db[events_table_name].join(aggregates_table_name, id: :aggregate_table_id, aggregate_id: aggregate_id)
170
170
  query = query.where &where_proc
171
- return query.select(rel, :aggregate_id, :sequence_number, :event_name, :event_data, :timestamp).order(:sequence_number).all
171
+ result = query.select(rel, :aggregate_id, :sequence_number, :event_name, :event_data, :timestamp).order(:sequence_number).all
172
172
  end
173
+
174
+ # result = nil
175
+ # Benchmark.bm do |x|
176
+ # x.report("find") {
177
+ # rel = "#{events_table_name}__aggregate_version".to_sym
178
+ # where_proc = eval("lambda{ #{rel} > after_aggregate_version }")
179
+ # driver.execute do |db|
180
+ # query = db[events_table_name].join(aggregates_table_name, id: :aggregate_table_id, aggregate_id: aggregate_id)
181
+ # query = query.where &where_proc
182
+ # result = query.select(rel, :aggregate_id, :sequence_number, :event_name, :event_data, :timestamp).order(:sequence_number).all
183
+ # end
184
+ # }
185
+
186
+ # end
187
+ # result
173
188
  end
174
189
  def get_current_aggregate_from_aggregates_table aggregate_id, aggregate_type, db
175
190
  aggregate_type = aggregate_type.to_s
@@ -48,7 +48,7 @@ module SandthornDriverSequel
48
48
  foreign_key :aggregate_table_id, aggr_table, on_update: :cascade
49
49
  Integer :aggregate_version, null: false
50
50
  String :event_name, size: 255, null: false
51
- Blob :event_data, null: true
51
+ String :event_data, text: true, null: true
52
52
  DateTime :timestamp, null: false
53
53
 
54
54
  index [:event_name]
@@ -76,7 +76,7 @@ module SandthornDriverSequel
76
76
  db.create_table(snapshots_table_name) do
77
77
  primary_key :id
78
78
  Integer :aggregate_version, null: false
79
- Blob :snapshot_data, null: false
79
+ String :snapshot_data, text: true, null: false
80
80
  foreign_key :aggregate_table_id, aggr_table, on_delete: :cascade, on_update: :cascade
81
81
  index [:aggregate_table_id], unique: true
82
82
  end
@@ -5,7 +5,7 @@ module SandthornDriverSequel
5
5
  def initialize args = {}
6
6
  @url = args.fetch(:url)
7
7
  Sequel.default_timezone = :utc
8
- @db = Sequel.connect(@url, :connection_handling => :stack)
8
+ @db = Sequel.connect(@url)
9
9
  end
10
10
  def execute &block
11
11
  return block.call @db
@@ -18,3 +18,23 @@ module SandthornDriverSequel
18
18
 
19
19
  end
20
20
  end
21
+
22
+
23
+ # module SandthornDriverSequel
24
+ # class SequelDriver
25
+ # def initialize args = {}
26
+ # @url = args.fetch(:url)
27
+ # Sequel.default_timezone = :utc
28
+ # end
29
+ # def execute &block
30
+ # Sequel.connect(@url) { |db| return block.call db}
31
+ # end
32
+ # def execute_in_transaction &block
33
+ # Sequel.connect(@url) do |db|
34
+ # db.transaction do
35
+ # return block.call db
36
+ # end
37
+ # end
38
+ # end
39
+ # end
40
+ # end
@@ -1,3 +1,3 @@
1
1
  module SandthornDriverSequel
2
- VERSION = "1.0.4"
2
+ VERSION = "1.0.5"
3
3
  end
@@ -31,6 +31,7 @@ Gem::Specification.new do |spec|
31
31
  spec.add_development_dependency "uuidtools"
32
32
  spec.add_development_dependency "ruby-beautify"
33
33
  spec.add_development_dependency "msgpack"
34
+ spec.add_development_dependency "snappy"
34
35
 
35
36
  spec.add_runtime_dependency "sequel"
36
37
  spec.add_runtime_dependency "pg"
@@ -0,0 +1,125 @@
1
+ require 'spec_helper'
2
+ require 'benchmark'
3
+ require 'yaml'
4
+
5
+ module Sandthorn
6
+ module AggregateRoot
7
+
8
+ describe "benchmark" do
9
+
10
+ before(:each) { prepare_for_test }
11
+ let(:test_events_20_events) do
12
+ e = []
13
+ e << {aggregate_version: 1, event_name: "new", event_data: "---\n:method_name: new\n:method_args: []\n:attribute_deltas:\n- :attribute_name: :@aggregate_id\n :old_value: \n :new_value: 0a74e545-be84-4506-8b0a-73e947856327\n"}
14
+ 19.times do |i|
15
+ e << {aggregate_version: i+2, event_name: "foo", event_data: "A2"}
16
+ end
17
+ e
18
+ end
19
+ let(:test_events_one_event) do
20
+ e = []
21
+ e << {aggregate_version: 1, event_name: "new", event_data: "B1" }
22
+ end
23
+ let(:test_events_two_events) do
24
+ e = []
25
+ e << {aggregate_version: 1, event_name: "new", event_data: "---\n:method_name: new\n:method_args: []\n:attribute_deltas:\n- :attribute_name: :@aggregate_id\n :old_value: \n :new_value: 0a74e545-be84-4506-8b0a-73e947856327\n"}
26
+ e << {aggregate_version: 2, event_name: "foo", event_data: "A2"}
27
+ end
28
+ let(:aggregate_id) {"c0456e26-2345-4f67-92fa-130b3a31a39a"}
29
+ let(:es) { event_store }
30
+
31
+
32
+ n = 500
33
+ describe "save" do
34
+ it "one event save 500 times" do
35
+ Benchmark.bm do |x|
36
+ x.report("new change save find") { for i in 1..n; es.save_events(test_events_one_event, 0, i.to_s, SandthornDriverSequel::EventStore); end }
37
+ end
38
+ end
39
+ it "two events save 500 times" do
40
+ Benchmark.bm do |x|
41
+ x.report("new change save find") { for i in 1..n; es.save_events(test_events_two_events, 0, i.to_s, SandthornDriverSequel::EventStore); end }
42
+ end
43
+ end
44
+ it "twenty events save 500 times" do
45
+ Benchmark.bm do |x|
46
+ x.report("new change save find") { for i in 1..n; es.save_events(test_events_20_events, 0, i.to_s, SandthornDriverSequel::EventStore); end }
47
+ end
48
+ end
49
+ end
50
+
51
+ describe "find" do
52
+ it "should find one event 500 times" do
53
+ es.save_events(test_events_one_event, 0, aggregate_id, SandthornDriverSequel::EventStore)
54
+
55
+ Benchmark.bm do |x|
56
+ x.report("find") { for i in 1..n; es.get_aggregate(aggregate_id, SandthornDriverSequel::EventStore); end }
57
+ end
58
+ end
59
+ it "should find two events 500 times" do
60
+ es.save_events(test_events_two_events, 0, aggregate_id, SandthornDriverSequel::EventStore)
61
+
62
+ Benchmark.bm do |x|
63
+ x.report("find") { for i in 1..n; es.get_aggregate(aggregate_id, SandthornDriverSequel::EventStore); end }
64
+ end
65
+ end
66
+ it "should find twenty events 500 times" do
67
+ es.save_events(test_events_20_events, 0, aggregate_id, SandthornDriverSequel::EventStore)
68
+
69
+ Benchmark.bm do |x|
70
+ x.report("find") { for i in 1..n; es.get_aggregate(aggregate_id, SandthornDriverSequel::EventStore); end }
71
+ end
72
+ end
73
+
74
+ end
75
+ describe "find with snapshot" do
76
+
77
+ it "should find one event that is snapshoted 500 times" do
78
+ snapshot_data = { event_data: YAML.dump(Object.new), aggregate_version: 1 }
79
+ es.save_events(test_events_one_event, 0, aggregate_id, SandthornDriverSequel::EventStore)
80
+ es.save_snapshot(snapshot_data, aggregate_id, SandthornDriverSequel::EventStore)
81
+
82
+ Benchmark.bm do |x|
83
+ x.report("find") { for i in 1..n; es.get_aggregate(aggregate_id, SandthornDriverSequel::EventStore); end }
84
+ end
85
+ end
86
+ it "should find two events that is snapshoted 500 times" do
87
+ snapshot_data = { event_data: YAML.dump(Object.new), aggregate_version: 2 }
88
+ es.save_events(test_events_two_events, 0, aggregate_id, SandthornDriverSequel::EventStore)
89
+ es.save_snapshot(snapshot_data, aggregate_id, SandthornDriverSequel::EventStore)
90
+ Benchmark.bm do |x|
91
+ x.report("find") { for i in 1..n; es.get_aggregate(aggregate_id, SandthornDriverSequel::EventStore); end }
92
+ end
93
+ end
94
+ it "should find twenty events that is snapshoted 500 times" do
95
+ snapshot_data = { event_data: YAML.dump(Object.new), aggregate_version: 19 }
96
+ es.save_events(test_events_20_events, 0, aggregate_id, SandthornDriverSequel::EventStore)
97
+ es.save_snapshot(snapshot_data, aggregate_id, SandthornDriverSequel::EventStore)
98
+
99
+ Benchmark.bm do |x|
100
+ x.report("find") { for i in 1..n; es.get_aggregate(aggregate_id, SandthornDriverSequel::EventStore); end }
101
+ end
102
+ end
103
+
104
+ end
105
+ # it "new save and find 500 aggregates" do
106
+
107
+ # Benchmark.bm do |x|
108
+ # x.report("new change save find") { for i in 1..n; es.save_events(test_events_a, 0, i.to_s, SandthornDriverSequel::EventStore); es.get_aggregate(i.to_s, SandthornDriverSequel::EventStore); end }
109
+ # end
110
+
111
+ # end
112
+
113
+ # it "should commit 500 actions" do
114
+ # Benchmark.bm do |x|
115
+ # x.report("commit") { for i in 1..n; test_object.change_name "#{i}"; end }
116
+ # end
117
+ # end
118
+ # it "should commit and save 500 actions" do
119
+ # Benchmark.bm do |x|
120
+ # x.report("commit save") { for i in 1..n; test_object.change_name("#{i}").save; end }
121
+ # end
122
+ # end
123
+ end
124
+ end
125
+ end
@@ -1,4 +1,5 @@
1
1
  require 'spec_helper'
2
+ require 'yaml'
2
3
 
3
4
  module SandthornDriverSequel
4
5
  describe EventStore do
@@ -6,7 +7,7 @@ module SandthornDriverSequel
6
7
  let(:aggregate_id) { @id ||= UUIDTools::UUID.random_create.to_s }
7
8
  let(:test_events) { [{aggregate_version: 1, event_data: nil, event_name: "new"},{aggregate_version: 2, event_data: nil, event_name: "foo"}] }
8
9
  let(:additional_events) { [{aggregate_version: 3, event_data: nil, event_name: "klopp"},{aggregate_version: 4, event_data: nil, event_name: "flipp"}] }
9
- let(:snapshot_data) { { event_data: Marshal.dump(Object.new), aggregate_version: 2 } }
10
+ let(:snapshot_data) { { event_data: YAML.dump(Object.new), aggregate_version: 2 } }
10
11
  let(:save_snapshot) {event_store.save_snapshot snapshot_data, aggregate_id, SandthornDriverSequel::EventStore}
11
12
  let(:save_events) {event_store.save_events test_events, 0, aggregate_id, SandthornDriverSequel::EventStore}
12
13
  let(:save_additional_events) {event_store.save_events additional_events, 2, aggregate_id, SandthornDriverSequel::EventStore}
@@ -22,6 +22,7 @@ end
22
22
 
23
23
  def event_store_url
24
24
  "sqlite://spec/db/event_store.sqlite3"
25
+ #"postgres://morganhallgren@localhost:5432/test_1"
25
26
  end
26
27
 
27
28
  def event_store context: :test
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sandthorn_driver_sequel
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.4
4
+ version: 1.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lars Krantz
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-03-11 00:00:00.000000000 Z
12
+ date: 2014-03-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -179,6 +179,20 @@ dependencies:
179
179
  - - ">="
180
180
  - !ruby/object:Gem::Version
181
181
  version: '0'
182
+ - !ruby/object:Gem::Dependency
183
+ name: snappy
184
+ requirement: !ruby/object:Gem::Requirement
185
+ requirements:
186
+ - - ">="
187
+ - !ruby/object:Gem::Version
188
+ version: '0'
189
+ type: :development
190
+ prerelease: false
191
+ version_requirements: !ruby/object:Gem::Requirement
192
+ requirements:
193
+ - - ">="
194
+ - !ruby/object:Gem::Version
195
+ version: '0'
182
196
  - !ruby/object:Gem::Dependency
183
197
  name: sequel
184
198
  requirement: !ruby/object:Gem::Requirement
@@ -232,6 +246,7 @@ files:
232
246
  - lib/sandthorn_driver_sequel/version.rb
233
247
  - sandthorn_driver_sequel.gemspec
234
248
  - spec/asking_for_aggregates_to_snapshot_spec.rb
249
+ - spec/benchmark_spec.rb
235
250
  - spec/driver_interface_spec.rb
236
251
  - spec/event_store_with_context_spec.rb
237
252
  - spec/get_events_spec.rb
@@ -265,6 +280,7 @@ specification_version: 4
265
280
  summary: sequel driver for sandthorn
266
281
  test_files:
267
282
  - spec/asking_for_aggregates_to_snapshot_spec.rb
283
+ - spec/benchmark_spec.rb
268
284
  - spec/driver_interface_spec.rb
269
285
  - spec/event_store_with_context_spec.rb
270
286
  - spec/get_events_spec.rb