grn2drn 1.0.0 → 1.0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2e0a8d7c5929c03621ad56721c0cd286ce9f4496
4
- data.tar.gz: 92308316da5be5c531c9620f7e6c19aec7b1481a
3
+ metadata.gz: 4940edafe622781d134c34040b00f8334c10dd49
4
+ data.tar.gz: ee1a8d7a39e90e1f7eea163233c6290aded4ac90
5
5
  SHA512:
6
- metadata.gz: 205e98ee3714af39439967bf13ac5c6e554435cba3f6c03930525b5befeb38f1768f164ea3d6f488a4fa2c90bbce937ef2969c84d149c88800134c0b1c4b1f40
7
- data.tar.gz: bed2c4438e9a1d00414f43bdc55c18ca97718bb7c697fba37c41131e1be50f97a1f76ae6099a7c3ff8a2d325715ca7720d1ebee5c3ab6b8f6cb405a80b17b5cd
6
+ metadata.gz: b90c36e609d65ae8555f7176fb38a6236f649ffdb5232e788abf3b5bca542dff3f1ec337e545e7fb89a46ba5ec54256c5074bfae615437c08b7f40c92989c5b4
7
+ data.tar.gz: 57500ec0df6177e1399641d305deb8903138092c2e17f4d54950b6d71628bd1273e83d1758e27d26af7d618ce2de0a0c940f2eff4b7680948b8cab4ed5c5d005
data/README.md CHANGED
@@ -16,10 +16,27 @@ Grn2drn is a command to convert a *.grn dump file to a JSONs file for Droonga.
16
16
 
17
17
  ## Basic usage
18
18
 
