lib/perobs/BTreeBlob.rb in perobs-2.3.1 vs lib/perobs/BTreeBlob.rb in perobs-2.4.0
- old
+ new
@@ -25,10 +25,12 @@
# 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 'zlib'
+require 'perobs/Log'
+
module PEROBS
# This class manages the usage of the data blobs in the corresponding
# HashedBlobsDB object.
class BTreeBlob
@@ -75,11 +77,11 @@
else
bytes = raw.bytesize
crc32 = Zlib.crc32(raw, 0)
start_address = reserve_bytes(id, bytes, crc32)
if write_to_blobs_file(raw, start_address) != bytes
- raise RuntimeError, 'Object length does not match written bytes'
+ PEROBS.log.fatal 'Object length does not match written bytes'
end
write_index
end
end
@@ -116,12 +118,12 @@
break
end
end
unless found
- raise ArgumentError,
- "Cannot find an entry for ID #{'%016X' % id} #{id} to mark"
+ PEROBS.log.fatal "Cannot find an entry for ID #{'%016X' % id} " +
+ "#{id} to mark"
end
write_index
end
@@ -134,12 +136,11 @@
@entries.each do |entry|
return entry[MARKED] != 0 if entry[ID] == id
end
return false if ignore_errors
- raise ArgumentError,
- "Cannot find an entry for ID #{'%016X' % id} to check"
+ PEROBS.log.fatal "Cannot find an entry for ID #{'%016X' % id} to check"
end
# Remove all entries from the index that have not been marked.
# @return [Array] List of deleted object IDs.
def delete_unmarked_entries
@@ -163,19 +164,19 @@
# Run a basic consistency check.
# @param repair [TrueClass/FalseClass] Not used right now
# @return [TrueClass/FalseClass] Always true right now
def check(repair = false)
# Determine size of the data blobs file.
- data_file_size = File.exists?(@blobs_file_name) ?
+ data_file_size = File.exist?(@blobs_file_name) ?
File.size(@blobs_file_name) : 0
next_start = 0
prev_entry = nil
@entries.each do |entry|
# Entries should never overlap
if prev_entry && next_start > entry[START]
- raise RuntimeError,
+ PEROBS.log.fatal
"#{@dir}: Index entries are overlapping\n" +
"ID: #{'%016X' % prev_entry[ID]} " +
"Start: #{prev_entry[START]} " +
"Bytes: #{prev_entry[BYTES]}\n" +
"ID: #{'%016X' % entry[ID]} Start: #{entry[START]} " +
@@ -183,11 +184,11 @@
end
next_start = entry[START] + entry[BYTES]
# Entries must fit within the data file
if next_start > data_file_size
- raise RuntimeError,
+ PEROBS.log.fatal
"#{@dir}: Entry for ID #{'%016X' % entry[ID]} " +
"goes beyond 'data' file " +
"size (#{data_file_size})\n" +
"ID: #{'%016X' % entry[ID]} Start: #{entry[START]} " +
"Bytes: #{entry[BYTES]}"
@@ -207,29 +208,28 @@
# @return [Fixnum] number of bytes written
def write_to_blobs_file(raw, address)
begin
File.write(@blobs_file_name, raw, address)
rescue => e
- raise IOError,
- "Cannot write blobs file #{@blobs_file_name}: #{e.message}"
+ PEROBS.log.fatal "Cannot write blobs file #{@blobs_file_name}: " +
+ e.message
end
end
# Read _bytes_ bytes from the file starting at offset _address_.
# @param entry [Array] Index entry for the object
# @return [String] Raw bytes of the blob.
def read_from_blobs_file(entry)
begin
raw = File.read(@blobs_file_name, entry[BYTES], entry[START])
rescue => e
- raise IOError,
- "Cannot read blobs file #{@blobs_file_name}: #{e.message}"
+ PEROBS.log.fatal "Cannot read blobs file #{@blobs_file_name}: " +
+ e.message
end
if Zlib.crc32(raw, 0) != entry[CRC]
- raise RuntimeError,
- "BTreeBlob for object #{entry[ID]} has been corrupted: " +
- "Checksum mismatch"
+ PEROBS.log.fatal "BTreeBlob for object #{entry[ID]} has been " +
+ "corrupted: Checksum mismatch"
end
raw
end
@@ -294,11 +294,11 @@
@entries = []
@entries_by_id = {}
entry_bytes = 29
entry_format = 'QQQCL'
restore_crc = false
- if File.exists?(@index_file_name)
+ if File.exist?(@index_file_name)
begin
File.open(@index_file_name, 'rb') do |f|
# Since version 2.3.0, all index files start with a header.
# Earlier versions did not yet have this header. The header is 24
# bytes long. The 2nd set of 8 bytes must be 0 to distinguish the
@@ -336,22 +336,22 @@
# If the index file was written with version <= 2.2.0 we have
# to compute the CRC from the data blob.
begin
raw = File.read(@blobs_file_name, e[BYTES], e[START])
rescue => e
- raise IOError,
- "Cannot read blobs file #{@blobs_file_name}: #{e.message}"
+ PEROBS.log.fatal "Cannot read blobs file " +
+ "#{@blobs_file_name}: #{e.message}"
end
e[CRC] = Zlib.crc32(raw)
end
@entries << e
@entries_by_id[e[ID]] = e
end
end
rescue => e
- raise RuntimeError,
- "BTreeBlob file #{@index_file_name} corrupted: #{e.message}"
+ PEROBS.log.fatal "BTreeBlob file #{@index_file_name} corrupted: " +
+ e.message
end
end
end
def write_index
@@ -362,12 +362,11 @@
@entries.each do |entry|
f.write(entry.pack('QQQCL'))
end
end
rescue => e
- raise RuntimeError,
- "Cannot write BTreeBlob index file #{@index_file_name}: " +
- e.message
+ PEROBS.log.fatal "Cannot write BTreeBlob index file " +
+ "#{@index_file_name}: " + e.message
end
end
def split_blob
# Rename the index file to hide the blob file from the DB.