hotdog 0.1.17 → 0.1.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 79e4334fc2d2f63537cbaa32bc44c2e5d069b4f5
4
- data.tar.gz: bc9fdf8a14f3ab1a8ab02caa22e9841bfbff5181
3
+ metadata.gz: 8deb5895caacf3aa169eaeb03aec5beb9a71a4d0
4
+ data.tar.gz: 9f26224eff745ee809401e997040509eea818d10
5
5
  SHA512:
6
- metadata.gz: e4e0aacb47ea09c873bbc7f39ee0b003d004716b619a5983eeb7c994a6c57ad4fd3f1286e2b9d1db89819cbf967745289d4e15a7d78109cf880f128173212227
7
- data.tar.gz: e3f328b83968a36e98bedf91ced0e68e2f89a2edec886c1a6f2377a7e44d42d7158fdc76201ef53a27ddcd8a8176ae8be7a4db0e8771c7b47c75183e7092ec9b
6
+ metadata.gz: 5b77afe9612c45e00862544ae90c2d01dfa8dd5f4eeff755ec2c431ae17d98ac702df9e655b26819f771978a7b4e3a75ec8403230c863f932251aa9225697c19
7
+ data.tar.gz: 14e5e13f1d5f0e4532b5ca7b7cd981cb2eda765b24af1575bf5448acaf07426402dcebc3f774c611e746f99d8a70950cddb44ab9643ec85bcf65307a47a0e711
@@ -18,7 +18,7 @@ module Hotdog
18
18
  application: self,
19
19
  confdir: find_confdir(File.expand_path(".")),
20
20
  debug: false,
21
- expiry: 300,
21
+ expiry: 1800,
22
22
  fixed_string: false,
23
23
  force: false,
24
24
  format: "plain",
@@ -202,7 +202,7 @@ module Hotdog
202
202
  end
203
203
 
204
204
  class ExpressionNode
205
- def evaluate(environment)
205
+ def evaluate(environment, options={})
206
206
  raise(NotImplementedError)
207
207
  end
208
208
  end
@@ -216,7 +216,7 @@ module Hotdog
216
216
  @left = left
217
217
  @right = right
218
218
  end
219
- def evaluate(environment)
219
+ def evaluate(environment, options={})
220
220
  case @op
221
221
  when "&&", "&", /\Aand\z/i
222
222
  left_values = @left.evaluate(environment)
@@ -243,7 +243,7 @@ module Hotdog
243
243
  @op = op
244
244
  @expression = expression
245
245
  end
246
- def evaluate(environment)
246
+ def evaluate(environment, options={})
247
247
  case @op
248
248
  when "!", "~", /\Anot\z/i
249
249
  values = @expression.evaluate(environment)
@@ -275,7 +275,7 @@ module Hotdog
275
275
  def attribute?
276
276
  !(attribute.nil? or attribute.to_s.empty?)
277
277
  end
278
- def evaluate(environment)
278
+ def evaluate(environment, options={})
279
279
  if identifier?
280
280
  if attribute?
281
281
  case identifier
@@ -307,24 +307,51 @@ module Hotdog
307
307
  WHERE LOWER(tags.value) = LOWER(?);
308
308
  EOS
309
309
  else
310
- values = []
310
+ return []
311
311
  end
312
312
  end
313
- if not environment.fixed_string? and values.empty?
313
+ if values.empty?
314
+ fallback(environment, options)
315
+ else
316
+ values
317
+ end
318
+ end
319
+
320
+ def fallback(environment, options={})
321
+ if environment.fixed_string?
322
+ []
323
+ else
314
324
  # fallback to glob expression
315
325
  identifier_glob = identifier.gsub(/[-.\/_]/, "?") if identifier?
316
326
  attribute_glob = attribute.gsub(/[-.\/_]/, "?") if attribute?
317
327
  if (identifier? and identifier != identifier_glob) or (attribute? and attribute != attribute_glob)
318
328
  environment.logger.info("fallback to glob expression: %s:%s" % [identifier_glob, attribute_glob])
319
- values = TagGlobExpressionNode.new(identifier_glob, attribute_glob).evaluate(environment)
329
+ values = TagGlobExpressionNode.new(identifier_glob, attribute_glob).evaluate(environment, options)
330
+ if values.empty?
331
+ reload(environment, options)
332
+ else
333
+ values
334
+ end
335
+ else
336
+ []
320
337
  end
321
338
  end
322
- values
339
+ end
340
+
341
+ def reload(environment, options={})
342
+ ttl = options.fetch(:ttl, 1)
343
+ if 0 < ttl
344
+ environment.logger.info("force reloading all hosts and tags.")
345
+ environment.reload(force: true)
346
+ self.class.new(identifier, attribute).evaluate(environment, options.merge(ttl: ttl-1))
347
+ else
348
+ []
349
+ end
323
350
  end
