Class: ActiveRecord::ConnectionAdapters::TokyoTyrantAdapter

Inherits:
AbstractTokyoCabinetAdapter show all
Defined in:
lib/active_record/connection_adapters/tokyotyrant_adapter.rb

Instance Method Summary collapse

Methods inherited from AbstractTokyoCabinetAdapter

#delete_sql, #insert_sql, #select, #supports_count_distinct?, #update_sql

Constructor Details

#initialize(connection, logger, config) ⇒ TokyoTyrantAdapter

Returns a new instance of TokyoTyrantAdapter.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/active_record/connection_adapters/tokyotyrant_adapter.rb', line 17

def initialize(connection, logger, config)
  super(connection, logger, TokyoTyrant::RDBQRY)
  @database = {}

  config.fetch(:database).map do |table_name, attribute|
    attribute.keys.each do |k|
      attribute[k.to_s] = attribute[k]
    end

    @database[table_name.to_s] = {
      :host => attribute.fetch('host').to_s,
      :port => attribute.fetch('port', 0).to_i,
      :timeout => attribute.fetch('timeout', 0).to_i,
    }
  end
end

Instance Method Details

#disconnect!Object



38
39
40
41
42
43
44
45
46
# File 'lib/active_record/connection_adapters/tokyotyrant_adapter.rb', line 38

def disconnect!
  super

  @connection.keys.each do |table_name|
    conn = @connection[table_name]
    conn.close
    @connection.delete(table_name)
  end
end

#setindex(table_name, name, type) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/active_record/connection_adapters/tokyotyrant_adapter.rb', line 105

def setindex(table_name, name, type)
  type = {
    :lexical => TokyoTyrant::RDBTBL::ITLEXICAL,
    :decimal => TokyoTyrant::RDBTBL::ITDECIMAL,
    :token   => TokyoTyrant::RDBTBL::ITTOKEN,
    :qgram   => TokyoTyrant::RDBTBL::ITQGRAM,
    :opt     => TokyoTyrant::RDBTBL::ITOPT,
    :void    => TokyoTyrant::RDBTBL::ITVOID,
    :keep    => TokyoTyrant::RDBTBL::ITKEEP,
  }.fetch(type)

  name = name.to_s

  unless (tdb = @connection[table_name])
    host, port, timeout = @database.fetch(table_name).values_at(:host, :port, :timeout)
    tdb = TokyoTyrant::RDBTBL::new

    unless tdb.open(host, port, timeout)
      ecode = tdb.ecode
      raise "%s: %s:%s" % [tdb.errmsg(ecode), host, port]
    end
  end

  begin
    unless tdb.setindex(name, type)
      ecode = tdb.ecode
      raise "%s: %s:%s" % [tdb.errmsg(ecode), host, port]
    end
  ensure
    unless tdb.close
      ecode = tdb.ecode
      raise tdb.errmsg(ecode)
    end
  end
end

#table_exists?(table_name) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/active_record/connection_adapters/tokyotyrant_adapter.rb', line 34

def table_exists?(table_name)
  @database.has_key?(table_name)
end

#tdbopen(table_name, readonly = false) {|tdb| ... } ⇒ Object

Yields:

  • (tdb)


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/active_record/connection_adapters/tokyotyrant_adapter.rb', line 48

def tdbopen(table_name, readonly = false)
  unless table_exists?(table_name)
    raise "Table does not exist: #{table_name}"
  end

  unless (tdb = @connection[table_name])
    host, port, timeout = @database.fetch(table_name).values_at(:host, :port, :timeout)
    tdb = TokyoTyrant::RDBTBL::new

    unless tdb.open(host, port, timeout)
      ecode = tdb.ecode
      raise "%s: %s:%s" % [tdb.errmsg(ecode), host, port]
    end
  end

  yield(tdb)
  @connection[table_name] = tdb
end