Module: ThinpXML

Included in:
MetadataAnalysis
Defined in:
lib/thinp_xml/emitter.rb,
lib/thinp_xml.rb,
lib/thinp_xml/listener.rb,
lib/thinp_xml/thinp/emit.rb,
lib/thinp_xml/thinp/parse.rb,
lib/thinp_xml/thinp/utils.rb,
lib/thinp_xml/distribution.rb,
lib/thinp_xml/thinp/builder.rb,
lib/thinp_xml/thinp/metadata.rb

Overview


Defined Under Namespace

Modules: Base, EmitterDetail, ParseDetail Classes: Builder, ConstDistribution, Device, Distribution, Mapping, Metadata, Superblock, UniformDistribution

Constant Summary collapse

THINP_TOOLS_VERSION =
'0.1.5'
UNIFORM_REGEX =

/uniform\[(\d+)..(\d+)\]/
SUPERBLOCK_FIELDS =
[[:uuid, :string],
[:time, :int],
[:transaction, :int],
[:data_block_size, :int],
[:nr_data_blocks, :int]]
MAPPING_FIELDS =
[[:origin_begin, :int],
[:data_begin, :int],
[:length, :int],
[:time, :int]]
DEVICE_FIELDS =
[[:dev_id, :int],
[:mapped_blocks, :int],
[:transaction, :int],
[:creation_time, :int],
[:snap_time, :int],
[:mappings, :object]]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.field_names(flds) ⇒ Object



20
21
22
# File 'lib/thinp_xml/thinp/metadata.rb', line 20

def self.field_names(flds)
  flds.map {|p| p[0]}
end

.tools_are_installedObject



4
5
6
# File 'lib/thinp_xml.rb', line 4

def self.tools_are_installed
  true
end

Instance Method Details

#compare_devs(md_dev1, md_dev2) ⇒ Object

returns 3 arrays of mappings: unique to first arg, common, unique to second arg



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/thinp_xml/thinp/utils.rb', line 85

def compare_devs(md_dev1, md_dev2)
  m1 = md_dev1.mappings
  m2 = md_dev2.mappings

  left = Array.new
  center = Array.new
  right = Array.new

  expand_mappings(m1, m2).each do |pair|
    if pair[0].data_begin == pair[1].data_begin &&
        pair[0].time == pair[1].time
      # mappings are the same
      center << pair[0]
    else
      left << pair[0]
      right << pair[1]
    end
  end

  [left, center, right].each {|a| a.reject {|e| e.data_begin == nil}}
  [left, center, right]
end

#compare_thins(md1, md2, dev_id) ⇒ Object



108
109
110
111
# File 'lib/thinp_xml/thinp/utils.rb', line 108

def compare_thins(md1, md2, dev_id)
  compare_devs(get_device(md1, dev_id),
               get_device(md2, dev_id))
end

#expand_mappings(left, right) ⇒ Object

Turns 2 lists of mappings, into a list of pairs of mappings. These pairs cover identical regions. nil is used for the data_begin if that region isn’t mapped.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
75
76
77
78
79
80
81
# File 'lib/thinp_xml/thinp/utils.rb', line 17

def expand_mappings(left, right)
  pairs = Array.new
  i1 = 0
  i2 = 0

  m1 = left[i1]
  m2 = right[i2]

  # look away now ...
  loop do
    if !m1 && !m2
      return pairs
    elsif !m1
      pairs << [Mapping.new(m2.origin_begin, nil, m2.length, m2.time),
                m2]
      m2 = nil
    elsif !m2
      pairs << [m1,
                Mapping.new(m1.origin_begin, nil, m1.length, m1.time)]
      m1 = nil
    elsif m1.origin_begin < m2.origin_begin
      if m1.origin_begin + m1.length <= m2.origin_begin
        pairs << [Mapping.new(m1.origin_begin, m1.data_begin, m1.length, m1.time),
                  Mapping.new(m1.origin_begin, nil, m1.length, m1.time)]
        i1 += 1
        m1 = left[i1]
      else
        len = m2.origin_begin - m1.origin_begin
        pairs << [Mapping.new(m1.origin_begin, m1.data_begin, len, m1.time),
                  Mapping.new(m1.origin_begin, nil, len, m1.time)]
        m1 = Mapping.new(m1.origin_begin + len, m1.data_begin + len, m1.length - len, m1.time)
      end
    elsif m2.origin_begin < m1.origin_begin
      if m2.origin_begin + m2.length <= m1.origin_begin
        pairs << [Mapping.new(m2.origin_begin, nil, m2.length, m2.time),
                  Mapping.new(m2.origin_begin, m2.data_begin, m2.length, m2.time)]
        i2 += 1
        m2 = right[i2]
      else
        len = m1.origin_begin - m2.origin_begin
        pairs << [Mapping.new(m2.origin_begin, nil, len, m2.time),
                  Mapping.new(m2.origin_begin, m2.data_begin, len, m2.time)]
        m2 = Mapping.new(m2.origin_begin + len, m2.data_begin + len, m2.length - len, m2.time)
      end
    else
      len = [m1.length, m2.length].min
      pairs << [Mapping.new(m1.origin_begin, m1.data_begin, len, m1.time),
                Mapping.new(m1.origin_begin, m2.data_begin, len, m2.time)]
      if m1.length < m2.length
        i1 += 1
        m1 = left[i1]
        m2 = Mapping.new(m2.origin_begin + len, m2.data_begin + len, m2.length - len, m2.time)
      elsif m2.length < m1.length
        i2 += 1
        m1 = Mapping.new(m1.origin_begin + len, m1.data_begin + len, m1.length - len, m1.time)
        m2 = right[i2]
      else
        i1 += 1
        i2 += 1
        m1 = left[i1]
        m2 = right[i2]
      end
    end
  end
end

#get_device(md, dev_id) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/thinp_xml/thinp/utils.rb', line 6

def get_device(md, dev_id)
  md.devices.each do |dev|
    if dev.dev_id == dev_id
      return dev
    end
  end
end

#parse_distribution(str) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/thinp_xml/distribution.rb', line 40

def parse_distribution(str)
  m = UNIFORM_REGEX.match(str)
  if m
    ThinpXML::UniformDistribution.new(m[1].to_i, m[2].to_i)
  else
    ConstDistribution.new(Integer(str))
  end
end

#read_xml(io) ⇒ Object



52
53
54
55
56
# File 'lib/thinp_xml/thinp/parse.rb', line 52

def read_xml(io)
  l = ParseDetail::Listener.new
  REXML::Document.parse_stream(io, l)
  l.
end

#write_xml(metadata, io) ⇒ Object




33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/thinp_xml/thinp/emit.rb', line 33

def write_xml(, io)
  e = EmitterDetail::ThinpEmitter.new(io)

  e.emit_superblock(.superblock) do
    .devices.each do |dev|
      e.emit_device(dev) do
        dev.mappings.each do |m|
          e.emit_mapping(m)
        end
      end
    end
  end
end