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 +4 -4
- data/lib/rails_event_store_active_record.rb +1 -0
- data/lib/rails_event_store_active_record/batch_enumerator.rb +37 -0
- data/lib/rails_event_store_active_record/event_repository.rb +15 -12
- data/lib/rails_event_store_active_record/event_repository_reader.rb +24 -15
- data/lib/rails_event_store_active_record/version.rb +1 -1
- data/rails_event_store_active_record.gemspec +1 -1
- metadata +9 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0deda45046d7c0914a105aa67fe9c694550a7a3c07aec439a80a5b01a2a13418
|
4
|
+
data.tar.gz: 664c1aa606c495b81081861adca51af9d9829f40cdafaba61b9b73be586fa867
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
16
|
-
|
17
|
-
event
|
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)
|
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(
|
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
|
-
|
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
|
118
|
-
|
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 &&
|
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 = ->(
|
22
|
-
|
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
|
-
|
28
|
+
build_serialized_record(record) if record
|
26
29
|
elsif spec.last?
|
27
30
|
record = stream.last
|
28
|
-
|
31
|
+
build_serialized_record(record) if record
|
29
32
|
else
|
30
|
-
stream.map(&method(:
|
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
|
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,
|
63
|
+
[condition, record_id]
|
63
64
|
end
|
64
65
|
|
65
|
-
def
|
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,
|
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
|
85
|
+
def build_serialized_record(record)
|
77
86
|
RubyEventStore::SerializedRecord.new(
|
78
87
|
event_id: record.event.id,
|
79
88
|
metadata: record.event.metadata,
|
@@ -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.
|
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.
|
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-
|
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.
|
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.
|
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.
|
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: []
|