]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - lib/rbot/registry.rb
6afc7622e745dfd54db608fef627b235ac96337a
[user/henk/code/ruby/rbot.git] / lib / rbot / registry.rb
1 require 'rbot/dbhash'
2
3 module Irc
4
5   # this class is now used purely for upgrading from prior versions of rbot
6   # the new registry is split into multiple DBHash objects, one per plugin
7   class BotRegistry
8     def initialize(bot)
9       @bot = bot
10       upgrade_data
11       upgrade_data2
12     end
13
14     # check for older versions of rbot with data formats that require updating
15     # NB this function is called _early_ in init(), pretty much all you have to
16     # work with is @bot.botclass.
17     def upgrade_data
18       if File.exist?("#{@bot.botclass}/registry.db")
19         puts "upgrading old-style (rbot 0.9.5 or earlier) plugin registry to new format"
20         old = BDB::Hash.open("#{@bot.botclass}/registry.db", nil, 
21                              "r+", 0600, "set_pagesize" => 1024,
22                              "set_cachesize" => [0, 32 * 1024, 0])
23         new = BDB::CIBtree.open("#{@bot.botclass}/plugin_registry.db", nil, 
24                                 BDB::CREATE | BDB::EXCL | BDB::TRUNCATE,
25                                 0600, "set_pagesize" => 1024,
26                                 "set_cachesize" => [0, 32 * 1024, 0])
27         old.each {|k,v|
28           new[k] = v
29         }
30         old.close
31         new.close
32         File.delete("#{@bot.botclass}/registry.db")
33       end
34     end
35     
36     def upgrade_data2
37       if File.exist?("#{@bot.botclass}/plugin_registry.db")
38         Dir.mkdir("#{@bot.botclass}/registry")
39         dbs = Hash.new
40         puts "upgrading previous (rbot 0.9.9 or earlier) plugin registry to new split format"
41         old = BDB::CIBtree.open("#{@bot.botclass}/plugin_registry.db", nil, 
42           "r+", 0600, "set_pagesize" => 1024,
43           "set_cachesize" => [0, 32 * 1024, 0])
44         old.each {|k,v|
45           prefix,key = k.split("/", 2)
46           prefix.downcase!
47           unless dbs.has_key?(prefix)
48             puts "creating db #{@bot.botclass}/registry/#{prefix}.db"
49             dbs[prefix] = BDB::CIBtree.open("#{@bot.botclass}/registry/#{prefix}.db",
50               nil, BDB::CREATE | BDB::EXCL | BDB::TRUNCATE,
51               0600, "set_pagesize" => 1024,
52               "set_cachesize" => [0, 32 * 1024, 0])
53           end
54           dbs[prefix][key] = v
55         }
56         old.close
57         File.rename("#{@bot.botclass}/plugin_registry.db", "#{@bot.botclass}/plugin_registry.db.old")
58         dbs.each {|k,v|
59           puts "closing db #{k}"
60           v.close
61         }
62       end
63     end
64   end
65   
66
67   # This class provides persistent storage for plugins via a hash interface.
68   # The default mode is an object store, so you can store ruby objects and
69   # reference them with hash keys. This is because the default store/restore
70   # methods of the plugins' RegistryAccessor are calls to Marshal.dump and
71   # Marshal.restore,
72   # for example:
73   #   blah = Hash.new
74   #   blah[:foo] = "fum"
75   #   @registry[:blah] = blah
76   # then, even after the bot is shut down and disconnected, on the next run you
77   # can access the blah object as it was, with:
78   #   blah = @registry[:blah]
79   # The registry can of course be used to store simple strings, fixnums, etc as
80   # well, and should be useful to store or cache plugin data or dynamic plugin
81   # configuration. 
82   #
83   # WARNING:
84   # in object store mode, don't make the mistake of treating it like a live
85   # object, e.g. (using the example above)
86   #   @registry[:blah][:foo] = "flump"
87   # will NOT modify the object in the registry - remember that BotRegistry#[]
88   # returns a Marshal.restore'd object, the object you just modified in place
89   # will disappear. You would need to:
90   #   blah = @registry[:blah]
91   #   blah[:foo] = "flump"
92   #   @registry[:blah] = blah
93
94   # If you don't need to store objects, and strictly want a persistant hash of
95   # strings, you can override the store/restore methods to suit your needs, for
96   # example (in your plugin):
97   #   def initialize
98   #     class << @registry
99   #       def store(val)
100   #         val
101   #       end
102   #       def restore(val)
103   #         val
104   #       end
105   #     end
106   #   end
107   # Your plugins section of the registry is private, it has its own namespace
108   # (derived from the plugin's class name, so change it and lose your data).
109   # Calls to registry.each etc, will only iterate over your namespace.
110   class BotRegistryAccessor
111     # plugins don't call this - a BotRegistryAccessor is created for them and
112     # is accessible via @registry.
113     def initialize(bot, name)
114       @bot = bot
115       @name = name.downcase
116       @registry = DBTree.new bot, "registry/#{@name}"
117       @default = nil
118       # debug "initializing registry accessor with name #{@name}"
119     end
120
121     # convert value to string form for storing in the registry
122     # defaults to Marshal.dump(val) but you can override this in your module's
123     # registry object to use any method you like.
124     # For example, if you always just handle strings use:
125     #   def store(val)
126     #     val
127     #   end
128     def store(val)
129       Marshal.dump(val)
130     end
131
132     # restores object from string form, restore(store(val)) must return val.
133     # If you override store, you should override restore to reverse the
134     # action.
135     # For example, if you always just handle strings use:
136     #   def restore(val)
137     #     val
138     #   end
139     def restore(val)
140       begin
141         Marshal.restore(val)
142       rescue Exception
143         $stderr.puts "failed to restore marshal data, falling back to default"
144         if @default != nil
145           begin
146             return Marshal.restore(@default)
147           rescue
148             return nil
149           end
150         else
151           return nil
152         end
153       end
154     end
155
156     # lookup a key in the registry
157     def [](key)
158       if @registry.has_key?(key)
159         return restore(@registry[key])
160       elsif @default != nil
161         return restore(@default)
162       else
163         return nil
164       end
165     end
166
167     # set a key in the registry
168     def []=(key,value)
169       @registry[key] = store(value)
170     end
171
172     # set the default value for registry lookups, if the key sought is not
173     # found, the default will be returned. The default default (har) is nil.
174     def set_default (default)
175       @default = store(default)
176     end
177
178     # just like Hash#each
179     def each(&block)
180       @registry.each {|key,value|
181         block.call(key, restore(value))
182       }
183     end
184     
185     # just like Hash#each_key
186     def each_key(&block)
187       @registry.each {|key, value|
188         block.call(key)
189       }
190     end
191     
192     # just like Hash#each_value
193     def each_value(&block)
194       @registry.each {|key, value|
195         block.call(restore(value))
196       }
197     end
198
199     # just like Hash#has_key?
200     def has_key?(key)
201       return @registry.has_key?(key)
202     end
203     alias include? has_key?
204     alias member? has_key?
205
206     # just like Hash#has_both?
207     def has_both?(key, value)
208       return @registry.has_both?(key, store(value))
209     end
210     
211     # just like Hash#has_value?
212     def has_value?(value)
213       return @registry.has_value?(store(value))
214     end
215
216     # just like Hash#index?
217     def index(value)
218       ind = @registry.index(store(value))
219       if ind
220         return ind
221       else
222         return nil
223       end
224     end
225     
226     # delete a key from the registry
227     def delete(key)
228       return @registry.delete(key)
229     end
230
231     # returns a list of your keys
232     def keys
233       return @registry.keys
234     end
235
236     # Return an array of all associations [key, value] in your namespace
237     def to_a
238       ret = Array.new
239       @registry.each {|key, value|
240         ret << [key, restore(value)]
241       }
242       return ret
243     end
244     
245     # Return an hash of all associations {key => value} in your namespace
246     def to_hash
247       ret = Hash.new
248       @registry.each {|key, value|
249         ret[key] = restore(value)
250       }
251       return ret
252     end
253
254     # empties the registry (restricted to your namespace)
255     def clear
256       @registry.clear
257     end
258     alias truncate clear
259
260     # returns an array of the values in your namespace of the registry
261     def values
262       ret = Array.new
263       self.each {|k,v|
264         ret << restore(v)
265       }
266       return ret
267     end
268
269     # returns the number of keys in your registry namespace
270     def length
271       self.keys.length
272     end
273     alias size length
274
275     def flush
276       @registry.flush
277     end
278     
279   end
280
281 end