Class: ActiveRecord::ConnectionAdapters::TokyoCabinetAdapter

Inherits:
AbstractTokyoCabinetAdapter show all
Defined in:
lib/active_record/connection_adapters/tokyocabinet_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) ⇒ TokyoCabinetAdapter

Returns a new instance of TokyoCabinetAdapter.



21
22
23
24
# File 'lib/active_record/connection_adapters/tokyocabinet_adapter.rb', line 21

def initialize(connection, logger, config)
  super(connection, logger, TokyoCabinet::TDBQRY)
  @config = config
end

Instance Method Details

#setindex(table_name, name, type) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/active_record/connection_adapters/tokyocabinet_adapter.rb', line 71

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

  name = name.to_s
  path = tdbpath(table_name)
  tdb = TokyoCabinet::TDB::new

  unless tdb.open(path, TokyoCabinet::TDB::OWRITER | TokyoCabinet::TDB::OCREAT)
    ecode = tdb.ecode
    raise "%s: %s" % [tdb.errmsg(ecode), path]
  end

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

#table_exists?(table_name) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
# File 'lib/active_record/connection_adapters/tokyocabinet_adapter.rb', line 26

def table_exists?(table_name)
  path = tdbpath(table_name)
  File.exist?(path)
end

#tdbopen(table_name, readonly = false) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/active_record/connection_adapters/tokyocabinet_adapter.rb', line 31

def tdbopen(table_name, readonly = false)
  path = tdbpath(table_name)

  if File.exist?(path) and readonly
    omode = TokyoCabinet::TDB::OREADER
  else
    omode = TokyoCabinet::TDB::OWRITER | TokyoCabinet::TDB::OCREAT
  end

  tdb = TokyoCabinet::TDB::new

  unless tdb.open(path, omode)
    ecode = tdb.ecode
    raise "%s: %s" % [tdb.errmsg(ecode), path]
  end

  begin
    yield(tdb)
  ensure
    unless tdb.close
      ecode = tdb.ecode
      raise tdb.errmsg(ecode)
    end
  end
end