4 error "Got exception: "+e
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 # make BTree lookups case insensitive
12 def bdb_bt_compare(a, b)
13 if a == nil || b == nil
14 warning "CIBTree: comparing #{a.inspect} (#{self[a].inspect}) with #{b.inspect} (#{self[b].inspect})"
16 (a||'').downcase <=> (b||'').downcase
23 # DBHash is for tying a hash to disk (using bdb).
24 # Call it with an identifier, for example "mydata". It'll look for
25 # mydata.db, if it exists, it will load and reference that db.
26 # Otherwise it'll create and empty db called mydata.db
29 # absfilename:: use +key+ as an actual filename, don't prepend the bot's
30 # config path and don't append ".db"
31 def initialize(bot, key, absfilename=false)
34 if absfilename && File.exist?(key)
35 # db already exists, use it
36 @db = DBHash.open_db(key)
37 elsif File.exist?(@bot.botclass + "/#{key}.db")
38 # db already exists, use it
39 @db = DBHash.open_db(@bot.botclass + "/#{key}.db")
42 @db = DBHash.create_db(key)
45 @db = DBHash.create_db(@bot.botclass + "/#{key}.db")
49 def method_missing(method, *args, &block)
50 return @db.send(method, *args, &block)
53 def DBHash.create_db(name)
54 debug "DBHash: creating empty db #{name}"
55 return BDB::Hash.open(name, nil,
56 BDB::CREATE | BDB::EXCL, 0600)
59 def DBHash.open_db(name)
60 debug "DBHash: opening existing db #{name}"
61 return BDB::Hash.open(name, nil, "r+", 0600)
67 # DBTree is a BTree equivalent of DBHash, with case insensitive lookups.
70 # TODO: make this customizable
71 # Note that it must be at least four times lg_bsize
72 @@lg_max = 8*1024*1024
73 # absfilename:: use +key+ as an actual filename, don't prepend the bot's
74 # config path and don't append ".db"
75 def initialize(bot, key, absfilename=false)
80 @@env = BDB::Env.open("#{@bot.botclass}", BDB::INIT_TRANSACTION | BDB::CREATE | BDB::RECOVER, "set_lg_max" => @@lg_max)
81 debug "DBTree: environment opened with max log size #{@@env.conf['lg_max']}"
83 debug "DBTree: failed to open environment: #{e}. Retrying ..."
84 @@env = BDB::Env.open("#{@bot.botclass}", BDB::INIT_TRANSACTION | BDB::CREATE | BDB::RECOVER)
86 #@@env = BDB::Env.open("#{@bot.botclass}", BDB::CREATE | BDB::INIT_MPOOL | BDB::RECOVER)
89 if absfilename && File.exist?(key)
90 # db already exists, use it
91 @db = DBTree.open_db(key)
94 @db = DBTree.create_db(key)
95 elsif File.exist?(@bot.botclass + "/#{key}.db")
96 # db already exists, use it
97 @db = DBTree.open_db(@bot.botclass + "/#{key}.db")
100 @db = DBTree.create_db(@bot.botclass + "/#{key}.db")
104 def method_missing(method, *args, &block)
105 return @db.send(method, *args, &block)
108 def DBTree.create_db(name)
109 debug "DBTree: creating empty db #{name}"
110 return @@env.open_db(BDB::CIBtree, name, nil, BDB::CREATE | BDB::EXCL, 0600)
113 def DBTree.open_db(name)
114 debug "DBTree: opening existing db #{name}"
115 return @@env.open_db(BDB::CIBtree, name, nil, "r+", 0600)
118 def DBTree.cleanup_logs()
120 debug "DBTree: checkpointing ..."
126 debug "DBTree: flushing log ..."
128 logs = @@env.log_archive(BDB::ARCH_ABS)
129 debug "DBTree: deleting archivable logs: #{logs.join(', ')}."
140 debug "General stats:"
143 debug @@env.lock_stat
149 debug "Couldn't dump stats"
153 def DBTree.cleanup_env()
155 debug "DBTree: checking transactions ..."
156 has_active_txn = @@env.txn_stat["st_nactive"] > 0
158 warning "DBTree: not all transactions completed!"
161 debug "DBTree: closing environment #{@@env}"
166 debug "DBTree: keeping file because of incomplete transactions"
168 debug "DBTree: cleaning up environment in #{path}"
169 BDB::Env.remove("#{path}")
172 error "failed to clean up environment: #{e.inspect}"