Class: Ole::Storage::RangesIOResizeable

Inherits:
RangesIO
  • Object
show all
Defined in:
lib/ole/storage/base.rb

Overview

like normal RangesIO, but Ole::Storage specific. the ranges are backed by an AllocationTable, and can be resized. used for read/write to 2 streams:

  1. serialized dirent data

  2. sbat table data

  3. all dirents but through RangesIOMigrateable below

Note that all internal access to first_block is through accessors, as it is sometimes useful to redirect it.

Direct Known Subclasses

RangesIOMigrateable

Instance Attribute Summary collapse

Attributes inherited from RangesIO

#io, #mode, #pos, #ranges, #size

Instance Method Summary collapse

Methods inherited from RangesIO

#close, #eof?, #gets, #inspect, open, #read, #rewind, #write

Constructor Details

#initialize(bat, mode = 'r', params = {}) ⇒ RangesIOResizeable

Returns a new instance of RangesIOResizeable.

Raises:

  • (ArgumentError)


604
605
606
607
608
609
610
611
612
613
# File 'lib/ole/storage/base.rb', line 604

def initialize bat, mode='r', params={}
	mode, params = 'r', mode if Hash === mode
	first_block, size = params.values_at :first_block, :size
	raise ArgumentError, 'must specify first_block' unless first_block
	@bat = bat
	self.first_block = first_block
	# we now cache the blocks chain, for faster resizing.
	@blocks = @bat.chain first_block
	super @bat.io, mode, :ranges => @bat.ranges(@blocks, size)
end

Instance Attribute Details

#batObject (readonly)

Returns the value of attribute bat.



602
603
604
# File 'lib/ole/storage/base.rb', line 602

def bat
  @bat
end

#first_blockObject

Returns the value of attribute first_block.



603
604
605
# File 'lib/ole/storage/base.rb', line 603

def first_block
  @first_block
end

Instance Method Details

#truncate(size) ⇒ Object



615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
# File 'lib/ole/storage/base.rb', line 615

def truncate size
	# note that old_blocks is != @ranges.length necessarily. i'm planning to write a
	# merge_ranges function that merges sequential ranges into one as an optimization.
	@bat.resize_chain @blocks, size
	@pos = size if @pos > size
	self.ranges = @bat.ranges(@blocks, size)
	self.first_block = @blocks.empty? ? AllocationTable::EOC : @blocks.first

	# don't know if this is required, but we explicitly request our @io to grow if necessary
	# we never shrink it though. maybe this belongs in allocationtable, where smarter decisions
	# can be made.
	# maybe its ok to just seek out there later??
	max = @ranges.map { |pos, len| pos + len }.max || 0
	@io.truncate max if max > @io.size
end