Commit ae11d316 authored by Matija Čupić's avatar Matija Čupić Committed by Michael Kozono

Create finder for searching branch names via redis

parent ae271da6
# frozen_string_literal: true
module Repositories
class BranchNamesFinder
attr_reader :repository, :params
def initialize(repository, params = {})
@repository = repository
@params = params
end
def execute
return unless search
repository.search_branch_names(search)
end
private
def search
@params[:search].presence
end
end
end
......@@ -288,6 +288,10 @@ class Repository
false
end
def search_branch_names(pattern)
redis_set_cache.search('branch_names', pattern) { branch_names }
end
def languages
return [] if empty?
......
---
title: Create finder for searching branch names via redis.
merge_request: 58439
author:
type: performance
......@@ -49,5 +49,20 @@ module Gitlab
write(key, yield)
end
# Searches the cache set using SSCAN with the MATCH option. The MATCH
# parameter is the pattern argument.
# See https://redis.io/commands/scan#the-match-option for more information.
# Returns an Enumerator that enumerates all SSCAN hits.
def search(key, pattern, &block)
full_key = cache_key(key)
with do |redis|
exists = redis.exists(full_key)
write(key, yield) unless exists
redis.sscan_each(full_key, match: pattern)
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Repositories::BranchNamesFinder do
let(:project) { create(:project, :repository) }
let(:branch_names_finder) { described_class.new(project.repository, search: 'conflict-*') }
describe '#execute' do
subject(:execute) { branch_names_finder.execute }
it 'filters branch names' do
expect(execute).to contain_exactly(
'conflict-binary-file',
'conflict-resolvable',
'conflict-contains-conflict-markers',
'conflict-missing-side',
'conflict-start',
'conflict-non-utf8',
'conflict-too-large'
)
end
end
end
......@@ -124,6 +124,18 @@ RSpec.describe Gitlab::RepositorySetCache, :clean_gitlab_redis_cache do
end
end
describe '#search' do
subject do
cache.search(:foo, 'val*') do
%w[value helloworld notvalmatch]
end
end
it 'returns search pattern matches from the key' do
is_expected.to contain_exactly('value')
end
end
describe '#include?' do
it 'checks inclusion in the Redis set' do
cache.write(:foo, ['value'])
......
......@@ -170,6 +170,22 @@ RSpec.describe Repository do
end
end
describe '#search_branch_names' do
subject(:search_branch_names) { repository.search_branch_names('conflict-*') }
it 'returns matching branch names' do
expect(search_branch_names).to contain_exactly(
'conflict-binary-file',
'conflict-resolvable',
'conflict-contains-conflict-markers',
'conflict-missing-side',
'conflict-start',
'conflict-non-utf8',
'conflict-too-large'
)
end
end
describe '#list_last_commits_for_tree' do
let(:path_to_commit) do
{
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment