lib/perobs/IndexTree.rb in perobs-2.4.0 vs lib/perobs/IndexTree.rb in perobs-2.4.1

- old
+ new

@@ -23,10 +23,11 @@ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +require 'perobs/Log' require 'perobs/FixedSizeBlobFile' require 'perobs/IndexTreeNode' module PEROBS @@ -89,34 +90,43 @@ # Return an IndexTreeNode object that corresponds to the given address. # @param nibble [Fixnum] Index of the nibble the node should correspond to # @param address [Integer] Address of the node in @nodes or nil def get_node(nibble, address = nil) + if nibble >= 16 + # We only support 64 bit keys, so nibble cannot be larger than 15. + PEROBS.log.fatal "Nibble must be within 0 - 15 but is #{nibble}" + end # Generate a mask for the least significant bits up to and including the # nibble. mask = (2 ** ((1 + nibble) * 4)) - 1 - if address && (node = @node_cache[address & mask]) - # We have an address and have found the node in the node cache. - return node - else + #if address && (node = @node_cache[address & mask]) + # # We have an address and have found the node in the node cache. + # return node + #else + begin # We don't have a IndexTreeNode object yet for this node. Create it # with the data from the 'database_index' file. node = IndexTreeNode.new(self, nibble, address) # Add the node to the node cache if it's up to MAX_CACHED_LEVEL levels # down from the root. - @node_cache[address & mask] = node if nibble <= MAX_CACHED_LEVEL + #@node_cache[address & mask] = node if nibble <= MAX_CACHED_LEVEL return node end end # Delete a node from the tree that corresponds to the address. # @param nibble [Fixnum] The corresponding nibble for the node # @param address [Integer] The address of the node in @nodes def delete_node(nibble, address) + if nibble >= 16 + # We only support 64 bit keys, so nibble cannot be larger than 15. + PEROBS.log.fatal "Nibble must be within 0 - 15 but is #{nibble}" + end # First delete the node from the node cache. mask = (2 ** ((1 + nibble) * 4)) - 1 - @node_cache.delete(address & mask) + #@node_cache.delete(address & mask) # Then delete it from the 'database_index' file. @nodes.delete_blob(address) end # Store a ID/value touple into the tree. The value can later be retrieved @@ -147,10 +157,10 @@ @root.delete_value(id) end # Check if the index is OK and matches the flat_file data. def check(flat_file) - @root.check(flat_file) + @root.check(flat_file, 0) end # Convert the tree into a human readable form. # @return [String] def inspect