lib/tracksperanto/buffer_io.rb in tracksperanto-2.2.4 vs lib/tracksperanto/buffer_io.rb in tracksperanto-2.3.0
- old
+ new
@@ -28,20 +28,36 @@
def close!
__getobj__.close! if @tempfile_in
__setobj__(nil)
end
+ # Sometimes you just need to upgrade to a File forcibly (for example if you want)
+ # to have an object with many iterators sitting on it. We also flush here.
+ def to_file
+ replace_with_tempfile unless @tempfile_in
+ flush
+ self
+ end
+
+ # Tells whether this one is on disk
+ def file_backed?
+ @tempfile_in
+ end
+
private
+ def replace_with_tempfile
+ sio = __getobj__
+ tf = Tempfile.new("tracksperanto-xbuf")
+ tf.write(sio.string)
+ tf.flush # Needed of we will reopen this file soon from another thread/loop
+ sio.string = ""
+ GC.start
+ __setobj__(tf)
+
+ @tempfile_in = true
+ end
+
def replace_with_tempfile_if_needed
- return if @tempfile_in
- io = __getobj__
- if io.pos > MAX_IN_MEM_BYTES
- tf = Tempfile.new("tracksperanto-xbuf")
- tf.write(io.string)
- io.string = ""
- GC.start
- __setobj__(tf)
- @tempfile_in = true
- end
+ replace_with_tempfile if !@tempfile_in && pos > MAX_IN_MEM_BYTES
end
end
\ No newline at end of file