Skip to content

Commit 7fb5d4c

Browse files
committed
Assume batching with #each_batch, at least for now.
Still better story than wrapping each item to be returned as array.
1 parent dd68b4a commit 7fb5d4c

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

ruby_event_store/lib/ruby_event_store/specification.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,16 @@ def limit(count)
7373

7474
def each_batch
7575
return to_enum(:each_batch) unless block_given?
76-
repository.read(result).each do |batch|
77-
yield Array(batch).map { |serialized_record| mapper.serialized_record_to_event(serialized_record) }
76+
77+
result_ = result.batched? ? result : result.tap { |r| r.batch_size = DEFAULT_BATCH_SIZE }
78+
repository.read(result_).each do |batch|
79+
yield batch.map { |serialized_record| mapper.serialized_record_to_event(serialized_record) }
7880
end
7981
end
8082

8183
def each
8284
return to_enum unless block_given?
85+
8386
each_batch do |batch|
8487
batch.each { |event| yield event }
8588
end

ruby_event_store/spec/specification_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,20 +226,23 @@ module RubyEventStore
226226
specify do
227227
records = [test_record, test_record]
228228
repository.append_to_stream(records, Stream.new("Dummy"), ExpectedVersion.none)
229+
229230
expect(specification.from(records[0].event_id).each.to_a).to eq([TestEvent.new(event_id: records[1].event_id)])
230231
end
231232

232233
specify do
233234
batch_size = 100
234235
records = (batch_size * 10).times.map { test_record }
235236
repository.append_to_stream(records, Stream.new("batch"), ExpectedVersion.none)
237+
236238
expect(specification.stream("batch").in_batches.each_batch.to_a.size).to eq(10)
237239
end
238240

239241
specify do
240242
batch_size = 100
241243
records = (batch_size * 10).times.map { test_record }
242244
repository.append_to_stream(records, Stream.new("batch"), ExpectedVersion.none)
245+
243246
expect(specification.stream("batch").in_batches.each.to_a.size).to eq(1000)
244247
end
245248

@@ -284,6 +287,14 @@ module RubyEventStore
284287
expect(specification.in_batches_of(1000).result).to eq(specification.in_batches(1000).result)
285288
end
286289

290+
specify do
291+
records = 200.times.map { test_record }
292+
repository.append_to_stream(records, Stream.new("whatever"), ExpectedVersion.none)
293+
294+
expect(specification.each_batch.to_a).to eq(specification.in_batches.each_batch.to_a)
295+
expect(specification.each_batch.to_a).not_to eq(specification.in_batches(1000).each_batch.to_a)
296+
end
297+
287298
let(:repository) { InMemoryRepository.new }
288299
let(:mapper) { Mappers::Default.new }
289300
let(:specification) { Specification.new(repository, mapper) }

0 commit comments

Comments
 (0)