4 # :title: Berkeley DB interface
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"
11 fatal "rbot couldn't load the bdb module: #{e.pretty_inspect}"
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"
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."
30 # make BTree lookups case insensitive
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})"
37 (a||'').downcase <=> (b||'').downcase
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
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)
55 relfilename = @bot.path key
57 if absfilename && File.exist?(key)
58 # db already exists, use it
59 @db = DBHash.open_db(key)
62 @db = DBHash.create_db(key)
63 elsif File.exist? relfilename
64 # db already exists, use it
65 @db = DBHash.open_db relfilename
68 @db = DBHash.create_db relfilename
72 def method_missing(method, *args, &block)
73 return @db.send(method, *args, &block)
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)
82 def DBHash.open_db(name)
83 debug "DBHash: opening existing db #{name}"
84 return BDB::Hash.open(name, nil, "r+", 0600)
90 # DBTree is a BTree equivalent of DBHash, with case insensitive lookups.
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)
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']}"
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)
109 #@@env = BDB::Env.open(@bot.botclass, BDB::CREATE | BDB::INIT_MPOOL | BDB::RECOVER)
112 relfilename = @bot.path key
115 if absfilename && File.exist?(key)
116 # db already exists, use it
117 @db = DBTree.open_db(key)
120 @db = DBTree.create_db(key)
121 elsif File.exist? relfilename
122 # db already exists, use it
123 @db = DBTree.open_db relfilename
126 @db = DBTree.create_db relfilename
130 def method_missing(method, *args, &block)
131 return @db.send(method, *args, &block)
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)
139 def DBTree.open_db(name)
140 debug "DBTree: opening existing db #{name}"
141 return @@env.open_db(BDB::CIBtree, name, nil, "r+", 0600)
144 def DBTree.cleanup_logs()
146 debug "DBTree: checkpointing ..."
148 rescue Exception => e
149 debug "Failed: #{e.pretty_inspect}"
152 debug "DBTree: flushing log ..."
154 logs = @@env.log_archive(BDB::ARCH_ABS)
155 debug "DBTree: deleting archivable logs: #{logs.join(', ')}."
159 rescue Exception => e
160 debug "Failed: #{e.pretty_inspect}"
166 debug "General stats:"
169 debug @@env.lock_stat
175 debug "Couldn't dump stats"
179 def DBTree.cleanup_env()
181 debug "DBTree: checking transactions ..."
182 has_active_txn = @@env.txn_stat["st_nactive"] > 0
184 warning "DBTree: not all transactions completed!"
187 debug "DBTree: closing environment #{@@env}"
192 debug "DBTree: keeping file because of incomplete transactions"
194 debug "DBTree: cleaning up environment in #{path}"
195 BDB::Env.remove("#{path}")
197 rescue Exception => e
198 error "failed to clean up environment: #{e.pretty_inspect}"
210 # This class is now used purely for upgrading from prior versions of rbot
211 # the new registry is split into multiple DBHash objects, one per plugin
219 # check for older versions of rbot with data formats that require updating
220 # NB this function is called _early_ in init(), pretty much all you have to
221 # work with is @bot.botclass.
223 oldreg = @bot.path 'registry.db'
224 newreg = @bot.path 'plugin_registry.db'
225 if File.exist?(oldreg)
226 log _("upgrading old-style (rbot 0.9.5 or earlier) plugin registry to new format")
227 old = BDB::Hash.open(oldreg, nil, "r+", 0600)
228 new = BDB::CIBtree.open(newreg, nil, BDB::CREATE | BDB::EXCL, 0600)
234 File.rename(oldreg, oldreg + ".old")
239 oldreg = @bot.path 'plugin_registry.db'
240 newdir = @bot.path 'registry'
241 if File.exist?(oldreg)
242 Dir.mkdir(newdir) unless File.exist?(newdir)
243 env = BDB::Env.open(@bot.botclass, BDB::INIT_TRANSACTION | BDB::CREATE | BDB::RECOVER)# | BDB::TXN_NOSYNC)
245 log _("upgrading previous (rbot 0.9.9 or earlier) plugin registry to new split format")
246 old = BDB::CIBtree.open(oldreg, nil, "r+", 0600, "env" => env)
248 prefix,key = k.split("/", 2)
250 # subregistries were split with a +, now they are in separate folders
251 if prefix.gsub!(/\+/, "/")
252 # Ok, this code needs to be put in the db opening routines
253 dirs = File.dirname("#{@bot.botclass}/registry/#{prefix}.db").split("/")
254 dirs.length.times { |i|
255 dir = dirs[0,i+1].join("/")+"/"
256 unless File.exist?(dir)
257 log _("creating subregistry directory #{dir}")
262 unless dbs.has_key?(prefix)
263 log _("creating db #{@bot.botclass}/registry/#{prefix}.db")
264 dbs[prefix] = BDB::CIBtree.open("#{@bot.botclass}/registry/#{prefix}.db",
265 nil, BDB::CREATE | BDB::EXCL,
271 File.rename(oldreg, oldreg + ".old")
273 log _("closing db #{k}")
281 # This class provides persistent storage for plugins via a hash interface.
282 # The default mode is an object store, so you can store ruby objects and
283 # reference them with hash keys. This is because the default store/restore
284 # methods of the plugins' RegistryAccessor are calls to Marshal.dump and
289 # @registry[:blah] = blah
290 # then, even after the bot is shut down and disconnected, on the next run you
291 # can access the blah object as it was, with:
292 # blah = @registry[:blah]
293 # The registry can of course be used to store simple strings, fixnums, etc as
294 # well, and should be useful to store or cache plugin data or dynamic plugin
298 # in object store mode, don't make the mistake of treating it like a live
299 # object, e.g. (using the example above)
300 # @registry[:blah][:foo] = "flump"
301 # will NOT modify the object in the registry - remember that Registry#[]
302 # returns a Marshal.restore'd object, the object you just modified in place
303 # will disappear. You would need to:
304 # blah = @registry[:blah]
305 # blah[:foo] = "flump"
306 # @registry[:blah] = blah
308 # If you don't need to store objects, and strictly want a persistant hash of
309 # strings, you can override the store/restore methods to suit your needs, for
310 # example (in your plugin):
321 # Your plugins section of the registry is private, it has its own namespace
322 # (derived from the plugin's class name, so change it and lose your data).
323 # Calls to registry.each etc, will only iterate over your namespace.
326 attr_accessor :recovery
328 # plugins don't call this - a Registry::Accessor is created for them and
329 # is accessible via @registry.
330 def initialize(bot, name)
332 @name = name.downcase
333 @filename = @bot.path 'registry', @name
334 dirs = File.dirname(@filename).split("/")
335 dirs.length.times { |i|
336 dir = dirs[0,i+1].join("/")+"/"
337 unless File.exist?(dir)
338 debug "creating subregistry directory #{dir}"
346 # debug "initializing registry accessor with name #{@name}"
350 @registry ||= DBTree.new @bot, "registry/#{@name}"
354 # debug "fushing registry #{registry}"
361 # debug "closing registry #{registry}"
366 # convert value to string form for storing in the registry
367 # defaults to Marshal.dump(val) but you can override this in your module's
368 # registry object to use any method you like.
369 # For example, if you always just handle strings use:
377 # restores object from string form, restore(store(val)) must return val.
378 # If you override store, you should override restore to reverse the
380 # For example, if you always just handle strings use:
387 rescue Exception => e
388 error _("failed to restore marshal data for #{val.inspect}, attempting recovery or fallback to default")
390 if defined? @recovery and @recovery
392 return @recovery.call(val)
393 rescue Exception => ee
394 error _("marshal recovery failed, trying default")
402 # lookup a key in the registry
404 if File.exist?(@filename) && registry.has_key?(key)
405 return restore(registry[key])
411 # set a key in the registry
413 registry[key] = store(value)
416 # set the default value for registry lookups, if the key sought is not
417 # found, the default will be returned. The default default (har) is nil.
418 def set_default (default)
423 @default && (@default.dup rescue @default)
426 # just like Hash#each
427 def each(set=nil, bulk=0, &block)
428 return nil unless File.exist?(@filename)
429 registry.each(set, bulk) {|key,value|
430 block.call(key, restore(value))
434 # just like Hash#each_key
435 def each_key(set=nil, bulk=0, &block)
436 return nil unless File.exist?(@filename)
437 registry.each_key(set, bulk) {|key|
442 # just like Hash#each_value
443 def each_value(set=nil, bulk=0, &block)
444 return nil unless File.exist?(@filename)
445 registry.each_value(set, bulk) { |value|
446 block.call(restore(value))
450 # just like Hash#has_key?
452 return false unless File.exist?(@filename)
453 return registry.has_key?(key)
455 alias include? has_key?
456 alias member? has_key?
459 # just like Hash#has_both?
460 def has_both?(key, value)
461 return false unless File.exist?(@filename)
462 return registry.has_both?(key, store(value))
465 # just like Hash#has_value?
466 def has_value?(value)
467 return false unless File.exist?(@filename)
468 return registry.has_value?(store(value))
471 # just like Hash#index?
473 return nil unless File.exist?(@filename)
474 ind = registry.index(store(value))
482 # delete a key from the registry
484 return default unless File.exist?(@filename)
485 return registry.delete(key)
488 # returns a list of your keys
490 return [] unless File.exist?(@filename)
494 # Return an array of all associations [key, value] in your namespace
496 return [] unless File.exist?(@filename)
498 registry.each {|key, value|
499 ret << [key, restore(value)]
504 # Return an hash of all associations {key => value} in your namespace
506 return {} unless File.exist?(@filename)
508 registry.each {|key, value|
509 ret[key] = restore(value)
514 # empties the registry (restricted to your namespace)
516 return true unless File.exist?(@filename)
521 # returns an array of the values in your namespace of the registry
523 return [] unless File.exist?(@filename)
531 def sub_registry(prefix)
532 return Accessor.new(@bot, @name + "/" + prefix.to_s)
535 # returns the number of keys in your registry namespace