Class: Irc::DBTree

Inherits:
Object show all
Defined in:
lib/rbot/registry/tc.rb,
lib/rbot/registry/bdb.rb

Overview

DBTree is a BTree equivalent of DBHash, with case insensitive lookups.

Constant Summary collapse

@@env =
nil
@@lg_max =

TODO: make this customizable Note that it must be at least four times lg_bsize

8*1024*1024

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bot, key, absfilename = false) ⇒ DBTree

absfilename

use key as an actual filename, don’t prepend the bot’s config path and don’t append “.db”



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
140
# File 'lib/rbot/registry/tc.rb', line 107

def initialize(bot, key, absfilename=false)
  @bot = bot
  @key = key

  relfilename = @bot.path key
  relfilename << '.tdb'

  if absfilename && File.exist?(key)
    # db already exists, use it
    @db = DBTree.open_db(key)
  elsif absfilename
    # create empty db
    @db = DBTree.create_db(key)
  elsif File.exist? relfilename
    # db already exists, use it
    @db = DBTree.open_db relfilename
  else
    # create empty db
    @db = DBTree.create_db relfilename
  end
  oldbasename = (absfilename ? key : relfilename).gsub(/\.tdb$/, ".db")
  if File.exists? oldbasename and defined? BDB
    # upgrading
    warning "Upgrading old database #{oldbasename}..."
    oldb = ::BDB::Btree.open(oldbasename, nil, "r", 0600)
    oldb.each_key do |k|
      @db.outlist k
      @db.putlist k, (oldb.duplicates(k, false))
    end
    oldb.close
    File.rename oldbasename, oldbasename+".bak"
  end
  @db
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



142
143
144
# File 'lib/rbot/registry/tc.rb', line 142

def method_missing(method, *args, &block)
  return @db.send(method, *args, &block)
end

Class Method Details

.cleanup_envObject



170
171
172
# File 'lib/rbot/registry/tc.rb', line 170

def DBTree.cleanup_env()
  # no-op
end

.cleanup_logsObject



162
163
164
# File 'lib/rbot/registry/tc.rb', line 162

def DBTree.cleanup_logs()
  # no-op
end

.create_db(name) ⇒ Object



146
147
148
149
150
151
152
# File 'lib/rbot/registry/tc.rb', line 146

def DBTree.create_db(name)
  debug "DBTree: creating empty db #{name}"
  db = TokyoCabinet::CIBDB.new
  res = db.open(name, TokyoCabinet::CIBDB::OREADER | TokyoCabinet::CIBDB::OCREAT | TokyoCabinet::CIBDB::OWRITER)
   warning "DBTree: creating empty db #{name}: #{db.errmsg(db.ecode) unless res}"
  return db
end

.open_db(name) ⇒ Object



154
155
156
157
158
159
160
# File 'lib/rbot/registry/tc.rb', line 154

def DBTree.open_db(name)
  debug "DBTree: opening existing db #{name}"
  db = TokyoCabinet::CIBDB.new
  res = db.open(name, TokyoCabinet::CIBDB::OREADER | TokyoCabinet::CIBDB::OWRITER)
   warning "DBTree:opening db #{name}: #{db.errmsg(db.ecode) unless res}"
  return db
end

.statsObject



166
167
168
# File 'lib/rbot/registry/tc.rb', line 166

def DBTree.stats()
  # no-op
end