Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add first/last to read specification #399

Merged
merged 27 commits into from
Jul 30, 2018
Merged
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
619b912
New methods to read first & last events from specified read spec
mpraglowski Jul 18, 2018
1fc66bf
Implementation of first & last read spec methond on AR event reposito…
mpraglowski Jul 18, 2018
b96360a
Additional requirements for event repository implementations
mpraglowski Jul 18, 2018
afaaf64
Handle first/last reads in ROM event repository
mpraglowski Jul 18, 2018
d19b46c
Simplify fetching last event from a stream using new API
mpraglowski Jul 18, 2018
8af5d0d
Unnecessary code remove
mpraglowski Jul 19, 2018
5de1b93
Killing the mutants
mpraglowski Jul 19, 2018
a02ff95
No need for that if here
mpraglowski Jul 19, 2018
7082e7a
DRY'd up and work around `last` to use `first`
joelvh Jul 20, 2018
fbc5c81
Additional test to kill mutations
mpraglowski Jul 22, 2018
eb9bf12
API documentation
mpraglowski Jul 22, 2018
8f1f88e
Stop using Specification private internals in ROM repository
mpraglowski Jul 22, 2018
3c6b205
Specification is immutable - never modify it directly
mpraglowski Jul 23, 2018
14b2936
Fix expectation - no mutatable specification
mpraglowski Jul 23, 2018
970452d
Symbols over constants
mpraglowski Jul 23, 2018
b319a24
Killing mutants
mpraglowski Jul 23, 2018
083b4ec
Additional tests to verify offset works correctly when reading first/…
mpraglowski Jul 23, 2018
8c10eab
Use specification limit instead of count
mpraglowski Jul 23, 2018
757b4ac
The read last optimalization for ROM event repository fix when not re…
mpraglowski Jul 25, 2018
a677aa5
Kill a few more mutants
mpraglowski Jul 25, 2018
951fb59
Inline methods.
mpraglowski Jul 26, 2018
a1179ef
More precise expectations.
mpraglowski Jul 26, 2018
bf4decb
Missing docs added.
mpraglowski Jul 26, 2018
bbac69c
Be like enumerable - nils over errors
mpraglowski Jul 26, 2018
15636c0
And actually implement nils over errors in event repositories.
mpraglowski Jul 26, 2018
f489dd0
No need to check for nils here - build in into build_event_entity
mpraglowski Jul 30, 2018
9d0b696
Killing mutants
mpraglowski Jul 30, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Stop using Specification private internals in ROM repository
  • Loading branch information
mpraglowski committed Jul 23, 2018
commit 8f1f88e81b94d4dce141d3c204543fcaba7c7af1
18 changes: 2 additions & 16 deletions ruby_event_store-rom/lib/ruby_event_store/rom/event_repository.rb
Original file line number Diff line number Diff line change
@@ -72,14 +72,7 @@ def has_event?(event_id)
end

def last_stream_event(stream)
@events.read(
:backward,
stream,
from: :head,
limit: 1,
read_as: nil,
batch_size: nil
).first
@events.last_stream_event(stream)
end

def read_event(event_id)
@@ -91,14 +84,7 @@ def read_event(event_id)
def read(specification)
raise ReservedInternalName if specification.stream_name.eql?(@stream_entries.stream_entries.class::SERIALIZED_GLOBAL_STREAM_NAME)

@events.read(
specification.direction,
specification.stream,
from: specification.start,
limit: (specification.count if specification.limit?),
read_as: specification.read_as,
batch_size: specification.batch_size
)
@events.read(specification)
end

private
Original file line number Diff line number Diff line change
@@ -35,33 +35,33 @@ def by_id(event_id)
events.map_with(:event_to_serialized_record).by_pk(event_id).one!
end

MATERIALIZE_READ_AS = {
RubyEventStore::Specification::BATCH => :to_ary,
RubyEventStore::Specification::FIRST => :first,
RubyEventStore::Specification::LAST => :first
}.freeze
def last_stream_event(stream)
query = stream_entries.ordered(:backward, stream)
query = query_builder(query, limit: 1)
query.first
end

def read(direction, stream, from:, limit:, read_as:, batch_size:)
unless from.equal?(:head)
offset_entry_id = stream_entries.by_stream_and_event_id(stream, from).fetch(:id)
def read(specification)
unless specification.head?
offset_entry_id = stream_entries.by_stream_and_event_id(specification.stream, specification.start).fetch(:id)
end

# Note: `last` is problematic, so we switch direction and get `first`.
# See `MATERIALIZE_READ_AS`
if read_as == RubyEventStore::Specification::LAST
direction = direction == :forward ? :backward : :forward
direction = specification.direction
limit = specification.count if specification.limit?
if specification.last?
direction = specification.forward? ? :backward : :forward
end

query = stream_entries.ordered(direction, stream, offset_entry_id)
query = stream_entries.ordered(direction, specification.stream, offset_entry_id)

if read_as == RubyEventStore::Specification::BATCH
if specification.batched?
reader = ->(offset, limit) do
query_builder(query, offset: offset, limit: limit).to_ary
end
BatchEnumerator.new(batch_size, limit || Float::INFINITY, reader).each
BatchEnumerator.new(specification.batch_size, limit || Float::INFINITY, reader).each
else
materialize_method = MATERIALIZE_READ_AS.fetch(read_as, :each)
query_builder(query, limit: limit).__send__(materialize_method)
query = query_builder(query, limit: limit)
specification.first? || specification.last? ? query.first : query.each
end
end