groonga-client 0.3.5 → 0.3.6

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: b81898dfb1545f8ea8096d3e5632351d6fa0c9d6
4
- data.tar.gz: 74aacd0492c441e64f3553f66e5cb11d6f5f4d9e
3
+ metadata.gz: a6c355134e4059b95ea7d3746803c7f4341d9247
4
+ data.tar.gz: 9c3ebae53243d5fa4cb0ab8395763db7e2e4bcfe
5
5
  SHA512:
6
- metadata.gz: f241f17e797564649c9e48932f707e9f00437b963d3aa652d7c274ee93cd9ffd7401e70ee4fb88ad37b95a1b436c4004c5bdb52ca243050f63b717be5cb9f87f
7
- data.tar.gz: f2a88c1e238bffcde125363cb1a175a1cc9b433e8ef6b0898f42376edbd6f38d8fa48a4d8ca95734a67a5a4e7c3c128ec2a40314c01a32c40e13b25fb24ca183
6
+ metadata.gz: a91f6bd599929636c25aa63654c4a026b9d951b68c1f1ae25c6355fdfb2cef205ed76afe13af041e8eb895e7039a898cf8ea278b806b23c004447e9e1980bd2a
7
+ data.tar.gz: 55997467909ceb21090d53379b9d70968c3b2bd79f9a590a9f93d41cb1cf873335718af0b84a2ca719b8bdbfcb653f9fdc59260d68c8f029034b66bd001a32f6
@@ -0,0 +1,23 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- ruby -*-
3
+ #
4
+ # Copyright (C) 2015-2016 Kouhei Sutou <kou@clear-code.com>
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 as published by the Free Software Foundation; either
9
+ # version 2.1 of the License, or (at your option) any later version.
10
+ #
11
+ # This library is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
+ # Lesser General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Lesser General Public
17
+ # License along with this library; if not, write to the Free Software
18
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+
20
+ require "groonga/client/cli"
21
+
22
+ cli = Groonga::Client::CLI.new
23
+ exit(cli.run(ARGV))
data/doc/text/news.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # NEWS
2
2
 
3
+ ## 0.3.6 - 2016-12-20
4
+
5
+ ### Improvements
6
+
7
+ * Added `Groonga::Client::Test::GroongaServerRunner#using_running_server?`.
8
+
9
+ * Merged groonga-client-cli. Now `groonga-client` command is available.
10
+
11
+ * `Groonga::Client::Response::Schema#plugins`: Supported method access.
12
+
13
+ * `Groonga::Client::Test::GroongaServerRunner`: Supported restoring DB for existing server.
14
+
3
15
  ## 0.3.5 - 2016-12-19
4
16
 
5
17
  ### Improvements
@@ -43,9 +43,13 @@ Gem::Specification.new do |spec|
43
43
  spec.files += Dir.glob("lib/**/*.rb")
44
44
  spec.files += Dir.glob("doc/text/*")
45
45
  spec.test_files += Dir.glob("test/**/*")
46
+ Dir.chdir("bin") do
47
+ spec.executables = Dir.glob("*")
48
+ end
46
49
 
47
50
  spec.add_runtime_dependency("gqtp", ">= 1.0.4")
48
51
  spec.add_runtime_dependency("groonga-command", ">= 1.2.8")
52
+ spec.add_runtime_dependency("groonga-command-parser", ">= 1.0.7")
49
53
  spec.add_runtime_dependency("hashie")
50
54
 
51
55
  spec.add_development_dependency("bundler")
@@ -54,5 +58,4 @@ Gem::Specification.new do |spec|
54
58
  spec.add_development_dependency("test-unit-rr")
55
59
  spec.add_development_dependency("packnga")
56
60
  spec.add_development_dependency("redcarpet")
57
- spec.add_development_dependency("groonga-command-parser")
58
61
  end
