Class: Groonga::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/groonga/client.rb,
lib/groonga/client/cli.rb,
lib/groonga/client/error.rb,
lib/groonga/client/command.rb,
lib/groonga/client/default.rb,
lib/groonga/client/request.rb,
lib/groonga/client/version.rb,
lib/groonga/client/spec-helper.rb,
lib/groonga/client/test-helper.rb,
lib/groonga/client/test/fixture.rb,
lib/groonga/client/empty-request.rb,
lib/groonga/client/protocol/gqtp.rb,
lib/groonga/client/protocol/http.rb,
lib/groonga/client/request/error.rb,
lib/groonga/client/response/base.rb,
lib/groonga/client/response/dump.rb,
lib/groonga/client/response/load.rb,
lib/groonga/client/response/quit.rb,
lib/groonga/client/script-syntax.rb,
lib/groonga/client/protocol/error.rb,
lib/groonga/client/request/select.rb,
lib/groonga/client/response/check.rb,
lib/groonga/client/response/error.rb,
lib/groonga/client/request/generic.rb,
lib/groonga/client/response/defrag.rb,
lib/groonga/client/response/delete.rb,
lib/groonga/client/response/schema.rb,
lib/groonga/client/response/select.rb,
lib/groonga/client/response/status.rb,
lib/groonga/client/response/log-put.rb,
lib/groonga/client/response/register.rb,
lib/groonga/client/response/log-level.rb,
lib/groonga/client/response/lock-clear.rb,
lib/groonga/client/response/log-reopen.rb,
lib/groonga/client/response/table-list.rb,
lib/groonga/client/protocol/http/coolio.rb,
lib/groonga/client/protocol/http/thread.rb,
lib/groonga/client/response/cache-limit.rb,
lib/groonga/client/response/column-list.rb,
lib/groonga/client/response/table-create.rb,
lib/groonga/client/response/table-remove.rb,
lib/groonga/client/response/column-create.rb,
lib/groonga/client/response/column-remove.rb,
lib/groonga/client/response/column-rename.rb,
lib/groonga/client/protocol/http/synchronous.rb,
lib/groonga/client/test/groonga-server-runner.rb,
lib/groonga/client/protocol/http/path-resolvable.rb

Defined Under Namespace

Modules: Default, Protocol, Request, Response, ScriptSyntax, SpecHelper, Test, TestHelper Classes: CLI, Command, EmptyRequest, Error

Constant Summary collapse

VERSION =
"0.5.1"
@@deafult_options =
{}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client

Options Hash (options):

  • :url (String, URI::Generic, URI::HTTP, URI::HTTPS)

    The URL of Groonga server.

  • :protocol (:gqtp, :http, :https)

    The protocol that is used by the client.

  • :user (String)

    User ID. Currently used for HTTP Basic Authentication.

  • :password (String)

    Password. Currently used for HTTP Basic Authentication.


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/groonga/client.rb', line 90

def initialize(options={})
  options = self.class.default_options.merge(options)
  url = options[:url] || build_url(options)
  url = URI.parse(url) unless url.is_a?(URI::Generic)
  options[:url] = url
  options[:read_timeout] ||= Default::READ_TIMEOUT

  @connection = nil
  case url.scheme
  when "gqtp"
    @connection = Groonga::Client::Protocol::GQTP.new(url, options)
  when "http", "https"
    @connection = Groonga::Client::Protocol::HTTP.new(url, options)
  else
    message = "unsupported scheme: <#{url.scheme}>: "
    message << "supported: [gqtp, http, https]"
    raise ArgumentError, message
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object


201
202
203
204
205
206
207
# File 'lib/groonga/client.rb', line 201

def method_missing(name, *args, &block)
  if groonga_command_name?(name)
    execute(name, *args, &block)
  else
    super
  end
end

Class Method Details

.default_optionsHash

Returns The default options for new.

Since:

  • 0.2.0


36
37
38
# File 'lib/groonga/client.rb', line 36

def default_options
  @@deafult_options
end

.default_options=(options) ⇒ Object

