Skip to content

Commit

Permalink
Assume batching with #each_batch, at least for now.
Browse files Browse the repository at this point in the history
Still better story than wrapping each item to be returned as array.
  • Loading branch information
mostlyobvious committed May 30, 2018
1 parent dd68b4a commit 7fb5d4c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
7 changes: 5 additions & 2 deletions ruby_event_store/lib/ruby_event_store/specification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,16 @@ def limit(count)

def each_batch
return to_enum(:each_batch) unless block_given?
repository.read(result).each do |batch|
yield Array(batch).map { |serialized_record| mapper.serialized_record_to_event(serialized_record) }

result_ = result.batched? ? result : result.tap { |r| r.batch_size = DEFAULT_BATCH_SIZE }
repository.read(result_).each do |batch|
yield batch.map { |serialized_record| mapper.serialized_record_to_event(serialized_record) }
end
end

def each
return to_enum unless block_given?

each_batch do |batch|
batch.each { |event| yield event }
end
Expand Down
11 changes: 11 additions & 0 deletions ruby_event_store/spec/specification_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -226,20 +226,23 @@ module RubyEventStore
specify do
records = [test_record, test_record]
repository.append_to_stream(records, Stream.new("Dummy"), ExpectedVersion.none)

expect(specification.from(records[0].event_id).each.to_a).to eq([TestEvent.new(event_id: records[1].event_id)])
end

specify do
batch_size = 100
records = (batch_size * 10).times.map { test_record }
repository.append_to_stream(records, Stream.new("batch"), ExpectedVersion.none)

expect(specification.stream("batch").in_batches.each_batch.to_a.size).to eq(10)
end

specify do
batch_size = 100
records = (batch_size * 10).times.map { test_record }
repository.append_to_stream(records, Stream.new("batch"), ExpectedVersion.none)

expect(specification.stream("batch").in_batches.each.to_a.size).to eq(1000)
end

Expand Down Expand Up @@ -284,6 +287,14 @@ module RubyEventStore
expect(specification.in_batches_of(1000).result).to eq(specification.in_batches(1000).result)
end

specify do
records = 200.times.map { test_record }
repository.append_to_stream(records, Stream.new("whatever"), ExpectedVersion.none)

expect(specification.each_batch.to_a).to eq(specification.in_batches.each_batch.to_a)
expect(specification.each_batch.to_a).not_to eq(specification.in_batches(1000).each_batch.to_a)
end

let(:repository) { InMemoryRepository.new }
let(:mapper) { Mappers::Default.new }
let(:specification) { Specification.new(repository, mapper) }
Expand Down

0 comments on commit 7fb5d4c

Please sign in to comment.