Class: XRBP::SHAMap::InnerNode
- 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
Instance Attribute Summary collapse
-
#common ⇒ Object
Returns the value of attribute common.
-
#depth ⇒ Object
Returns the value of attribute depth.
-
#hashes ⇒ Object
Returns the value of attribute hashes.
-
#is_branch ⇒ Object
Returns the value of attribute is_branch.
Attributes inherited from Node
Instance Method Summary collapse
-
#canonicalize_child(branch, node) ⇒ Object
Canonicalize and store child node at branch.
-
#child(branch) ⇒ Object
Returns child containing in given branch.
-
#child_hash(branch) ⇒ Object
Returns hash of child on given branch.
- #common_prefix?(key) ⇒ Boolean
-
#empty? ⇒ Boolean
Returns true if node has no children.
-
#empty_branch?(branch) ⇒ Boolean
Return true if specified branch is empty, else false.
-
#initialize(args = {}) ⇒ InnerNode
constructor
A new instance of InnerNode.
- #inner? ⇒ Boolean
-
#update_hash ⇒ Object
Update this node’s hash from child hashes.
- #v2? ⇒ Boolean
Methods inherited from Node
Methods included from NodeFactory
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
#common ⇒ Object
Returns the value of attribute common.
8 9 10 |
# File 'lib/xrbp/nodestore/shamap/inner_node.rb', line 8 def common @common end |
#depth ⇒ Object
Returns the value of attribute depth.
8 9 10 |
# File 'lib/xrbp/nodestore/shamap/inner_node.rb', line 8 def depth @depth end |
#hashes ⇒ Object
Returns the value of attribute hashes.
8 9 10 |
# File 'lib/xrbp/nodestore/shamap/inner_node.rb', line 8 def hashes @hashes end |
#is_branch ⇒ Object
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
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
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
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
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
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
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
24 25 26 |
# File 'lib/xrbp/nodestore/shamap/inner_node.rb', line 24 def inner? true end |
#update_hash ⇒ Object
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
20 21 22 |
# File 'lib/xrbp/nodestore/shamap/inner_node.rb', line 20 def v2? @v2 end |