Class: OsCtl::Lib::Zfs::ObjsetStats::PoolTree

Inherits:
Object
  • Object
show all
Defined in:
lib/libosctl/zfs/objset_stats/pool_tree.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pool) ⇒ PoolTree

Returns a new instance of PoolTree.

Parameters:

  • pool (String)


6
7
8
9
10
11
# File 'lib/libosctl/zfs/objset_stats/pool_tree.rb', line 6

def initialize(pool)
  @pool = pool
  @list = []
  @index = {}
  @root = nil
end

Instance Attribute Details

#poolObject (readonly)

Returns the value of attribute pool.



3
4
5
# File 'lib/libosctl/zfs/objset_stats/pool_tree.rb', line 3

def pool
  @pool
end

#rootObject (readonly)

Returns the value of attribute root.



3
4
5
# File 'lib/libosctl/zfs/objset_stats/pool_tree.rb', line 3

def root
  @root
end

Instance Method Details

#<<(objset) ⇒ Object

Parameters:



14
15
16
17
18
# File 'lib/libosctl/zfs/objset_stats/pool_tree.rb', line 14

def <<(objset)
  @list << objset
  @index[objset.dataset_name] = objset
  @root = objset if objset.dataset_name == pool
end

#[](name) ⇒ Object

Parameters:

  • name (String)


21
22
23
# File 'lib/libosctl/zfs/objset_stats/pool_tree.rb', line 21

def [](name)
  @index[name]
end

#aggregate_stats(into: nil) ⇒ Object



50
51
52
# File 'lib/libosctl/zfs/objset_stats/pool_tree.rb', line 50

def aggregate_stats(into: nil)
  root.aggregate_stats(into:)
end

#buildObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/libosctl/zfs/objset_stats/pool_tree.rb', line 25

def build
  sorted = @list.sort { |a, b| a.dataset_name <=> b.dataset_name }
  sorted.shift

  parent_stack = [root]

  sorted.each do |objset|
    parent_n = 0

    parent_stack.reverse_each.with_index do |parent, i|
      if objset.dataset_name.start_with?("#{parent.dataset_name}/")
        parent.subdatasets << objset
        break
      end

      parent_n = i + 1
    end

    parent_stack = parent_stack[0..(parent_stack.size - parent_n)]
    parent_stack << objset
  end

  @list.clear
end


54
55
56
57
58
59
# File 'lib/libosctl/zfs/objset_stats/pool_tree.rb', line 54

def print(objset, indent: 0)
  puts "#{' ' * indent}#{objset.dataset_name}:"
  objset.subdatasets.each do |subset|
    print(subset, indent: indent + 2)
  end
end