@@ -0,0 +1,233 @@
1
+ # Copyright (C) 2015-2016 Kouhei Sutou <kou@clear-code.com>
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 as published by the Free Software Foundation; either
6
+ # version 2.1 of the License, or (at your option) any later version.
7
+ #
8
+ # This library 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 GNU
11
+ # Lesser General Public License for more details.
12
+ #
13
+ # You should have received a copy of the GNU Lesser General Public
14
+ # License along with this library; if not, write to the Free Software
15
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
+
17
+ require "ostruct"
18
+ require "optparse"
19
+ require "json"
20
+ require "securerandom"
21
+
22
+ require "groonga/client"
23
+
24
+ require "groonga/command/parser"
25
+
26
+ module Groonga
27
+ class Client
28
+ class CLI
29
+ def initialize
30
+ @protocol = :http
31
+ @host = "localhost"
32
+ @port = nil
33
+
34
+ @read_timeout = Client::Default::READ_TIMEOUT
35
+
36
+ @chunk = false
37
+
38
+ @runner_options = {
39
+ :split_load_chunk_size => 10000,
40
+ :generate_request_id => false,
41
+ }
42
+ end
43
+
44
+ def run(argv)
45
+ command_file_paths = parse_command_line(argv)
46
+
47
+ @client = Client.new(:protocol => @protocol,
48
+ :host => @host,
49
+ :port => @port,
50
+ :read_timeout => @read_timeout,
51
+ :chunk => @chunk,
52
+ :backend => :synchronous)
53
+ runner = Runner.new(@client, @runner_options)
54
+
55
+ if command_file_paths.empty?
56
+ $stdin.each_line do |line|
57
+ runner << line
58
+ end
59
+ else
60
+ command_file_paths.each do |command_file_path|
61
+ File.open(command_file_path) do |command_file|
62
+ last_line = nil
63
+ command_file.each_line do |line|
64
+ last_line = line
65
+ runner << line
66
+ end
67
+ if last_line and !last_line.end_with?("\n")
68
+ runner << "\n"
69
+ end
70
+ end
71
+ end
72
+ end
73
+ runner.finish
74
+
75
+ true
76
+ end
77
+
78
+ private
79
+ def parse_command_line(argv)
80
+ parser = OptionParser.new
81
+ parser.version = VERSION
82
+ parser.banner += " GROONGA_COMMAND_FILE1 GROONGA_COMMAND_FILE2 ..."
83
+
84
+ parser.separator("")
85
+
86
+ parser.separator("Connection:")
87
+
88
+ available_protocols = [:http, :gqtp]
89
+ parser.on("--protocol=PROTOCOL", [:http, :gqtp],
90
+ "Protocol to connect to Groonga server.",
91
+ "[#{available_protocols.join(", ")}]",
92
+ "(#{@protocol})") do |protocol|
93
+ @protocol = protocol
94
+ end
95
+
96
+ parser.on("--host=HOST",
97
+ "Groonga server to be connected.",
98
+ "(#{@host})") do |host|
99
+ @host = host
100
+ end
101
+
102
+ parser.on("--port=PORT", Integer,
103
+ "Port number of Groonga server to be connected.",
104
+ "(auto)") do |port|
105
+ @port = port
106
+ end
107
+
108
+ parser.on("--read-timeout=TIMEOUT", Integer,
109
+ "Timeout on reading response from Groonga server.",
110
+ "You can disable timeout by specifying -1.",
111
+ "(#{@read_timeout})") do |timeout|
112
+ @read_timeout = timeout
113
+ end
114
+
115
+ parser.on("--split-load-chunk-size=SIZE", Integer,
116
+ "Split a large load to small loads.",
117
+ "Each small load has at most SIZE records.",
118
+ "Set 0 to SIZE to disable this feature.",
119
+ "(#{@runner_options[:split_load_chunk_size]})") do |size|
120
+ @runner_options[:split_load_chunk_size] = size
121
+ end
122
+
123
+ parser.on("--[no-]generate-request-id",
124
+ "Add auto generated request ID to all commands.",
125
+ "(#{@runner_options[:generate_request_id]})") do |boolean|
126
+ @runner_options[:generate_request_id] = boolean
127
+ end
128
+
129
+ parser.on("--[no-]chunk",
130
+ "Use \"Transfer-Encoding: chunked\" for load command.",
131
+ "HTTP only.",
132
+ "(#{@chunk})") do |boolean|
133
+ @chunk = boolean
134
+ end
135
+
136
+ command_file_paths = parser.parse(argv)
137
+
138
+ @port ||= default_port(@protocol)
139
+
140
+ command_file_paths
141
+ end
142
+
143
+ def default_port(protocol)
144
+ case protocol
145
+ when :http
146
+ 10041
147
+ when :gqtp
148
+ 10043
149
+ end
150
+ end
151
+
152
+ class Runner
153
+ def initialize(client, options={})
154
+ @client = client
155
+ @split_load_chunk_size = options[:split_load_chunk_size] || 10000
156
+ @generate_request_id = options[:generate_request_id]
157
+ @load_values = []
158
+ @parser = create_command_parser
159
+ end
160
+
161
+ def <<(line)
162
+ @parser << line
163
+ end
164
+
165
+ def finish
166
+ @parser.finish
167
+ end
168
+
169
+ private
170
+ def create_command_parser
171
+ parser = Groonga::Command::Parser.new
172
+
173
+ parser.on_command do |command|
174
+ run_command(command)
175
+ end
176
+
177
+ parser.on_load_columns do |command, columns|
178
+ command[:columns] ||= columns.join(",")
179
+ end
180
+
181
+ parser.on_load_value do |command, value|
182
+ unless command[:values]
183
+ @load_values << value
184
+ if @load_values.size == @split_load_chunk_size
185
+ consume_load_values(command)
186
+ end
187
+ end
188
+ command.original_source.clear
189
+ end
190
+
191
+ parser.on_load_complete do |command|
192
+ if command[:values]
193
+ run_command(client, command)
194
+ else
195
+ consume_load_values(command)
196
+ end
197
+ end
198
+
199
+ parser
200
+ end
201
+
202
+ def consume_load_values(load_command)
203
+ return if @load_values.empty?
204
+
205
+ values_json = "["
206
+ @load_values.each_with_index do |value, i|
207
+ values_json << "," unless i.zero?
208
+ values_json << "\n"
209
+ values_json << JSON.generate(value)
210
+ end
211
+ values_json << "\n]\n"
212
+ load_command[:values] = values_json
213
+ run_command(load_command)
214
+ @load_values.clear
215
+ load_command[:values] = nil
216
+ end
217
+
218
+ def run_command(command)
219
+ command[:request_id] ||= SecureRandom.uuid if @generate_request_id
220
+ response = @client.execute(command)
221
+ case command.output_type
222
+ when :json
223
+ puts(JSON.pretty_generate([response.header, response.body]))
224
+ when :xml
225
+ puts(response.raw)
226
+ else
227
+ puts(response.body)
228
+ end
229
+ end
230
+ end
231
+ end
232
+ end
233
+ end
@@ -25,6 +25,16 @@ module Groonga
25
25
  class Schema < Base
