groonga-client 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (35) hide show
  1. checksums.yaml +7 -0
  2. data/.yardopts +4 -0
  3. data/Gemfile +21 -0
  4. data/README.md +66 -0
  5. data/Rakefile +44 -0
  6. data/doc/text/news.md +5 -0
  7. data/groonga-client.gemspec +58 -0
  8. data/lib/groonga/client.rb +165 -0
  9. data/lib/groonga/client/command.rb +49 -0
  10. data/lib/groonga/client/protocol/gqtp.rb +93 -0
  11. data/lib/groonga/client/protocol/http.rb +54 -0
  12. data/lib/groonga/client/response.rb +36 -0
  13. data/lib/groonga/client/response/base.rb +114 -0
  14. data/lib/groonga/client/response/cache_limit.rb +30 -0
  15. data/lib/groonga/client/response/check.rb +29 -0
  16. data/lib/groonga/client/response/clearlock.rb +30 -0
  17. data/lib/groonga/client/response/column_create.rb +30 -0
  18. data/lib/groonga/client/response/column_list.rb +56 -0
  19. data/lib/groonga/client/response/column_remove.rb +30 -0
  20. data/lib/groonga/client/response/column_rename.rb +30 -0
  21. data/lib/groonga/client/response/defrag.rb +30 -0
  22. data/lib/groonga/client/response/delete.rb +30 -0
  23. data/lib/groonga/client/response/dump.rb +30 -0
  24. data/lib/groonga/client/response/load.rb +30 -0
  25. data/lib/groonga/client/response/log_level.rb +30 -0
  26. data/lib/groonga/client/response/log_put.rb +30 -0
  27. data/lib/groonga/client/response/log_reopen.rb +30 -0
  28. data/lib/groonga/client/response/quit.rb +30 -0
  29. data/lib/groonga/client/response/register.rb +30 -0
  30. data/lib/groonga/client/response/status.rb +29 -0
  31. data/lib/groonga/client/response/table_list.rb +57 -0
  32. data/lib/groonga/client/version.rb +5 -0
  33. data/test/run-test.rb +44 -0
  34. data/test/test-client.rb +300 -0
  35. metadata +201 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 486d7005cfe3c244166bdc9227958043b53115ed
4
+ data.tar.gz: 08cf812cc95984efa717aa75319ccc2cb1521386
5
+ SHA512:
6
+ metadata.gz: f9222918a4338736caa918355a77c964a74e0b0a6c861942d3c2314d6e189ac869c6200d751a5be3afe2a267e21aa40689594696a5095ecd558f61708a71cd4e
7
+ data.tar.gz: 299134381bc0235bba588869339d748c034025f306b50e49bd148246a97c79d9814cdad1a41a5d30ddd6adc35640dbca5090a6cf0300c6b793be3f1815bd5281
data/.yardopts ADDED
@@ -0,0 +1,4 @@
1
+ --output-dir doc/reference/en
2
+ lib/**/*.rb
3
+ -
4
+ doc/text/**/*
data/Gemfile ADDED
@@ -0,0 +1,21 @@
1
+ # -*- mode: ruby; coding: utf-8 -*-
2
+ #
3
+ # Copyright (C) 2013 Haruka Yoshihara <yoshihara@clear-code.com>
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 as published by the Free Software Foundation; either
8
+ # version 2.1 of the License, or (at your option) any later version.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+
19
+ source "https://rubygems.org/"
20
+
21
+ gemspec
data/README.md ADDED
@@ -0,0 +1,66 @@
1
+ # README
2
+
3
+ ## Name
4
+
5
+ groonga-client
6
+
7
+ ## Description
8
+
9
+ Groonga-client is a client for groonga (http://groonga.org/)
10
+ implemented with pure ruby.
11
+
12
+ Groonga-client gem supports HTTP or
13
+ [GQTP (Groonga Query Transfer Protocol)](http://groonga.org/docs/spec/gqtp.html)
14
+ as the protocol using a client. You can use it without groonga
15
+ package.
16
+
17
+ ## Install
18
+
19
+ % gem install groonga-client
20
+
21
+ ## Usage
22
+
23
+ TODO: use commands with parameters for examples
24
+ (e.g. table_create, load, select)
25
+
26
+ ### GQTP
27
+
28
+ Groonga::Client.open(:host => host, :port => port, :protocol => :gqtp) do |client|
29
+ tables = client.table_list
30
+ tables.each do |table|
31
+ table.name
32
+ end
33
+ end
34
+
35
+ ### HTTP
36
+
37
+ Groonga::Client.open(:host => host, :port => port, :protocol => :http) do |client|
38
+ tables = client.table_list
39
+ tables.each do |table|
40
+ table.name
41
+ end
42
+ end
43
+
44
+ ## Dependencies
45
+
46
+ * Ruby 1.9.3
47
+
48
+ ## Mailing list
49
+
50
+ * English: [groonga-talk@lists.sourceforge.net](https://lists.sourceforge.net/lists/listinfo/groonga-talk)
51
+ * Japanese: [groonga-dev@lists.sourceforge.jp](http://lists.sourceforge.jp/mailman/listinfo/groonga-dev)
52
+
53
+ ## Thanks
54
+
55
+ * ...
56
+
57
+ ## Authors
58
+
59
+ * Haruka Yoshihara \<yoshihara@clear-code.com\>
60
+
61
+ ## License
62
+
63
+ LGPLv2.1 or later. See doc/text/lgpl-2.1.txt for details.
64
+
65
+ (Kouhei Sutou has a right to change the license including contributed
66
+ patches.)
data/Rakefile ADDED
@@ -0,0 +1,44 @@
1
+ # -*- mode: ruby; coding: utf-8 -*-
2
+ #
3
+ # Copyright (C) 2013 Haruka Yoshihara <yoshihara@clear-code.com>
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 as published by the Free Software Foundation; either
8
+ # version 2.1 of the License, or (at your option) any later version.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+
19
+ task :default => :test
20
+
21
+ require "rubygems"
22
+ require "bundler/gem_helper"
23
+ require "packnga"
24
+
25
+ base_dir = File.join(File.dirname(__FILE__))
26
+
27
+ helper = Bundler::GemHelper.new(base_dir)
28
+ def helper.version_tag
29
+ version
30
+ end
31
+
32
+ helper.install
33
+ spec = helper.gemspec
34
+
35
+ Packnga::DocumentTask.new(spec) do
36
+ end
37
+
38
+ Packnga::ReleaseTask.new(spec) do
39
+ end
40
+
41
+ desc "Run tests"
42
+ task :test do
43
+ ruby("test/run-test.rb")
44
+ end
data/doc/text/news.md ADDED
@@ -0,0 +1,5 @@
1
+ # NEWS
2
+
3
+ ## 0.0.1 - 2013-06-27
4
+
5
+ Initial release!!!
@@ -0,0 +1,58 @@
1
+ # -*- mode: ruby; coding: utf-8 -*-
2
+ #
3
+ # Copyright (C) 2013 Haruka Yoshihara <yoshihara@clear-code.com>
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 as published by the Free Software Foundation; either
8
+ # version 2.1 of the License, or (at your option) any later version.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+
19
+ clean_white_space = lambda do |entry|
20
+ entry.gsub(/(\A\n+|\n+\z)/, '') + "\n"
21
+ end
22
+
23
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "lib"))
24
+ require "groonga/client/version"
25
+
26
+ Gem::Specification.new do |spec|
27
+ spec.name = "groonga-client"
28
+ spec.version = Groonga::Client::VERSION
29
+ spec.homepage = "https://github.com/ranguba/groonga-client"
30
+ spec.authors = ["Haruka Yoshihara", "Kouhei Sutou", "Kosuke Asami"]
31
+ spec.email = ["yshr04hrk@gmail.com", "kou@clear-code.com", "tfortress58@gmail.com"]
32
+
33
+ readme = File.read("README.md")
34
+ readme.force_encoding("UTF-8") if readme.respond_to?(:force_encoding)
35
+ entries = readme.split(/^\#\#\s(.*)$/)
36
+ clean_white_space.call(entries[entries.index("Description") + 1])
37
+ description = clean_white_space.call(entries[entries.index("Description") + 1])
38
+ spec.summary, spec.description, = description.split(/\n\n+/, 3)
39
+ spec.license = "LGPLv2.1 or later"
40
+ spec.files = ["README.md", "Rakefile", "Gemfile", "#{spec.name}.gemspec"]
41
+ spec.files += [".yardopts"]
42
+ spec.files += Dir.glob("lib/**/*.rb")
43
+ spec.files += Dir.glob("doc/text/*")
44
+ spec.test_files += Dir.glob("test/**/*")
45
+ # Dir.chdir("bin") do
46
+ # spec.executables = Dir.glob("*")
47
+ # end
48
+
49
+ spec.add_runtime_dependency("gqtp")
50
+ spec.add_runtime_dependency("groonga-command", ">= 1.0.1")
51
+
52
+ spec.add_development_dependency("bundler")
53
+ spec.add_development_dependency("rake")
54
+ spec.add_development_dependency("test-unit")
55
+ spec.add_development_dependency("test-unit-notify")
56
+ spec.add_development_dependency("packnga")
57
+ spec.add_development_dependency("redcarpet")
58
+ end
@@ -0,0 +1,165 @@
1
+ # -*- coding: utf-8 -*-
2
+ #
3
+ # Copyright (C) 2013 Haruka Yoshihara <yoshihara@clear-code.com>
4
+ # Copyright (C) 2013 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/command"
21
+ require "groonga/client/protocol/gqtp"
22
+ require "groonga/client/protocol/http"
23
+
24
+ module Groonga
25
+ class Client
26
+ class << self
27
+ def open(options={}, &block)
28
+ client = new(options)
29
+ if block_given?
30
+ yield(client)
31
+ client.close
32
+ else
33
+ client
34
+ end
35
+ end
36
+ end
37
+
38
+ attr_reader :protocol
39
+ attr_reader :connection
40
+
41
+ def initialize(options)
42
+ @protocol = options.delete(:protocol) || :gqtp
43
+
44
+ @connection = nil
45
+ if @protocol == :gqtp
46
+ @connection = Groonga::Client::Protocol::GQTP.new(options)
47
+ else
48
+ @connection = Groonga::Client::Protocol::HTTP.new(options)
49
+ end
50
+ end
51
+
52
+ def close
53
+ end
54
+
55
+ def cache_limit(parameters)
56
+ execute_command("cache_limit", parameters)
57
+ end
58
+
59
+ def check(parameters)
60
+ execute_command("check", parameters)
61
+ end
62
+
63
+ def clearlock(parameters={})
64
+ execute_command("clearlock", parameters)
65
+ end
66
+
67
+ def column_create(parameters)
68
+ execute_command("column_create", parameters)
69
+ end
70
+
71
+ def column_list(parameters)
72
+ execute_command("column_list", parameters)
73
+ end
74
+
75
+ def column_remove(parameters)
76
+ execute_command("column_remove", parameters)
77
+ end
78
+
79
+ def column_rename(parameters)
80
+ execute_command("column_rename", parameters)
81
+ end
82
+
83
+ def defrag(parameters={})
84
+ execute_command("defrag", parameters)
85
+ end
86
+
87
+ def delete(parameters)
88
+ execute_command("delete", parameters)
89
+ end
90
+
91
+ def dump(parameters={})
92
+ execute_command("dump", parameters)
93
+ end
94
+
95
+ def load(parameters)
96
+ execute_command("load", parameters)
97
+ end
98
+
99
+ def log_level(parameters)
100
+ execute_command("log_level", parameters)
101
+ end
102
+
103
+ def log_put(parameters)
104
+ execute_command("log_put", parameters)
105
+ end
106
+
107
+ def log_reopen(parameters={})
108
+ execute_command("log_reopen", parameters)
109
+ end
110
+
111
+ def quit(parameters={})
112
+ execute_command("quit", parameters)
113
+ end
114
+
115
+ def register(parameters)
116
+ execute_command("register", parameters)
117
+ end
118
+
119
+ def select(parameters)
120
+ end
121
+
122
+ def shutdown(parameters={})
123
+ end
124
+
125
+ def status(parameters={})
126
+ execute_command("status", parameters)
127
+ end
128
+
129
+ def table_create(parameters)
130
+ end
131
+
132
+ def table_list(parameters={})
133
+ execute_command("table_list", parameters)
134
+ end
135
+
136
+ def table_remove(parameters)
137
+ end
138
+
139
+ def table_rename(parameters)
140
+ end
141
+
142
+ def truncate(parameters)
143
+ end
144
+
145
+ def execute(command, &block)
146
+ Client::Command.new(command).execute(@connection, &block)
147
+ end
148
+
149
+ private
150
+ def execute_command(command_name, parameters={})
151
+ parameters = normalize_parameters(parameters)
152
+ command_class = Groonga::Command.find(command_name)
153
+ command = command_class.new(command_name, parameters)
154
+ execute(command)
155
+ end
156
+
157
+ def normalize_parameters(parameters)
158
+ normalized_parameters = {}
159
+ parameters.each do |key, value|
160
+ normalized_parameters[key] = value.to_s
161
+ end
162
+ normalized_parameters
163
+ end
164
+ end
165
+ end
@@ -0,0 +1,49 @@
1
+ # -*- coding: utf-8 -*-
2
+ #
3
+ # Copyright (C) 2013 Haruka Yoshihara <yoshihara@clear-code.com>
4
+ # Copyright (C) 2013 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/command"
21
+ require "groonga/client/response"
22
+
23
+ module Groonga
24
+ class Client
25
+ class Command
26
+ def initialize(command)
27
+ @command = command
28
+ end
29
+
30
+ def execute(connection)
31
+ async = block_given?
32
+
33
+ response = nil
34
+ request = connection.send(@command) do |raw_response|
35
+ response_class = Groonga::Client::Response.find(@command.name)
36
+ response = response_class.parse(raw_response, @command.output_type)
37
+ yield(response) if async
38
+ end
39
+
40
+ if async
41
+ request
42
+ else
43
+ request.wait
44
+ response
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,93 @@
1
+ # -*- coding: utf-8 -*-
2
+ #
3
+ # Copyright (C) 2013 Haruka Yoshihara <yoshihara@clear-code.com>
4
+ # Copyright (C) 2013 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 "erb"
21
+
22
+ require "gqtp"
23
+ require "json"
24
+
25
+ module Groonga
26
+ class Client
27
+ module Protocol
28
+ class GQTP
29
+ def initialize(options)
30
+ @client = ::GQTP::Client.new(options)
31
+ end
32
+
33
+ def send(command)
34
+ formatted_command = command.to_command_format
35
+ raw_response = RawResponse.new(command)
36
+ @client.send(formatted_command) do |header, body|
37
+ raw_response.header = header
38
+ raw_response.body = body
39
+ response = raw_response.to_groonga_command_compatible_response
40
+ yield(response)
41
+ end
42
+ end
43
+
44
+ class RawResponse
45
+ include ERB::Util
46
+
47
+ attr_accessor :header
48
+ attr_accessor :body
49
+ def initialize(command)
50
+ @start_time = Time.now.to_f
51
+ @command = command
52
+ @header = nil
53
+ @body = nil
54
+ end
55
+
56
+ def to_groonga_command_compatible_response
57
+ case @command.output_type
58
+ when :json
59
+ convert_for_json
60
+ when :xml
61
+ convert_for_xml
62
+ when :none
63
+ @body
64
+ end
65
+ end
66
+
67
+ private
68
+ def convert_for_json
69
+ elapsed_time = Time.now.to_f - @start_time
70
+ header = [
71
+ @header.status,
72
+ @start_time,
73
+ elapsed_time,
74
+ ]
75
+ header_in_json = JSON.generate(header)
76
+ "[#{header_in_json},#{@body}]"
77
+ end
78
+
79
+ def convert_for_xml
80
+ code = @header.status
81
+ up = @start_time.to_f
82
+ elapsed = Time.now.to_f - @start_time.to_f
83
+ <<-XML
84
+ <RESULT CODE="#{h(code)}" UP="#{h(up)}" ELAPSED="#{h(elapsed)}">
85
+ #{@body}
86
+ </RESULT>
87
+ XML
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end