class Groonga::Client

Constants

VERSION

Public Class Methods

default_options() click to toggle source

@return [Hash] The default options for {Groonga::Client.new}.

@since 0.2.0

# File lib/groonga/client.rb, line 38
def default_options
  @@deafult_options
end
default_options=(options) click to toggle source

@param options [Hash] The new default options for

{Groonga::Client.new}.

@since 0.2.0

# File lib/groonga/client.rb, line 46
def default_options=(options)
  @@deafult_options = options
end
new(options={}) click to toggle source

@macro initialize_options

# File lib/groonga/client.rb, line 92
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)
  when "file"
    @connection = Groonga::Client::Protocol::File.new(url, options)
  else
    message = "unsupported scheme: <#{url.scheme}>: "
    message << "supported: [gqtp, http, https, file]"
    raise ArgumentError, message
  end
end
open(options={}) { |client| ... } click to toggle source

@!macro [new] initialize_options

@param options [Hash] The options.
@option options [String, URI::Generic, URI::HTTP, URI::HTTPS]
  :url The URL of Groonga server.
@option options [:gqtp, :http, :https] :protocol The
  protocol that is used by the client.
@option options [String] :user User ID. Currently used for HTTP
  Basic Authentication.
@option options [String] :password Password. Currently used for
  HTTP Basic Authentication.

@overload open(options={})

Opens a new client connection.

@macro initialize_options
@return [Client] The opened client.

@overload open(options={}) {|client| }

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

@macro initialize_options
@yield [client] Gives a opened client to the block. The opened
  client is closed automatically when the block is finished.
@yieldparam client [Client] The opened client.
@yieldreturn [Object] Any object.
@return [Object] Any object that is returned by the block.
# File lib/groonga/client.rb, line 77
def open(options={}, &block)
  client = new(options)
  if block_given?
    begin
      yield(client)
    ensure
      client.close
    end
  else
    client
  end
end

Public Instance Methods

close() { || ... } click to toggle source

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

@overload close

Closes synchronously.

@return [Boolean] true when the opened connection is closed.
   false when there is no connection.

@overload close {}

Closes asynchronously.

@yield [] Calls the block when the opened connection is closed.
@return [#wait] The request object. If you want to wait until
   the request is processed. You can send #wait message to the
   request.
# File lib/groonga/client.rb, line 131
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) click to toggle source
# File lib/groonga/client.rb, line 171
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) click to toggle source
# File lib/groonga/client.rb, line 153
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
method_missing(name, *args, &block) click to toggle source
Calls superclass method
# File lib/groonga/client.rb, line 205
def method_missing(name, *args, &block)
  if groonga_command_name?(name) and args.size <= 1
    execute(name, *args, &block)
  else
    super
  end
end
request(name) click to toggle source
# File lib/groonga/client.rb, line 183
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) click to toggle source
Calls superclass method
# File lib/groonga/client.rb, line 213
def respond_to_missing?(name, include_private)
  if groonga_command_name?(name)
    true
  else
    super
  end
end

Private Instance Methods

build_url(options) click to toggle source
# File lib/groonga/client.rb, line 222
def build_url(options)
  scheme = (options.delete(:protocol) || "gqtp").to_s
  host = options.delete(:host) || options.delete(:address) || "127.0.0.1"
  port = options.delete(:port) || default_port(scheme)
  path = options.delete(:path) || default_path(scheme)
  user = options.delete(:user)
  password = options.delete(:password)
  if user and password
    userinfo = "#{user}:#{password}"
  else
    userinfo = nil
  end

  arguments = [
    scheme,
    userinfo,
    host,
    port,
    nil,
    path,
    nil,
    nil,
    nil,
  ]
  case scheme
  when "http"
    URI::HTTP.new(*arguments)
  when "https"
    URI::HTTPS.new(*arguments)
  else
    URI::Generic.new(*arguments)
  end
end
default_path(scheme) click to toggle source
# File lib/groonga/client.rb, line 267
def default_path(scheme)
  case scheme
  when "http", "https"
    "/d/"
  else
    nil
  end
end
default_port(scheme) click to toggle source
# File lib/groonga/client.rb, line 256
def default_port(scheme)
  case scheme
  when "gqtp"
    10043
  when "http", "https"
    10041
  else
    nil
  end
end
execute_command(command, &block) click to toggle source
# File lib/groonga/client.rb, line 292
def execute_command(command, &block)
  Client::Command.new(command).execute(@connection, &block)
end
groonga_command_name?(name) click to toggle source
# File lib/groonga/client.rb, line 276
def groonga_command_name?(name)
  /\A[a-zA-Z][a-zA-Z\d_]+\z/ === name.to_s
end
normalize_parameters(parameters) click to toggle source
# File lib/groonga/client.rb, line 280
def normalize_parameters(parameters)
  normalized_parameters = {}
  parameters.each do |key, value|
    if defined?(Arrow) and value.is_a?(Arrow::Table)
      normalized_parameters[key] = value
    else
      normalized_parameters[key] = value.to_s
    end
  end
  normalized_parameters
end