Class: Oedipus::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/oedipus/connection.rb,
lib/oedipus/connection/pool.rb,
lib/oedipus/connection/registry.rb

Overview

Provides an interface for talking to SphinxQL.

Currently this class wraps a native mysql extension.

Defined Under Namespace

Modules: Registry Classes: Pool

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Connection

Instantiate a new Connection to a SphinxQL host.

The connection will be established on initialization.

The underlying implementation uses a thread-safe connection pool.

Parameters:

  • server (String)

    a ‘hostname:port’ string

  • options (Hash)

    a Hash containing :host and :port



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/oedipus/connection.rb', line 28

def initialize(options)
  @options =
    if options.kind_of?(String)
      Hash[ [:host, :port].zip(options.split(":")) ]
    else
      options.dup
    end.tap { |o| o[:port] = o[:port].to_i }

  @pool = Pool.new(
    host: @options[:host],
    port: @options[:port],
    size: @options.fetch(:pool_size, 8),
    ttl:  60
  )

  assert_valid_pool unless @options[:verify] == false
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



15
16
17
# File 'lib/oedipus/connection.rb', line 15

def options
  @options
end

Instance Method Details

#[](index_name) ⇒ Index Also known as: index

Acess a specific index for querying.

Parameters:

  • index_name (String)

    the name of an existing index in Sphinx

Returns:

  • (Index)

    an index that can be queried



53
54
55
# File 'lib/oedipus/connection.rb', line 53

def [](index_name)
  Index.new(index_name, self)
end

#closeObject

Disconnect from the remote host.

There is no need to explicitly re-connect after invoking this; connections are re-established as needed.



114
115
116
# File 'lib/oedipus/connection.rb', line 114

def close
  @pool.dispose
end

#execute(sql, *bind_values) ⇒ Fixnum

Execute a non-read query.

Note that SphinxQL does not support prepared statements.

Parameters:

  • sql (String)

    a SphinxQL query, such as INSERT or REPLACE

  • bind_values (Object...)

    values to be substituted in place of ‘?’ in the query

Returns:

  • (Fixnum)

    the number of affected rows



106
107
108
# File 'lib/oedipus/connection.rb', line 106

def execute(sql, *bind_values)
  @pool.acquire { |conn| conn.execute(sql, *bind_values) }
end

#multi_query(sql, *bind_values) ⇒ Array

Execute one or more queries in a batch.

Queries should be separated by semicolons. Results are returned in a 2-dimensional array.

Note that SphinxQL does not support prepared statements.

Parameters:

  • sql (String)

    one or more SphinxQL statements, separated by semicolons

  • bind_values (Object...)

    values to be substituted in place of ‘?’ in the query

Returns:

  • (Array)

    an array of arrays, containing the returned records



74
75
76
# File 'lib/oedipus/connection.rb', line 74

def multi_query(sql, *bind_values)
  @pool.acquire { |conn| conn.query(sql, *bind_values) }
end

#query(sql, *bind_values) ⇒ Array

Execute a single read query.

Note that SphinxQL does not support prepared statements.

Parameters:

  • sql (String)

    a single SphinxQL statement

  • bind_values (Object...)

    values to be substituted in place of ‘?’ in the query

Returns:

  • (Array)

    an array of Hashes containing the matched records



90
91
92
# File 'lib/oedipus/connection.rb', line 90

def query(sql, *bind_values)
  @pool.acquire { |conn| conn.query(sql, *bind_values).first }
end