4 # :title: Registry: Persistent storage interface and factory
6 # This class provides persistent storage for plugins via a hash interface.
7 # The default mode is an object store, so you can store ruby objects and
8 # reference them with hash keys. This is because the default store/restore
9 # methods of the plugins' RegistryAccessor are calls to Marshal.dump and
14 # @registry[:blah] = blah
15 # then, even after the bot is shut down and disconnected, on the next run you
16 # can access the blah object as it was, with:
17 # blah = @registry[:blah]
18 # The registry can of course be used to store simple strings, fixnums, etc as
19 # well, and should be useful to store or cache plugin data or dynamic plugin
23 # in object store mode, don't make the mistake of treating it like a live
24 # object, e.g. (using the example above)
25 # @registry[:blah][:foo] = "flump"
26 # will NOT modify the object in the registry - remember that Registry#[]
27 # returns a Marshal.restore'd object, the object you just modified in place
28 # will disappear. You would need to:
29 # blah = @registry[:blah]
30 # blah[:foo] = "flump"
31 # @registry[:blah] = blah
33 # If you don't need to store objects, and strictly want a persistant hash of
34 # strings, you can override the store/restore methods to suit your needs, for
35 # example (in your plugin):
46 # Your plugins section of the registry is private, it has its own namespace
47 # (derived from the plugin's class name, so change it and lose your data).
48 # Calls to registry.each etc, will only iterate over your namespace.
55 # Dynamically loads the specified registry type library.
56 def initialize(format=nil)
57 @libpath = File.join(File.dirname(__FILE__), 'registry')
59 load File.join(@libpath, @format+'.rb') if format
62 # Returns a list of supported registry database formats.
64 Dir.glob(File.join(@libpath, '*.rb')).map do |name|
65 File.basename(name, File.extname(name))
69 # Creates a new Accessor object for the specified database filename.
70 def create(path, filename)
71 # The get_impl method will return a list of all the classes that
72 # implement the accessor interface, since we only ever load one
73 # (the configured one) accessor implementation, we can just assume
74 # it to be the correct accessor to use.
75 cls = AbstractAccessor.get_impl.first
76 cls.new(File.join(path, 'registry_' + @format, filename.downcase))
79 # Helper method that will return a list of supported registry formats.
81 @@formats ||= Registry.new.discover
84 # Abstract database accessor (a hash-like interface).
85 class AbstractAccessor
87 # lets the user define a recovery procedure in case the Marshal
88 # deserialization fails, it might be manually recover data.
89 # NOTE: weird legacy stuff, used by markov plugin (WTH?)
90 attr_accessor :recovery
92 def initialize(filename)
93 debug 'init registry accessor for: ' + filename
95 @name = File.basename filename
101 def sub_registry(prefix)
102 path = File.join(@filename.gsub(/\.[^\/\.]+$/,''), prefix.to_s)
103 return self.class.new(path)
106 # creates the registry / subregistry folders
108 debug 'create folders for: ' + @filename
109 dirs = File.dirname(@filename).split("/")
110 dirs.length.times { |i|
111 dir = dirs[0,i+1].join("/")+"/"
112 unless File.exist?(dir)
118 # Will return true if the database file exists.
120 File.exists? @filename
123 # convert value to string form for storing in the registry
124 # defaults to Marshal.dump(val) but you can override this in your module's
125 # registry object to use any method you like.
126 # For example, if you always just handle strings use:
134 # restores object from string form, restore(store(val)) must return val.
135 # If you override store, you should override restore to reverse the
137 # For example, if you always just handle strings use:
144 rescue Exception => e
145 error _("failed to restore marshal data for #{val.inspect}, attempting recovery or fallback to default")
147 if defined? @recovery and @recovery
149 return @recovery.call(val)
150 rescue Exception => ee
151 error _("marshal recovery failed, trying default")
159 # Returned instead of nil if key wasnt found.
160 def set_default (default)
165 @default && (@default.dup rescue @default)
168 # Opens the database (if not already open) for read/write access.
170 create_folders unless dbexists?
173 # Forces flush/sync the database on disk.
175 return unless @registry
179 # Should optimize/vacuum the database.
181 return unless @registry
185 # Closes the database.
187 return unless @registry
192 # lookup a key in the registry
194 if dbexists? and registry.has_key?(key.to_s)
195 return restore(registry[key.to_s])
201 # set a key in the registry
203 registry[key.to_s] = store(value)
208 return nil unless dbexists?
209 registry.each do |key|
210 block.call(key, self[key])
223 # like Hash#each_value
224 def each_value(&block)
225 self.each do |key, value|
230 # just like Hash#has_key?
232 return nil unless dbexists?
233 return registry.has_key?(key.to_s)
236 alias include? has_key?
237 alias member? has_key?
240 # just like Hash#has_value?
241 def has_value?(value)
242 return nil unless dbexists?
243 return registry.has_value?(store(value))
246 # just like Hash#index?
249 return k if v == value
254 # delete a key from the registry
256 return default unless dbexists?
257 return registry.delete(key.to_s)
260 # returns a list of your keys
262 return [] unless dbexists?
266 # just like Hash#has_both?
267 def has_both?(key, value)
268 return false unless dbexists?
269 registry.has_key?(key.to_s) and registry.has_value?(store(value))
272 # Return an array of all associations [key, value] in your namespace
274 return [] unless dbexists?
276 self.each {|key, value|
282 # Return an hash of all associations {key => value} in your namespace
284 return {} unless dbexists?
286 self.each {|key, value|
292 # empties the registry (restricted to your namespace)
294 return unless dbexists?
299 # returns an array of the values in your namespace of the registry
301 return [] unless dbexists?
309 # returns the number of keys in your registry namespace
311 return 0 unless dbexists?
316 # Returns all classes from the namespace that implement this interface
318 ObjectSpace.each_object(Class).select { |klass| klass < self }