1 # Copyright (C) 2002 Tom Gilbert.
3 # Permission is hereby granted, free of charge, to any person obtaining a copy
4 # of this software and associated documentation files (the "Software"), to
5 # deal in the Software without restriction, including without limitation the
6 # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 # sell copies of the Software, and to permit persons to whom the Software is
8 # furnished to do so, subject to the following conditions:
10 # The above copyright notice and this permission notice shall be included in
11 # all copies of the Software and its documentation and acknowledgment shall be
12 # given in the documentation and software packages that this Software was
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 # THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 # IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 puts "Got exception: "+e
26 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"
30 # make BTree lookups case insensitive
33 def bdb_bt_compare(a, b)
34 a.downcase <=> b.downcase
41 # DBHash is for tying a hash to disk (using bdb).
42 # Call it with an identifier, for example "mydata". It'll look for
43 # mydata.db, if it exists, it will load and reference that db.
44 # Otherwise it'll create and empty db called mydata.db
47 # absfilename:: use +key+ as an actual filename, don't prepend the bot's
48 # config path and don't append ".db"
49 def initialize(bot, key, absfilename=false)
52 if absfilename && File.exist?(key)
53 # db already exists, use it
54 @db = DBHash.open_db(key)
55 elsif File.exist?(@bot.botclass + "/#{key}.db")
56 # db already exists, use it
57 @db = DBHash.open_db(@bot.botclass + "/#{key}.db")
60 @db = DBHash.create_db(key)
63 @db = DBHash.create_db(@bot.botclass + "/#{key}.db")
67 def method_missing(method, *args, &block)
68 return @db.send(method, *args, &block)
71 def DBHash.create_db(name)
72 debug "DBHash: creating empty db #{name}"
73 return BDB::Hash.open(name, nil,
74 BDB::CREATE | BDB::EXCL | BDB::TRUNCATE,
75 0600, "set_pagesize" => 1024,
76 "set_cachesize" => [(0), (32 * 1024), (0)])
79 def DBHash.open_db(name)
80 debug "DBHash: opening existing db #{name}"
81 return BDB::Hash.open(name, nil,
82 "r+", 0600, "set_pagesize" => 1024,
83 "set_cachesize" => [(0), (32 * 1024), (0)])
89 # DBTree is a BTree equivalent of DBHash, with case insensitive lookups.
92 # absfilename:: use +key+ as an actual filename, don't prepend the bot's
93 # config path and don't append ".db"
94 def initialize(bot, key, absfilename=false)
97 if absfilename && File.exist?(key)
98 # db already exists, use it
99 @db = DBTree.open_db(key)
102 @db = DBTree.create_db(key)
103 elsif File.exist?(@bot.botclass + "/#{key}.db")
104 # db already exists, use it
105 @db = DBTree.open_db(@bot.botclass + "/#{key}.db")
108 @db = DBTree.create_db(@bot.botclass + "/#{key}.db")
112 def method_missing(method, *args, &block)
113 return @db.send(method, *args, &block)
116 def DBTree.create_db(name)
117 debug "DBTree: creating empty db #{name}"
118 return BDB::CIBtree.open(name, nil,
119 BDB::CREATE | BDB::EXCL | BDB::TRUNCATE,
120 0600, "set_pagesize" => 1024,
121 "set_cachesize" => [(0), (32 * 1024), (0)])
124 def DBTree.open_db(name)
125 debug "DBTree: opening existing db #{name}"
126 return BDB::CIBtree.open(name, nil,
127 "r+", 0600, "set_pagesize" => 1024,
128 "set_cachesize" => [0, 32 * 1024, 0])