26
26
  Response.register("schema", self)
27
27
 
28
+ # @return [Hash<String, Plugin>] Key is plugin name and
29
+ # value is the definition of the plugin.
30
+ #
31
+ # @since 0.3.6
32
+ def plugins
33
+ @plugins ||= HashValueConverter.convert(@body["plugins"]) do |raw_plugin|
34
+ Plugin[raw_plugin]
35
+ end
36
+ end
37
+
28
38
  # @return [Hash<String, Type>] Key is type name and
29
39
  # value is the definition of the type.
30
40
  #
@@ -102,6 +112,11 @@ module Groonga
102
112
  end
103
113
  end
104
114
 
115
+ class Plugin < ::Hash
116
+ include Hashie::Extensions::MergeInitializer
117
+ include Hashie::Extensions::MethodAccess
118
+ end
119
+
105
120
  class Type < ::Hash
106
121
  include Hashie::Extensions::MergeInitializer
107
122
  include Hashie::Extensions::MethodAccess
@@ -17,6 +17,8 @@
17
17
  require "rbconfig"
18
18
  require "fileutils"
19
19
 
20
+ require "groonga/command/parser"
21
+
20
22
  module Groonga
21
23
  class Client
22
24
  module Test
@@ -32,6 +34,9 @@ module Groonga
32
34
  def run
33
35
  if groonga_server_running?
34
36
  @using_running_server = true
37
+ @dump = Groonga::Client.open(url: @url) do |client|
38
+ client.dump.body
39
+ end
35
40
  else
36
41
  return if @groonga.nil?
37
42
  @tmp_dir = create_tmp_dir
@@ -50,11 +55,8 @@ module Groonga
50
55
  def stop
51
56
  if @using_running_server
52
57
  Groonga::Client.open(url: @url) do |client|
53
- schema = client.schema
54
- schema.tables.each do |name, _|
55
- client.delete(table: name,
56
- filter: "true")
57
- end
58
+ remove_all(client)
59
+ restore(client)
58
60
  end
59
61
  else
60
62
  if @pid
@@ -69,6 +71,10 @@ module Groonga
69
71
  end
70
72
  end
71
73
 
