4 # :title: Berkeley DB interface
9 fatal "rbot couldn't load the bdb module, perhaps you need to install it? try: http://www.ruby-lang.org/en/raa-list.rhtml?name=bdb"
11 fatal "rbot couldn't load the bdb module: #{e.pretty_inspect}"
18 if BDB::VERSION_MAJOR < 4
19 fatal "Your bdb (Berkeley DB) version #{BDB::VERSION} is too old!"
20 fatal "rbot will only run with bdb version 4 or higher, please upgrade."
21 fatal "For maximum reliability, upgrade to version 4.2 or higher."
22 raise BDB::Fatal, BDB::VERSION + " is too old"
25 if BDB::VERSION_MAJOR == 4 and BDB::VERSION_MINOR < 2
26 warning "Your bdb (Berkeley DB) version #{BDB::VERSION} may not be reliable."
27 warning "If possible, try upgrade version 4.2 or later."
30 # make BTree lookups case insensitive
33 def bdb_bt_compare(a, b)
34 if a == nil || b == nil
35 warning "CIBTree: comparing #{a.inspect} (#{self[a].inspect}) with #{b.inspect} (#{self[b].inspect})"
37 (a||'').downcase <=> (b||'').downcase
44 # DBHash is for tying a hash to disk (using bdb).
45 # Call it with an identifier, for example "mydata". It'll look for
46 # mydata.db, if it exists, it will load and reference that db.
47 # Otherwise it'll create and empty db called mydata.db
50 # absfilename:: use +key+ as an actual filename, don't prepend the bot's
51 # config path and don't append ".db"
52 def initialize(bot, key, absfilename=false)
55 if absfilename && File.exist?(key)
56 # db already exists, use it
57 @db = DBHash.open_db(key)
58 elsif File.exist?(@bot.botclass + "/#{key}.db")
59 # db already exists, use it
60 @db = DBHash.open_db(@bot.botclass + "/#{key}.db")
63 @db = DBHash.create_db(key)
66 @db = DBHash.create_db(@bot.botclass + "/#{key}.db")
70 def method_missing(method, *args, &block)
71 return @db.send(method, *args, &block)
74 def DBHash.create_db(name)
75 debug "DBHash: creating empty db #{name}"
76 return BDB::Hash.open(name, nil,
77 BDB::CREATE | BDB::EXCL, 0600)
80 def DBHash.open_db(name)
81 debug "DBHash: opening existing db #{name}"
82 return BDB::Hash.open(name, nil, "r+", 0600)
88 # DBTree is a BTree equivalent of DBHash, with case insensitive lookups.
91 # TODO: make this customizable
92 # Note that it must be at least four times lg_bsize
93 @@lg_max = 8*1024*1024
94 # absfilename:: use +key+ as an actual filename, don't prepend the bot's
95 # config path and don't append ".db"
96 def initialize(bot, key, absfilename=false)
101 @@env = BDB::Env.open("#{@bot.botclass}", BDB::INIT_TRANSACTION | BDB::CREATE | BDB::RECOVER, "set_lg_max" => @@lg_max)
102 debug "DBTree: environment opened with max log size #{@@env.conf['lg_max']}"
104 debug "DBTree: failed to open environment: #{e.pretty_inspect}. Retrying ..."
105 @@env = BDB::Env.open("#{@bot.botclass}", BDB::INIT_TRANSACTION | BDB::CREATE | BDB::RECOVER)
107 #@@env = BDB::Env.open("#{@bot.botclass}", BDB::CREATE | BDB::INIT_MPOOL | BDB::RECOVER)
110 if absfilename && File.exist?(key)
111 # db already exists, use it
112 @db = DBTree.open_db(key)
115 @db = DBTree.create_db(key)
116 elsif File.exist?(@bot.botclass + "/#{key}.db")
117 # db already exists, use it
118 @db = DBTree.open_db(@bot.botclass + "/#{key}.db")
121 @db = DBTree.create_db(@bot.botclass + "/#{key}.db")
125 def method_missing(method, *args, &block)
126 return @db.send(method, *args, &block)
129 def DBTree.create_db(name)
130 debug "DBTree: creating empty db #{name}"
131 return @@env.open_db(BDB::CIBtree, name, nil, BDB::CREATE | BDB::EXCL, 0600)
134 def DBTree.open_db(name)
135 debug "DBTree: opening existing db #{name}"
136 return @@env.open_db(BDB::CIBtree, name, nil, "r+", 0600)
139 def DBTree.cleanup_logs()
141 debug "DBTree: checkpointing ..."
143 rescue Exception => e
144 debug "Failed: #{e.pretty_inspect}"
147 debug "DBTree: flushing log ..."
149 logs = @@env.log_archive(BDB::ARCH_ABS)
150 debug "DBTree: deleting archivable logs: #{logs.join(', ')}."
154 rescue Exception => e
155 debug "Failed: #{e.pretty_inspect}"
161 debug "General stats:"
164 debug @@env.lock_stat
170 debug "Couldn't dump stats"
174 def DBTree.cleanup_env()
176 debug "DBTree: checking transactions ..."
177 has_active_txn = @@env.txn_stat["st_nactive"] > 0
179 warning "DBTree: not all transactions completed!"
182 debug "DBTree: closing environment #{@@env}"
187 debug "DBTree: keeping file because of incomplete transactions"
189 debug "DBTree: cleaning up environment in #{path}"
190 BDB::Env.remove("#{path}")
192 rescue Exception => e
193 error "failed to clean up environment: #{e.pretty_inspect}"