Class: Irc::Bot::Registry

Inherits:
Object show all
Defined in:
lib/rbot/registry/tc.rb,
lib/rbot/registry/bdb.rb

Overview

This class is now used purely for upgrading from prior versions of rbot the new registry is split into multiple DBHash objects, one per plugin

Defined Under Namespace

Classes: Accessor

Instance Method Summary collapse

Constructor Details

#initialize(bot) ⇒ Registry

Returns a new instance of Registry.



184
185
186
187
188
# File 'lib/rbot/registry/tc.rb', line 184

def initialize(bot)
  @bot = bot
  upgrade_data
  upgrade_data2
end

Instance Method Details

#upgrade_dataObject

check for older versions of rbot with data formats that require updating NB this function is called early in init(), pretty much all you have to work with is @bot.botclass.



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/rbot/registry/tc.rb', line 193

def upgrade_data
  if defined? DBHash
    oldreg = @bot.path 'registry.db'
    newreg = @bot.path 'plugin_registry.db'
    if File.exist?(oldreg)
      log _("upgrading old-style (rbot 0.9.5 or earlier) plugin registry to new format")
      old = ::BDB::Hash.open(oldreg, nil, "r+", 0600)
      new = TokyoCabinet::CIBDB.new
      new.open(name, TokyoCabinet::CIBDB::OREADER | TokyoCabinet::CIBDB::OCREAT | TokyoCabinet::CIBDB::OWRITER)
      old.each_key do |k|
        new.outlist k
        new.putlist k, (old.duplicates(k, false))
      end
      old.close
      new.close
      File.rename(oldreg, oldreg + ".old")
    end
  else
    warning "Won't upgrade data: BDB not installed"
  end
end

#upgrade_data2Object



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/rbot/registry/tc.rb', line 215

def upgrade_data2
  oldreg = @bot.path 'plugin_registry.db'
  newdir = @bot.path 'registry'
  if File.exist?(oldreg)
    Dir.mkdir(newdir) unless File.exist?(newdir)
    env = BDB::Env.open(@bot.botclass, BDB::INIT_TRANSACTION | BDB::CREATE | BDB::RECOVER)# | BDB::TXN_NOSYNC)
    dbs = Hash.new
    log _("upgrading previous (rbot 0.9.9 or earlier) plugin registry to new split format")
    old = BDB::CIBtree.open(oldreg, nil, "r+", 0600, "env" => env)
    old.each {|k,v|
      prefix,key = k.split("/", 2)
      prefix.downcase!
      # subregistries were split with a +, now they are in separate folders
      if prefix.gsub!(/\+/, "/")
        # Ok, this code needs to be put in the db opening routines
        dirs = File.dirname("#{@bot.botclass}/registry/#{prefix}.db").split("/")
        dirs.length.times { |i|
          dir = dirs[0,i+1].join("/")+"/"
          unless File.exist?(dir)
            log _("creating subregistry directory #{dir}")
            Dir.mkdir(dir)
          end
        }
      end
      unless dbs.has_key?(prefix)
        log _("creating db #{@bot.botclass}/registry/#{prefix}.db")
        dbs[prefix] = TokyoCabinet::CIBDB.open("#{@bot.botclass}/registry/#{prefix}.db",
         TokyoCabinet::CIBDB::OREADER | TokyoCabinet::CIBDB::OCREAT | TokyoCabinet::CIBDB::OWRITER)
      end
      dbs[prefix][key] = v
    }
    old.close
    File.rename(oldreg, oldreg + ".old")
    dbs.each {|k,v|
      log _("closing db #{k}")
      v.close
    }
    env.close
  end
end