rails_event_store_active_record 1.1.0 → 1.3.0

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
  SHA256:
3
- metadata.gz: 5968e2e91ed12b5ddfa6597aa022e261d9abd3d1cf060f43bd41b8fc58e3a506
4
- data.tar.gz: 17a713daaf14b8b2a42b9aedeb80beae5c054704c49e18469f08dc87ff6d9572
3
+ metadata.gz: 0deda45046d7c0914a105aa67fe9c694550a7a3c07aec439a80a5b01a2a13418
4
+ data.tar.gz: 664c1aa606c495b81081861adca51af9d9829f40cdafaba61b9b73be586fa867
5
5
  SHA512:
6
- metadata.gz: 4ca8fc592daffd5b6729b4ae026cd9b791dfd47a0e98a408f5a57ce92a21214ecd1266b47731f35176bf2eba6b69429f8242967243387048a9d972bf4d796d36
7
- data.tar.gz: 4df7433ad7a9d2eccc2c41a1d379d797454917dd54a6308a25c833231c97158b8983307c209cf4c020545d688c686ad833d06207743d682dfaabfb96b67be91a
6
+ metadata.gz: ef624e7e400a25baa7a6b8a444c585a0d568673914f15d51a22a910f1152e2239585df0bf8cfe0ecd958b768bb14d2c7813035f5cd80263aebcbfdb307909440
7
+ data.tar.gz: 218e460ff03c3efbb924a9a0911a7e82e38d2c7684d430c8b8791d674bdfa1f1a61953386f1008a3a728d5b9918f1de21aacf5d831429bb54328ea93c372d45f
@@ -6,6 +6,7 @@ require 'rails_event_store_active_record/generators/limit_for_event_id_generator
6
6
  require 'rails_event_store_active_record/generators/binary_data_and_metadata_generator'
7
7
  require 'rails_event_store_active_record/event'
8
8
  require 'rails_event_store_active_record/event_repository'
9
+ require 'rails_event_store_active_record/batch_enumerator'
9
10
  require 'rails_event_store_active_record/event_repository_reader'
10
11
  require 'rails_event_store_active_record/index_violation_detector'
11
12
  require 'rails_event_store_active_record/pg_linearized_event_repository'
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RailsEventStoreActiveRecord
4
+ class BatchEnumerator
5
+ def initialize(batch_size, total_limit, reader)
6
+ @batch_size = batch_size
7
+ @total_limit = total_limit
8
+ @reader = reader
9
+ end
10
+
11
+ def each
12
+ return to_enum unless block_given?
13
+ offset_id = nil
14
+
15
+ 0.step(total_limit - 1, batch_size) do |batch_offset|
16
+ batch_limit = [batch_size, total_limit - batch_offset].min
17
+ results, offset_id = reader.call(offset_id, batch_limit)
18
+
19
+ break if results.empty?
20
+ yield results
21
+ end
22
+ end
23
+
24
+ def first
25
+ each.first
26
+ end
27
+
28
+ def to_a
29
+ each.to_a
30
+ end
31
+
32
+ private
33
+
34
+ attr_reader :batch_size, :total_limit, :reader
35
+ end
36
+ end
37
+
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'active_support/core_ext/array'
3
4
  require 'activerecord-import'
4
5
 
5
6
  module RailsEventStoreActiveRecord
@@ -12,9 +13,13 @@ module RailsEventStoreActiveRecord
12
13
  end
13
14
 
14
15
  def append_to_stream(events, stream, expected_version)
15
- add_to_stream(Array(events), stream, expected_version, true) do |event|
16
- build_event_record(event).save!
17
- event.event_id
16
+ records, event_ids = [], []
17
+ Array(events).each do |event|
18
+ records << build_event_hash(event)
19
+ event_ids << event.event_id
20
+ end
21
+ add_to_stream(event_ids, stream, expected_version, true) do
22
+ Event.import(records)
18
23
  end
19
24
  end
20
25
 
@@ -23,9 +28,7 @@ module RailsEventStoreActiveRecord
23
28
  (event_ids - Event.where(id: event_ids).pluck(:id)).each do |id|
24
29
  raise RubyEventStore::EventNotFound.new(id)
25
30
  end
26
- add_to_stream(event_ids, stream, expected_version, nil) do |event_id|
27
- event_id
28
- end
31
+ add_to_stream(event_ids, stream, expected_version, nil)
29
32
  end
30
33
 
31
34
  def delete_stream(stream)
@@ -68,14 +71,14 @@ module RailsEventStoreActiveRecord
68
71
 
69
72
  private
70
73
 
71
- def add_to_stream(collection, stream, expected_version, include_global, &to_event_id)
74
+ def add_to_stream(event_ids, stream, expected_version, include_global)
72
75
  last_stream_version = ->(stream_) { EventInStream.where(stream: stream_.name).order("position DESC").first.try(:position) }
73
76
  resolved_version = expected_version.resolve_for(stream, last_stream_version)
74
77
 
75
78
  start_transaction do
76
- in_stream = collection.flat_map.with_index do |element, index|
79
+ yield if block_given?
80
+ in_stream = event_ids.flat_map.with_index do |event_id, index|
77
81
  position = compute_position(resolved_version, index)
78
- event_id = to_event_id.call(element)
79
82
  collection = []
80
83
  collection.unshift({
81
84
  stream: SERIALIZED_GLOBAL_STREAM_NAME,
@@ -114,13 +117,13 @@ module RailsEventStoreActiveRecord
114
117
  IndexViolationDetector.new.detect(message)
115
118
  end
116
119
 
117
- def build_event_record(serialized_record)
118
- Event.new(
120
+ def build_event_hash(serialized_record)
121
+ {
119
122
  id: serialized_record.event_id,
120
123
  data: serialized_record.data,
121
124
  metadata: serialized_record.metadata,
122
125
  event_type: serialized_record.event_type
123
- )
126
+ }
124
127
  end
125
128
 
126
129
  # Overwritten in a sub-class
@@ -9,7 +9,7 @@ module RailsEventStoreActiveRecord
9
9
 
10
10
  def last_stream_event(stream)
11
11
  record = EventInStream.where(stream: stream.name).order('position DESC, id DESC').first
12
- record && build_event_instance(record)
12
+ record && build_serialized_record(record)
13
13
  end
14
14
 
15
15
  def read(spec)
@@ -18,16 +18,19 @@ module RailsEventStoreActiveRecord
18
18
  stream = read_scope(spec)
19
19
 
20
20
  if spec.batched?
21
- batch_reader = ->(offset, limit) { stream.offset(offset).limit(limit).map(&method(:build_event_instance)) }
22
- RubyEventStore::BatchEnumerator.new(spec.batch_size, spec.limit, batch_reader).each
21
+ batch_reader = ->(offset_id, limit) do
22
+ records = offset_id.nil? ? stream.limit(limit) : stream.where(start_offset_condition(spec, offset_id)).limit(limit)
23
+ [records.map(&method(:build_serialized_record)), records.last]
24
+ end
25
+ BatchEnumerator.new(spec.batch_size, spec.limit, batch_reader).each
23
26
  elsif spec.first?
24
27
  record = stream.first
25
- build_event_instance(record) if record
28
+ build_serialized_record(record) if record
26
29
  elsif spec.last?
27
30
  record = stream.last
28
- build_event_instance(record) if record
31
+ build_serialized_record(record) if record
29
32
  else
30
- stream.map(&method(:build_event_instance)).each
33
+ stream.map(&method(:build_serialized_record)).each
31
34
  end
32
35
  end
33
36
 
@@ -55,25 +58,31 @@ module RailsEventStoreActiveRecord
55
58
  specification.stream.global? ? EventRepository::SERIALIZED_GLOBAL_STREAM_NAME : specification.stream.name
56
59
  end
57
60
 
58
- def start_condition(specification)
59
- event_record =
60
- EventInStream.find_by!(event_id: specification.start, stream: normalize_stream_name(specification))
61
+ def start_offset_condition(specification, record_id)
61
62
  condition = specification.forward? ? 'event_store_events_in_streams.id > ?' : 'event_store_events_in_streams.id < ?'
62
- [condition, event_record]
63
+ [condition, record_id]
63
64
  end
64
65
 
65
- def stop_condition(specification)
66
- event_record =
67
- EventInStream.find_by!(event_id: specification.stop, stream: normalize_stream_name(specification))
66
+ def stop_offset_condition(specification, record_id)
68
67
  condition = specification.forward? ? 'event_store_events_in_streams.id < ?' : 'event_store_events_in_streams.id > ?'
69
- [condition, event_record]
68
+ [condition, record_id]
69
+ end
70
+
71
+ def start_condition(specification)
72
+ start_offset_condition(specification,
73
+ EventInStream.find_by!(event_id: specification.start, stream: normalize_stream_name(specification)))
74
+ end
75
+
76
+ def stop_condition(specification)
77
+ stop_offset_condition(specification,
78
+ EventInStream.find_by!(event_id: specification.stop, stream: normalize_stream_name(specification)))
70
79
  end
71
80
 
72
81
  def order(spec)
73
82
  spec.forward? ? 'ASC' : 'DESC'
74
83
  end
75
84
 
76
- def build_event_instance(record)
85
+ def build_serialized_record(record)
77
86
  RubyEventStore::SerializedRecord.new(
78
87
  event_id: record.event.id,
79
88
  metadata: record.event.metadata,
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RailsEventStoreActiveRecord
4
- VERSION = "1.1.0"
4
+ VERSION = "1.3.0"
5
5
  end
@@ -25,7 +25,7 @@ Gem::Specification.new do |spec|
25
25
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
26
  spec.require_paths = ['lib']
27
27
 
28
- spec.add_dependency 'ruby_event_store', '= 1.1.0'
28
+ spec.add_dependency 'ruby_event_store', '= 1.3.0'
29
29
  spec.add_dependency 'activesupport', '>= 3.0'
30
30
  spec.add_dependency 'activemodel', '>= 3.0'
31
31
  spec.add_dependency 'activerecord-import', '>= 1.0.2'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_event_store_active_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Arkency
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-07-28 00:00:00.000000000 Z
11
+ date: 2020-12-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ruby_event_store
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - '='
18
18
  - !ruby/object:Gem::Version
19
- version: 1.1.0
19
+ version: 1.3.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - '='
25
25
  - !ruby/object:Gem::Version
26
- version: 1.1.0
26
+ version: 1.3.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: activesupport
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -81,6 +81,7 @@ files:
81
81
  - bin/console
82
82
  - bin/setup
83
83
  - lib/rails_event_store_active_record.rb
84
+ - lib/rails_event_store_active_record/batch_enumerator.rb
84
85
  - lib/rails_event_store_active_record/event.rb
85
86
  - lib/rails_event_store_active_record/event_repository.rb
86
87
  - lib/rails_event_store_active_record/event_repository_reader.rb
@@ -104,7 +105,7 @@ metadata:
104
105
  changelog_uri: https://github.com/RailsEventStore/rails_event_store/releases
105
106
  source_code_uri: https://github.com/RailsEventStore/rails_event_store
106
107
  bug_tracker_uri: https://github.com/RailsEventStore/rails_event_store/issues
107
- post_install_message:
108
+ post_install_message:
108
109
  rdoc_options: []
109
110
  require_paths:
110
111
  - lib
@@ -119,8 +120,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
119
120
  - !ruby/object:Gem::Version
120
121
  version: '0'
121
122
  requirements: []
122
- rubygems_version: 3.0.3
123
- signing_key:
123
+ rubygems_version: 3.1.4
124
+ signing_key:
124
125
  specification_version: 4
125
126
  summary: Active Record events repository for Rails Event Store
126
127
  test_files: []