groonga-client 0.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.
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
@@ -0,0 +1,54 @@
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 "open-uri"
21
+
22
+ module Groonga
23
+ class Client
24
+ module Protocol
25
+ class Request
26
+ def initialize(thread)
27
+ @thread = thread
28
+ end
29
+
30
+ def wait
31
+ @thread.join
32
+ end
33
+ end
34
+
35
+ class HTTP
36
+ def initialize(options)
37
+ @host = options[:host] || "127.0.0.1"
38
+ @port = options[:port] || 10041
39
+ end
40
+
41
+ def send(command)
42
+ url = "http://#{@host}:#{@port}#{command.to_uri_format}"
43
+ thread = Thread.new do
44
+ open(url) do |response|
45
+ body = response.read
46
+ yield(body)
47
+ end
48
+ end
49
+ Request.new(thread)
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,36 @@
1
+ # -*- 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
+ require "groonga/client/response/cache_limit"
20
+ require "groonga/client/response/check"
21
+ require "groonga/client/response/clearlock"
22
+ require "groonga/client/response/column_create"
23
+ require "groonga/client/response/column_list"
24
+ require "groonga/client/response/column_remove"
25
+ require "groonga/client/response/column_rename"
26
+ require "groonga/client/response/defrag"
27
+ require "groonga/client/response/delete"
28
+ require "groonga/client/response/dump"
29
+ require "groonga/client/response/load"
30
+ require "groonga/client/response/log_level"
31
+ require "groonga/client/response/log_put"
32
+ require "groonga/client/response/log_reopen"
33
+ require "groonga/client/response/quit"
34
+ require "groonga/client/response/register"
35
+ require "groonga/client/response/status"
36
+ require "groonga/client/response/table_list"
@@ -0,0 +1,114 @@
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 "rexml/document"
21
+
22
+ require "json"
23
+
24
+ module Groonga
25
+ class Client
26
+ module Response
27
+ class << self
28
+ @@registered_commands = {}
29
+ def register(name, klass)
30
+ @@registered_commands[name] = klass
31
+ end
32
+
33
+ def find(name)
34
+ @@registered_commands[name] || Base
35
+ end
36
+ end
37
+
38
+ class Base
39
+ class << self
40
+ def parse(raw_response, type)
41
+ case type
42
+ when :json
43
+ header, body = JSON.parse(raw_response)
44
+ when :xml
45
+ header, body = parse_xml(raw_response)
46
+ else
47
+ header = nil
48
+ body = raw_response
49
+ end
50
+ response = new(header, body)
51
+ response.raw = raw_response
52
+ response
53
+ end
54
+
55
+ private
56
+ def parse_xml(response)
57
+ # FIXME: Use more fast XML parser
58
+ # Extract as a class
59
+ document = REXML::Document.new(response)
60
+ root_element = document.root
61
+ if root_element.name == "RESULT"
62
+ result_element = root_element
63
+ header = parse_xml_header(result_element)
64
+ body = parse_xml_body(result_element.elements[1])
65
+ else
66
+ header = nil
67
+ body = parse_xml_body(root_element)
68
+ end
69
+ [header, body]
70
+ end
71
+
72
+ def parse_xml_header(result_element)
73
+ attributes = result_element.attributes
74
+ code = Integer(attributes["CODE"])
75
+ up = Float(attributes["UP"])
76
+ elapsed = Float(attributes["ELAPSED"])
77
+ [code, up, elapsed]
78
+ end
79
+
80
+ def parse_xml_body(body_element)
81
+ xml_to_ruby(body_element)
82
+ end
83
+
84
+ def xml_to_ruby(element)
85
+ elements = element.elements
86
+ if elements.empty?
87
+ case element.name
88
+ when "NULL"
89
+ nil
90
+ when "INT"
91
+ Integer(element.text)
92
+ else
93
+ element.text
94
+ end
95
+ else
96
+ elements.collect do |child|
97
+ xml_to_ruby(child)
98
+ end
99
+ end
100
+ end
101
+ end
102
+
103
+ attr_accessor :header, :body
104
+ attr_accessor :raw
105
+
106
+ def initialize(header, body)
107
+ @header = header
108
+ @body = body
109
+ @raw = nil
110
+ end
111
+ end
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,30 @@
1
+ # -*- 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
+ require "groonga/client/response/base"
20
+
21
+ module Groonga
22
+ class Client
23
+ module Response
24
+ class CacheLimit < Base
25
+ Response.register("cache_limit", self)
26
+ end
27
+ end
28
+ end
29
+ end
30
+
@@ -0,0 +1,29 @@
1
+ # -*- 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
+ require "groonga/client/response/base"
20
+
21
+ module Groonga
22
+ class Client
23
+ module Response
24
+ class Check < Base
25
+ Response.register("check", self)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,30 @@
1
+ # -*- 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
+ require "groonga/client/response/base"
20
+
21
+ module Groonga
22
+ class Client
23
+ module Response
24
+ class ClearLock < Base
25
+ Response.register("clearlock", self)
26
+ end
27
+ end
28
+ end
29
+ end
30
+
@@ -0,0 +1,30 @@
1
+ # -*- 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
+ require "groonga/client/response/base"
20
+
21
+ module Groonga
22
+ class Client
23
+ module Response
24
+ class ColumnCreate < Base
25
+ Response.register("column_create", self)
26
+ end
27
+ end
28
+ end
29
+ end
30
+
@@ -0,0 +1,56 @@
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/response/base"
21
+
22
+ module Groonga
23
+ class Client
24
+ module Response
25
+ class ColumnList < Base
26
+ Response.register("column_list", self)
27
+
28
+ def initialize(header, body)
29
+ super(header, parse_body(body))
30
+ end
31
+
32
+ def parse_body(body)
33
+ properties = body.first
34
+ infos = body[1..-1]
35
+ infos.collect do |info|
36
+ column = Column.new
37
+ properties.each_with_index do |(name, _), i|
38
+ column.send("#{name}=", info[i])
39
+ end
40
+ column
41
+ end
42
+ end
43
+
44
+ class Column < Struct.new(:id,
45
+ :name,
46
+ :path,
47
+ :type,
48
+ :flags,
49
+ :domain,
50
+ :range,
51
+ :source)
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,30 @@
1
+ # -*- 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
+ require "groonga/client/response/base"
20
+
21
+ module Groonga
22
+ class Client
23
+ module Response
24
+ class ColumnRemove < Base
25
+ Response.register("column_remove", self)
26
+ end
27
+ end
28
+ end
29
+ end
30
+
@@ -0,0 +1,30 @@
1
+ # -*- 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
+ require "groonga/client/response/base"
20
+
21
+ module Groonga
22
+ class Client
23
+ module Response
24
+ class ColumnRename < Base
25
+ Response.register("column_rename", self)
26
+ end
27
+ end
28
+ end
29
+ end
30
+