Skip to content

Commit

Permalink
Moved AugeasKeysCache to the CFA namespace.
Browse files Browse the repository at this point in the history
  • Loading branch information
mvidner committed Nov 3, 2016
1 parent 874bced commit 7317132
Showing 1 changed file with 30 additions and 30 deletions.
60 changes: 30 additions & 30 deletions lib/cfa/augeas_parser.rb
Expand Up @@ -320,44 +320,44 @@ def report_error(aug)
raise "Augeas parsing/serializing error: #{msg} at #{location}"
end
end
end

# Cache that holds all avaiable keys in augeas tree. It is used to
# prevent too many aug.match calls which are expensive.
class AugeasKeysCache
STORE_PREFIX = "/store".freeze
# Cache that holds all avaiable keys in augeas tree. It is used to
# prevent too many aug.match calls which are expensive.
class AugeasKeysCache
STORE_PREFIX = "/store".freeze

# initialize cache from passed augeas object
def initialize(aug)
fill_cache(aug)
end
# initialize cache from passed augeas object
def initialize(aug)
fill_cache(aug)
end

# returns list of keys available on given prefix
def keys_for_prefix(prefix)
@cache[prefix] || []
end
# returns list of keys available on given prefix
def keys_for_prefix(prefix)
@cache[prefix] || []
end

private
private

def fill_cache(aug)
@cache = {}
search_path = "#{STORE_PREFIX}/*"
loop do
matches = aug.match(search_path)
break if matches.empty?
assign_matches(matches, @cache)
def fill_cache(aug)
@cache = {}
search_path = "#{STORE_PREFIX}/*"
loop do
matches = aug.match(search_path)
break if matches.empty?
assign_matches(matches, @cache)

search_path += "/*"
search_path += "/*"
end
end
end

def assign_matches(matches, cache)
matches.each do |match|
split_index = match.rindex("/")
prefix = match[0..(split_index - 1)]
key = match[(split_index + 1)..-1]
cache[prefix] ||= []
cache[prefix] << key
def assign_matches(matches, cache)
matches.each do |match|
split_index = match.rindex("/")
prefix = match[0..(split_index - 1)]
key = match[(split_index + 1)..-1]
cache[prefix] ||= []
cache[prefix] << key
end
end
end
end

0 comments on commit 7317132

Please sign in to comment.