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 | BDB::TRUNCATE,
54 0600, "set_pagesize" => 1024,
55 "set_cachesize" => [(0), (32 * 1024), (0)])
58 def DBHash.open_db(name)
59 debug "DBHash: opening existing db #{name}"
60 return BDB::Hash.open(name, nil,
61 "r+", 0600, "set_pagesize" => 1024,
62 "set_cachesize" => [(0), (32 * 1024), (0)])
68 # DBTree is a BTree equivalent of DBHash, with case insensitive lookups.
71 # absfilename:: use +key+ as an actual filename, don't prepend the bot's
72 # config path and don't append ".db"
73 def initialize(bot, key, absfilename=false)
76 if absfilename && File.exist?(key)
77 # db already exists, use it
78 @db = DBTree.open_db(key)
81 @db = DBTree.create_db(key)
82 elsif File.exist?(@bot.botclass + "/#{key}.db")
83 # db already exists, use it
84 @db = DBTree.open_db(@bot.botclass + "/#{key}.db")
87 @db = DBTree.create_db(@bot.botclass + "/#{key}.db")
91 def method_missing(method, *args, &block)
92 return @db.send(method, *args, &block)
95 def DBTree.create_db(name)
96 debug "DBTree: creating empty db #{name}"
97 return BDB::CIBtree.open(name, nil,
98 BDB::CREATE | BDB::EXCL | BDB::TRUNCATE,
99 0600, "set_pagesize" => 1024,
100 "set_cachesize" => [(0), (32 * 1024), (0)])
103 def DBTree.open_db(name)
104 debug "DBTree: opening existing db #{name}"
105 return BDB::CIBtree.open(name, nil,
106 "r+", 0600, "set_pagesize" => 1024,
107 "set_cachesize" => [0, 32 * 1024, 0])