]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - lib/rbot/registry.rb
attempt to resolve #65
[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     def flush
122       @registry.flush
123     end
124
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:
129     #   def store(val)
130     #     val
131     #   end
132     def store(val)
133       Marshal.dump(val)
134     end
135
136     # restores object from string form, restore(store(val)) must return val.
137     # If you override store, you should override restore to reverse the
138     # action.
139     # For example, if you always just handle strings use:
140     #   def restore(val)
141     #     val
142     #   end
143     def restore(val)
144       begin
145         Marshal.restore(val)
146       rescue Exception
147         $stderr.puts "failed to restore marshal data, falling back to default"
148         if @default != nil
149           begin
150             return Marshal.restore(@default)
151           rescue
152             return nil
153           end
154         else
155           return nil
156         end
157       end
158     end
159
160     # lookup a key in the registry
161     def [](key)
162       if @registry.has_key?(key)
163         return restore(@registry[key])
164       elsif @default != nil
165         return restore(@default)
166       else
167         return nil
168       end
169     end
170
171     # set a key in the registry
172     def []=(key,value)
173       @registry[key] = store(value)
174     end
175
176     # set the default value for registry lookups, if the key sought is not
177     # found, the default will be returned. The default default (har) is nil.
178     def set_default (default)
179       @default = store(default)
180     end
181
182     # just like Hash#each
183     def each(&block)
184       @registry.each {|key,value|
185         block.call(key, restore(value))
186       }
187     end
188     
189     # just like Hash#each_key
190     def each_key(&block)
191       @registry.each {|key, value|
192         block.call(key)
193       }
194     end
195     
196     # just like Hash#each_value
197     def each_value(&block)
198       @registry.each {|key, value|
199         block.call(restore(value))
200       }
201     end
202
203     # just like Hash#has_key?
204     def has_key?(key)
205       return @registry.has_key?(key)
206     end
207     alias include? has_key?
208     alias member? has_key?
209
210     # just like Hash#has_both?
211     def has_both?(key, value)
212       return @registry.has_both?(key, store(value))
213     end
214     
215     # just like Hash#has_value?
216     def has_value?(value)
217       return @registry.has_value?(store(value))
218     end
219
220     # just like Hash#index?
221     def index(value)
222       ind = @registry.index(store(value))
223       if ind
224         return ind
225       else
226         return nil
227       end
228     end
229     
230     # delete a key from the registry
231     def delete(key)
232       return @registry.delete(key)
233     end
234
235     # returns a list of your keys
236     def keys
237       return @registry.keys
238     end
239
240     # Return an array of all associations [key, value] in your namespace
241     def to_a
242       ret = Array.new
243       @registry.each {|key, value|
244         ret << [key, restore(value)]
245       }
246       return ret
247     end
248     
249     # Return an hash of all associations {key => value} in your namespace
250     def to_hash
251       ret = Hash.new
252       @registry.each {|key, value|
253         ret[key] = restore(value)
254       }
255       return ret
256     end
257
258     # empties the registry (restricted to your namespace)
259     def clear
260       @registry.clear
261     end
262     alias truncate clear
263
264     # returns an array of the values in your namespace of the registry
265     def values
266       ret = Array.new
267       self.each {|k,v|
268         ret << restore(v)
269       }
270       return ret
271     end
272
273     # returns the number of keys in your registry namespace
274     def length
275       self.keys.length
276     end
277     alias size length
278
279     def flush
280       @registry.flush
281     end
282     
283   end
284
285 end