Skip to content

Commit

Permalink
Limit number of searched streams to 10
Browse files Browse the repository at this point in the history
Co-authored-by: Szymon Fiedler <[email protected]>
  • Loading branch information
lukaszreszke and fidel committed Mar 20, 2024
1 parent f9a6cdf commit b41cc6b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def event_in_stream?(event_id, stream)
end

def search_streams(stream)
@stream_klass.where("lower(stream) LIKE ?", "#{stream.downcase}%").order("position DESC, id DESC").pluck(:stream).map { |name| Stream.new(name) }
@stream_klass.where("lower(stream) LIKE ?", "#{stream.downcase}%").order("position DESC, id DESC").limit(10).pluck(:stream).map { |name| Stream.new(name) }
end

private
Expand Down
12 changes: 12 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 @@ -208,6 +208,18 @@ module ActiveRecord
expect(repository.search_streams("Dummy")).to eq([s3, s2, s1])
end

specify "limits searched streams to 10" do
11.times do |index|
repository.append_to_stream(
[SRecord.new(event_id: e1 = SecureRandom.uuid)],
Stream.new("Dummy$#{e1}"),
ExpectedVersion.any
)
end

expect(repository.search_streams("Du").size).to eq 10
end

specify "finds no streams when search phrase doesn't match anything" do
repository.append_to_stream(
[SRecord.new(event_id: e1 = SecureRandom.uuid)],
Expand Down
13 changes: 12 additions & 1 deletion ruby_event_store-browser/spec/api/search_streams_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module RubyEventStore
::RSpec.describe Browser do
include Browser::IntegrationHelpers

specify "something" do
specify "finds desired streams" do
event_store.publish(DummyEvent.new, stream_name: "dummy-1")
event_store.publish(DummyEvent.new, stream_name: "dummy-2")

Expand All @@ -26,5 +26,16 @@ module RubyEventStore
[{ "id" => "dummy-2", "type" => "streams" }, { "id" => "dummy-1", "type" => "streams" }]
)
end

specify "limits results to 10" do
11.times do |index|
event_store.publish(DummyEvent.new, stream_name: "dummy-#{index}")
end

api_client.get "/api/search_streams/dum"

expect(api_client.last_response).to be_ok
expect(api_client.parsed_body["data"].size).to eq(10)
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ def search_streams(stream_name)
streams
.select { |name,| name.downcase.include?(stream_name.downcase) }
.to_a
.take(10)
.reverse
.map { |name,| Stream.new(name) }
end
Expand Down

0 comments on commit b41cc6b

Please sign in to comment.