Class: XRBP::SHAMap::InnerNode

Inherits:
Node
  • Object
show all
Defined in:
lib/xrbp/nodestore/shamap/inner_node.rb

Overview

A DB entry which may contain references of up to 16-child nodes, facilitating abstract tree-like traversal.

This class simply encapsulates children w/ hashes

Constant Summary

Constants inherited from Node

Node::LEAF_TYPES, Node::TYPES

Instance Attribute Summary collapse

Attributes inherited from Node

#hash

Instance Method Summary collapse

Methods inherited from Node

#leaf?, #tree_node?

Methods included from NodeFactory

#make

Constructor Details

#initialize(args = {}) ⇒ InnerNode

Returns a new instance of InnerNode.



10
11
12
13
14
15
16
17
18
# File 'lib/xrbp/nodestore/shamap/inner_node.rb', line 10

def initialize(args={})
  @v2    = args[:v2]
  @depth = args[:depth] || 0

  @common    = {}
  @hashes    = {}
  @children  = []
  @is_branch = 0
end

Instance Attribute Details

#commonObject

Returns the value of attribute common.



8
9
10
# File 'lib/xrbp/nodestore/shamap/inner_node.rb', line 8

def common
  @common
end

#depthObject

Returns the value of attribute depth.



8
9
10
# File 'lib/xrbp/nodestore/shamap/inner_node.rb', line 8

def depth
  @depth
end

#hashesObject

Returns the value of attribute hashes.



8
9
10
# File 'lib/xrbp/nodestore/shamap/inner_node.rb', line 8

def hashes
  @hashes
end

#is_branchObject

Returns the value of attribute is_branch.



8
9
10
# File 'lib/xrbp/nodestore/shamap/inner_node.rb', line 8

def is_branch
  @is_branch
end

Instance Method Details

#canonicalize_child(branch, node) ⇒ Object

Canonicalize and store child node at branch

Raises:

  • (ArgumentError)


66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/xrbp/nodestore/shamap/inner_node.rb', line 66

def canonicalize_child(branch, node)
  raise ArgumentError unless branch >= 0 &&
                             branch < 16
  raise unless node
  raise unless node.hash == hashes[branch]

  if @children[branch]
    return @children[branch]
  else
    return @children[branch] = node
  end
end

#child(branch) ⇒ Object

Returns child containing in given branch

Raises:

  • (ArgumentError)


59
60
61
62
63
# File 'lib/xrbp/nodestore/shamap/inner_node.rb', line 59

def child(branch)
  raise ArgumentError unless branch >= 0 &&
                             branch < 16
  @children[branch]
end

#child_hash(branch) ⇒ Object

Returns hash of child on given branch

Raises:

  • (ArgumentError)


52
53
54
55
56
# File 'lib/xrbp/nodestore/shamap/inner_node.rb', line 52

def child_hash(branch)
  raise ArgumentError unless branch >= 0 &&
                             branch < 16
  hashes[branch]
end

#common_prefix?(key) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
31
32
33
34
35
36
37
38
# File 'lib/xrbp/nodestore/shamap/inner_node.rb', line 28

def common_prefix?(key)
  hd = depth/2
  0.upto(hd) do |d|
    return false if common[d] != key[d]
  end

  return (common[hd] & 0xF0) &&
         (key[hd]    & 0xF0) if depth & 1

  return true
end

#empty?Boolean

Returns true if node has no children

Returns:

  • (Boolean)


41
42
43
# File 'lib/xrbp/nodestore/shamap/inner_node.rb', line 41

def empty?
  is_branch == 0
end

#empty_branch?(branch) ⇒ Boolean

Return true if specified branch is empty, else false

Returns:

  • (Boolean)


47
48
49
# File 'lib/xrbp/nodestore/shamap/inner_node.rb', line 47

def empty_branch?(branch)
  (is_branch & (1 << branch)) == 0
end

#inner?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/xrbp/nodestore/shamap/inner_node.rb', line 24

def inner?
  true
end

#update_hashObject

Update this node’s hash from child hashes



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/xrbp/nodestore/shamap/inner_node.rb', line 80

def update_hash
  nh = nil

  if is_branch != 0
    sha512 = OpenSSL::Digest::SHA512.new
    sha512 << HASH_+PREFIXES[:inner_node]
    hashes.each { |k,h|
      sha512 << v
    }
    nh = sha512.digest
  end

  return false if nh == self.hash
  self.hash = nh
  return true
end

#v2?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/xrbp/nodestore/shamap/inner_node.rb', line 20

def v2?
  @v2
end