]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - rbot/dbhash.rb
that wasn't ideal
[user/henk/code/ruby/rbot.git] / rbot / dbhash.rb
1 # Copyright (C) 2002 Tom Gilbert.
2 #
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:
9 #
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
13 # used.
14 #
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.
21
22 begin
23   require 'bdb'
24 rescue Exception => e
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"
27   exit 2
28 end
29
30 # make BTree lookups case insensitive
31 module BDB
32   class CIBtree < Btree
33     def bdb_bt_compare(a, b)
34       a.downcase <=> b.downcase
35     end
36   end
37 end
38
39 module Irc
40
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
45   class DBHash
46     
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)
50       @bot = bot
51       @key = key
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")
58       elsif absfilename
59         # create empty db
60         @db = DBHash.create_db(key)
61       else
62         # create empty db
63         @db = DBHash.create_db(@bot.botclass + "/#{key}.db")
64       end
65     end
66
67     def method_missing(method, *args, &block)
68       return @db.send(method, *args, &block)
69     end
70
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)])
77     end
78
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)])
84     end
85     
86   end
87
88   
89   # DBTree is a BTree equivalent of DBHash, with case insensitive lookups.
90   class DBTree
91     
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)
95       @bot = bot
96       @key = key
97       if absfilename && File.exist?(key)
98         # db already exists, use it
99         @db = DBTree.open_db(key)
100       elsif absfilename
101         # create empty db
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")
106       else
107         # create empty db
108         @db = DBTree.create_db(@bot.botclass + "/#{key}.db")
109       end
110     end
111
112     def method_missing(method, *args, &block)
113       return @db.send(method, *args, &block)
114     end
115
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)])
122     end
123
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])
129     end
130     
131   end
132
133 end