Skip to content

Commit

Permalink
Allow caller to set compression level and get last write offset.
Browse files Browse the repository at this point in the history
This last is useful for building an index on the starting positions of
records as they are written. Due to buffering and blocking behavior
these are not necessarily the same as the positions reported by
calling #tell before each #write call.
  • Loading branch information
csw committed Aug 5, 2012
1 parent 9521548 commit 7cffe7f
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions lib/bio-bgzf/writer.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
require 'zlib'

module Bio::BGZF

class Writer
include Bio::BGZF

attr_reader :f, :buf
attr_reader :f, :buf, :level

# Return the virtual offset of the last {#write} call. This is a
# hook for e.g. building an index on the fly.
attr_reader :last_write_pos

def initialize(f)
def initialize(f, level=2)
@f = f
@level = level
@buf = ''
@buf_write_pos = 0
if block_given?
begin
yield self
Expand All @@ -24,22 +32,30 @@ def tell
def write_buf
if buf.size > 0
raise "Buffer too large: #{buf.bytesize}" if buf.bytesize > MAX_BYTES
block = pack(buf)
block = pack(buf, level)
f.write(block)
@buf = ''
@buf_write_pos = tell
end
end

# @api private
def _cur_write_pos
@buf_write_pos + buf.bytesize
end

def write(s)
if s.bytesize > MAX_BYTES
write_buf
@last_write_pos = _cur_write_pos
_each_slice(s) do |slice|
write(slice)
end
else
if (s.bytesize + buf.bytesize) > MAX_BYTES
write_buf
end
@last_write_pos = _cur_write_pos
buf << s
end
end
Expand Down

0 comments on commit 7cffe7f

Please sign in to comment.