]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - lib/rbot/dbhash.rb
Fix #65 and #95. Disable topic built-in command since the new topic plugin handles...
[user/henk/code/ruby/rbot.git] / lib / rbot / dbhash.rb
1 begin
2   require 'bdb'
3 rescue Exception => e
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"
6   exit 2
7 end
8
9 # make BTree lookups case insensitive
10 module BDB
11   class CIBtree < Btree
12     def bdb_bt_compare(a, b)
13       a.downcase <=> b.downcase
14     end
15   end
16 end
17
18 module Irc
19
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
24   class DBHash
25     
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)
29       @bot = bot
30       @key = key
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")
37       elsif absfilename
38         # create empty db
39         @db = DBHash.create_db(key)
40       else
41         # create empty db
42         @db = DBHash.create_db(@bot.botclass + "/#{key}.db")
43       end
44     end
45
46     def method_missing(method, *args, &block)
47       return @db.send(method, *args, &block)
48     end
49
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)
54     end
55
56     def DBHash.open_db(name)
57       debug "DBHash: opening existing db #{name}"
58       return BDB::Hash.open(name, nil, "r+", 0600)
59     end
60     
61   end
62
63   
64   # DBTree is a BTree equivalent of DBHash, with case insensitive lookups.
65   class DBTree
66     @@env=nil
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)
70       @bot = bot
71       @key = key
72       if @@env.nil?
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)
75       end
76       
77       if absfilename && File.exist?(key)
78         # db already exists, use it
79         @db = DBTree.open_db(key)
80       elsif absfilename
81         # create empty db
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")
86       else
87         # create empty db
88         @db = DBTree.create_db(@bot.botclass + "/#{key}.db")
89       end
90     end
91
92     def method_missing(method, *args, &block)
93       return @db.send(method, *args, &block)
94     end
95
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)
101     end
102
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)
107     end
108     
109   end
110
111 end