Skip to content

Commit

Permalink
Introduce search_streams in EventRepository
Browse files Browse the repository at this point in the history
Search streams searches for streams with similar name
  • Loading branch information
lukaszreszke committed Mar 19, 2024
1 parent 58f22b0 commit e930fbf
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ def count(specification)
@repo_reader.count(specification)
end

def search_streams(stream_name)
@repo_reader.search_streams(stream_name)
end

def update_messages(records)
hashes = records.map { |record| upsert_hash(record, record.serialize(serializer)) }
for_update = records.map(&:event_id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ def event_in_stream?(event_id, stream)
@stream_klass.where(event_id: event_id, stream: stream.name).exists?
end

def search_streams(stream)
@stream_klass.where("stream LIKE ?", "#{stream}%").pluck(:stream).uniq
end

private

attr_reader :serializer
Expand Down
51 changes: 51 additions & 0 deletions ruby_event_store-active_record/spec/event_repository_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,57 @@ module ActiveRecord
expect(event_record.valid_at).to eq(t2)
end

specify "finds streams that match the search phrase" do
repository.append_to_stream(
[SRecord.new(event_id: e1 = SecureRandom.uuid)],
Stream.new("Dummy$#{e1}"),
ExpectedVersion.any
)
repository.append_to_stream(
[SRecord.new(event_id: e2 = SecureRandom.uuid)],
Stream.new("Dummy$#{e2}"),
ExpectedVersion.any
)
repository.append_to_stream(
[SRecord.new(event_id: e3 = SecureRandom.uuid)],
Stream.new("Dummy$#{e3}"),
ExpectedVersion.any
)

expect(repository.search_streams("Du")).to eq(["Dummy$#{e1}", "Dummy$#{e2}", "Dummy$#{e3}"])
expect(repository.search_streams("du")).to eq(["Dummy$#{e1}", "Dummy$#{e2}", "Dummy$#{e3}"])
expect(repository.search_streams("dum")).to eq(["Dummy$#{e1}", "Dummy$#{e2}", "Dummy$#{e3}"])
expect(repository.search_streams("dumm")).to eq(["Dummy$#{e1}", "Dummy$#{e2}", "Dummy$#{e3}"])
expect(repository.search_streams("dummy")).to eq(["Dummy$#{e1}", "Dummy$#{e2}", "Dummy$#{e3}"])
expect(repository.search_streams("Dummy")).to eq(["Dummy$#{e1}", "Dummy$#{e2}", "Dummy$#{e3}"])
end

specify "finds no streams when search phrase doesn't match anything" do
repository.append_to_stream(
[SRecord.new(event_id: e1 = SecureRandom.uuid)],
Stream.new("Dummy$#{e1}"),
ExpectedVersion.any
)
repository.append_to_stream(
[SRecord.new(event_id: e2 = SecureRandom.uuid)],
Stream.new("Dummy$#{e2}"),
ExpectedVersion.any
)
repository.append_to_stream(
[SRecord.new(event_id: e3 = SecureRandom.uuid)],
Stream.new("Dummy$#{e3}"),
ExpectedVersion.any
)

expect(repository.search_streams("Da")).to eq([])
expect(repository.search_streams("da")).to eq([])
expect(repository.search_streams("dam")).to eq([])
expect(repository.search_streams("damm")).to eq([])
expect(repository.search_streams("dammy")).to eq([])
expect(repository.search_streams("Dammy")).to eq([])
end


specify "with batches and bi-temporal queries use offset + limit" do
repository.append_to_stream(
[
Expand Down

0 comments on commit e930fbf

Please sign in to comment.