| 1 |
|
| 2 |
|
| 3 |
require "fileutils" |
| 4 |
require "optparse" |
| 5 |
require "json" |
| 6 |
|
| 7 |
class CopyRelatedFiles |
| 8 |
def initialize |
| 9 |
@database_path = nil |
| 10 |
@groonga = "groonga" |
| 11 |
@destination_path = "." |
| 12 |
@targets = [] |
| 13 |
end |
| 14 |
|
| 15 |
def run(argv) |
| 16 |
return false unless parse_options(argv) |
| 17 |
|
| 18 |
ensure_schema |
| 19 |
copy |
| 20 |
|
| 21 |
true |
| 22 |
end |
| 23 |
|
| 24 |
private |
| 25 |
def parse_options(argv) |
| 26 |
parser = OptionParser.new |
| 27 |
parser.banner += " DATABASE_PATH" |
| 28 |
parser.on("--groonga=PATH", |
| 29 |
"The path to the groonga command.", |
| 30 |
"#{@grronga}") do |path| |
| 31 |
@groonga = path |
| 32 |
end |
| 33 |
parser.on("--destination=PATH", |
| 34 |
"The path to the destination directory.", |
| 35 |
"#{@destination_path}") do |path| |
| 36 |
@destination_path = path |
| 37 |
end |
| 38 |
parser.on("--target=TARGET", |
| 39 |
"The target table/column to be copied.", |
| 40 |
"You can specify this option multiple times", |
| 41 |
"to copy multiple tables/columns.") do |target| |
| 42 |
@targets << target |
| 43 |
end |
| 44 |
|
| 45 |
@database_path, = parser.parse!(argv) |
| 46 |
not @database_path.nil? |
| 47 |
end |
| 48 |
|
| 49 |
def ensure_schema |
| 50 |
response = "" |
| 51 |
IO.pipe do |input, output| |
| 52 |
pid = spawn(@groonga, |
| 53 |
@database_path, |
| 54 |
"schema", |
| 55 |
"--output_pretty", "yes", |
| 56 |
:out => output) |
| 57 |
output.close |
| 58 |
input.each_line do |line| |
| 59 |
response << line |
| 60 |
end |
| 61 |
Process.waitpid(pid) |
| 62 |
end |
| 63 |
@schema = JSON.parse(response)[1] |
| 64 |
end |
| 65 |
|
| 66 |
def copy |
| 67 |
FileUtils.mkdir_p(@destination_path) |
| 68 |
copy_builtin_files |
| 69 |
copy_target_files |
| 70 |
end |
| 71 |
|
| 72 |
def copy_builtin_files |
| 73 |
suffixes = [ |
| 74 |
"", |
| 75 |
".001", |
| 76 |
".conf", |
| 77 |
".options", |
| 78 |
".0000000", |
| 79 |
] |
| 80 |
suffixes.each do |suffix| |
| 81 |
FileUtils.cp("#{@database_path}#{suffix}", |
| 82 |
@destination_path) |
| 83 |
end |
| 84 |
end |
| 85 |
|
| 86 |
def copy_target_files |
| 87 |
target_ids.each do |id| |
| 88 |
FileUtils.cp(Dir.glob("%s.%07X*" % [@database_path, id]), |
| 89 |
@destination_path) |
| 90 |
end |
| 91 |
end |
| 92 |
|
| 93 |
def target_ids |
| 94 |
ids = [] |
| 95 |
processed_targets = {} |
| 96 |
@targets.each do |target| |
| 97 |
table_name, column_name = target.split(".", 2) |
| 98 |
table = @schema["tables"][table_name] |
| 99 |
ids.concat(extract_ids_from_table(table, processed_targets)) |
| 100 |
next if column_name.nil? |
| 101 |
column = table["columns"][column_name] |
| 102 |
ids.concat(extract_ids_from_column(column, processed_targets)) |
| 103 |
end |
| 104 |
ids.uniq |
| 105 |
end |
| 106 |
|
| 107 |
def extract_ids_from_table(table, processed_targets) |
| 108 |
return [] if processed_targets.key?(table) |
| 109 |
processed_targets[table] = true |
| 110 |
|
| 111 |
ids = [] |
| 112 |
ids << table["id"] |
| 113 |
key_type = table["key_type"] |
| 114 |
if key_type && key_type["type"] == "reference" |
| 115 |
ids.concat(extract_ids_from_table(@schema["tables"][key_type["name"]], |
| 116 |
processed_targets)) |
| 117 |
end |
| 118 |
value_type = table["value_type"] |
| 119 |
if value_type && value_type["type"] == "reference" |
| 120 |
ids.concat(extract_ids_from_table(@schema["tables"][value_type["name"]], |
| 121 |
processed_targets)) |
| 122 |
end |
| 123 |
ids |
| 124 |
end |
| 125 |
|
| 126 |
def extract_ids_from_column(column, processed_targets) |
| 127 |
return [] if processed_targets.key?(column) |
| 128 |
processed_targets[column] = true |
| 129 |
|
| 130 |
ids = [] |
| 131 |
ids << column["id"] |
| 132 |
value_type = column["value_type"] |
| 133 |
if value_type && value_type["type"] == "reference" |
| 134 |
ids.concat(extract_ids_from_table(@schema["tables"][value_type["name"]], |
| 135 |
processed_targets)) |
| 136 |
end |
| 137 |
sources = column["sources"] || [] |
| 138 |
sources.each do |source| |
| 139 |
source_table = @schema["tables"][source["table"]] |
| 140 |
ids.concat(extract_ids_from_table(source_table, processed_targets)) |
| 141 |
source_name = source["name"] |
| 142 |
source_column = source_table["columns"][source_name] |
| 143 |
ids.concat(extract_ids_from_column(source_column, processed_targets)) |
| 144 |
end |
| 145 |
indexes = column["indexes"] || [] |
| 146 |
indexes.each do |index| |
| 147 |
index_table = @schema["tables"][index["table"]] |
| 148 |
ids.concat(extract_ids_from_table(index_table, processed_targets)) |
| 149 |
index_column = index_table["columns"][index["name"]] |
| 150 |
ids.concat(extract_ids_from_column(index_column, processed_targets)) |
| 151 |
end |
| 152 |
ids |
| 153 |
end |
| 154 |
end |
| 155 |
|
| 156 |
command = CopyRelatedFiles.new |
| 157 |
exit(command.run(ARGV)) |