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