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 relfilename = @bot.path key
57 if absfilename && File.exist?(key)
58 # db already exists, use it
59 @db = DBHash.open_db(key)
62 @db = DBHash.create_db(key)
63 elsif File.exist? relfilename
64 # db already exists, use it
65 @db = DBHash.open_db relfilename
68 @db = DBHash.create_db relfilename
72 def method_missing(method, *args, &block)
73 return @db.send(method, *args, &block)
76 def DBHash.create_db(name)
77 debug "DBHash: creating empty db #{name}"
78 return BDB::Hash.open(name, nil,
79 BDB::CREATE | BDB::EXCL, 0600)
82 def DBHash.open_db(name)
83 debug "DBHash: opening existing db #{name}"
84 return BDB::Hash.open(name, nil, "r+", 0600)
90 # DBTree is a BTree equivalent of DBHash, with case insensitive lookups.
93 # TODO: make this customizable
94 # Note that it must be at least four times lg_bsize
95 @@lg_max = 8*1024*1024
96 # absfilename:: use +key+ as an actual filename, don't prepend the bot's
97 # config path and don't append ".db"
98 def initialize(bot, key, absfilename=false)
103 @@env = BDB::Env.open(@bot.botclass, BDB::INIT_TRANSACTION | BDB::CREATE | BDB::RECOVER, "set_lg_max" => @@lg_max)
104 debug "DBTree: environment opened with max log size #{@@env.conf['lg_max']}"
106 debug "DBTree: failed to open environment: #{e.pretty_inspect}. Retrying ..."
107 @@env = BDB::Env.open(@bot.botclass, BDB::INIT_TRANSACTION | BDB::CREATE | BDB::RECOVER)
109 #@@env = BDB::Env.open(@bot.botclass, BDB::CREATE | BDB::INIT_MPOOL | BDB::RECOVER)
112 relfilename = @bot.path key
115 if absfilename && File.exist?(key)
116 # db already exists, use it
117 @db = DBTree.open_db(key)
120 @db = DBTree.create_db(key)
121 elsif File.exist? relfilename
122 # db already exists, use it
123 @db = DBTree.open_db relfilename
126 @db = DBTree.create_db relfilename
130 def method_missing(method, *args, &block)
131 return @db.send(method, *args, &block)
134 def DBTree.create_db(name)
135 debug "DBTree: creating empty db #{name}"
136 return @@env.open_db(BDB::CIBtree, name, nil, BDB::CREATE | BDB::EXCL, 0600)
139 def DBTree.open_db(name)
140 debug "DBTree: opening existing db #{name}"
141 return @@env.open_db(BDB::CIBtree, name, nil, "r+", 0600)
144 def DBTree.cleanup_logs()
146 debug "DBTree: checkpointing ..."
148 rescue Exception => e
149 debug "Failed: #{e.pretty_inspect}"
152 debug "DBTree: flushing log ..."
154 logs = @@env.log_archive(BDB::ARCH_ABS)
155 debug "DBTree: deleting archivable logs: #{logs.join(', ')}."
159 rescue Exception => e
160 debug "Failed: #{e.pretty_inspect}"
166 debug "General stats:"
169 debug @@env.lock_stat
175 debug "Couldn't dump stats"
179 def DBTree.cleanup_env()
181 debug "DBTree: checking transactions ..."
182 has_active_txn = @@env.txn_stat["st_nactive"] > 0
184 warning "DBTree: not all transactions completed!"
187 debug "DBTree: closing environment #{@@env}"
192 debug "DBTree: keeping file because of incomplete transactions"
194 debug "DBTree: cleaning up environment in #{path}"
195 BDB::Env.remove("#{path}")
197 rescue Exception => e
198 error "failed to clean up environment: #{e.pretty_inspect}"