Class: Ole::Storage::RangesIOMigrateable

Inherits:
RangesIOResizeable show all
Defined in:
lib/ole/storage/base.rb

Overview

like RangesIOResizeable, but Ole::Storage::Dirent specific. provides for migration between bats based on size, and updating the dirent.

Instance Attribute Summary collapse

Attributes inherited from RangesIOResizeable

#bat

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(dirent, mode = 'r') ⇒ RangesIOMigrateable

Returns a new instance of RangesIOMigrateable.



636
637
638
639
640
# File 'lib/ole/storage/base.rb', line 636

def initialize dirent, mode='r'
	@dirent = dirent
	super @dirent.ole.bat_for_size(@dirent.size), mode,
		:first_block => @dirent.first_block, :size => @dirent.size
end

Instance Attribute Details

#direntObject (readonly)

Returns the value of attribute dirent.



635
636
637
# File 'lib/ole/storage/base.rb', line 635

def dirent
  @dirent
end

Instance Method Details

#first_blockObject

forward this to the dirent



671
672
673
# File 'lib/ole/storage/base.rb', line 671

def first_block
	@dirent.first_block
end

#first_block=(val) ⇒ Object



675
676
677
# File 'lib/ole/storage/base.rb', line 675

def first_block= val
	@dirent.first_block = val
end

#truncate(size) ⇒ Object



642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
# File 'lib/ole/storage/base.rb', line 642

def truncate size
	bat = @dirent.ole.bat_for_size size
	if bat.class != @bat.class
		# bat migration needed! we need to backup some data. the amount of data
		# should be <= @ole.header.threshold, so we can just hold it all in one buffer.
		# backup this
		pos = [@pos, size].min
		self.pos = 0
		keep = read [@size, size].min
		# this does a normal truncate to 0, removing our presence from the old bat, and
		# rewrite the dirent's first_block
		super 0
		@bat = bat
		# just change the underlying io from right under everyone :)
		@io = bat.io
		# important to do this now, before the write. as the below write will always
		# migrate us back to sbat! this will now allocate us +size+ in the new bat.
		super
		self.pos = 0
		write keep
		self.pos = pos
	else
		super
	end
	# now just update the file
	@dirent.size = size
end