Class: DiskBlockFile

Inherits:
BlockFile show all
Defined in:
lib/geotree/diskblockfile.rb

Overview

Block file that stores its contents to disk

Constant Summary

Constants inherited from BlockFile

BlockFile::FIRST_BLOCK_ID, BlockFile::HDR_BLOCKSIZE_, BlockFile::HDR_MAXINDEX_, BlockFile::HDR_RECYCLEINDEX_, BlockFile::HDR_SIZE_BYTES_, BlockFile::HDR_USERSTART_, BlockFile::HDR_VERSION_, BlockFile::INT_BYTES_, BlockFile::RC_ENTRIES_START_, BlockFile::RC_ENTRIES_USED_, BlockFile::RC_PREV_DIR_NAME_, BlockFile::USER_HEADER_INTS, BlockFile::VERSION_

Instance Attribute Summary

Attributes inherited from BlockFile

#block_size

Instance Method Summary collapse

Methods inherited from BlockFile

#alloc, #alloc_buffer, clear_block, #close, copy_block, #dump, #free, #inspect, #name_max, #open, #open?, read_int, #read_user, #to_s, write_int, #write_user

Constructor Details

#initialize(block_size, path) ⇒ DiskBlockFile

Returns a new instance of DiskBlockFile.



7
8
9
10
# File 'lib/geotree/diskblockfile.rb', line 7

def initialize(block_size, path)
 @path = path 
 super(block_size)
end

Instance Method Details

#close_storageObject



55
56
57
58
# File 'lib/geotree/diskblockfile.rb', line 55

def close_storage
  flush
  @file = nil   
end

#flushObject



60
61
62
# File 'lib/geotree/diskblockfile.rb', line 60

def flush
  @file.flush
end

#open_storageObject

Raises:

  • (IOError)


47
48
49
50
51
52
53
# File 'lib/geotree/diskblockfile.rb', line 47

def open_storage
 existed = File.file?(@path)
 @file = File.open(@path, existed ? "r+b" : "w+b")
 raise IOError if !@file
 
 existed
end

#read(block_name, dest_buffer = nil) ⇒ Object

Raises:

  • (IOError)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/geotree/diskblockfile.rb', line 12

def read(block_name, dest_buffer = nil)
  db = false
 
  dest_buffer ||= alloc_buffer

  offset = block_size * block_name
  
  @file.pos = offset
     
  @file.read(block_size,dest_buffer)
  raise IOError if (dest_buffer.size != block_size)
  
  !db || hex_dump(dest_buffer,"Disk.read #{block_name}")
   
  dest_buffer 
end

#write(block_name, src_buffer) ⇒ Object

Raises:

  • (ArgumentError)


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/geotree/diskblockfile.rb', line 29

def write(block_name, src_buffer)
 
  db = false
  !db || pr("Disk.write %d\n",block_name)
  !db || hex_dump(src_buffer)

  offset = block_size * block_name
  @file.pos = offset  
       
  raise ArgumentError if src_buffer.size != block_size
  
  count = @file.write(src_buffer)
  
  if count != src_buffer.size
    raise IOError,"wrote #{count} bytes instead of #{src_buffer.size}"
  end
end