Skip to content

Commit

Permalink
batch enumerator optimization
Browse files Browse the repository at this point in the history
no need to fetch next batch if the previous one is smaller than
specified batch size
  • Loading branch information
pjurewicz committed Dec 6, 2023
1 parent 3315021 commit 0ae6f8d
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ def each
batch_limit = [batch_size, total_limit - batch_offset].min
results, offset_id = reader.call(offset_id, batch_limit)

break if results.empty?
yield results
yield results if results.any?
break if results.size < batch_size
end
end

Expand Down
9 changes: 4 additions & 5 deletions ruby_event_store-active_record/spec/event_repository_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -204,15 +204,14 @@ module ActiveRecord
Stream.new("Dummy"),
ExpectedVersion.any
)
::ActiveRecord::Base.logger = Logger.new(STDOUT)

expect {
repository.read(specification.in_batches.as_at.result).to_a
}.to match_query /SELECT.*FROM.*event_store_events.*ORDER BY .*event_store_events.*created_at.* ASC,.*event_store_events.*id.* ASC LIMIT.*.OFFSET.*/,
2
}.to match_query /SELECT.*FROM.*event_store_events.*ORDER BY .*event_store_events.*created_at.* ASC,.*event_store_events.*id.* ASC LIMIT.*.OFFSET.*/
expect {
repository.read(specification.in_batches.as_of.result).to_a
}.to match_query /SELECT.*FROM.*event_store_events.*ORDER BY .*COALESCE(.*event_store_events.*valid_at.*, .*event_store_events.*created_at.*).* ASC,.*event_store_events.*id.* ASC LIMIT.*.OFFSET.*/,
2
}.to match_query /SELECT.*FROM.*event_store_events.*ORDER BY .*COALESCE(.*event_store_events.*valid_at.*, .*event_store_events.*created_at.*).* ASC,.*event_store_events.*id.* ASC LIMIT.*.OFFSET.*/
end

specify "with batches and non-bi-temporal queries use monotonic ids" do
Expand All @@ -239,7 +238,7 @@ module ActiveRecord
)

expect {
repository.read(specification.in_batches.result).to_a
repository.read(specification.in_batches(3).result).to_a
}.to match_query /SELECT.*FROM.*event_store_events.*WHERE.*event_store_events.id >*.*ORDER BY .*event_store_events.*id.* ASC LIMIT.*/
end

Expand Down
4 changes: 2 additions & 2 deletions ruby_event_store/lib/ruby_event_store/batch_enumerator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ def each
batch_limit = [batch_size, total_limit - batch_offset].min
result = reader.call(batch_offset, batch_limit)

break if result.empty?
yield result
yield result if result.any?
break if result.size < batch_size
end
end

Expand Down

0 comments on commit 0ae6f8d

Please sign in to comment.