4 puts "Got exception: "+e
5 puts "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 a.downcase <=> b.downcase
20 # DBHash is for tying a hash to disk (using bdb).
21 # Call it with an identifier, for example "mydata". It'll look for
22 # mydata.db, if it exists, it will load and reference that db.
23 # Otherwise it'll create and empty db called mydata.db
26 # absfilename:: use +key+ as an actual filename, don't prepend the bot's
27 # config path and don't append ".db"
28 def initialize(bot, key, absfilename=false)
31 if absfilename && File.exist?(key)
32 # db already exists, use it
33 @db = DBHash.open_db(key)
34 elsif File.exist?(@bot.botclass + "/#{key}.db")
35 # db already exists, use it
36 @db = DBHash.open_db(@bot.botclass + "/#{key}.db")
39 @db = DBHash.create_db(key)
42 @db = DBHash.create_db(@bot.botclass + "/#{key}.db")
46 def method_missing(method, *args, &block)
47 return @db.send(method, *args, &block)
50 def DBHash.create_db(name)
51 debug "DBHash: creating empty db #{name}"
52 return BDB::Hash.open(name, nil,
53 BDB::CREATE | BDB::EXCL, 0600)
56 def DBHash.open_db(name)
57 debug "DBHash: opening existing db #{name}"
58 return BDB::Hash.open(name, nil, "r+", 0600)
64 # DBTree is a BTree equivalent of DBHash, with case insensitive lookups.
67 # absfilename:: use +key+ as an actual filename, don't prepend the bot's
68 # config path and don't append ".db"
69 def initialize(bot, key, absfilename=false)
73 #@@env = BDB::Env.open("#{@bot.botclass}", BDB::INIT_TRANSACTION | BDB::CREATE | BDB::RECOVER)
74 @@env = BDB::Env.open("#{@bot.botclass}", BDB::CREATE | BDB::INIT_MPOOL | BDB::RECOVER)
77 if absfilename && File.exist?(key)
78 # db already exists, use it
79 @db = DBTree.open_db(key)
82 @db = DBTree.create_db(key)
83 elsif File.exist?(@bot.botclass + "/#{key}.db")
84 # db already exists, use it
85 @db = DBTree.open_db(@bot.botclass + "/#{key}.db")
88 @db = DBTree.create_db(@bot.botclass + "/#{key}.db")
92 def method_missing(method, *args, &block)
93 return @db.send(method, *args, &block)
96 def DBTree.create_db(name)
97 debug "DBTree: creating empty db #{name}"
98 return BDB::CIBtree.open(name, nil,
99 BDB::CREATE | BDB::EXCL,
100 0600, "env" => @@env)
103 def DBTree.open_db(name)
104 debug "DBTree: opening existing db #{name}"
105 return BDB::CIBtree.open(name, nil,
106 "r+", 0600, "env" => @@env)