Module: XMLFormat

Includes:
REXML
Defined in:
lib/thinp_xml/thinp/xml_format.rb

Defined Under Namespace

Classes: Device, Emitter, Listener, Mapping, Metadata, Superblock

Constant Summary collapse

SUPERBLOCK_FIELDS =
[[:uuid, :string],
[:time, :int],
											 [:flags, :int],
											 [:version, :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



33
34
35
# File 'lib/thinp_xml/thinp/xml_format.rb', line 33

def self.field_names(flds)
  flds.map {|p| p[0]}
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



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/thinp_xml/thinp/xml_format.rb', line 259

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



282
283
284
285
# File 'lib/thinp_xml/thinp/xml_format.rb', line 282

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

#emit_device(e, dev, &block) ⇒ Object



152
153
154
# File 'lib/thinp_xml/thinp/xml_format.rb', line 152

def emit_device(e, dev, &block)
  e.emit_tag(dev, 'device', :dev_id, :mapped_blocks, :transaction, :creation_time, :snap_time, &block)
end

#emit_mapping(e, m) ⇒ Object



156
157
158
159
160
161
162
# File 'lib/thinp_xml/thinp/xml_format.rb', line 156

def emit_mapping(e, m)
  if m.length == 1
    e.emit_line("<single_mapping origin_block=\"#{m.origin_begin}\" data_block=\"#{m.data_begin}\" time=\"#{m.time}\"/>")
  else
    e.emit_tag(m, 'range_mapping', :origin_begin, :data_begin, :length, :time)
  end
end

#emit_superblock(e, sb, &block) ⇒ Object



148
149
150
# File 'lib/thinp_xml/thinp/xml_format.rb', line 148

def emit_superblock(e, sb, &block)
  e.emit_tag(sb, 'superblock', :uuid, :time, :transaction, :flags, :version, :data_block_size, :nr_data_blocks, &block)
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.



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
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
255
# File 'lib/thinp_xml/thinp/xml_format.rb', line 191

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




180
181
182
183
184
185
186
# File 'lib/thinp_xml/thinp/xml_format.rb', line 180

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

#read_xml(io) ⇒ Object



110
111
112
113
114
# File 'lib/thinp_xml/thinp/xml_format.rb', line 110

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

#write_xml(metadata, io) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/thinp_xml/thinp/xml_format.rb', line 164

def write_xml(, io)
  e = Emitter.new(io)

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