grn2drn 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2e0a8d7c5929c03621ad56721c0cd286ce9f4496
4
+ data.tar.gz: 92308316da5be5c531c9620f7e6c19aec7b1481a
5
+ SHA512:
6
+ metadata.gz: 205e98ee3714af39439967bf13ac5c6e554435cba3f6c03930525b5befeb38f1768f164ea3d6f488a4fa2c90bbce937ef2969c84d149c88800134c0b1c4b1f40
7
+ data.tar.gz: bed2c4438e9a1d00414f43bdc55c18ca97718bb7c697fba37c41131e1be50f97a1f76ae6099a7c3ff8a2d325715ca7720d1ebee5c3ab6b8f6cb405a80b17b5cd
data/Gemfile ADDED
@@ -0,0 +1,20 @@
1
+ # -*- ruby -*-
2
+ #
3
+ # Copyright (C) 2013-2014 Droonga Project
4
+ #
5
+ # This program is free software: you can redistribute it and/or modify
6
+ # it under the terms of the GNU General Public License as published by
7
+ # the Free Software Foundation, either version 3 of the License, or
8
+ # (at your option) any later version.
9
+ #
10
+ # This program 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
13
+ # GNU General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU General Public License
16
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
17
+
18
+ source "https://rubygems.org"
19
+
20
+ gemspec
data/README.md ADDED
@@ -0,0 +1,38 @@
1
+ # README
2
+
3
+ ## Name
4
+
5
+ grn2drn
6
+
7
+ ## Description
8
+
9
+ Grn2drn is a command to convert a *.grn dump file to a JSONs file for Droonga.
10
+
11
+ ## Install
12
+
13
+ ```
14
+ % gem install grn2drn
15
+ ```
16
+
17
+ ## Basic usage
18
+
19
+ ```
20
+ % grn2drn --dataset Droonga /path/to/grn/dump/file
21
+ ```
22
+
23
+ ## Mailing list
24
+
25
+ * English: [groonga-talk@lists.sourceforge.net](https://lists.sourceforge.net/lists/listinfo/groonga-talk)
26
+ * Japanese: [groonga-dev@lists.sourceforge.jp](http://lists.sourceforge.jp/mailman/listinfo/groonga-dev)
27
+
28
+ ## Thanks
29
+
30
+ * ...
31
+
32
+ ## Copyright
33
+
34
+ Copyright (c) 2014 Droonga Project
35
+
36
+ ## License
37
+
38
+ GPLv3 or later. See LICENSE.txt for details.
data/Rakefile ADDED
@@ -0,0 +1,41 @@
1
+ # -*- mode: ruby; coding: utf-8 -*-
2
+ #
3
+ # Copyright (C) 2013 Droonga Project
4
+ #
5
+ # This program is free software: you can redistribute it and/or modify
6
+ # it under the terms of the GNU General Public License as published by
7
+ # the Free Software Foundation, either version 3 of the License, or
8
+ # (at your option) any later version.
9
+ #
10
+ # This program 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
13
+ # GNU General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU General Public License
16
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
17
+
18
+ task :default => :test
19
+
20
+ require "bundler/gem_helper"
21
+ require "packnga"
22
+
23
+ base_dir = File.join(File.dirname(__FILE__))
24
+
25
+ helper = Bundler::GemHelper.new(base_dir)
26
+ def helper.version_tag
27
+ version
28
+ end
29
+
30
+ helper.install
31
+ spec = helper.gemspec
32
+
33
+ Packnga::DocumentTask.new(spec) do |task|
34
+ task.original_language = "en"
35
+ task.translate_languages = ["ja"]
36
+ end
37
+
38
+ desc "Run tests"
39
+ task :test do
40
+ ruby("test/run-test.rb")
41
+ end
data/bin/grn2drn ADDED
@@ -0,0 +1,91 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- coding: utf-8 -*-
3
+ #
4
+ # Copyright (C) 2013-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 "ostruct"
20
+ require "optparse"
21
+
22
+ require "grn2drn/converter"
23
+
24
+ options = OpenStruct.new
25
+ option_parser = OptionParser.new do |parser|
26
+ parser.separator("")
27
+ parser.separator("Converts Groonga commands to Droonga messages")
28
+
29
+ parser.separator("")
30
+ parser.separator("Required parameters:")
31
+ parser.on("--dataset=DATASET",
32
+ "Use DATASET as \"dataset\" field value") do |dataset|
33
+ options.dataset = dataset
34
+ end
35
+
36
+ parser.separator("")
37
+ parser.separator("Optional parameters:")
38
+ parser.on("--id-prefix=PREFIX",
39
+ "Use PREFIX as prefix of IDs") do |prefix|
40
+ options.id_prefix = prefix
41
+ end
42
+
43
+ parser.on("--date=DATE",
44
+ "Use DATE as \"date\" field value") do |date|
45
+ options.date = date
46
+ end
47
+
48
+ parser.on("--reply-to=TO",
49
+ "Use TO as \"replyTo\" field value",
50
+ "Responses from Droonga engine will be sent to TO.") do |to|
51
+ options.reply_to = to
52
+ end
53
+ end
54
+ args = option_parser.parse!(ARGV)
55
+
56
+ if options.dataset.nil?
57
+ puts("--dataset option is missed.")
58
+ exit(false)
59
+ end
60
+
61
+ convert_options = {
62
+ :id_prefix => options.id_prefix,
63
+ :date => options.date,
64
+ :reply_to => options.reply_to,
65
+ :dataset => options.dataset,
66
+ }
67
+ converter = Grn2Drn::Converter.new(convert_options)
68
+
69
+ source_file = args[0]
70
+ result_file = args[1]
71
+
72
+ input = nil
73
+ if source_file.nil?
74
+ input = STDIN.read
75
+ else
76
+ input = File.read(source_file)
77
+ end
78
+
79
+ result_file = args[1]
80
+
81
+ if result_file.nil?
82
+ converter.convert(input) do |command|
83
+ puts(JSON.generate(command))
84
+ end
85
+ else
86
+ File.open("w", result_file) do |file|
87
+ converter.convert(input) do |command|
88
+ file.puts(JSON.generate(command))
89
+ end
90
+ end
91
+ end
data/grn2drn.gemspec ADDED
@@ -0,0 +1,52 @@
1
+ # -*- mode: ruby; coding: utf-8 -*-
2
+ #
3
+ # Copyright (C) 2013-2014 Droonga Project
4
+ #
5
+ # This program is free software: you can redistribute it and/or modify
6
+ # it under the terms of the GNU General Public License as published by
7
+ # the Free Software Foundation, either version 3 of the License, or
8
+ # (at your option) any later version.
9
+ #
10
+ # This program 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
13
+ # GNU General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU General Public License
16
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
17
+
18
+ clean_white_space = lambda do |entry|
19
+ entry.gsub(/(\A\n+|\n+\z)/, '') + "\n"
20
+ end
21
+
22
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "lib"))
23
+ require "grn2drn/version"
24
+
25
+ Gem::Specification.new do |spec|
26
+ spec.name = "grn2drn"
27
+ spec.version = Grn2Drn::VERSION
28
+ spec.homepage = "https://github.com/droonga/grn2drn"
29
+ spec.authors = ["Droonga Project"]
30
+ spec.email = ["droonga@groonga.org"]
31
+ readme = File.read("README.md")
32
+ readme.force_encoding("UTF-8")
33
+ entries = readme.split(/^\#\#\s(.*)$/)
34
+ description = clean_white_space.call(entries[entries.index("Description") + 1])
35
+ spec.summary, spec.description, = description.split(/\n\n+/, 3)
36
+ spec.license = "GPLv3 or later"
37
+ spec.files = ["README.md", "Rakefile", "Gemfile", "#{spec.name}.gemspec"]
38
+ spec.files += Dir.glob("doc/text/**/*.rb")
39
+ spec.files += Dir.glob("lib/**/*.rb")
40
+ Dir.chdir("bin") do
41
+ spec.executables = Dir.glob("*")
42
+ end
43
+
44
+ spec.add_runtime_dependency("json")
45
+ spec.add_runtime_dependency("groonga-command-parser")
46
+
47
+ spec.add_development_dependency("bundler")
48
+ spec.add_development_dependency("rake")
49
+ spec.add_development_dependency("test-unit")
50
+ spec.add_development_dependency("packnga")
51
+ spec.add_development_dependency("kramdown")
52
+ end
@@ -0,0 +1,160 @@
1
+ # -*- coding: utf-8 -*-
2
+ #
3
+ # Copyright (C) 2013-2014 Droonga Project
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 version 2.1 as published by the Free Software Foundation.
8
+ #
9
+ # This library is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
+ # Lesser General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU Lesser General Public
15
+ # License along with this library; if not, write to the Free Software
16
+ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
+
18
+ require "digest/sha1"
19
+ require "time"
20
+ require "json"
21
+
22
+ require "groonga/command/parser"
23
+
24
+ module Grn2Drn
25
+ class Converter
26
+ def initialize(options={})
27
+ @options = options
28
+ @count = 0
29
+
30
+ @command_parser = Groonga::Command::Parser.new
31
+ end
32
+
33
+ def convert(input, &block)
34
+ @command_parser.on_command do |command|
35
+ case command.name
36
+ when "table_create"
37
+ yield create_table_create_command(command)
38
+ when "table_remove"
39
+ yield create_table_remove_command(command)
40
+ when "column_create"
41
+ yield create_column_create_command(command)
42
+ when "select"
43
+ yield create_select_command(command)
44
+ end
45
+ end
46
+
47
+ parsed_values = nil
48
+ parsed_columns = nil
49
+ @command_parser.on_load_start do |command|
50
+ parsed_values = []
51
+ parsed_columns = nil
52
+ end
53
+ @command_parser.on_load_columns do |command, columns|
54
+ parsed_columns = columns
55
+ end
56
+ @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)
63
+ end
64
+
65
+ input.each_line do |line|
66
+ @command_parser << line
67
+ end
68
+ @command_parser.finish
69
+ end
70
+
71
+ private
72
+ def create_message(type, body)
73
+ id_prefix = @options[:id_prefix]
74
+ if id_prefix.nil?
75
+ id = new_unique_id
76
+ else
77
+ id = "#{id_prefix}:#{@count}"
78
+ @count += 1
79
+ end
80
+
81
+ {
82
+ "id" => id,
83
+ "date" => format_date(@options[:date] || Time.now),
84
+ "replyTo" => @options[:reply_to],
85
+ "dataset" => @options[:dataset],
86
+ "type" => type,
87
+ "body" => body,
88
+ }
89
+ end
90
+
91
+ def new_unique_id
92
+ now = Time.now
93
+ now_msec = now.to_i * 1000 + now.usec
94
+ random_string = rand(36 ** 16).to_s(36) # Base36
95
+ Digest::SHA1.hexdigest("#{now_msec}:#{random_string}")
96
+ end
97
+
98
+ def format_date(time)
99
+ time.iso8601
100
+ end
101
+
102
+ def stringify_keys(hash)
103
+ stringified_hash = {}
104
+ hash.each do |key, value|
105
+ stringified_hash[key.to_s] = value
106
+ end
107
+ stringified_hash
108
+ end
109
+
110
+ def command_to_body(command)
111
+ stringify_keys(command.arguments)
112
+ end
113
+
114
+ def create_table_create_command(command)
115
+ create_message("table_create", command_to_body(command))
116
+ end
117
+
118
+ def create_table_remove_command(command)
119
+ create_message("table_remove", command_to_body(command))
120
+ end
121
+
122
+ def create_column_create_command(command)
123
+ create_message("column_create", command_to_body(command))
124
+ end
125
+
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
148
+ end
149
+ end
150
+ body["values"] = record_values unless record_values.empty?
151
+
152
+ yield create_message("add", body)
153
+ end
154
+ end
155
+
156
+ def create_select_command(command)
157
+ create_message("select", command_to_body(command))
158
+ end
159
+ end
160
+ end
@@ -0,0 +1,18 @@
1
+ # Copyright (C) 2014 Droonga Project
2
+ #
3
+ # This program is free software: you can redistribute it and/or modify
4
+ # it under the terms of the GNU General Public License as published by
5
+ # the Free Software Foundation, either version 3 of the License, or
6
+ # (at your option) any later version.
7
+ #
8
+ # This program is distributed in the hope that it will be useful,
9
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
+ # GNU General Public License for more details.
12
+ #
13
+ # You should have received a copy of the GNU General Public License
14
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
15
+
16
+ module Grn2Drn
17
+ VERSION = "1.0.0"
18
+ end
metadata ADDED
@@ -0,0 +1,151 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: grn2drn
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Droonga Project
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: groonga-command-parser
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: test-unit
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: packnga
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: kramdown
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: ''
112
+ email:
113
+ - droonga@groonga.org
114
+ executables:
115
+ - grn2drn
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - README.md
120
+ - Rakefile
121
+ - Gemfile
122
+ - grn2drn.gemspec
123
+ - lib/grn2drn/converter.rb
124
+ - lib/grn2drn/version.rb
125
+ - bin/grn2drn
126
+ homepage: https://github.com/droonga/grn2drn
127
+ licenses:
128
+ - GPLv3 or later
129
+ metadata: {}
130
+ post_install_message:
131
+ rdoc_options: []
132
+ require_paths:
133
+ - lib
134
+ required_ruby_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - '>='
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ required_rubygems_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - '>='
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ requirements: []
145
+ rubyforge_project:
146
+ rubygems_version: 2.0.14
147
+ signing_key:
148
+ specification_version: 4
149
+ summary: Grn2drn is a command to convert a *.grn dump file to a JSONs file for Droonga.
150
+ test_files: []
151
+ has_rdoc: