]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - lib/rbot/registry.rb
All lib/rbot files are now upgraded to the new logging feature
[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         log "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         log "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             log "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           log "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 => e
151         warning "failed to restore marshal data for #{val.inspect}, falling back to default"
152         debug e.inspect
153         debug e.backtrace.join("\n")
154         if @default != nil
155           begin
156             return Marshal.restore(@default)
157           rescue
158             return nil
159           end
160         else
161           return nil
162         end
163       end
164     end
165
166     # lookup a key in the registry
167     def [](key)
168       if @registry.has_key?(key)
169         return restore(@registry[key])
170       elsif @default != nil
171         return restore(@default)
172       else
173         return nil
174       end
175     end
176
177     # set a key in the registry
178     def []=(key,value)
179       @registry[key] = store(value)
180     end
181
182     # set the default value for registry lookups, if the key sought is not
183     # found, the default will be returned. The default default (har) is nil.
184     def set_default (default)
185       @default = store(default)
186     end
187
188     # just like Hash#each
189     def each(&block)
190       @registry.each {|key,value|
191         block.call(key, restore(value))
192       }
193     end
194     
195     # just like Hash#each_key
196     def each_key(&block)
197       @registry.each {|key, value|
198         block.call(key)
199       }
200     end
201     
202     # just like Hash#each_value
203     def each_value(&block)
204       @registry.each {|key, value|
205         block.call(restore(value))
206       }
207     end
208
209     # just like Hash#has_key?
210     def has_key?(key)
211       return @registry.has_key?(key)
212     end
213     alias include? has_key?
214     alias member? has_key?
215
216     # just like Hash#has_both?
217     def has_both?(key, value)
218       return @registry.has_both?(key, store(value))
219     end
220     
221     # just like Hash#has_value?
222     def has_value?(value)
223       return @registry.has_value?(store(value))
224     end
225
226     # just like Hash#index?
227     def index(value)
228       ind = @registry.index(store(value))
229       if ind
230         return ind
231       else
232         return nil
233       end
234     end
235     
236     # delete a key from the registry
237     def delete(key)
238       return @registry.delete(key)
239     end
240
241     # returns a list of your keys
242     def keys
243       return @registry.keys
244     end
245
246     # Return an array of all associations [key, value] in your namespace
247     def to_a
248       ret = Array.new
249       @registry.each {|key, value|
250         ret << [key, restore(value)]
251       }
252       return ret
253     end
254     
255     # Return an hash of all associations {key => value} in your namespace
256     def to_hash
257       ret = Hash.new
258       @registry.each {|key, value|
259         ret[key] = restore(value)
260       }
261       return ret
262     end
263
264     # empties the registry (restricted to your namespace)
265     def clear
266       @registry.clear
267     end
268     alias truncate clear
269
270     # returns an array of the values in your namespace of the registry
271     def values
272       ret = Array.new
273       self.each {|k,v|
274         ret << restore(v)
275       }
276       return ret
277     end
278
279     def sub_registry(prefix)
280       return BotRegistryAccessor.new(@bot, @name + "/" + prefix)
281     end
282
283     # returns the number of keys in your registry namespace
284     def length
285       self.keys.length
286     end
287     alias size length
288
289   end
290
291 end