Class: PEROBS::BTreeNodeCache

Inherits:
Object
  • Object
show all
Defined in:
lib/perobs/BTreeNodeCache.rb

Instance Method Summary collapse

Constructor Details

#initializeBTreeNodeCache

Returns a new instance of BTreeNodeCache.



34
35
36
# File 'lib/perobs/BTreeNodeCache.rb', line 34

def initialize
  clear
end

Instance Method Details

#[](address) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/perobs/BTreeNodeCache.rb', line 38

def [](address)
  if (node = @modified_nodes[address])
    return node
  end

  if (node = @top_nodes[address])
    return node
  end

  if (node = @ephemeral_nodes[address])
    return node
  end

  nil
end

#clearObject

Remove all nodes from the cache.



98
99
100
101
102
# File 'lib/perobs/BTreeNodeCache.rb', line 98

def clear
  @top_nodes = {}
  @ephemeral_nodes = {}
  @modified_nodes = {}
end

#delete(address) ⇒ Object

Remove a node from the cache.

Parameters:

  • address (Integer)

    address of node to remove.



82
83
84
85
86
# File 'lib/perobs/BTreeNodeCache.rb', line 82

def delete(address)
  @ephemeral_nodes.delete(address)
  @top_nodes.delete(address)
  @modified_nodes.delete(address)
end

#flush(now = false) ⇒ Object

Flush all dirty nodes into the backing store.



89
90
91
92
93
94
95
# File 'lib/perobs/BTreeNodeCache.rb', line 89

def flush(now = false)
  if now || @modified_nodes.size > 1024
    @modified_nodes.each_value { |node| node.write_node }
    @modified_nodes = {}
  end
  @ephemeral_nodes = {}
end

#insert(node) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/perobs/BTreeNodeCache.rb', line 61

def insert(node)
  unless node
    PEROBS.log.fatal "nil cannot be cached"
  end
  node = node.get_node if node.is_a?(BTreeNodeLink)

  @ephemeral_nodes[node.node_address] = node

  if !@top_nodes.include?(node) && node.is_top?
    @top_nodes[node.node_address] = node
  end
end

#mark_as_modified(node) ⇒ Object



74
75
76
77
78
# File 'lib/perobs/BTreeNodeCache.rb', line 74

def mark_as_modified(node)
  node = node.get_node if node.is_a?(BTreeNodeLink)
  @modified_nodes[node.node_address] = node
  insert(node)
end

#set_root(node) ⇒ Object



54
55
56
57
58
59
# File 'lib/perobs/BTreeNodeCache.rb', line 54

def set_root(node)
  node = node.get_node if node.is_a?(BTreeNodeLink)

  @top_nodes = {}
  @top_nodes[node.node_address] = node
end