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