Commit 1cc26e0f authored by Grzegorz Bizon's avatar Grzegorz Bizon

Improve performance of `StringPath`

parent bd4899be
...@@ -5,15 +5,15 @@ module Gitlab ...@@ -5,15 +5,15 @@ module Gitlab
# This is IO-operations safe class, that does similar job to # This is IO-operations safe class, that does similar job to
# Ruby's Pathname but without the risk of accessing filesystem. # Ruby's Pathname but without the risk of accessing filesystem.
# #
# TODO: better support for './' and '../' # TODO: better support for '../' and './'
# #
class StringPath class StringPath
attr_reader :path, :universe attr_reader :path, :universe
def initialize(path, universe) def initialize(path, universe)
@path = prepare(path) @path = prepare(path)
@universe = universe.map { |entry| prepare(entry) } @universe = Set.new(universe.map { |entry| prepare(entry) })
@universe.unshift('./') unless @universe.include?('./') @universe.add('./')
end end
def to_s def to_s
...@@ -64,7 +64,10 @@ module Gitlab ...@@ -64,7 +64,10 @@ module Gitlab
end end
def children def children
descendants.select { |descendant| descendant.parent == self } return [] unless directory?
return @children if @children
children = @universe.select { |entry| entry =~ %r{^#{@path}[^/]+/?$} }
@children = children.map { |path| new(path) }
end end
def directories def directories
...@@ -85,6 +88,10 @@ module Gitlab ...@@ -85,6 +88,10 @@ module Gitlab
@path == other.path && @universe == other.universe @path == other.path && @universe == other.universe
end end
def inspect
"#{self.class.name}: #{@path}"
end
private private
def new(path) def new(path)
......
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