Since:

  • 0.2.0


44
45
46
# File 'lib/groonga/client.rb', line 44

def default_options=(options)
  @@deafult_options = options
end

.open(options = {}) ⇒ Client .open(options = {}) {|client| ... } ⇒ Object

Overloads:

  • .open(options = {}) ⇒ Client

    Opens a new client connection.

    Options Hash (options):

    • :url (String, URI::Generic, URI::HTTP, URI::HTTPS)

      The URL of Groonga server.

    • :protocol (:gqtp, :http, :https)

      The protocol that is used by the client.

    • :user (String)

      User ID. Currently used for HTTP Basic Authentication.

    • :password (String)

      Password. Currently used for HTTP Basic Authentication.

  • .open(options = {}) {|client| ... } ⇒ Object

    Opens a new client connection while the block is evaluated. The block is finished the opened client is closed.

    Options Hash (options):

    • :url (String, URI::Generic, URI::HTTP, URI::HTTPS)

      The URL of Groonga server.

    • :protocol (:gqtp, :http, :https)

      The protocol that is used by the client.

    • :user (String)

      User ID. Currently used for HTTP Basic Authentication.

    • :password (String)

      Password. Currently used for HTTP Basic Authentication.

    Yields:

    • (client)

      Gives a opened client to the block. The opened client is closed automatically when the block is finished.

    Yield Parameters:

    • client (Client)

      The opened client.

    Yield Returns:

    • (Object)

      Any object.


75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/groonga/client.rb', line 75

def open(options={}, &block)
  client = new(options)
  if block_given?
    begin
      yield(client)
    ensure
      client.close
    end
  else
    client
  end
end

Instance Method Details

#closeBoolean #close({}) { ... } ⇒ #wait

Closes the opened client connection if the current connection is still opened. You can't send a new command after you call this method.

Overloads:

  • #closeBoolean

    Closes synchronously.

  • #close({}) { ... } ⇒ #wait

    Closes asynchronously.

    Yields:

    • [] Calls the block when the opened connection is closed.


127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/groonga/client.rb', line 127

def close(&block)
  sync = !block_given?
  if @connection
    close_request = @connection.close do
      yield unless sync
      @connection = nil
    end
    if sync
      close_request.wait
      true
    else
      close_request
    end
  else
    if sync
      false
    else
      EmptyRequest.new
    end
  end
end

#execute(command_or_name, parameters = {}, &block) ⇒ Object


167
168
169
170
171
172
173
174
175
176
177
# File 'lib/groonga/client.rb', line 167

def execute(command_or_name, parameters={}, &block)
  if command_or_name.is_a?(Groonga::Command::Base)
    command = command_or_name
  else
    command_name = command_or_name
    parameters = normalize_parameters(parameters)
    command_class = Groonga::Command.find(command_name)
    command = command_class.new(command_name, parameters)
  end
  execute_command(command, &block)
end

#load(parameters, &block) ⇒ Object


149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/groonga/client.rb', line 149

def load(parameters, &block)
  values = parameters[:values]
  if values.is_a?(Array)
    json = "["
    values.each_with_index do |value, i|
      if i.zero?
        json << "\n"
      else
        json << ",\n"
      end
      json << JSON.generate(value)
    end
    json << "\n]"
    parameters[:values] = json
  end
  execute(:load, parameters, &block)
end

#request(name) ⇒ Object


179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/groonga/client.rb', line 179

def request(name)
  command_name_module = Module.new do
    define_method :command_name do
      name
    end
  end

  client = self
  open_client_module = Module.new do
    define_method :open_client do |&block|
      block.call(client)
    end
  end

  extensions = [
    command_name_module,
    open_client_module,
  ]
  request_class = Request.find(name)
  request_class.new(nil, extensions)
end

#respond_to_missing?(name, include_private) ⇒ Boolean


209
210
211
212
213
214
215
# File 'lib/groonga/client.rb', line 209

def respond_to_missing?(name, include_private)
  if groonga_command_name?(name)
    true
  else
    super
  end
end