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], [: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
-
#compare_devs(md_dev1, md_dev2) ⇒ Object
returns 3 arrays of mappings: unique to first arg, common, unique to second arg.
- #compare_thins(md1, md2, dev_id) ⇒ Object
- #emit_device(e, dev, &block) ⇒ Object
- #emit_mapping(e, m) ⇒ Object
- #emit_superblock(e, sb, &block) ⇒ Object
-
#expand_mappings(left, right) ⇒ Object
Turns 2 lists of mappings, into a list of pairs of mappings.
-
#get_device(md, dev_id) ⇒ Object
————————————————————–.
- #read_xml(io) ⇒ Object
- #write_xml(metadata, io) ⇒ Object
Class Method Details
.field_names(flds) ⇒ Object
31 32 33 |
# File 'lib/thinp_xml/thinp/xml_format.rb', line 31 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
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/thinp_xml/thinp/xml_format.rb', line 257 def compare_devs(md_dev1, md_dev2) m1 = md_dev1.mappings m2 = md_dev2.mappings left = Array.new center = Array.new right = Array.new (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
280 281 282 283 |
# File 'lib/thinp_xml/thinp/xml_format.rb', line 280 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
150 151 152 |
# File 'lib/thinp_xml/thinp/xml_format.rb', line 150 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
154 155 156 157 158 159 160 |
# File 'lib/thinp_xml/thinp/xml_format.rb', line 154 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
146 147 148 |
# File 'lib/thinp_xml/thinp/xml_format.rb', line 146 def emit_superblock(e, sb, &block) e.emit_tag(sb, 'superblock', :uuid, :time, :transaction, :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.
189 190 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 |
# File 'lib/thinp_xml/thinp/xml_format.rb', line 189 def (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
178 179 180 181 182 183 184 |
# File 'lib/thinp_xml/thinp/xml_format.rb', line 178 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
108 109 110 111 112 |
# File 'lib/thinp_xml/thinp/xml_format.rb', line 108 def read_xml(io) l = Listener.new Document.parse_stream(io, l) l. end |
#write_xml(metadata, io) ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/thinp_xml/thinp/xml_format.rb', line 162 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 |