324
351
  end
325
352
 
326
353
  class TagGlobExpressionNode < TagExpressionNode
327
- def evaluate(environment)
354
+ def evaluate(environment, options={})
328
355
  if identifier?
329
356
  if attribute?
330
357
  case identifier
@@ -356,19 +383,14 @@ module Hotdog
356
383
  WHERE LOWER(tags.value) GLOB LOWER(?);
357
384
  EOS
358
385
  else
359
- values = []
386
+ return []
360
387
  end
361
388
  end
362
- if not environment.fixed_string? and values.empty?
363
- # fallback to glob expression
364
- identifier_glob = identifier.gsub(/[-.\/_]/, "?") if identifier?
365
- attribute_glob = attribute.gsub(/[-.\/:_]/, "?") if attribute?
366
- if (identifier? and identifier != identifier_glob) or (attribute? and attribute != attribute_glob)
367
- environment.logger.info("fallback to glob expression: %s:%s" % [identifier_glob, attribute_glob])
368
- values = TagGlobExpressionNode.new(identifier_glob, attribute_glob).evaluate(environment)
369
- end
389
+ if values.empty?
390
+ fallback(environment, options)
391
+ else
392
+ values
370
393
  end
371
- values
372
394
  end
373
395
  end
374
396
 
@@ -378,7 +400,7 @@ module Hotdog
378
400
  attribute = attribute.sub(%r{\A/(.*)/\z}) { $1 } if attribute
379
401
  super(identifier, attribute)
380
402
  end
381
- def evaluate(environment)
403
+ def evaluate(environment, options={})
382
404
  if identifier?
383
405
  if attribute?
384
406
  case identifier
@@ -388,14 +410,14 @@ module Hotdog
388
410
  WHERE LOWER(hosts.name) REGEXP LOWER(?);
389
411
  EOS
390
412
  else
391
- environment.execute(<<-EOS, identifier, attribute).map { |row| row.first }
413
+ values = environment.execute(<<-EOS, identifier, attribute).map { |row| row.first }
392
414
  SELECT DISTINCT hosts_tags.host_id FROM hosts_tags
393
415
  INNER JOIN tags ON hosts_tags.tag_id = tags.id
394
416
  WHERE LOWER(tags.name) REGEXP LOWER(?) AND LOWER(tags.value) REGEXP LOWER(?);
395
417
  EOS
396
418
  end
397
419
  else
398
- environment.execute(<<-EOS, identifier, identifier, identifier).map { |row| row.first }
420
+ values = environment.execute(<<-EOS, identifier, identifier, identifier).map { |row| row.first }
399
421
  SELECT DISTINCT hosts_tags.host_id FROM hosts_tags
400
422
  INNER JOIN hosts ON hosts_tags.host_id = hosts.id
401
423
  INNER JOIN tags ON hosts_tags.tag_id = tags.id
@@ -410,10 +432,14 @@ module Hotdog
410
432
  WHERE LOWER(tags.value) REGEXP LOWER(?);
411
433
  EOS
412
434
  else
413
- values = []
435
+ return []
414
436
  end
415
437
  end
416
- values
438
+ if values.empty?
439
+ reload(environment)
440
+ else
441
+ values
442
+ end
417
443
  end
418
444
  end
419
445
  end
@@ -9,12 +9,15 @@ module Hotdog
9
9
  module Commands
10
10
  class BaseCommand
11
11
  PERSISTENT_DB = "persistent.db"
12
+ MASK_DATABASE = 0xffff0000
13
+ MASK_QUERY = 0x0000ffff
12
14
 
13
15
  def initialize(application)
14
16
  @application = application
15
17
  @logger = application.options[:logger]
16
18
  @options = application.options
17
19
  @dog = Dogapi::Client.new(options[:api_key], options[:application_key])
20
+ @prepared_statements = {}
18
21
  end
19
22
  attr_reader :application
20
23
  attr_reader :logger
