Class: Conveyor::Upgrader

Inherits:
Object
  • Object
show all
Defined in:
lib/conveyor/upgrader.rb

Instance Method Summary collapse

Constructor Details

#initialize(directory) ⇒ Upgrader

Returns a new instance of Upgrader.



13
14
15
# File 'lib/conveyor/upgrader.rb', line 13

def initialize directory
  @directory = directory
end

Instance Method Details

#create_tmp_dir!Object



110
111
112
113
114
115
116
117
# File 'lib/conveyor/upgrader.rb', line 110

def create_tmp_dir!
  loop do
    tmp_dir = File.join('/tmp', String.random_alphanumeric)
    if !File.exists?(tmp_dir)
      return tmp_dir
    end
  end
end

#from_0Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/conveyor/upgrader.rb', line 51

def from_0
  Dir.glob(@directory + "/*").each do |d|

    if File.directory?(d)
      # create tmp dir
      tmp_dir = create_tmp_dir!
      puts "writing to #{tmp_dir}"
      chan = Channel.new(tmp_dir)

      Dir.glob("#{d}/[0-9]*").each do |f|
        puts "upgrading #{f}"
        size = File.size(f)
        f    = File.open(f)

        while f.pos < size
          l       = f.readline.strip
          header  = parse_headers_0(l)
          content = f.read(header[:length])
          f.readline # newline chomp

          chan.commit(content, Time.parse(header[:time]))
        end
      end

      puts "upgrading iterator"
      iterator = File.open(File.join(d, 'iterator')) do |f|
        l = nil
        while !f.eof? && l = f.readline
        end
        if l
          chan.rewind :id => l.strip.to_i
        end
      end

      Dir.glob(File.join(d, 'iterator-*')) do |i|
        group = i.split('/').last.split('-').last
        puts "upgrading group iterator for #{group}"
        iterator = File.open(i) do |f|
          l = nil
          while !f.eof? && l = f.readline
          end
          if l
            chan.rewind :id => l.strip.to_i, :group => group
          end
        end
      end

      puts "backing up #{d} to #{d}.bak"
      FileUtils.mv d, d + ".bak"

      puts "copying from #{tmp_dir} to #{d}"
      FileUtils.cp_r tmp_dir, d

      puts "deleting temp data"
      FileUtils.rm_r tmp_dir
    end
  end
end

#parse_headers_0(str, index_file = false) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/conveyor/upgrader.rb', line 36

def parse_headers_0 str, index_file = false
  pattern =  '\A(\d+) (\d{4}\-\d{2}\-\d{2}T\d{2}\:\d{2}\:\d{2}[+\-]\d{2}\:\d{2}) (\d+) (\d+) ([a-f0-9]+)'
  pattern += ' (\d+)' if index_file
  pattern += '\Z'
  m = str.match(Regexp.new(pattern))
  {
    :id => m.captures[0].to_i,
    :time => m.captures[1],
    :offset => m.captures[2].to_i,
    :length => m.captures[3].to_i,
    :hash => m.captures[4], 
    :file => (index_file ? m.captures[5].to_i : nil)
  }.reject {|k,v| v == nil}
end

#source_versionObject



21
22
23
24
25
26
27
# File 'lib/conveyor/upgrader.rb', line 21

def source_version
  if File.exists?(version_path) && File.size(version_path) > 0
    File.open(version_path).read.to_i
  else
    0
  end
end

#upgradeObject



29
30
31
32
33
34
# File 'lib/conveyor/upgrader.rb', line 29

def upgrade
  case source_version
  when 0
    from_0
  end
end

#version_pathObject



17
18
19
# File 'lib/conveyor/upgrader.rb', line 17

def version_path
  File.join(@directory, 'version')
end