Class: Lvm2Thin::BTree

Inherits:
Object
  • Object
show all
Defined in:
lib/VolumeManager/LVM/thin/btree.rb

Direct Known Subclasses

DataMap, MappingTree

Constant Summary collapse

FLAGS =
{ :internal => 1, :leaf => 2}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(superblock, root_address, value_type) ⇒ BTree

Returns a new instance of BTree.



7
8
9
10
11
# File 'lib/VolumeManager/LVM/thin/btree.rb', line 7

def initialize(superblock, root_address, value_type)
  @superblock   = superblock
  @root_address = root_address
  @value_type   = value_type
end

Instance Attribute Details

#root_addressObject

Returns the value of attribute root_address.



5
6
7
# File 'lib/VolumeManager/LVM/thin/btree.rb', line 5

def root_address
  @root_address
end

Instance Method Details

#[](key) ⇒ Object



79
80
81
# File 'lib/VolumeManager/LVM/thin/btree.rb', line 79

def [](key)
  return to_h[key]
end

#entriesObject



59
60
61
62
63
64
# File 'lib/VolumeManager/LVM/thin/btree.rb', line 59

def entries
  @entries ||= begin
    @superblock.seek value_base
    @superblock.read_structs @value_type, num_entries
  end
end

#entry_for(key) ⇒ Object



66
67
68
# File 'lib/VolumeManager/LVM/thin/btree.rb', line 66

def entry_for(key)
  entries[keys.index(key)]
end

#internal?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/VolumeManager/LVM/thin/btree.rb', line 20

def internal?
  (root['flags'] & FLAGS[:internal]) != 0
end

#key_address(i) ⇒ Object



40
41
42
# File 'lib/VolumeManager/LVM/thin/btree.rb', line 40

def key_address(i)
  key_base + i * 8
end

#key_baseObject



36
37
38
# File 'lib/VolumeManager/LVM/thin/btree.rb', line 36

def key_base
  root_address + DISK_NODE.size
end

#keysObject



52
53
54
55
56
57
# File 'lib/VolumeManager/LVM/thin/btree.rb', line 52

def keys
  @keys ||= begin
    @superblock.seek key_base
    @superblock.read(num_entries * 8).unpack("Q#{num_entries}")
  end
end

#leaf?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/VolumeManager/LVM/thin/btree.rb', line 24

def leaf?
  (root['flags'] & FLAGS[:leaf]) != 0
end

#max_entriesObject



32
33
34
# File 'lib/VolumeManager/LVM/thin/btree.rb', line 32

def max_entries
  @max_entries ||= root['max_entries']
end

#num_entriesObject



28
29
30
# File 'lib/VolumeManager/LVM/thin/btree.rb', line 28

def num_entries
  @num_entries ||= root['nr_entries']
end

#rootObject



13
14
15
16
17
18
# File 'lib/VolumeManager/LVM/thin/btree.rb', line 13

def root
  @root ||= begin
    @superblock.seek root_address
    @superblock.read_struct DISK_NODE
  end
end

#to_hObject



70
71
72
73
74
75
76
77
# File 'lib/VolumeManager/LVM/thin/btree.rb', line 70

def to_h
  @h ||=
    Hash[0.upto(num_entries-1).collect do |i|
      k = keys[i]
      e = entries[i].kind_of?(BTree) ? entries[i].to_h : entries[i]
      [k, e]
    end]
end

#value_address(i) ⇒ Object



48
49
50
# File 'lib/VolumeManager/LVM/thin/btree.rb', line 48

def value_address(i)
  value_base + @value_type.size * i
end

#value_baseObject



44
45
46
# File 'lib/VolumeManager/LVM/thin/btree.rb', line 44

def value_base
  key_address(max_entries)
end