4 error "Got exception: #{e.pretty_inspect}"
5 error "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"
9 if BDB::VERSION_MAJOR < 4
10 fatal "Your bdb (Berkeley DB) version #{BDB::VERSION} is too old!"
11 fatal "rbot will only run with bdb version 4 or higher, please upgrade."
12 fatal "For maximum reliability, upgrade to version 4.2 or higher."
13 raise BDB::Fatal, BDB::VERSION + " is too old"
16 if BDB::VERSION_MAJOR == 4 and BDB::VERSION_MINOR < 2
17 warning "Your bdb (Berkeley DB) version #{BDB::VERSION} may not be reliable."
18 warning "If possible, try upgrade version 4.2 or later."
21 # make BTree lookups case insensitive
24 def bdb_bt_compare(a, b)
25 if a == nil || b == nil
26 warning "CIBTree: comparing #{a.inspect} (#{self[a].inspect}) with #{b.inspect} (#{self[b].inspect})"
28 (a||'').downcase <=> (b||'').downcase
35 # DBHash is for tying a hash to disk (using bdb).
36 # Call it with an identifier, for example "mydata". It'll look for
37 # mydata.db, if it exists, it will load and reference that db.
38 # Otherwise it'll create and empty db called mydata.db
41 # absfilename:: use +key+ as an actual filename, don't prepend the bot's
42 # config path and don't append ".db"
43 def initialize(bot, key, absfilename=false)
46 if absfilename && File.exist?(key)
47 # db already exists, use it
48 @db = DBHash.open_db(key)
49 elsif File.exist?(@bot.botclass + "/#{key}.db")
50 # db already exists, use it
51 @db = DBHash.open_db(@bot.botclass + "/#{key}.db")
54 @db = DBHash.create_db(key)
57 @db = DBHash.create_db(@bot.botclass + "/#{key}.db")
61 def method_missing(method, *args, &block)
62 return @db.send(method, *args, &block)
65 def DBHash.create_db(name)
66 debug "DBHash: creating empty db #{name}"
67 return BDB::Hash.open(name, nil,
68 BDB::CREATE | BDB::EXCL, 0600)
71 def DBHash.open_db(name)
72 debug "DBHash: opening existing db #{name}"
73 return BDB::Hash.open(name, nil, "r+", 0600)
79 # DBTree is a BTree equivalent of DBHash, with case insensitive lookups.
82 # TODO: make this customizable
83 # Note that it must be at least four times lg_bsize
84 @@lg_max = 8*1024*1024
85 # absfilename:: use +key+ as an actual filename, don't prepend the bot's
86 # config path and don't append ".db"
87 def initialize(bot, key, absfilename=false)
92 @@env = BDB::Env.open("#{@bot.botclass}", BDB::INIT_TRANSACTION | BDB::CREATE | BDB::RECOVER, "set_lg_max" => @@lg_max)
93 debug "DBTree: environment opened with max log size #{@@env.conf['lg_max']}"
95 debug "DBTree: failed to open environment: #{e.pretty_inspect}. Retrying ..."
96 @@env = BDB::Env.open("#{@bot.botclass}", BDB::INIT_TRANSACTION | BDB::CREATE | BDB::RECOVER)
98 #@@env = BDB::Env.open("#{@bot.botclass}", BDB::CREATE | BDB::INIT_MPOOL | BDB::RECOVER)
101 if absfilename && File.exist?(key)
102 # db already exists, use it
103 @db = DBTree.open_db(key)
106 @db = DBTree.create_db(key)
107 elsif File.exist?(@bot.botclass + "/#{key}.db")
108 # db already exists, use it
109 @db = DBTree.open_db(@bot.botclass + "/#{key}.db")
112 @db = DBTree.create_db(@bot.botclass + "/#{key}.db")
116 def method_missing(method, *args, &block)
117 return @db.send(method, *args, &block)
120 def DBTree.create_db(name)
121 debug "DBTree: creating empty db #{name}"
122 return @@env.open_db(BDB::CIBtree, name, nil, BDB::CREATE | BDB::EXCL, 0600)
125 def DBTree.open_db(name)
126 debug "DBTree: opening existing db #{name}"
127 return @@env.open_db(BDB::CIBtree, name, nil, "r+", 0600)
130 def DBTree.cleanup_logs()
132 debug "DBTree: checkpointing ..."
134 rescue Excpetion => e
135 debug "Failed: #{e.pretty_inspect}"
138 debug "DBTree: flushing log ..."
140 logs = @@env.log_archive(BDB::ARCH_ABS)
141 debug "DBTree: deleting archivable logs: #{logs.join(', ')}."
145 rescue Exception => e
146 debug "Failed: #{e.pretty_inspect}"
152 debug "General stats:"
155 debug @@env.lock_stat
161 debug "Couldn't dump stats"
165 def DBTree.cleanup_env()
167 debug "DBTree: checking transactions ..."
168 has_active_txn = @@env.txn_stat["st_nactive"] > 0
170 warning "DBTree: not all transactions completed!"
173 debug "DBTree: closing environment #{@@env}"
178 debug "DBTree: keeping file because of incomplete transactions"
180 debug "DBTree: cleaning up environment in #{path}"
181 BDB::Env.remove("#{path}")
183 rescue Exception => e
184 error "failed to clean up environment: #{e.pretty_inspect}"