@@ -31,14 +34,27 @@ module Hotdog
31
34
  q += " -- VALUES (#{args.map { |arg| Array === arg ? "(#{arg.join(", ")})" : arg.inspect }.join(", ")})"
32
35
  end
33
36
  logger.debug(q)
34
- @db.execute(query, args)
37
+ prepare(@db, query).execute(args)
35
38
  end
36
39
 
37
40
  def fixed_string?()
38
41
  @options[:fixed_string]
39
42
  end
40
43
 
44
+ def reload(options={})
45
+ if @db
46
+ close_db(@db)
47
+ @db = nil
48
+ end
49
+ update_db(options)
50
+ end
51
+
41
52
  private
53
+ def prepare(db, query)
54
+ k = (db.hash & MASK_DATABASE) | (query.hash & MASK_QUERY)
55
+ @prepared_statements[k] ||= db.prepare(query)
56
+ end
57
+
42
58
  def format(result, options={})
43
59
  @options[:formatter].format(result, @options.merge(options))
44
60
  end
@@ -120,12 +136,22 @@ module Hotdog
120
136
  [result, fields]
121
137
  end
122
138
 
139
+ def close_db(db, options={})
140
+ @prepared_statements = @prepared_statements.reject { |k, statement|
141
+ (db.hash & MASK_DATABASE == k & MASK_DATABASE).tap do |delete_p|
142
+ statement.close() if delete_p
143
+ end
144
+ }
145
+ db.close()
146
+ end
147
+
123
148
  def update_db(options={})
149
+ options = @options.merge(options)
124
150
  if @db.nil?
125
- FileUtils.mkdir_p(@options[:confdir])
126
- persistent = File.join(@options[:confdir], PERSISTENT_DB)
151
+ FileUtils.mkdir_p(options[:confdir])
152
+ persistent = File.join(options[:confdir], PERSISTENT_DB)
127
153
 
128
- if not @options[:force] and File.exist?(persistent) and Time.new < File.mtime(persistent) + @options[:expiry]
154
+ if not options[:force] and File.exist?(persistent) and Time.new < File.mtime(persistent) + options[:expiry]
129
155
  begin
130
156
  persistent_db = SQLite3::Database.new(persistent)
131
157
  persistent_db.execute("SELECT id, name FROM hosts LIMIT 1")
@@ -191,7 +217,7 @@ module Hotdog
191
217
  FileUtils.rm_f(persistent)
192
218
  persistent_db = SQLite3::Database.new(persistent)
193
219
  copy_db(memory_db, persistent_db)
194
- persistent_db.close
220
+ close_db(persistent_db)
195
221
  @db = memory_db
196
222
  else
197
223
  @db
@@ -223,17 +249,17 @@ module Hotdog
223
249
 
224
250
  def select_from_hosts(db)
225
251
  logger.debug("select_from_hosts()")
226
- db.execute("SELECT id, name FROM hosts")
252
+ prepare(db, "SELECT id, name FROM hosts").execute()
227
253
  end
228
254
 
229
255
  def select_from_tags(db)
230
256
  logger.debug("select_from_tags()")
231
- db.execute("SELECT id, name, value FROM tags")
257
+ prepare(db, "SELECT id, name, value FROM tags").execute()
232
258
  end
233
259
 
234
260
  def select_from_hosts_tags(db)
235
261
  logger.debug("select_from_hosts_tags()")
236
- db.execute("SELECT host_id, tag_id FROM hosts_tags")
262
+ prepare(db, "SELECT host_id, tag_id FROM hosts_tags").execute()
237
263
  end
238
264
 
239
265
  def create_table_hosts(db)
@@ -284,32 +310,32 @@ module Hotdog
284
310
 
285
311
  def insert_into_tags(db, tag_id, tag_name, tag_value)
286
312
  logger.debug("insert_into_tags(%s, %s, %s)" % [tag_id.inspect, tag_name.inspect, tag_value.inspect])
287
- db.execute("INSERT INTO tags (id, name, value) VALUES (?, ?, ?)", tag_id, tag_name, tag_value)
313
+ prepare(db, "INSERT INTO tags (id, name, value) VALUES (?, ?, ?)").execute(tag_id, tag_name, tag_value)
288
314
  end
289
315
 
290
316
  def insert_or_ignore_into_tags(db, tag_name, tag_value)
291
317
  logger.debug("insert_or_ignore_into_tags(%s, %s)" % [tag_name.inspect, tag_value.inspect])
292
- db.execute("INSERT OR IGNORE INTO tags (name, value) VALUES (?, ?)", tag_name, tag_value)
318
+ prepare(db, "INSERT OR IGNORE INTO tags (name, value) VALUES (?, ?)").execute(tag_name, tag_value)
293
319
  end
294
320
 
295
321
  def insert_into_hosts(db, host_id, host_name)
296
322
  logger.debug("insert_into_hosts(%s, %s)" % [host_id.inspect, host_name.inspect])
297
- db.execute("INSERT INTO hosts (id, name) VALUES (?, ?)", host_id, host_name)
323
+ prepare(db, "INSERT INTO hosts (id, name) VALUES (?, ?)").execute(host_id, host_name)
298
324
  end
299
325
 
300
326
  def insert_or_ignore_into_hosts(db, host_name)
301
327
  logger.debug("insert_or_ignore_into_hosts(%s)" % [host_name.inspect])
302
- db.execute("INSERT OR IGNORE INTO hosts (name) VALUES (?)", host_name)
328
+ prepare(db, "INSERT OR IGNORE INTO hosts (name) VALUES (?)").execute(host_name)
303
329
  end
304
330
 
305
331
  def insert_into_hosts_tags(db, host_id, tag_id)
306
332
  logger.debug("insert_into_hosts_tags(%s, %s)" % [host_id.inspect, tag_id.inspect])
307
- db.execute("INSERT INTO hosts_tags (host_id, tag_id) VALUES (?, ?)", host_id, tag_id)
333
+ prepare(db, "INSERT INTO hosts_tags (host_id, tag_id) VALUES (?, ?)").execute(host_id, tag_id)
308
334
  end
309
335
 
310
336
  def insert_or_replace_into_hosts_tags(db, host_name, tag_name, tag_value)
311
337
  logger.debug("insert_or_replace_into_hosts_tags(%s, %s, %s)" % [host_name.inspect, tag_name.inspect, tag_value.inspect])
312
- db.execute(<<-EOS, host_name, tag_name, tag_value)
338
+ prepare(db, <<-EOS).execute(host_name, tag_name, tag_value)
313
339
  INSERT OR REPLACE INTO hosts_tags (host_id, tag_id)
314
340
  SELECT host.id, tag.id FROM
315
341
  ( SELECT id FROM hosts WHERE LOWER(name) = LOWER(?) ) AS host,
@@ -319,12 +345,12 @@ module Hotdog
319
345
 
320
346
  def select_name_from_hosts_by_id(db, host_id)
321
347
  logger.debug("select_name_from_hosts_by_id(%s)" % [host_id.inspect])
322
- db.execute("SELECT name FROM hosts WHERE id = ? LIMIT 1", host_id).map { |row| row.first }.first
348
+ prepare(db, "SELECT name FROM hosts WHERE id = ? LIMIT 1").execute(host_id).map { |row| row.first }.first
323
349
  end
324
350
 
325
351
  def select_tag_values_from_hosts_tags_by_host_id_and_tag_name_glob(db, host_id, tag_name)
326
352
  logger.debug("select_tag_values_from_hosts_tags_by_host_id_and_tag_name_glob(%s, %s)" % [host_id.inspect, tag_name.inspect])
327
- db.execute(<<-EOS, host_id, tag_name).map { |row| row.first }.join(",")
353
+ prepare(db, <<-EOS).execute(host_id, tag_name).map { |row| row.first }.join(",")
328
354
  SELECT tags.value FROM hosts_tags
329
355
  INNER JOIN tags ON hosts_tags.tag_id = tags.id
330
356
  WHERE hosts_tags.host_id = ? AND LOWER(tags.name) GLOB LOWER(?);
@@ -333,7 +359,7 @@ module Hotdog
333
359
 
334
360
  def select_tag_values_from_hosts_tags_by_host_id_and_tag_name(db, host_id, tag_name)
335
361
  logger.debug("select_tag_values_from_hosts_tags_by_host_id_and_tag_name(%s, %s)" % [host_id.inspect, tag_name.inspect])
336
- db.execute(<<-EOS, host_id, tag_name).map { |row| row.first }.join(",")
362
+ prepare(db, <<-EOS).execute(host_id, tag_name).map { |row| row.first }.join(",")
337
363
  SELECT tags.value FROM hosts_tags
338
364
  INNER JOIN tags ON hosts_tags.tag_id = tags.id
339
365
  WHERE hosts_tags.host_id = ? AND LOWER(tags.name) = LOWER(?);
@@ -342,7 +368,7 @@ module Hotdog
342
368
 
343
369
  def select_tag_names_from_hosts_tags_by_host_id(db, host_id)
344
370
  logger.debug("select_tag_names_from_hosts_tags_by_host_id(%s)" % [host_id.inspect])
345
- db.execute(<<-EOS, host_id).map { |row| row.first }
371
+ prepare(db, <<-EOS).execute(host_id).map { |row| row.first }
346
372
  SELECT DISTINCT tags.name FROM hosts_tags
347
373
  INNER JOIN tags ON hosts_tags.tag_id = tags.id
348
374
  WHERE hosts_tags.host_id = ?;
@@ -1,3 +1,3 @@
1
1
  module Hotdog
2
- VERSION = "0.1.17"
2
+ VERSION = "0.1.18"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hotdog
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.17
4
+ version: 0.1.18
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yamashita Yuu
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-08-25 00:00:00.000000000 Z
11
+ date: 2015-08-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler