Debian Codegraph @

tools/groonga-object-list-checker.rb

raw symbols SCIP
1 #!/usr/bin/env ruby
2 #
3 # Copyright(C) 2016 Kouhei Sutou <[email protected]>
4 #
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License, or (at your option) any later version.
9 #
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
19 require "pp"
20 require "json"
21 require "groonga/command/parser"
22
23 if ARGV.empty?
24 puts("Usage:")
25 puts(" #{$0} schema.grn object_list_result.json")
26 puts(" #{$0} schema.grn < object_list_result.json")
27 puts(" groonga DB_PATH object_list | #{$0} schema.grn")
28 exit(false)
29 end
30
31 schema_grn = ARGV.shift
32
33 schema = {}
34 Groonga::Command::Parser.parse(File.read(schema_grn)) do |type, *args|
35 case type
36 when :on_command
37 command, = args
38 case command.name
39 when "table_create"
40 if command.table_no_key?
41 type = "table:no_key"
42 elsif command.table_pat_key?
43 type = "table:pat_key"
44 elsif command.table_dat_key?
45 type = "table:dat_key"
46 else
47 type = "table:hash_key"
48 end
49 name = command[:name]
50 schema[name] ||= []
51 schema[name] << {
52 :type => type,
53 :flags => command.flags.join("|"),
54 }
55 when "column_create"
56 if command.column_index?
57 type = "column:index"
58 elsif command.column_vector? or command.type == "ShortText"
59 type = "column:var_size"
60 else
61 type = "column:fix_size"
62 end
63 name = "#{command[:table]}.#{command[:name]}"
64 schema[name] ||= []
65 schema[name] << {
66 :type => type,
67 :flags => command.flags.join("|"),
68 }
69 end
70 end
71 end
72
73 MAX_RESERVED_ID = 255
74 response = JSON.parse(ARGF.read)
75 body = response[1]
76 body.each do |name, object|
77 id = object["id"]
78 next if id <= MAX_RESERVED_ID
79 normalized_name = name.gsub(/\d{4}\d{2}(?:\d{2})?/, "YYYYMMDD")
80
81 definitions = schema[normalized_name]
82 if definitions.nil?
83 next if object["type"]["name"] == "proc"
84 puts("Unknown table/column: #{name}(#{id})")
85 exit(false)
86 end
87
88 type = object["type"]
89 if type.nil?
90 puts("[invalid][no-type] #{id}:#{name}")
91 puts(PP.pp(object, "").gsub(/^/, " "))
92 next
93 end
94
95 type_name = type["name"]
96 valid_type_names = definitions.collect {|definition| definition[:type]}
97 unless valid_type_names.include?(type["name"])
98 expected = "expected:[#{valid_type_names.join(", ")}]"
99 puts("[invalid][wrong-type] #{id}:#{name} <#{type_name}> #{expected}")
100 puts(PP.pp(object, "").gsub(/^/, " "))
101 puts(PP.pp(definitions, "").gsub(/^/, " "))
102 next
103 end
104 end