Class: StrokeDB::BlockVolume

Inherits:
Object
  • Object
show all
Defined in:
lib/strokedb/volumes/block_volume.rb

Overview

TODO: inherit from the common AbstractVolume

Defined Under Namespace

Classes: VolumeClosedException

Constant Summary collapse

HEADER_LENGTH =

block_size, blocks_count

8
DEFAULT_BLOCKS_COUNT =
1024
DEFAULT_PATH =
"."

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ BlockVolume

Open a volume in a directory :path, with UUID :uuid and a specified :block_size. If the file does not exist, it is created and filled with zero bytes up to the specified size. Otherwise, it is just opened and ready for reads and writes. File contains block_count blocks of :block_size: bytes size. When insertion is done to a new position, file is autoextended.

Required params: :block_size and :uuid Default :path is “.”, :blocks_count is 1024

Example:

DataVolume.new(:uuid => uuid, :path => "/var/dir", :block_size => 1024)


25
26
27
28
29
30
# File 'lib/strokedb/volumes/block_volume.rb', line 25

def initialize(options = {})
  @options = options.stringify_keys.reverse_merge(
    'path' => DEFAULT_PATH, 
    'blocks_count' => DEFAULT_BLOCKS_COUNT)
  initialize_file
end

Instance Attribute Details

#blocks_countObject (readonly)

Returns the value of attribute blocks_count.



6
7
8
# File 'lib/strokedb/volumes/block_volume.rb', line 6

def blocks_count
  @blocks_count
end

#file_pathObject (readonly)

Returns the value of attribute file_path.



6
7
8
# File 'lib/strokedb/volumes/block_volume.rb', line 6

def file_path
  @file_path
end

Instance Method Details

#block_sizeObject



72
73
74
# File 'lib/strokedb/volumes/block_volume.rb', line 72

def block_size
  @options['block_size']
end

#close!Object

Close the volume file. You cannot read/insert after that operation. In such case, VolumeClosedException is raised. Call DataVolume.new to open volume again.



56
57
58
# File 'lib/strokedb/volumes/block_volume.rb', line 56

def close!
  safe_close
end

#delete!Object

Close and delete the volume file. You cannot read/insert after that operation. In such case, VolumeClosedException is raised.



63
64
65
66
# File 'lib/strokedb/volumes/block_volume.rb', line 63

def delete!
  safe_close
  File.delete(@file_path)
end

#insert(index, data) ⇒ Object

Write some data to the end of the file. Returns record position.



44
45
46
47
48
49
50
# File 'lib/strokedb/volumes/block_volume.rb', line 44

def insert(index, data)
  extend_volume! if index >= @blocks_count
  csize = @block_size
  @file.seek(HEADER_LENGTH + index * csize)
  @file.write(data)
  self
end

#pathObject



68
69
70
# File 'lib/strokedb/volumes/block_volume.rb', line 68

def path
  @options['path']
end

#raise_volume_closed(*args) ⇒ Object

read and insert methods are aliased to this when file is closed or deleted.



143
144
145
# File 'lib/strokedb/volumes/block_volume.rb', line 143

def raise_volume_closed(*args)
  raise VolumeClosedException, "Throw this object away and instantiate another one."
end

#read(index) ⇒ Object

Read a record sitting in a position in the volume file. Record length is stored in a first 4 bytes before the record.



35
36
37
38
39
# File 'lib/strokedb/volumes/block_volume.rb', line 35

def read(index)
  csize = @block_size
  @file.seek(HEADER_LENGTH + index * csize)
  @file.readbytes(csize)
end

#uuidObject



80
81
82
83
84
85
86
87
88
89
# File 'lib/strokedb/volumes/block_volume.rb', line 80

def uuid
  case @options['uuid'] 
  when /^#{UUID_RE}$/
    @options['uuid']
  when nil
    @options['uuid'] = Util.random_uuid
  else
    @options['uuid'] = @options['uuid'].to_formatted_uuid
  end
end