Class: CacheXML::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/thinp_xml/cache/builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBuilder

Returns a new instance of Builder.



10
11
12
13
14
15
16
17
18
19
# File 'lib/thinp_xml/cache/builder.rb', line 10

def initialize
  @uuid = ''
  @block_size = 128
  @nr_cache_blocks = 0
  @policy_name = 'mq'
  @mapping_policy = :random
  @nr_mappings = 0
  @dirty_percentage = 0
  @hint_width = 4
end

Instance Attribute Details

#block_sizeObject

Returns the value of attribute block_size.



7
8
9
# File 'lib/thinp_xml/cache/builder.rb', line 7

def block_size
  @block_size
end

#dirty_percentageObject

Returns the value of attribute dirty_percentage.



8
9
10
# File 'lib/thinp_xml/cache/builder.rb', line 8

def dirty_percentage
  @dirty_percentage
end

#hint_widthObject

Returns the value of attribute hint_width.



8
9
10
# File 'lib/thinp_xml/cache/builder.rb', line 8

def hint_width
  @hint_width
end

#mapping_policyObject

Returns the value of attribute mapping_policy.



8
9
10
# File 'lib/thinp_xml/cache/builder.rb', line 8

def mapping_policy
  @mapping_policy
end

#nr_cache_blocksObject

Returns the value of attribute nr_cache_blocks.



7
8
9
# File 'lib/thinp_xml/cache/builder.rb', line 7

def nr_cache_blocks
  @nr_cache_blocks
end

#nr_mappingsObject

Returns the value of attribute nr_mappings.



8
9
10
# File 'lib/thinp_xml/cache/builder.rb', line 8

def nr_mappings
  @nr_mappings
end

#policy_nameObject

Returns the value of attribute policy_name.



7
8
9
# File 'lib/thinp_xml/cache/builder.rb', line 7

def policy_name
  @policy_name
end

#uuidObject

Returns the value of attribute uuid.



7
8
9
# File 'lib/thinp_xml/cache/builder.rb', line 7

def uuid
  @uuid
end

Instance Method Details

#generateObject



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
# File 'lib/thinp_xml/cache/builder.rb', line 21

def generate
  superblock = Superblock.new(@uuid, @block_size.to_i,
                              @nr_cache_blocks.to_i,
                              @policy_name, @hint_width)
  mappings = []

  case @mapping_policy
  when :linear
    cb = 0
    ob = safe_rand(@nr_cache_blocks - @nr_mappings)

    @nr_mappings.times do
      dirty = safe_rand(100) < @dirty_percentage
      mappings << Mapping.new(cb, ob, dirty)
      cb += 1
      ob += 1
    end

  when :random
    origin_blocks = []

    ob = 0
    @nr_mappings.times do
      origin_blocks << ob
      ob += 1
    end

    0.upto(@nr_mappings - 1) do |n|
      tmp = origin_blocks[n]

      index = n + rand(@nr_mappings - n)
      origin_blocks[n] = origin_blocks[index]
      origin_blocks[index] = tmp

      dirty = safe_rand(100) < @dirty_percentage
      mappings << Mapping.new(n, origin_blocks[n], dirty)
    end

  else
    raise "unknown mapping policy"
  end

  hints = []

  Metadata.new(superblock, mappings, hints)
end