19
+ For migrating Groonga to Droonga:
20
+
21
+ ```
22
+ % grndump /path/to/groonga/database > dump.grn
23
+ % grn2drn --dataset Droonga dump.grn > droonga.jsons
24
+ ```
25
+
26
+ You can send the converted data by `droonga-request` or `droonga-send`
27
+ command. They are included in
28
+ [droonga-client gem](http://rubygems.org/gems/droonga-client).
29
+
30
+
31
+ For creating [catalog.json](http://droonga.org/reference/catalog/):
32
+
19
33
  ```
20
- % grn2drn --dataset Droonga /path/to/grn/dump/file
34
+ % grndump --no-dump-tables /path/to/groonga/database > schema.grn
35
+ % grn2drn-schema schema.grn > schema.json
21
36
  ```
22
37
 
38
+ It generates JSON that can be embedded into catalog.json.
39
+
23
40
  ## Mailing list
24
41
 
25
42
  * English: [groonga-talk@lists.sourceforge.net](https://lists.sourceforge.net/lists/listinfo/groonga-talk)
@@ -19,7 +19,7 @@
19
19
  require "ostruct"
20
20
  require "optparse"
21
21
 
22
- require "grn2drn/converter"
22
+ require "grn2drn/command-converter"
23
23
 
24
24
  options = OpenStruct.new
25
25
  option_parser = OptionParser.new do |parser|
@@ -64,28 +64,35 @@ convert_options = {
64
64
  :reply_to => options.reply_to,
65
65
  :dataset => options.dataset,
66
66
  }
67
- converter = Grn2Drn::Converter.new(convert_options)
67
+ converter = Grn2Drn::CommandConverter.new(convert_options)
68
68
 
69
69
  source_file = args[0]
70
70
  result_file = args[1]
71
71
 
72
- input = nil
73
- if source_file.nil?
74
- input = STDIN.read
75
- else
76
- input = File.read(source_file)
72
+ def open_input(source_file)
73
+ if source_file.nil?
74
+ yield($stdin)
75
+ else
76
+ File.open(source_file) do |input|
77
+ yield(input)
78
+ end
79
+ end
77
80
  end
78
81
 
79
- result_file = args[1]
80
-
81
- if result_file.nil?
82
- converter.convert(input) do |command|
83
- puts(JSON.generate(command))
82
+ def open_output(result_file)
83
+ if result_file.nil?
84
+ yield($stdout)
85
+ else
86
+ File.open("w", result_file) do |output|
87
+ yield(output)
88
+ end
84
89
  end
85
- else
86
- File.open("w", result_file) do |file|
90
+ end
91
+
92
+ open_input(source_file) do |input|
93
+ open_output(result_file) do |output|
87
94
  converter.convert(input) do |command|
88
- file.puts(JSON.generate(command))
95
+ output.puts(JSON.generate(command))
89
96
  end
90
97
  end
91
98
  end
@@ -0,0 +1,30 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- coding: utf-8 -*-
3
+ #
4
+ # Copyright (C) 2014 Droonga Project
5
+ #
6
+ # This library is free software; you can redistribute it and/or
7
+ # modify it under the terms of the GNU Lesser General Public
8
+ # License version 2.1 as published by the Free Software Foundation.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
+
19
+ require "json"
20
+
21
+ require "grn2drn/schema-converter"
22
+
23
+ converter = Grn2Drn::SchemaConverter.new
24
+ begin
25
+ schema = converter.convert(ARGF)
26
+ rescue Grn2Drn::Error
27
+ puts($!.message)
28
+ exit(false)
29
+ end
30
+ puts(JSON.pretty_generate(schema))
@@ -22,7 +22,7 @@ require "json"
22
22
  require "groonga/command/parser"
23
23
 
24
24
  module Grn2Drn
25
- class Converter
25
+ class CommandConverter
26
26
  def initialize(options={})
27
27
  @options = options
28
28
  @count = 0
@@ -44,22 +44,13 @@ module Grn2Drn
44
44
  end
45
45
  end
46
46
 
47
- parsed_values = nil
48
47
  parsed_columns = nil
49
- @command_parser.on_load_start do |command|
50
- parsed_values = []
51
- parsed_columns = nil
52
- end
53
48
  @command_parser.on_load_columns do |command, columns|
54
49
  parsed_columns = columns
55
50
  end
56
51
  @command_parser.on_load_value do |command, value|
57
- parsed_values << value
58
- end
59
- @command_parser.on_load_complete do |command|
60
- command[:columns] ||= parsed_columns.join(",") if parsed_columns
61
- command[:values] = parsed_values.to_json
62
- split_load_command_to_add_commands(command, &block)
52
+ yield create_add_command(command, parsed_columns, value)
53
+ command.original_source.clear
63
54
  end
64
55
 
65
56
  input.each_line do |line|
@@ -123,34 +114,30 @@ module Grn2Drn
123
114
  create_message("column_create", command_to_body(command))
124
115
  end
125
116
 
126
- def split_load_command_to_add_commands(command, &block)
127
- columns = command.columns
128
- values = command[:values]
129
- values = JSON.parse(values)
130
- values.each do |record|
131
- body = {
132
- "table" => command[:table],
133
- }
134
-
135
- if record.is_a?(Hash)
136
- record = record.dup
137
- body["key"] = record.delete("_key")
138
- record_values = record
139
- else
140
- record_values = {}
141
- record.each_with_index do |value, column_index|
142
- column = columns[column_index]
143
- if column == "_key"
144
- body["key"] = value
145
- else
146
- record_values[column] = value
147
- end
117
+ def create_add_command(command, columns, record)
118
+ table = command[:table]
119
+ body = {
120
+ "table" => table,
121
+ }
122
+
123
+ if record.is_a?(Hash)
124
+ values = record.dup
125
+ body["key"] = values.delete("_key")
126
+ body["values"] = values
127
+ else
128
+ values = {}
129
+ record.each_with_index do |value, column_index|
130
+ column = columns[column_index]
131
+ if column == "_key"
132
+ body["key"] = value
133
+ else
134
+ values[column] = value
148
135
  end
149
136
  end
150
- body["values"] = record_values unless record_values.empty?
151
-
152
- yield create_message("add", body)
137
+ body["values"] = values
153
138
  end
139
+
140
+ create_message("add", body)
154
141
  end
155
142
 
156
143
  def create_select_command(command)
@@ -0,0 +1,19 @@
1
+ # Copyright (C) 2014 Droonga Project
2
+ #
3
+ # This library is free software; you can redistribute it and/or
4
+ # modify it under the terms of the GNU Lesser General Public
5
+ # License version 2.1 as published by the Free Software Foundation.
6
+ #
7
+ # This library is distributed in the hope that it will be useful,
8
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
9
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10
+ # Lesser General Public License for more details.
11
+ #
12
+ # You should have received a copy of the GNU Lesser General Public
13
+ # License along with this library; if not, write to the Free Software
14
+ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15
+
16
+ module Grn2Drn
17
+ class Error < StandardError
18
+ end
19
+ end
@@ -0,0 +1,207 @@
1
+ # Copyright (C) 2014 Droonga Project
2
+ #
3
+ # This library is free software; you can redistribute it and/or
4
+ # modify it under the terms of the GNU Lesser General Public
5
+ # License version 2.1 as published by the Free Software Foundation.
6
+ #
7
+ # This library is distributed in the hope that it will be useful,
8
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
9
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10
+ # Lesser General Public License for more details.
11
+ #
12
+ # You should have received a copy of the GNU Lesser General Public
13
+ # License along with this library; if not, write to the Free Software
14
+ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15
+
16
+ require "groonga/command/parser"
17
+
18
+ require "grn2drn/error"
19
+
20
+ module Grn2Drn
21
+ class SchemaConverter
22
+ class UnknownTable < Error
23
+ attr_reader :table
24
+ def initialize(table)
25
+ @table = table
26
+ super("Unknown table: <#{@table}>")
27
+ end
28
+ end
29
+
30
+ def initialize(options={})
31
+ @options = options
32
+ end
33
+
34
+ def convert(input)
35
+ schema = Schema.new
36
+
37
+ command_parser = Groonga::Command::Parser.new
38
+ command_parser.on_command do |command|
39
+ case command.name
40
+ when "table_create"
41
+ schema.on_table_create_command(command)
42
+ when "column_create"
43
+ schema.on_column_create_command(command)
44
+ end
45
+ end
46
+
47
+ command_parser.on_load_value do |command, value|
48
+ command.original_source.clear
49
+ end
50
+
51
+ input.each_line do |line|
52
+ command_parser << line
53
+ end
54
+ command_parser.finish
55
+
56
+ schema.to_droonga_schema
57
+ end
58
+
59
+ class Schema
60
+ def initialize
61
+ @tables = {}
62
+ end
63
+
64
+ def on_table_create_command(command)
65
+ @tables[command[:name]] = Table.new(command)
66
+ end
67
+
68
+ def on_column_create_command(command)
69
+ table_name = command.table
70
+ table = @tables[table_name]
71
+ raise UnknownTable.new(table_name) if table.nil?
72
+ table.add_column(command[:name],
73
+ Column.new(command))
74
+ end
75
+
76
+ def to_droonga_schema
77
+ droonga_schema = {}
78
+ @tables.each do |name, table|
79
+ droonga_schema[name] = table.to_droonga_schema
80
+ end
81
+ droonga_schema
82
+ end
83
+ end
84
+
85
+ class Table
86
+ def initialize(table_create_command)
87
+ @command = table_create_command
88
+ @columns = {}
89
+ end
90
+
91
+ def add_column(name, column)
92
+ @columns[name] = column
93
+ end
94
+
95
+ def to_droonga_schema
96
+ schema = {}
97
+ set_schema_item(schema, "type", type)
98
+ set_schema_item(schema, "keyType", key_type)
99
+ set_schema_item(schema, "tokenizer", tokenizer)
100
+ set_schema_item(schema, "normalizer", normalizer)
101
+ set_schema_item(schema, "columns", droonga_schema_columns)
102
+ schema
103
+ end
104
+
105
+ private
106
+ def set_schema_item(schema, key, value)
107
+ return if value.nil?
108
+ schema[key] = value
109
+ end
110
+
111
+ def type
112
+ if @command.table_no_key?
113
+ "Array"
114
+ elsif @command.table_hash_key?
115
+ "Hash"
116
+ elsif @command.table_pat_key?
117
+ "PatriciaTrie"
118
+ elsif @command.table_dat_key?
119
+ "DoubleArrayTrie"
120
+ else
121
+ "Hash"
122
+ end
123
+ end
124
+
125
+ def key_type
126
+ type = @command.key_type
127
+ case type
128
+ when /\AInt/, /\AUInt/
129
+ "Integer"
130
+ else
131
+ type
132
+ end
133
+ end
134
+
135
+ def tokenizer
136
+ @command.default_tokenizer
137
+ end
138
+
139
+ def normalizer
140
+ @command.normalizer
141
+ end
142
+
143
+ def droonga_schema_columns
144
+ return nil if @columns.empty?
145
+ schema = {}
146
+ @columns.each do |name, column|
147
+ schema[name] = column.to_droonga_schema
148
+ end
149
+ schema
150
+ end
151
+ end
152
+
153
+ class Column
154
+ def initialize(column_create_command)
155
+ @command = column_create_command
156
+ end
157
+
158
+ def to_droonga_schema
159
+ schema = {}
160
+ set_schema_item(schema, "type", type)
161
+ set_schema_item(schema, "valueType", value_type)
162
+ set_schema_item(schema, "vectorOptions", vector_options)
163
+ set_schema_item(schema, "indexOptions", index_options)
164
+ schema
165
+ end
166
+
167
+ private
168
+ def set_schema_item(schema, key, value)
169
+ return if value.nil?
170
+ schema[key] = value
171
+ end
172
+
173
+ def type
174
+ if @command.column_scalar?
175
+ "Scalar"
176
+ elsif @command.column_vector?
177
+ "Vector"
178
+ elsif @command.column_index?
179
+ "Index"
180
+ else
181
+ "Scalar"
182
+ end
183
+ end
184
+
185
+ def value_type
186
+ @command.type
187
+ end
188
+
189
+ def vector_options
190
+ return nil unless @command.column_vector?
191
+ {
192
+ "weight" => @command.with_weight?,
193
+ }
194
+ end
195
+
196
+ def index_options
197
+ return nil unless @command.column_index?
198
+ {
199
+ "section" => @command.with_section?,
200
+ "weight" => @command.with_weight?,
201
+ "position" => @command.with_position?,
202
+ "sources" => @command.sources,
203
+ }
204
+ end
205
+ end
206
+ end
207
+ end
@@ -14,5 +14,5 @@
14
14
  # along with this program. If not, see <http://www.gnu.org/licenses/>.
15
15
 
16
16
  module Grn2Drn
17
- VERSION = "1.0.0"
17
+ VERSION = "1.0.1"
18
18
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grn2drn
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Droonga Project
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-09 00:00:00.000000000 Z
11
+ date: 2014-03-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -113,6 +113,7 @@ email:
113
113
  - droonga@groonga.org
114
114
  executables:
115
115
  - grn2drn
116
+ - grn2drn-schema
116
117
  extensions: []
117
118
  extra_rdoc_files: []
118
119
  files:
@@ -120,9 +121,12 @@ files:
120
121
  - Rakefile
121
122
  - Gemfile
122
123
  - grn2drn.gemspec
123
- - lib/grn2drn/converter.rb
124
+ - lib/grn2drn/command-converter.rb
124
125
  - lib/grn2drn/version.rb
126
+ - lib/grn2drn/error.rb
127
+ - lib/grn2drn/schema-converter.rb
125
128
  - bin/grn2drn
129
+ - bin/grn2drn-schema
126
130
  homepage: https://github.com/droonga/grn2drn
127
131
  licenses:
128
132
  - GPLv3 or later
@@ -143,7 +147,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
143
147
  version: '0'
144
148
  requirements: []
145
149
  rubyforge_project:
146
- rubygems_version: 2.0.14
150
+ rubygems_version: 2.0.13
147
151
  signing_key:
148
152
  specification_version: 4
149
153
  summary: Grn2drn is a command to convert a *.grn dump file to a JSONs file for Droonga.