Class: StrokeDB::ArchiveVolume

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

Defined Under Namespace

Classes: VolumeCapacityExceeded, VolumeClosedException

Constant Summary collapse

DEFAULT_SIZE =
64*1024*1024
DEFAULT_PATH =
"."

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ArchiveVolume

Open a volume in a directory :path, with UUID :uuid and a specified :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.

Defaults:

:path => "."
:size => 64 Mb

Example:

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


26
27
28
29
# File 'lib/strokedb/volumes/archive_volume.rb', line 26

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

Instance Attribute Details

#file_pathObject (readonly)

Returns the value of attribute file_path.



4
5
6
# File 'lib/strokedb/volumes/archive_volume.rb', line 4

def file_path
  @file_path
end

#tailObject (readonly)

Returns the value of attribute tail.



4
5
6
# File 'lib/strokedb/volumes/archive_volume.rb', line 4

def tail
  @tail
end

Instance Method Details

#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.



57
58
59
# File 'lib/strokedb/volumes/archive_volume.rb', line 57

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.



64
65
66
67
# File 'lib/strokedb/volumes/archive_volume.rb', line 64

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

#insert(data) ⇒ Object

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



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

def insert(data)
  raise VolumeCapacityExceeded if (new_tail = @tail + 4 + data.size) > size
  @file.seek(@tail)
  @file.write([data.size].pack('N') + data)
  t = @tail
  @tail = new_tail
  write_tail(@file, @tail)
  t
end

#pathObject



69
70
71
# File 'lib/strokedb/volumes/archive_volume.rb', line 69

def path
  @options['path']
end

#raise_volume_closed(*args) ⇒ Object

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



138
139
140
# File 'lib/strokedb/volumes/archive_volume.rb', line 138

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

#raw_uuidObject



88
89
90
# File 'lib/strokedb/volumes/archive_volume.rb', line 88

def raw_uuid
  @raw_uuid ||= uuid.to_raw_uuid
end

#read(position) ⇒ Object

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



34
35
36
37
38
# File 'lib/strokedb/volumes/archive_volume.rb', line 34

def read(position)
  @file.seek(position)
  size = @file.readbytes(4).unpack('N').first
  @file.readbytes(size)
end

#sizeObject



73
74
75
# File 'lib/strokedb/volumes/archive_volume.rb', line 73

def size
  @options['size']
end

#uuidObject



77
78
79
80
81
82
83
84
85
86
# File 'lib/strokedb/volumes/archive_volume.rb', line 77

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