Class: Droonga::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/droonga/client.rb,
lib/droonga/client/error.rb,
lib/droonga/client/version.rb,
lib/droonga/client/rate-limiter.rb,
lib/droonga/client/connection/http.rb,
lib/droonga/client/connection/error.rb,
lib/droonga/client/message_completer.rb,
lib/droonga/client/message_validator.rb,
lib/droonga/client/connection/empty-request.rb,
lib/droonga/client/connection/droonga-protocol.rb,
lib/droonga/client/connection/droonga-protocol/coolio.rb,
lib/droonga/client/connection/droonga-protocol/thread.rb

Defined Under Namespace

Modules: Connection Classes: ConnectionError, Error, MessageCompleter, MessageValidator, RateLimiter

Constant Summary collapse

DEFAULT_PROTOCOL =
:droonga
DEFAULT_HOST =
Socket.gethostname
DEFAULT_PORT =
10031
DEFAULT_TAG =
"droonga"
DEFAULT_DATASET =
"Default"
DEFAULT_TARGET_ROLE =
"any"
DEFAULT_TIMEOUT_SECONDS =
3
VERSION =
"0.2.2"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Creates a new Droonga Engine client.

Parameters:

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

    Options to connect Droonga Engine.

Options Hash (options):

  • :tag (String) — default: "droonga"

    The tag of the request message.

  • :host (String) — default: "127.0.0.1"

    The host name or IP address of the Droonga Engine to be connected.

  • :port (Integer) — default: 24224

    The port number of the Droonga Engine to be connected.

  • :receiver_host (String) — default: Socket.gethostname

    The host name or IP address to receive response from the Droonga Engine.

  • :receiver_port (Integer) — default: 0

    The port number to receive response from the Droonga Engine.

  • :timeout (Integer) — default: 5

    The timeout value for connecting to, writing to and reading from Droonga Engine.

  • :completion (Boolean) — default: true

    Do or do not complete required fields of input messages.

  • :validation (Boolean) — default: true

    Do or do not validate input messages.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/droonga/client.rb', line 87

def initialize(options={})
  @protocol = options[:protocol] || DEFAULT_PROTOCOL
  @connection = create_connection(options)
  @connection.on_error = lambda do |error|
    on_error(ConnectionError.new(error))
  end

  @completion = options[:completion] != false
  @validation = options[:validation] != false

  @completer = MessageCompleter.new(:default_dataset => options[:default_dataset],
                                    :default_timeout => options[:default_timeout],
                                    :default_target_role => options[:default_target_role])
  @validator = MessageValidator.new
end

Instance Attribute Details

#on_error=(value) ⇒ Object

Sets the attribute on_error

Parameters:

  • value

    the value to set the attribute on_error to.



37
38
39
# File 'lib/droonga/client.rb', line 37

def on_error=(value)
  @on_error = value
end

#protocolObject (readonly)

Returns the value of attribute protocol.



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

def protocol
  @protocol
end

Class Method Details

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

Opens a new connection and yields a Droonga::Client object to use the connection. The client is closed after the given block is finished.

Parameters:

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

    Options to connect Droonga Engine.

Options Hash (options):

  • :tag (String) — default: "droonga"

    The tag of the request message.

  • :host (String) — default: "127.0.0.1"

    The host name or IP address of the Droonga Engine to be connected.

  • :port (Integer) — default: 24224

    The port number of the Droonga Engine to be connected.

  • :receiver_host (String) — default: Socket.gethostname

    The host name or IP address to receive response from the Droonga Engine.

  • :receiver_port (Integer) — default: 0

    The port number to receive response from the Droonga Engine.

  • :timeout (Integer) — default: 5

    The timeout value for connecting to, writing to and reading from Droonga Engine.

  • :completion (Boolean) — default: true

    Do or do not complete required fields of input messages.

  • :validation (Boolean) — default: true

    Do or do not validate input messages.

Yields:

  • (client)

    Gives the opened client. It is alive while yielding.

Yield Parameters:

  • client (Client)

    The opened client.

Returns:

  • The return value from the given block.



58
59
60
61
62
63
64
65
# File 'lib/droonga/client.rb', line 58

def open(options={})
  client = new(options)
  begin
    yield(client)
  ensure
    client.close
  end
end

Instance Method Details

#closevoid

This method returns an undefined value.

Close the connection used by the client. You can't send any request anymore.



125
126
127
# File 'lib/droonga/client.rb', line 125

def close
  @connection.close
end

#complete(message) ⇒ Object



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

def complete(message)
  case @protocol
  when :http
    http_request = @connection.build_request(message)
    http_headers = {}
    http_request.canonical_each do |name, value|
      http_headers[name] = value
    end
    {
      "method"  => http_request.method,
      "path"    => http_request.path,
      "headers" => http_headers,
      "body"    => http_request.body,
    }
  when :droonga
    do_completion(message, :completion => true)
  else
    nil
  end
end

#request(message, options = {}, &block) ⇒ Object



109
110
111
112
113
# File 'lib/droonga/client.rb', line 109

def request(message, options={}, &block)
  message = do_completion(message, options)
  do_validation(message, options)
  @connection.request(message, options, &block)
end

#send(message, options = {}, &block) ⇒ Object



103
104
105
106
107
# File 'lib/droonga/client.rb', line 103

def send(message, options={}, &block)
  message = do_completion(message, options)
  do_validation(message, options)
  @connection.send(message, options, &block)
end

#subscribe(message, options = {}, &block) ⇒ Object



115
116
117
118
119
# File 'lib/droonga/client.rb', line 115

def subscribe(message, options={}, &block)
  message = do_completion(message, options)
  do_validation(message, options)
  @connection.subscribe(message, options, &block)
end