Navigation Menu

Skip to content

Commit

Permalink
Share target index enumerate code
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Oct 27, 2017
1 parent 30fec31 commit afb7ccd
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 79 deletions.
116 changes: 46 additions & 70 deletions lib/groonga/client/command-line/groonga-client-index-check.rb
Expand Up @@ -78,12 +78,27 @@ def initialize(client, methods, index_names)
private
def run_internal
succeeded = true
@methods.each do |method|
succeeded = false unless __send__("check_#{method}")
each_target_index_column do |index_column|
@methods.each do |method|
unless __send__("check_#{method}", index_column)
succeeded = false
end
end
end
succeeded
end

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

def check_target_table?(table_name)
unless @index_names.count > 0
return true
Expand Down Expand Up @@ -114,29 +129,10 @@ def check_target_column?(column)
false
end

def missing_source?(column)
column["type"] == "index" and column["source"].empty?
end

def check_source
missing_index_names = []
table_list.each do |table|
unless check_target_table?(table["name"])
next
end
column_list(table["name"]).each do |column|
unless check_target_column?(column)
next
end
if missing_source?(column)
missing_index_names << "#{column['domain']}.#{column['name']}"
end
end
end
missing_index_names.each do |column|
puts "index column:<#{column}> is missing source."
end
missing_index_names.empty?
def check_source(column)
return true unless column.source.empty?
$stderr.puts("Source is missing: <#{column.domain}.#{column.name}>")
false
end

def list_tokens(table_name)
Expand Down Expand Up @@ -182,53 +178,33 @@ def verify_tokens(table_name, old_column, new_column, tokens)
broken_index_tokens
end

def check_content
table_names = table_list.collect do |table|
if check_target_table?(table["name"])
table["name"]
end
end.compact
target_columns = []
table_names.each do |table_name|
column_list(table_name).collect do |column|
if check_target_column?(column)
target_columns << column
end
end
def check_content(index_column)
if index_column.source.empty?
$stderr.puts("Source is missing: <#{column.domain}.#{column.name}>")
return false
end
if target_columns.empty?
abort_run("Failed to check <#{@index_names.join(',')}> because there is no such a LEXCON.INDEX.")
end
broken_indexes = []
target_columns.each do |column|
table_name = column["domain"]
column_name = column["name"]
suffix = Time.now.strftime("%Y%m%d%H%M%S_%N")
new_column_name = "#{column_name}_#{suffix}"
if column["source"].empty?
puts("Failed to check <#{column['domain']}.#{column['name']}> because of missing source.")
next
end
type, source = column["source"].first.split(".")
flags = column["flags"].sub(/\|PERSISTENT/, '')
column_create(table_name,
new_column_name,
flags,
type,
source)
tokens = list_tokens(table_name)
puts "check #{tokens.count} tokens against <#{table_name}.#{column_name}>."
broken_index_tokens = verify_tokens(table_name, column_name,
new_column_name, tokens)
column_remove(table_name, new_column_name)
if broken_index_tokens.count > 0
broken_indexes << "#{table_name}.#{column_name}"
end
end
broken_indexes.each do |index_column|
puts "<#{index_column}> is broken."

table_name = index_column["domain"]
column_name = index_column["name"]
suffix = Time.now.strftime("%Y%m%d%H%M%S_%N")
new_column_name = "#{column_name}_#{suffix}"
type, source = index_column.sources.first.split(".")
flags = index_column["flags"].sub(/\|PERSISTENT/, '')
column_create(table_name,
new_column_name,
flags,
type,
source)
tokens = list_tokens(table_name)
broken_index_tokens = verify_tokens(table_name, column_name,
new_column_name, tokens)
column_remove(table_name, new_column_name)
if broken_index_tokens.empty?
true
else
$stderr.puts("Broken: #{table_name}.#{column_name}")
false
end
broken_indexes.empty?
end
end
end
Expand Down
14 changes: 5 additions & 9 deletions test/command-line/test-index-check.rb
Expand Up @@ -43,11 +43,11 @@ def test_source
Memos
COMMANDS

expected = <<CLIENT_OUTPUT
index column:<Terms.memos_content> is missing source.
CLIENT_OUTPUT
error_output = <<-OUTPUT
Source is missing: <Terms.memos_content>
OUTPUT

assert_equal([false, expected, ""],
assert_equal([false, "", error_output],
run_client_index_check("--method=source",
"Terms.memos_content"))
end
Expand All @@ -71,11 +71,7 @@ def test_content
]
COMMANDS

expected = <<CLIENT_OUTPUT
check 3 tokens against <Terms.memos_content>.
CLIENT_OUTPUT

assert_equal([true, expected, ""],
assert_equal([true, "", ""],
run_client_index_check("--method=content",
"Terms.memos_content"))
end
Expand Down

0 comments on commit afb7ccd

Please sign in to comment.