74
+ def using_running_server?
75
+ @using_running_server
76
+ end
77
+
72
78
  private
73
79
  def build_url
74
80
  default_options = Groonga::Client.default_options
@@ -152,6 +158,57 @@ module Groonga
152
158
  Process.kill(:KILL, @pid)
153
159
  Process.waitpid(@pid)
154
160
  end
161
+
162
+ def remove_all(client)
163
+ schema = client.schema
164
+ schema.tables.each_value do |table|
165
+ table.columns.each_value do |column|
166
+ client.column_remove(:table => table.name,
167
+ :name => column.name)
168
+ end
169
+ end
170
+ schema.tables.each_value do |table|
171
+ client.table_remove(:name => table.name)
172
+ end
173
+ schema.plugins.each_value do |plugin|
174
+ client.plugin_unregister(:name => plugin.name)
175
+ end
176
+ end
177
+
178
+ def restore(client)
179
+ return if @dump.empty?
180
+
181
+ parser = Groonga::Command::Parser.new
182
+
183
+ parser.on_command do |command|
184
+ client.execute(command)
185
+ end
186
+
187
+ parser.on_load_columns do |command, columns|
188
+ command[:columns] ||= columns.join(",")
189
+ end
190
+
191
+ load_values = []
192
+ parser.on_load_value do |command, value|
193
+ unless command[:values]
194
+ load_values << value
195
+ end
196
+ command.original_source.clear
197
+ end
198
+
199
+ parser.on_load_complete do |command|
200
+ unless command[:values]
201
+ command[:values] = JSON.generate(load_values)
202
+ load_values.clear
203
+ end
204
+ client.execute(command)
205
+ end
206
+
207
+ @dump.each_line do |line|
208
+ parser << line
209
+ end
210
+ parser.finish
211
+ end
155
212
  end
156
213
  end
157
214
  end
@@ -16,6 +16,6 @@
16
16
 
17
17
  module Groonga
18
18
  class Client
19
- VERSION = "0.3.5"
19
+ VERSION = "0.3.6"
20
20
  end
21
21
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: groonga-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.5
4
+ version: 0.3.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Haruka Yoshihara
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2016-12-19 00:00:00.000000000 Z
13
+ date: 2016-12-20 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: gqtp
@@ -41,27 +41,27 @@ dependencies:
41
41
  - !ruby/object:Gem::Version
42
42
  version: 1.2.8
43
43
  - !ruby/object:Gem::Dependency
44
- name: hashie
44
+ name: groonga-command-parser
45
45
  requirement: !ruby/object:Gem::Requirement
46
46
  requirements:
47
47
  - - ">="
48
48
  - !ruby/object:Gem::Version
49
- version: '0'
49
+ version: 1.0.7
50
50
  type: :runtime
51
51
  prerelease: false
52
52
  version_requirements: !ruby/object:Gem::Requirement
53
53
  requirements:
54
54
  - - ">="
55
55
  - !ruby/object:Gem::Version
56
- version: '0'
56
+ version: 1.0.7
57
57
  - !ruby/object:Gem::Dependency
58
- name: bundler
58
+ name: hashie
59
59
  requirement: !ruby/object:Gem::Requirement
60
60
  requirements:
61
61
  - - ">="
62
62
  - !ruby/object:Gem::Version
63
63
  version: '0'
64
- type: :development
64
+ type: :runtime
65
65
  prerelease: false
66
66
  version_requirements: !ruby/object:Gem::Requirement
67
67
  requirements:
@@ -69,7 +69,7 @@ dependencies:
69
69
  - !ruby/object:Gem::Version
70
70
  version: '0'
71
71
  - !ruby/object:Gem::Dependency
72
- name: rake
72
+ name: bundler
73
73
  requirement: !ruby/object:Gem::Requirement
74
74
  requirements:
75
75
  - - ">="
@@ -83,7 +83,7 @@ dependencies:
83
83
  - !ruby/object:Gem::Version
84
84
  version: '0'
85
85
  - !ruby/object:Gem::Dependency
86
- name: test-unit
86
+ name: rake
87
87
  requirement: !ruby/object:Gem::Requirement
88
88
  requirements:
89
89
  - - ">="
@@ -97,7 +97,7 @@ dependencies:
97
97
  - !ruby/object:Gem::Version
98
98
  version: '0'
99
99
  - !ruby/object:Gem::Dependency
