hotdog 0.4.0 → 0.4.1
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 +4 -4
- data/lib/hotdog/commands/hosts.rb +1 -1
- data/lib/hotdog/commands/search.rb +55 -45
- data/lib/hotdog/commands/tags.rb +2 -2
- data/lib/hotdog/version.rb +1 -1
- data/spec/parser/tag_glob_expression_spec.rb +6 -6
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ffbad3cddd1c8a203564a0a72f76f4afcdeedb31
|
4
|
+
data.tar.gz: 0b2a720a116b8a7744cfe91e83145106f6841bb6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e460694b774e72633795116768078474b89e3ff5196b1fd6081102264bd29f496b0b1e7addf9742c8912eeb6ec9fb0e1d8592220db10d4fa25e27a94a4fb4a91
|
7
|
+
data.tar.gz: 898f4efe04b91dff88a420d257c839391162354e5d985599940199c6afa91b37a3efba0f3c99fc48208241e5fa4ec72edbd48cacfef078932b18066a66145739
|
@@ -11,7 +11,7 @@ module Hotdog
|
|
11
11
|
else
|
12
12
|
if args.any? { |host_name| glob?(host_name) }
|
13
13
|
result = args.flat_map { |host_name|
|
14
|
-
execute("SELECT id FROM hosts WHERE name GLOB
|
14
|
+
execute("SELECT id FROM hosts WHERE LOWER(name) GLOB LOWER(?);", [host_name]).to_a.reduce(:+) || []
|
15
15
|
}
|
16
16
|
else
|
17
17
|
result = args.each_slice(SQLITE_LIMIT_COMPOUND_SELECT).flat_map { |args|
|
@@ -549,6 +549,7 @@ module Hotdog
|
|
549
549
|
@identifier = identifier
|
550
550
|
@attribute = attribute
|
551
551
|
@separator = separator
|
552
|
+
@fallback = nil
|
552
553
|
end
|
553
554
|
attr_reader :identifier
|
554
555
|
attr_reader :attribute
|
@@ -610,12 +611,26 @@ module Hotdog
|
|
610
611
|
if q = plan(options)
|
611
612
|
values = environment.execute(*q).map { |row| row.first }
|
612
613
|
if values.empty?
|
613
|
-
|
614
|
+
if options[:did_fallback]
|
615
|
+
[]
|
616
|
+
else
|
617
|
+
if not environment.fixed_string? and @fallback
|
618
|
+
# avoid optimizing @fallback to prevent infinite recursion
|
619
|
+
values = @fallback.evaluate(environment, options.merge(did_fallback: true))
|
620
|
+
if values.empty?
|
621
|
+
reload(environment, options)
|
622
|
+
else
|
623
|
+
values
|
624
|
+
end
|
625
|
+
else
|
626
|
+
reload(environment, options)
|
627
|
+
end
|
628
|
+
end
|
614
629
|
else
|
615
630
|
values
|
616
631
|
end
|
617
632
|
else
|
618
|
-
|
633
|
+
[]
|
619
634
|
end
|
620
635
|
end
|
621
636
|
|
@@ -623,25 +638,26 @@ module Hotdog
|
|
623
638
|
self.class == other.class and @identifier == other.identifier and @attribute == other.attribute
|
624
639
|
end
|
625
640
|
|
626
|
-
def
|
627
|
-
|
628
|
-
|
641
|
+
def optimize(options={})
|
642
|
+
# fallback to glob expression
|
643
|
+
if identifier?
|
644
|
+
prefix = (identifier.start_with?("*")) ? "" : "*"
|
645
|
+
suffix = (identifier.end_with?("*")) ? "" : "*"
|
646
|
+
identifier_glob = prefix + identifier.gsub(/[-.\/_]/, "?") + suffix
|
629
647
|
else
|
630
|
-
|
631
|
-
|
632
|
-
|
633
|
-
|
634
|
-
|
635
|
-
|
636
|
-
|
637
|
-
|
638
|
-
|
639
|
-
|
640
|
-
|
641
|
-
else
|
642
|
-
[]
|
643
|
-
end
|
648
|
+
identifier_glob = nil
|
649
|
+
end
|
650
|
+
if attribute?
|
651
|
+
prefix = (attribute.start_with?("*")) ? "" : "*"
|
652
|
+
suffix = (attribute.end_with?("*")) ? "" : "*"
|
653
|
+
attribute_glob = prefix + attribute.gsub(/[-.\/_]/, "?") + suffix
|
654
|
+
else
|
655
|
+
attribute_glob = nil
|
656
|
+
end
|
657
|
+
if (identifier? and identifier != identifier_glob) or (attribute? and attribute != attribute_glob)
|
658
|
+
@fallback = TagGlobExpressionNode.new(identifier_glob, attribute_glob, separator)
|
644
659
|
end
|
660
|
+
self
|
645
661
|
end
|
646
662
|
|
647
663
|
def reload(environment, options={})
|
@@ -649,7 +665,7 @@ module Hotdog
|
|
649
665
|
if 0 < ttl
|
650
666
|
environment.logger.info("force reloading all hosts and tags.")
|
651
667
|
environment.reload(force: true)
|
652
|
-
self.class.new(identifier, attribute, separator).evaluate(environment, options.merge(ttl: ttl-1))
|
668
|
+
self.class.new(identifier, attribute, separator).optimize(options).evaluate(environment, options.merge(ttl: ttl-1))
|
653
669
|
else
|
654
670
|
[]
|
655
671
|
end
|
@@ -657,9 +673,10 @@ module Hotdog
|
|
657
673
|
|
658
674
|
def dump(options={})
|
659
675
|
data = {}
|
660
|
-
data[:identifier] = @identifier if @identifier
|
661
|
-
data[:separator] = @separator if @separator
|
662
|
-
data[:attribute] = @attribute if @attribute
|
676
|
+
data[:identifier] = @identifier.to_s if @identifier
|
677
|
+
data[:separator] = @separator.to_s if @separator
|
678
|
+
data[:attribute] = @attribute.to_s if @attribute
|
679
|
+
data[:fallback ] = @fallback.dump(options) if @fallback
|
663
680
|
data
|
664
681
|
end
|
665
682
|
end
|
@@ -671,25 +688,25 @@ module Hotdog
|
|
671
688
|
case identifier
|
672
689
|
when /\Ahost\z/i
|
673
690
|
q = "SELECT hosts.id AS host_id FROM hosts " \
|
674
|
-
"WHERE hosts.name GLOB
|
691
|
+
"WHERE LOWER(hosts.name) GLOB LOWER(?);"
|
675
692
|
[q, [attribute]]
|
676
693
|
else
|
677
694
|
q = "SELECT DISTINCT hosts_tags.host_id FROM hosts_tags " \
|
678
695
|
"INNER JOIN tags ON hosts_tags.tag_id = tags.id " \
|
679
|
-
"WHERE tags.name GLOB ? AND tags.value GLOB
|
696
|
+
"WHERE LOWER(tags.name) GLOB LOWER(?) AND LOWER(tags.value) GLOB LOWER(?);"
|
680
697
|
[q, [identifier, attribute]]
|
681
698
|
end
|
682
699
|
else
|
683
700
|
if separator?
|
684
701
|
q = "SELECT DISTINCT hosts_tags.host_id FROM hosts_tags " \
|
685
702
|
"INNER JOIN tags ON hosts_tags.tag_id = tags.id " \
|
686
|
-
"WHERE tags.name GLOB
|
703
|
+
"WHERE LOWER(tags.name) GLOB LOWER(?);"
|
687
704
|
[q, [identifier]]
|
688
705
|
else
|
689
706
|
q = "SELECT DISTINCT hosts_tags.host_id FROM hosts_tags " \
|
690
707
|
"INNER JOIN hosts ON hosts_tags.host_id = hosts.id " \
|
691
708
|
"INNER JOIN tags ON hosts_tags.tag_id = tags.id " \
|
692
|
-
"WHERE hosts.name GLOB ? OR tags.name GLOB ? OR tags.value GLOB
|
709
|
+
"WHERE LOWER(hosts.name) GLOB LOWER(?) OR LOWER(tags.name) GLOB LOWER(?) OR LOWER(tags.value) GLOB LOWER(?);"
|
693
710
|
[q, [identifier, identifier, identifier]]
|
694
711
|
end
|
695
712
|
end
|
@@ -697,7 +714,7 @@ module Hotdog
|
|
697
714
|
if attribute?
|
698
715
|
q = "SELECT DISTINCT hosts_tags.host_id FROM hosts_tags " \
|
699
716
|
"INNER JOIN tags ON hosts_tags.tag_id = tags.id " \
|
700
|
-
"WHERE tags.value GLOB
|
717
|
+
"WHERE LOWER(tags.value) GLOB LOWER(?);"
|
701
718
|
[q, [attribute]]
|
702
719
|
else
|
703
720
|
nil
|
@@ -707,9 +724,10 @@ module Hotdog
|
|
707
724
|
|
708
725
|
def dump(options={})
|
709
726
|
data = {}
|
710
|
-
data[:identifier_glob] = @identifier if @identifier
|
711
|
-
data[:separator] = @separator if @separator
|
712
|
-
data[:attribute_glob] = @attribute if @attribute
|
727
|
+
data[:identifier_glob] = @identifier.to_s if @identifier
|
728
|
+
data[:separator] = @separator.to_s if @separator
|
729
|
+
data[:attribute_glob] = @attribute.to_s if @attribute
|
730
|
+
data[:fallback] = @fallback.dump(options) if @fallback
|
713
731
|
data
|
714
732
|
end
|
715
733
|
end
|
@@ -761,24 +779,16 @@ module Hotdog
|
|
761
779
|
end
|
762
780
|
end
|
763
781
|
|
764
|
-
def
|
765
|
-
|
766
|
-
values = environment.execute(*q).map { |row| row.first }
|
767
|
-
if values.empty?
|
768
|
-
reload(environment)
|
769
|
-
else
|
770
|
-
values
|
771
|
-
end
|
772
|
-
else
|
773
|
-
return []
|
774
|
-
end
|
782
|
+
def optimize(options={})
|
783
|
+
self # disable fallback
|
775
784
|
end
|
776
785
|
|
777
786
|
def dump(options={})
|
778
787
|
data = {}
|
779
|
-
data[:identifier_regexp] = @identifier if @identifier
|
780
|
-
data[:separator] = @separator if @separator
|
781
|
-
data[:attribute_regexp] = @attribute if @attribute
|
788
|
+
data[:identifier_regexp] = @identifier.to_s if @identifier
|
789
|
+
data[:separator] = @separator.to_s if @separator
|
790
|
+
data[:attribute_regexp] = @attribute.to_s if @attribute
|
791
|
+
data[:fallback] = @fallback.dump(options) if @fallback
|
782
792
|
data
|
783
793
|
end
|
784
794
|
end
|
data/lib/hotdog/commands/tags.rb
CHANGED
@@ -13,13 +13,13 @@ module Hotdog
|
|
13
13
|
if tags.all? { |tag_name, tag_value| tag_value.empty? }
|
14
14
|
result = tags.each_slice(SQLITE_LIMIT_COMPOUND_SELECT).flat_map { |tags|
|
15
15
|
q = "SELECT value FROM tags " \
|
16
|
-
"WHERE %s;" % tags.map { |tag_name, tag_value| glob?(tag_name) ? "name GLOB ?" : "name = ?" }.join(" OR ")
|
16
|
+
"WHERE %s;" % tags.map { |tag_name, tag_value| glob?(tag_name) ? "LOWER(name) GLOB LOWER(?)" : "name = ?" }.join(" OR ")
|
17
17
|
execute(q, tags.map { |tag_name, tag_value| tag_name }).map { |value| [value] }
|
18
18
|
}
|
19
19
|
else
|
20
20
|
result = tags.each_slice(SQLITE_LIMIT_COMPOUND_SELECT / 2).flat_map { |tags|
|
21
21
|
q = "SELECT value FROM tags " \
|
22
|
-
"WHERE %s;" % tags.map { |tag_name, tag_value| (glob?(tag_name) or glob?(tag_value)) ? "( name GLOB ? AND value GLOB ? )" : "( name = ? AND value = ? )" }.join(" OR ")
|
22
|
+
"WHERE %s;" % tags.map { |tag_name, tag_value| (glob?(tag_name) or glob?(tag_value)) ? "( LOWER(name) GLOB LOWER(?) AND LOWER(value) GLOB LOWER(?) )" : "( name = ? AND value = ? )" }.join(" OR ")
|
23
23
|
execute(q, tags).map { |value| [value] }
|
24
24
|
}
|
25
25
|
end
|
data/lib/hotdog/version.rb
CHANGED
@@ -13,7 +13,7 @@ describe "tag glob expression" do
|
|
13
13
|
expr = Hotdog::Commands::Search::TagGlobExpressionNode.new("host", "foo*", ":")
|
14
14
|
q = [
|
15
15
|
"SELECT hosts.id AS host_id FROM hosts",
|
16
|
-
"WHERE hosts.name GLOB
|
16
|
+
"WHERE LOWER(hosts.name) GLOB LOWER(?);",
|
17
17
|
]
|
18
18
|
allow(cmd).to receive(:execute).with(q.join(" "), ["foo*"]) {
|
19
19
|
[[1], [2], [3]]
|
@@ -27,7 +27,7 @@ describe "tag glob expression" do
|
|
27
27
|
q = [
|
28
28
|
"SELECT DISTINCT hosts_tags.host_id FROM hosts_tags",
|
29
29
|
"INNER JOIN tags ON hosts_tags.tag_id = tags.id",
|
30
|
-
"WHERE tags.name GLOB ? AND tags.value GLOB
|
30
|
+
"WHERE LOWER(tags.name) GLOB LOWER(?) AND LOWER(tags.value) GLOB LOWER(?);",
|
31
31
|
]
|
32
32
|
allow(cmd).to receive(:execute).with(q.join(" "), ["foo*", "bar*"]) {
|
33
33
|
[[1], [2], [3]]
|
@@ -41,7 +41,7 @@ describe "tag glob expression" do
|
|
41
41
|
q = [
|
42
42
|
"SELECT DISTINCT hosts_tags.host_id FROM hosts_tags",
|
43
43
|
"INNER JOIN tags ON hosts_tags.tag_id = tags.id",
|
44
|
-
"WHERE tags.name GLOB
|
44
|
+
"WHERE LOWER(tags.name) GLOB LOWER(?);",
|
45
45
|
]
|
46
46
|
allow(cmd).to receive(:execute).with(q.join(" "), ["foo*"]) {
|
47
47
|
[[1], [2], [3]]
|
@@ -56,7 +56,7 @@ describe "tag glob expression" do
|
|
56
56
|
"SELECT DISTINCT hosts_tags.host_id FROM hosts_tags",
|
57
57
|
"INNER JOIN hosts ON hosts_tags.host_id = hosts.id",
|
58
58
|
"INNER JOIN tags ON hosts_tags.tag_id = tags.id",
|
59
|
-
"WHERE hosts.name GLOB ? OR tags.name GLOB ? OR tags.value GLOB
|
59
|
+
"WHERE LOWER(hosts.name) GLOB LOWER(?) OR LOWER(tags.name) GLOB LOWER(?) OR LOWER(tags.value) GLOB LOWER(?);",
|
60
60
|
]
|
61
61
|
allow(cmd).to receive(:execute).with(q.join(" "), ["foo*", "foo*", "foo*"]) {
|
62
62
|
[[1], [2], [3]]
|
@@ -70,7 +70,7 @@ describe "tag glob expression" do
|
|
70
70
|
q = [
|
71
71
|
"SELECT DISTINCT hosts_tags.host_id FROM hosts_tags",
|
72
72
|
"INNER JOIN tags ON hosts_tags.tag_id = tags.id",
|
73
|
-
"WHERE tags.value GLOB
|
73
|
+
"WHERE LOWER(tags.value) GLOB LOWER(?);",
|
74
74
|
]
|
75
75
|
allow(cmd).to receive(:execute).with(q.join(" "), ["foo*"]) {
|
76
76
|
[[1], [2], [3]]
|
@@ -84,7 +84,7 @@ describe "tag glob expression" do
|
|
84
84
|
q = [
|
85
85
|
"SELECT DISTINCT hosts_tags.host_id FROM hosts_tags",
|
86
86
|
"INNER JOIN tags ON hosts_tags.tag_id = tags.id",
|
87
|
-
"WHERE tags.value GLOB
|
87
|
+
"WHERE LOWER(tags.value) GLOB LOWER(?);",
|
88
88
|
]
|
89
89
|
allow(cmd).to receive(:execute).with(q.join(" "), ["foo*"]) {
|
90
90
|
[[1], [2], [3]]
|