rails_event_store_active_record 1.0.0 → 1.2.2

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: 4ed651c7edb9903368e47faa650d73e41d49f32d2e3ebd6ef43be86a705eed09
4
- data.tar.gz: f4fbca40b999d6ae042456cf6e139088a9174e764b504af296c4e5c846dbe61a
3
+ metadata.gz: 8bab164390f5e3691fc4ad2ac4728781fcfa0b4054dfdb41612ce76e47e48d4c
4
+ data.tar.gz: e49768c59cafded1fd9b10a730d264e93ea15ed45eff055229d5b63eb7586bbf
5
5
  SHA512:
6
- metadata.gz: 3d694836036c3d29f4936b29b4ed25cc792a7b2b017186dee230dd422a1132633b4d8b412f5bd719b0ff41fc32ddeddc9f8647ed29ed47ed6988b8fce2386810
7
- data.tar.gz: 2c9369815872e85e3967709e79760c32ddf4ee1691f8ab48011890e9f7e0cd99f2a1b715d39eddec43fb2b6307a2076fd18595d93386071c95c6dffe24578746
6
+ metadata.gz: 7f31d0e5a8af03f017a331dc58f40911242bb6f24773fd0a772ec117e8107c13205cca61fa9dc4321b6d4f3ffce1b1d64dcebab745fcab6691e08c31de72f2c5
7
+ data.tar.gz: 48859c9ba6b8502109ac9523cd4b0815d133cd9023b37afc7c686fd6186ec948f6a068d3396cca0480e3d057e24cc9d7ced27a2527ea7f66f6051629400f0f55
@@ -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
+
@@ -12,9 +12,13 @@ module RailsEventStoreActiveRecord
12
12
  end
13
13
 
14
14
  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
15
+ records, event_ids = [], []
16
+ Array(events).each do |event|
17
+ records << build_event_hash(event)
18
+ event_ids << event.event_id
19
+ end
20
+ add_to_stream(event_ids, stream, expected_version, true) do
21
+ Event.import(records)
18
22
  end
19
23
  end
20
24
 
@@ -23,9 +27,7 @@ module RailsEventStoreActiveRecord
23
27
  (event_ids - Event.where(id: event_ids).pluck(:id)).each do |id|
24
28
  raise RubyEventStore::EventNotFound.new(id)
25
29
  end
26
- add_to_stream(event_ids, stream, expected_version, nil) do |event_id|
27
- event_id
28
- end
30
+ add_to_stream(event_ids, stream, expected_version, nil)
29
31
  end
30
32
 
31
33
  def delete_stream(stream)
@@ -68,14 +70,14 @@ module RailsEventStoreActiveRecord
68
70
 
69
71
  private
70
72
 
71
- def add_to_stream(collection, stream, expected_version, include_global, &to_event_id)
73
+ def add_to_stream(event_ids, stream, expected_version, include_global)
72
74
  last_stream_version = ->(stream_) { EventInStream.where(stream: stream_.name).order("position DESC").first.try(:position) }
73
75
  resolved_version = expected_version.resolve_for(stream, last_stream_version)
74
76
 
75
77
  start_transaction do
76
- in_stream = collection.flat_map.with_index do |element, index|
78
+ yield if block_given?
79
+ in_stream = event_ids.flat_map.with_index do |event_id, index|
77
80
  position = compute_position(resolved_version, index)
78
- event_id = to_event_id.call(element)
79
81
  collection = []
80
82
  collection.unshift({
81
83
  stream: SERIALIZED_GLOBAL_STREAM_NAME,
@@ -114,13 +116,13 @@ module RailsEventStoreActiveRecord
114
116
  IndexViolationDetector.new.detect(message)
115
117
  end
116
118
 
117
- def build_event_record(serialized_record)
118
- Event.new(
119
+ def build_event_hash(serialized_record)
120
+ {
119
121
  id: serialized_record.event_id,
120
122
  data: serialized_record.data,
121
123
  metadata: serialized_record.metadata,
122
124
  event_type: serialized_record.event_type
123
- )
125
+ }
124
126
  end
125
127
 
126
128
  # 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.0.0"
4
+ VERSION = "1.2.2"
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.0.0'
28
+ spec.add_dependency 'ruby_event_store', '= 1.2.2'
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.0.0
4
+ version: 1.2.2
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-04-28 00:00:00.000000000 Z
11
+ date: 2020-11-03 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.0.0
19
+ version: 1.2.2
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.0.0
26
+ version: 1.2.2
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: []