Class: Groonga::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/groonga/client.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/file.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/command-processor.rb,
lib/groonga/client/response/register.rb,
lib/groonga/client/response/log-level.rb,
lib/groonga/client/command-line/parser.rb,
lib/groonga/client/command-line/runner.rb,
lib/groonga/client/response/lock-clear.rb,
lib/groonga/client/response/log-reopen.rb,
lib/groonga/client/response/searchable.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/response/drilldownable.rb,
lib/groonga/client/response/logical-select.rb,
lib/groonga/client/protocol/http/synchronous.rb,
lib/groonga/client/test/groonga-server-runner.rb,
lib/groonga/client/command-line/groonga-client.rb,
lib/groonga/client/protocol/http/path-resolvable.rb,
lib/groonga/client/response/logical-range-filter.rb,
lib/groonga/client/command-line/groonga-client-index-check.rb,
lib/groonga/client/command-line/groonga-client-index-recreate.rb

Defined Under Namespace

Modules: CommandLine, Default, Protocol, Request, Response, ScriptSyntax, SpecHelper, Test, TestHelper Classes: Command, CommandProcessor, EmptyRequest, Error, ErrorResponse, InvalidResponse

Constant Summary collapse

VERSION =
"0.7.1"
@@deafult_options =
{}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.

Parameters:

  • options (Hash) (defaults to: {})

    The options.

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.



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



205
206
207
208
209
210
211
# 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

Class Method Details

.default_optionsHash

Returns The default options for new.

Returns:

  • (Hash)

    The default options for new.

Since:

  • 0.2.0



38
39
40
# File 'lib/groonga/client.rb', line 38

def default_options
  @@deafult_options
end

.default_options=(options) ⇒ Object

Parameters:

  • options (Hash)

    The new default options for new.

Since:

  • 0.2.0



46
47
48
# File 'lib/groonga/client.rb', line 46

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

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

Overloads:

  • .open(options = {}) ⇒ Client

    Opens a new client connection.

    Parameters:

    • options (Hash) (defaults to: {})

      The options.

    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.

    Returns:

    • (Client)

      The opened client.

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

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

    Parameters:

    • options (Hash) (defaults to: {})

      The options.

    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.

    Returns:

    • (Object)

      Any object that is returned by the block.



77
78
79
80
81
82
83
84
85
86
87
88
# 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

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.

    Returns:

    • (Boolean)

      true when the opened connection is closed. false when there is no connection.

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

    Closes asynchronously.

    Yields:

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

    Returns:

    • (#wait)

      The request object. If you want to wait until the request is processed. You can send #wait message to the request.



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# 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) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
# 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) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# 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

#request(name) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# 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) ⇒ Boolean

Returns:

  • (Boolean)


213
214
215
216
217
218
219
# File 'lib/groonga/client.rb', line 213

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