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