Navigation Menu

Skip to content

Commit

Permalink
index-check: support target specification by lexicon
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Oct 30, 2017
1 parent 6421287 commit 99b1030
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 36 deletions.
51 changes: 22 additions & 29 deletions lib/groonga/client/command-line/groonga-client-index-check.rb
Expand Up @@ -30,7 +30,7 @@ def initialize

def run(arguments)
parser = Parser.new
indexe_names = parser.parse(arguments) do |option_parser|
target_names = parser.parse(arguments) do |option_parser|
parse_command_line(option_parser)
end

Expand All @@ -39,14 +39,14 @@ def run(arguments)
end

parser.open_client do |client|
checker = Checker.new(client, @methods, indexe_names)
checker = Checker.new(client, @methods, target_names)
checker.run
end
end

private
def parse_command_line(parser)
parser.banner += " [LEXICON1.INDEX1 LEXICON2.INDEX2 ...]"
parser.banner += " [LEXICON1.INDEX1 LEXICON2.INDEX2 LEXICON3 ...]"

parser.separator("")
parser.separator("If no indexes are specified, " +
Expand All @@ -69,10 +69,10 @@ def parse_command_line(parser)
end

class Checker < Runner
def initialize(client, methods, index_names)
def initialize(client, methods, target_names)
super(client)
@methods = methods
@index_names = index_names
@target_names = target_names
end

private
Expand All @@ -90,43 +90,36 @@ def run_internal

def each_target_index_column
table_list.each do |table|
next unless check_target_table?(table.name)
next unless target_table?(table)
column_list(table.name).each do |column|
next unless check_target_column?(column)
next unless column.index?
next unless target_column?(column)
yield(column)
end
end
end

def check_target_table?(table_name)
unless @index_names.count > 0
return true
end
if @index_names.kind_of?(Array)
@index_names.each do |name|
table_part = name.split(".").first
return true if table_name == table_part
def target_table?(table)
return true if @target_names.empty?
@target_names.any? do |name|
if name.include?(".")
index_table_name = name.split(".").first
index_table_name == table.name
else
name == table.name
end
end
false
end

def check_target_column?(column)
unless @index_names.count > 0
return column["type"] == "index"
else
unless column["type"] == "index"
return false
end
end
if @index_names.kind_of?(Array)
@index_names.each do |name|
return true if name == "#{column['domain']}.#{column['name']}" or
name == column["domain"]
def target_column?(column)
return true if @target_names.empty?
@target_names.any? do |name|
if name.include?(".")
name == column.full_name
else
name == column["domain"]
end
end
false
end

def check_source(column)
Expand Down
68 changes: 61 additions & 7 deletions test/command-line/test-index-check.rb
Expand Up @@ -108,8 +108,48 @@ def test_all
index_check("--method=content"))
end

def test_table
restore(<<-COMMANDS)
table_create Memos TABLE_HASH_KEY ShortText
column_create Memos content COLUMN_SCALAR Text
table_create Terms1 TABLE_PAT_KEY ShortText \
--normalizer NormalizerAuto \
--default_tokenizer TokenBigram
column_create Terms1 memos_content1 \
COLUMN_INDEX|WITH_POSITION \
Memos content
column_create Terms1 memos_content2 \
COLUMN_INDEX|WITH_POSITION \
Memos content
table_create Terms2 TABLE_PAT_KEY ShortText \
--normalizer NormalizerAuto \
--default_tokenizer TokenBigram
column_create Terms2 memos_content \
COLUMN_INDEX|WITH_POSITION \
Memos content
load --table Memos
[
["_key", "content"],
["groonga", "Groonga is fast"]
]
delete --table Terms1 --key is
delete --table Terms2 --key is
COMMANDS

assert_equal([
false,
"",
"Broken: Terms1.memos_content1: <is>\n" +
"Broken: Terms1.memos_content2: <is>\n",
],
index_check("--method=content", "Terms1"))
end

def test_specify
def test_index_column
restore(<<-COMMANDS)
table_create Memos TABLE_HASH_KEY ShortText
column_create Memos content COLUMN_SCALAR Text
Expand Down Expand Up @@ -141,28 +181,42 @@ def test_specify
index_check("--method=content", "Terms.memos_content1"))
end

def test_broken_single_column
def test_index_columns
restore(<<-COMMANDS)
table_create Memos TABLE_HASH_KEY ShortText
column_create Memos content COLUMN_SCALAR Text
table_create Terms TABLE_PAT_KEY ShortText \
--normalizer NormalizerAuto \
--default_tokenizer TokenBigram
column_create Terms memos_content \
column_create Terms memos_content1 \
COLUMN_INDEX|WITH_POSITION \
Memos content
column_create Terms memos_content2 \
COLUMN_INDEX|WITH_POSITION \
Memos content
column_create Terms memos_content3 \
COLUMN_INDEX|WITH_POSITION \
Memos content
load --table Memos
[
{"_key": "groonga", "content": "Groonga is fast"}
["_key", "content"],
["groonga", "Groonga is fast"]
]
delete Terms --key is
delete --table Terms --key is
COMMANDS

assert_equal([false, "", "Broken: Terms.memos_content: <is>\n"],
index_check("--method=content"))
assert_equal([
false,
"",
"Broken: Terms.memos_content1: <is>\n" +
"Broken: Terms.memos_content2: <is>\n",
],
index_check("--method=content",
"Terms.memos_content1",
"Terms.memos_content2"))
end

def test_key
Expand Down

0 comments on commit 99b1030

Please sign in to comment.