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 db = cls.new(File.join(path, 'registry_' + @format, filename.downcase))
81 # Helper method that will return a list of supported registry formats.
83 @@formats ||= Registry.new.discover
86 # Abstract database accessor (a hash-like interface).
87 class AbstractAccessor
89 # lets the user define a recovery procedure in case the Marshal
90 # deserialization fails, it might be manually recover data.
91 # NOTE: weird legacy stuff, used by markov plugin (WTH?)
92 attr_accessor :recovery
94 def initialize(filename)
95 debug 'init registry accessor for: ' + filename
97 @name = File.basename filename
103 def sub_registry(prefix)
104 path = File.join(@filename.gsub(/\.[^\/\.]+$/,''), prefix.to_s)
105 return self.class.new(path)
108 # creates the registry / subregistry folders
110 debug 'create folders for: ' + @filename
111 dirs = File.dirname(@filename).split("/")
112 dirs.length.times { |i|
113 dir = dirs[0,i+1].join("/")+"/"
114 unless File.exist?(dir)
120 # Will return true if the database file exists.
122 File.exists? @filename
125 # convert value to string form for storing in the registry
126 # defaults to Marshal.dump(val) but you can override this in your module's
127 # registry object to use any method you like.
128 # For example, if you always just handle strings use:
136 # restores object from string form, restore(store(val)) must return val.
137 # If you override store, you should override restore to reverse the
139 # For example, if you always just handle strings use:
146 rescue Exception => e
147 error _("failed to restore marshal data for #{val.inspect}, attempting recovery or fallback to default")
149 if defined? @recovery and @recovery
151 return @recovery.call(val)
152 rescue Exception => ee
153 error _("marshal recovery failed, trying default")
161 # Returned instead of nil if key wasnt found.
162 def set_default (default)
167 @default && (@default.dup rescue @default)
170 # Opens the database (if not already open) for read/write access.
172 create_folders unless dbexists?
175 # Forces flush/sync the database on disk.
177 return unless @registry
181 # Should optimize/vacuum the database.
183 return unless @registry
187 # Closes the database.
189 return unless @registry
194 # lookup a key in the registry
196 if dbexists? and registry.has_key?(key.to_s)
197 return restore(registry[key.to_s])
203 # set a key in the registry
205 registry[key.to_s] = store(value)
210 return nil unless dbexists?
211 registry.each do |key|
212 block.call(key, self[key])
225 # like Hash#each_value
226 def each_value(&block)
227 self.each do |key, value|
232 # just like Hash#has_key?
234 return nil unless dbexists?
235 return registry.has_key?(key.to_s)
238 alias include? has_key?
239 alias member? has_key?
242 # just like Hash#has_value?
243 def has_value?(value)
244 return nil unless dbexists?
245 return registry.has_value?(store(value))
248 # just like Hash#index?
251 return k if v == value
256 # delete a key from the registry
258 return default unless dbexists?
259 return registry.delete(key.to_s)
262 # returns a list of your keys
264 return [] unless dbexists?
268 # just like Hash#has_both?
269 def has_both?(key, value)
270 return false unless dbexists?
271 registry.has_key?(key.to_s) and registry.has_value?(store(value))
274 # Return an array of all associations [key, value] in your namespace
276 return [] unless dbexists?
278 self.each {|key, value|
284 # Return an hash of all associations {key => value} in your namespace
286 return {} unless dbexists?
288 self.each {|key, value|
294 # empties the registry (restricted to your namespace)
296 return unless dbexists?
301 # returns an array of the values in your namespace of the registry
303 return [] unless dbexists?
311 # returns the number of keys in your registry namespace
313 return 0 unless dbexists?
318 # Returns all classes from the namespace that implement this interface
320 ObjectSpace.each_object(Class).select { |klass| klass < self }