lib/perobs/SpaceTree.rb in perobs-4.0.0 vs lib/perobs/SpaceTree.rb in perobs-4.1.0

- old
+ new

@@ -1,10 +1,10 @@ # encoding: UTF-8 # # = SpaceTree.rb -- Persistent Ruby Object Store # -# Copyright (c) 2016, 2017 by Chris Schlaeger <chris@taskjuggler.org> +# Copyright (c) 2016, 2017, 2018 by Chris Schlaeger <chris@taskjuggler.org> # # MIT License # # Permission is hereby granted, free of charge, to any person obtaining # a copy of this software and associated documentation files (the @@ -38,24 +38,25 @@ # nodes can link to other nodes with smaller spaces, same spaces and bigger # spaces. The advantage of the ternary tree is that all nodes have equal # size which drastically simplifies the backing store operation. class SpaceTree - attr_reader :nodes, :cache + attr_reader :nodes, :cache, :progressmeter # Manage the free spaces tree in the specified directory # @param dir [String] directory path of an existing directory - def initialize(dir) + def initialize(dir, progressmeter) @dir = dir + @progressmeter = progressmeter # This EquiBlobsFile contains the nodes of the SpaceTree. - @nodes = EquiBlobsFile.new(@dir, 'database_spaces', + @nodes = EquiBlobsFile.new(@dir, 'database_spaces', progressmeter, SpaceTreeNode::NODE_BYTES, 1) # Benchmark runs showed a cache size of 128 to be a good compromise # between read and write performance trade-offs and memory consumption. - @cache = PersistentObjectCache.new(128, SpaceTreeNode, self) + @cache = PersistentObjectCache.new(128, 5000, SpaceTreeNode, self) end # Open the SpaceTree file. def open @nodes.open @@ -72,13 +73,18 @@ @nodes.close @root_address = nil @cache.clear end + # @return true if file is currently open. + def is_open? + !@root_address.nil? + end + # Flush all pending writes to the file system. def sync - @cache.flush + @cache.flush(true) @nodes.sync end # Set a new root node for the SpaceTree # @param node [SpaceTreeNode] @@ -103,14 +109,16 @@ # @param size [Integer] size of the space in bytes def add_space(address, size) if size <= 0 PEROBS.log.fatal "Size (#{size}) must be larger than 0." end - if has_space?(address, size) - PEROBS.log.fatal "The space with address #{address} and size #{size} " + - "can't be added twice." - end + # The following check is fairly costly and should never trigger unless + # there is a bug in the PEROBS code. Only use this for debugging. + #if has_space?(address, size) + # PEROBS.log.fatal "The space with address #{address} and size " + + # "#{size} can't be added twice." + #end root.add_space(address, size) end # Get a space that has at least the requested size. # @param size [Integer] Required size in bytes @@ -155,11 +163,12 @@ # Check if the index is OK and matches the flat_file data (if given). # @param flat_file [FlatFile] Flat file to compare with # @return True if space list matches, flase otherwise def check(flat_file = nil) - @nodes.check - root.check(flat_file) + sync + return false unless @nodes.check + root.check(flat_file, @nodes.total_entries) end # Iterate over all entries and yield address and size. def each root.each do |node, mode, stack|