Class: Irc::Bot::Registry

Inherits:
Object show all
Defined in:
lib/rbot/registry.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.



9
10
11
12
13
# File 'lib/rbot/registry.rb', line 9

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.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rbot/registry.rb', line 18

def upgrade_data
  if File.exist?("#{@bot.botclass}/registry.db")
    log _("upgrading old-style (rbot 0.9.5 or earlier) plugin registry to new format")
    old = BDB::Hash.open("#{@bot.botclass}/registry.db", nil,
                         "r+", 0600)
    new = BDB::CIBtree.open("#{@bot.botclass}/plugin_registry.db", nil,
                            BDB::CREATE | BDB::EXCL,
                            0600)
    old.each {|k,v|
      new[k] = v
    }
    old.close
    new.close
    File.rename("#{@bot.botclass}/registry.db", "#{@bot.botclass}/registry.db.old")
  end
end

#upgrade_data2Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rbot/registry.rb', line 35

def upgrade_data2
  if File.exist?("#{@bot.botclass}/plugin_registry.db")
    Dir.mkdir("#{@bot.botclass}/registry") unless File.exist?("#{@bot.botclass}/registry")
    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("#{@bot.botclass}/plugin_registry.db", 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] = BDB::CIBtree.open("#{@bot.botclass}/registry/#{prefix}.db",
          nil, BDB::CREATE | BDB::EXCL,
          0600, "env" => env)
      end
      dbs[prefix][key] = v
    }
    old.close
    File.rename("#{@bot.botclass}/plugin_registry.db", "#{@bot.botclass}/plugin_registry.db.old")
    dbs.each {|k,v|
      log _("closing db #{k}")
      v.close
    }
    env.close
  end
end