100
- name: test-unit-rr
100
+ name: test-unit
101
101
  requirement: !ruby/object:Gem::Requirement
102
102
  requirements:
103
103
  - - ">="
@@ -111,7 +111,7 @@ dependencies:
111
111
  - !ruby/object:Gem::Version
112
112
  version: '0'
113
113
  - !ruby/object:Gem::Dependency
114
- name: packnga
114
+ name: test-unit-rr
115
115
  requirement: !ruby/object:Gem::Requirement
116
116
  requirements:
117
117
  - - ">="
@@ -125,7 +125,7 @@ dependencies:
125
125
  - !ruby/object:Gem::Version
126
126
  version: '0'
127
127
  - !ruby/object:Gem::Dependency
128
- name: redcarpet
128
+ name: packnga
129
129
  requirement: !ruby/object:Gem::Requirement
130
130
  requirements:
131
131
  - - ">="
@@ -139,7 +139,7 @@ dependencies:
139
139
  - !ruby/object:Gem::Version
140
140
  version: '0'
141
141
  - !ruby/object:Gem::Dependency
142
- name: groonga-command-parser
142
+ name: redcarpet
143
143
  requirement: !ruby/object:Gem::Requirement
144
144
  requirements:
145
145
  - - ">="
@@ -160,7 +160,8 @@ email:
160
160
  - yshr04hrk@gmail.com
161
161
  - kou@clear-code.com
162
162
  - tfortress58@gmail.com
163
- executables: []
163
+ executables:
164
+ - groonga-client
164
165
  extensions: []
165
166
  extra_rdoc_files: []
166
167
  files:
@@ -168,10 +169,12 @@ files:
168
169
  - Gemfile
169
170
  - README.md
170
171
  - Rakefile
172
+ - bin/groonga-client
171
173
  - doc/text/lgpl-2.1.txt
172
174
  - doc/text/news.md
173
175
  - groonga-client.gemspec
174
176
  - lib/groonga/client.rb
177
+ - lib/groonga/client/cli.rb
175
178
  - lib/groonga/client/command.rb
176
179
  - lib/groonga/client/default.rb
177
180
  - lib/groonga/client/empty-request.rb
@@ -270,29 +273,29 @@ specification_version: 4
270
273
  summary: Groonga-client is a client for Groonga (http://groonga.org/) implemented
271
274
  with pure Ruby. You can use it without Groonga.
272
275
  test_files:
276
+ - test/test-client.rb
273
277
  - test/test-script-syntax.rb
274
- - test/protocol/test-http.rb
278
+ - test/request/select/test-filter-parameter.rb
279
+ - test/request/select/test-output-columns-parameter.rb
280
+ - test/request/select/test-values-parameter.rb
281
+ - test/request/select/test-sort-keys-columns-parameter.rb
282
+ - test/request/test-base.rb
283
+ - test/request/test-select.rb
284
+ - test/test-command.rb
275
285
  - test/protocol/test-gqtp.rb
276
- - test/response/test-schema.rb
277
- - test/response/test-error.rb
278
- - test/response/test-table-remove.rb
279
- - test/response/test-select-command-version3.rb
280
- - test/response/test-table-list.rb
281
- - test/response/test-column-list.rb
286
+ - test/protocol/test-http.rb
287
+ - test/run-test.rb
288
+ - test/response/test-base.rb
282
289
  - test/response/test-load.rb
290
+ - test/response/test-column-list.rb
283
291
  - test/response/helper.rb
284
- - test/response/test-base.rb
285
- - test/response/test-select-command-version1.rb
292
+ - test/response/test-error.rb
293
+ - test/response/test-table-list.rb
286
294
  - test/response/test-status.rb
295
+ - test/response/test-schema.rb
296
+ - test/response/test-select-command-version3.rb
297
+ - test/response/test-select-command-version1.rb
287
298
  - test/response/test-table-create.rb
288
- - test/results/test-table-list.rb
299
+ - test/response/test-table-remove.rb
289
300
  - test/results/test-column-list.rb
290
- - test/test-command.rb
291
- - test/run-test.rb
292
- - test/request/select/test-values-parameter.rb
293
- - test/request/select/test-sort-keys-columns-parameter.rb
294
- - test/request/select/test-output-columns-parameter.rb
295
- - test/request/select/test-filter-parameter.rb
296
- - test/request/test-select.rb
297
- - test/request/test-base.rb
298
- - test/test-client.rb
301
+ - test/results/test-table-list.rb