]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - lib/rbot/dbhash.rb
Explicitly handle BDB::Fatal errors by dumping database stats and restarting
[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       if a == nil || b == nil
14         debug "CIBTree: WARNING: comparing #{a.inspect} (#{self[a].inspect}) with #{b.inspect} (#{self[b].inspect})"
15       end
16       (a||'').downcase <=> (b||'').downcase
17     end
18   end
19 end
20
21 module Irc
22
23   # DBHash is for tying a hash to disk (using bdb).
24   # Call it with an identifier, for example "mydata". It'll look for
25   # mydata.db, if it exists, it will load and reference that db.
26   # Otherwise it'll create and empty db called mydata.db
27   class DBHash
28
29     # absfilename:: use +key+ as an actual filename, don't prepend the bot's
30     #               config path and don't append ".db"
31     def initialize(bot, key, absfilename=false)
32       @bot = bot
33       @key = key
34       if absfilename && File.exist?(key)
35         # db already exists, use it
36         @db = DBHash.open_db(key)
37       elsif File.exist?(@bot.botclass + "/#{key}.db")
38         # db already exists, use it
39         @db = DBHash.open_db(@bot.botclass + "/#{key}.db")
40       elsif absfilename
41         # create empty db
42         @db = DBHash.create_db(key)
43       else
44         # create empty db
45         @db = DBHash.create_db(@bot.botclass + "/#{key}.db")
46       end
47     end
48
49     def method_missing(method, *args, &block)
50       return @db.send(method, *args, &block)
51     end
52
53     def DBHash.create_db(name)
54       debug "DBHash: creating empty db #{name}"
55       return BDB::Hash.open(name, nil, 
56       BDB::CREATE | BDB::EXCL, 0600)
57     end
58
59     def DBHash.open_db(name)
60       debug "DBHash: opening existing db #{name}"
61       return BDB::Hash.open(name, nil, "r+", 0600)
62     end
63
64   end
65
66
67   # DBTree is a BTree equivalent of DBHash, with case insensitive lookups.
68   class DBTree
69     @@env=nil
70     # TODO: make this customizable
71     # Note that it must be at least four times lg_bsize
72     @@lg_max = 2*1024*1024
73     # absfilename:: use +key+ as an actual filename, don't prepend the bot's
74     #               config path and don't append ".db"
75     def initialize(bot, key, absfilename=false)
76       @bot = bot
77       @key = key
78       if @@env.nil?
79         begin
80           @@env = BDB::Env.open("#{@bot.botclass}", BDB::INIT_TRANSACTION | BDB::CREATE | BDB::RECOVER, "set_lg_max" => @@lg_max)
81           debug "DBTree: environment opened with max log size #{@@env.conf['lg_max']}"
82         rescue => e
83           debug "DBTree: failed to open environment: #{e}. Retrying ..."
84           @@env = BDB::Env.open("#{@bot.botclass}", BDB::INIT_TRANSACTION | BDB::CREATE |  BDB::RECOVER)
85         end
86         #@@env = BDB::Env.open("#{@bot.botclass}", BDB::CREATE | BDB::INIT_MPOOL | BDB::RECOVER)
87       end
88
89       if absfilename && File.exist?(key)
90         # db already exists, use it
91         @db = DBTree.open_db(key)
92       elsif absfilename
93         # create empty db
94         @db = DBTree.create_db(key)
95       elsif File.exist?(@bot.botclass + "/#{key}.db")
96         # db already exists, use it
97         @db = DBTree.open_db(@bot.botclass + "/#{key}.db")
98       else
99         # create empty db
100         @db = DBTree.create_db(@bot.botclass + "/#{key}.db")
101       end
102     end
103
104     def method_missing(method, *args, &block)
105       return @db.send(method, *args, &block)
106     end
107
108     def DBTree.create_db(name)
109       debug "DBTree: creating empty db #{name}"
110       return @@env.open_db(BDB::CIBtree, name, nil, BDB::CREATE | BDB::EXCL, 0600)
111     end
112
113     def DBTree.open_db(name)
114       debug "DBTree: opening existing db #{name}"
115       return @@env.open_db(BDB::CIBtree, name, nil, "r+", 0600)
116     end
117
118     def DBTree.cleanup_logs()
119       begin
120         debug "DBTree: checkpointing ..."
121         @@env.checkpoint
122       rescue => e
123         debug "Failed: #{e}"
124       end
125       begin
126         debug "DBTree: flushing log ..."
127         @@env.log_flush
128         logs = @@env.log_archive(BDB::ARCH_ABS)
129         debug "DBTree: deleting archivable logs: #{logs.join(', ')}."
130         logs.each { |log|
131           File.delete(log)
132         }
133       rescue => e
134         debug "Failed: #{e}"
135       end
136     end
137
138     def DBTree.stats()
139       begin
140         debug "General stats:"
141         debug @@env.stat
142         debug "Lock stats:"
143         debug @@env.lock_stat
144         debug "Log stats:"
145         debug @@env.log_stat
146         debug "Txn stats:"
147         debug @@env.txn_stat
148       rescue
149         debug "Couldn't dump stats"
150       end
151     end
152
153     def DBTree.cleanup_env()
154       begin
155         debug "DBTree: checking transactions ..."
156         has_active_txn = @@env.txn_stat["st_nactive"] > 0
157         if has_active_txn
158           debug "DBTree: WARNING: not all transactions completed!"
159         end
160         DBTree.cleanup_logs
161         debug "DBTree: closing environment #{@@env}"
162         path = @@env.home
163         @@env.close
164         @@env = nil
165         if has_active_txn
166           debug "DBTree: keeping file because of incomplete transactions"
167         else
168           debug "DBTree: cleaning up environment in #{path}"
169           BDB::Env.remove("#{path}")
170         end
171       rescue => e
172         debug "Failed: #{e}"
173       end
174     end
175
176   end
177
178 end