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

- old
+ new

@@ -33,15 +33,10 @@ # The IndexTree maps the object ID to the address in the FlatFile. The # search in the tree is much faster than the linear search in the FlatFile. class IndexTree - # Determines how many levels of the IndexTree will be kept in memory to - # accerlerate the access. A number of 7 will keep up to 21845 entries in - # the cache but will accelerate the access to the FlatFile address. - MAX_CACHED_LEVEL = 7 - attr_reader :nodes, :ids def initialize(db_dir) # Directory path used to store the files. @db_dir = db_dir @@ -53,14 +48,10 @@ # The node sequence usually only reveals a partial match with the # requested ID. So, the leaves of the tree point to the object_id_index # file which contains the full object ID and the address of the # corresponding object in the FlatFile. @ids = FixedSizeBlobFile.new(db_dir, 'object_id_index', 2 * 8) - - # The first MAX_CACHED_LEVEL levels of nodes will be cached in memory to - # improve access times. - @node_cache = {} end # Open the tree files. def open @nodes.open @@ -70,10 +61,11 @@ # Close the tree files. def close @ids.close @nodes.close + @root = nil end # Flush out all unwritten data def sync @ids.sync @@ -82,11 +74,10 @@ # Delete all data from the tree. def clear @nodes.clear @ids.clear - @node_cache = {} @root = IndexTreeNode.new(self, 0, 0) end # Return an IndexTreeNode object that corresponds to the given address. # @param nibble [Fixnum] Index of the nibble the node should correspond to @@ -94,54 +85,34 @@ 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 - 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 - return node - end + # 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) + return node 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) - # Then delete it from the 'database_index' file. + + # 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 # by the ID again. IDs are always unique in the tree. If the ID already # exists in the tree, the value will be overwritten. # @param id [Integer] ID or key # @param value [Integer] value to store def put_value(id, value) - #MAX_CACHED_LEVEL.downto(0) do |i| - # mask = (2 ** ((1 + i) * 4)) - 1 - # if (node = @node_cache[value & mask]) - # return node.put_value(id, value) - # end - #end @root.put_value(id, value) end # Retrieve the value that was stored with the given ID. # @param id [Integer] ID of the value to retrieve