cfa 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/cfa/augeas_parser.rb +65 -11
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f23e76c32a5d06c47db240cebb1a663a2a8ac46f
4
- data.tar.gz: 957023925e970a78e46e37871c3eec06d57eea0b
3
+ metadata.gz: 462483ae4713b64048bae7313e741675c32e14ce
4
+ data.tar.gz: 3ae2dffb60221cf96ba4e7d09d22c558dbbf7ab9
5
5
  SHA512:
6
- metadata.gz: 5d9d85bb04225dcf40c5899aa936429b6a30f9dd09104b74d69a7895a4c5fe2dae976b0affefada166424647999e8c45a4cfbc41766ae8efd901e03554a6d8b3
7
- data.tar.gz: aa047a24855850d80377e0aa665aa47d307459db71111c1d1ae54a7cc598177300f065e0785725bc0ec814c69e27f593cb111cefcf75d7880aec5e8053c9c2a8
6
+ metadata.gz: 566cd667bcca679f1ce4e95105f8293f042822f9da63ad10063b2e717a6dca90dc57e20796f43332b9eadb27a4c3566ae4f412cf1203fd0bb0e3c0e92c6759d7
7
+ data.tar.gz: 26207c7926c12a48f003ca46366e5a3f58931852b872b01bf2620fcbb47c6546ce4534cc33f3db0af3edf7725772eac15f5f4517fbee60af34fe0b554224b2b3
@@ -125,13 +125,12 @@ module CFA
125
125
 
126
126
  # @note for internal usage only
127
127
  # @private
128
- def load_from_augeas(aug, prefix)
129
- matches = aug.match("#{prefix}/*")
130
-
131
- @data = matches.map do |aug_key|
128
+ def load_from_augeas(aug, prefix, keys_cache)
129
+ @data = keys_cache.keys_for_prefix(prefix).map do |key|
130
+ aug_key = prefix + "/" + key
132
131
  {
133
132
  key: load_key(prefix, aug_key),
134
- value: load_value(aug, aug_key)
133
+ value: load_value(aug, aug_key, keys_cache)
135
134
  }
136
135
  end
137
136
  end
@@ -178,16 +177,20 @@ module CFA
178
177
  end
179
178
 
180
179
  def load_key(prefix, aug_key)
181
- key = aug_key.sub(/^#{Regexp.escape(prefix)}\//, "")
182
- key.sub(/\[\d+\]$/, "[]")
180
+ # clean from key prefix and for collection remove number inside []
181
+ # +1 for size due to ending '/' not part of prefix
182
+ key = aug_key[(prefix.size + 1)..-1]
183
+ key.end_with?("]") ? key.sub(/\[\d+\]$/, "[]") : key
183
184
  end
184
185
 
185
- def load_value(aug, aug_key)
186
- nested = !aug.match("#{aug_key}/*").empty?
186
+ def load_value(aug, aug_key, keys_cache)
187
+ subkeys = keys_cache.keys_for_prefix(aug_key)
188
+
189
+ nested = !subkeys.empty?
187
190
  value = aug.get(aug_key)
188
191
  if nested
189
192
  subtree = AugeasTree.new
190
- subtree.load_from_augeas(aug, aug_key)
193
+ subtree.load_from_augeas(aug, aug_key, keys_cache)
191
194
  value ? AugeasTreeValue.new(subtree, value) : subtree
192
195
  else
193
196
  value
@@ -220,8 +223,10 @@ module CFA
220
223
  aug.set("/input", raw_string)
221
224
  report_error(aug) unless aug.text_store(@lens, "/input", "/store")
222
225
 
226
+ keys_cache = AugeasKeysCache.new(aug)
227
+
223
228
  tree = AugeasTree.new
224
- tree.load_from_augeas(aug, "/store")
229
+ tree.load_from_augeas(aug, "/store", keys_cache)
225
230
 
226
231
  return tree
227
232
  end
@@ -263,3 +268,52 @@ module CFA
263
268
  end
264
269
  end
265
270
  end
271
+
272
+ # Cache that holds all avaiable keys in augeas tree. It is used to
273
+ # prevent too many aug.match calls which are expensive.
274
+ class AugeasKeysCache
275
+ STORE_PREFIX = "/store".freeze
276
+ STORE_LEN = STORE_PREFIX.size
277
+ STORE_LEN_1 = STORE_LEN + 1
278
+
279
+ # initialize cache from passed augeas object
280
+ def initialize(aug)
281
+ fill_cache(aug)
282
+ end
283
+
284
+ # returns list of keys available on given prefix
285
+ def keys_for_prefix(prefix)
286
+ cut = prefix.length > STORE_LEN ? STORE_LEN_1 : STORE_LEN
287
+ path = prefix[cut..-1]
288
+ path = path.split("/")
289
+ matches = path.reduce(@cache) { |a, e| a[e] }
290
+
291
+ matches.keys
292
+ end
293
+
294
+ private
295
+
296
+ def fill_cache(aug)
297
+ @cache = {}
298
+ search_path = "#{STORE_PREFIX}/*"
299
+ loop do
300
+ matches = aug.match(search_path)
301
+ break if matches.empty?
302
+ assign_matches(matches, @cache)
303
+
304
+ search_path += "/*"
305
+ end
306
+ end
307
+
308
+ def assign_matches(matches, cache)
309
+ matches.each do |match|
310
+ path = match[STORE_LEN_1..-1].split("/")
311
+ leap = path.pop
312
+ target = path.reduce(cache) do |acc, p|
313
+ acc[p]
314
+ end
315
+
316
+ target[leap] = {}
317
+ end
318
+ end
319
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cfa
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Josef Reidinger
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-09-27 00:00:00.000000000 Z
11
+ date: 2016-10-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ruby-augeas