Skip to content

Commit

Permalink
Extract verification of incorrect any usage to separate method
Browse files Browse the repository at this point in the history
  • Loading branch information
swistak35 committed Jul 26, 2021
1 parent 391ec8e commit 378cef3
Showing 1 changed file with 13 additions and 16 deletions.
29 changes: 13 additions & 16 deletions ruby_event_store/lib/ruby_event_store/in_memory_repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,7 @@ def append_to_stream(records, stream, expected_version)
serialized_records = records.map { |record| record.serialize(serializer) }

with_synchronize(expected_version, stream) do |resolved_version|
if @verify_incorrect_any_usage
stream_positions = streams.fetch(stream.name, Array.new).map(&:position)
if resolved_version.nil?
raise UnsupportedVersionAnyUsage if !stream_positions.compact.empty?
else
raise UnsupportedVersionAnyUsage if stream_positions.include?(nil)
end
end
verify_incorrect_any_usage(resolved_version, stream)
raise WrongExpectedEventVersion unless resolved_version.nil? || last_stream_version(stream).equal?(resolved_version)

serialized_records.each_with_index do |serialized_record, index|
Expand All @@ -61,14 +54,7 @@ def link_to_stream(event_ids, stream, expected_version)
serialized_records = event_ids.map { |id| read_event(id) }

with_synchronize(expected_version, stream) do |resolved_version|
if @verify_incorrect_any_usage
stream_positions = streams.fetch(stream.name, Array.new).map(&:position)
if resolved_version.nil?
raise UnsupportedVersionAnyUsage if !stream_positions.compact.empty?
else
raise UnsupportedVersionAnyUsage if stream_positions.include?(nil)
end
end
verify_incorrect_any_usage(resolved_version, stream)
raise WrongExpectedEventVersion unless resolved_version.nil? || last_stream_version(stream).equal?(resolved_version)

serialized_records.each_with_index do |serialized_record, index|
Expand Down Expand Up @@ -229,6 +215,17 @@ def add_to_stream(stream, serialized_record, resolved_version, index)
streams[stream.name] << EventInStream.new(serialized_record.event_id, compute_position(resolved_version, index))
end

def verify_incorrect_any_usage(resolved_version, stream)
if @verify_incorrect_any_usage
stream_positions = streams.fetch(stream.name, Array.new).map(&:position)
if resolved_version.nil?
raise UnsupportedVersionAnyUsage if !stream_positions.compact.empty?
else
raise UnsupportedVersionAnyUsage if stream_positions.include?(nil)
end
end
end

attr_reader :streams, :mutex, :storage, :serializer, :next_global_position
end
end

0 comments on commit 378cef3

Please sign in to comment.