1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 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 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762
|
// Copyright ©2012 The bíogo Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package bgzf
import (
"bufio"
"bytes"
"compress/flate"
"compress/gzip"
"io"
"runtime"
"sync"
)
// countReader wraps flate.Reader, adding support for querying current offset.
type countReader struct {
// Underlying Reader.
fr flate.Reader
// Offset within the underlying reader.
off int64
}
// newCountReader returns a new countReader.
func newCountReader(r io.Reader) *countReader {
switch r := r.(type) {
case *countReader:
panic("bgzf: illegal use of internal type")
case flate.Reader:
return &countReader{fr: r}
default:
return &countReader{fr: bufio.NewReader(r)}
}
}
// Read is required to satisfy flate.Reader.
func (r *countReader) Read(p []byte) (int, error) {
n, err := r.fr.Read(p)
r.off += int64(n)
return n, err
}
// ReadByte is required to satisfy flate.Reader.
func (r *countReader) ReadByte() (byte, error) {
b, err := r.fr.ReadByte()
if err == nil {
r.off++
}
return b, err
}
// offset returns the current offset in the underlying reader.
func (r *countReader) offset() int64 { return r.off }
// seek moves the countReader to the specified offset using rs as the
// underlying reader.
func (r *countReader) seek(rs io.ReadSeeker, off int64) error {
_, err := rs.Seek(off, 0)
if err != nil {
return err
}
type reseter interface {
Reset(io.Reader)
}
switch cr := r.fr.(type) {
case reseter:
cr.Reset(rs)
default:
r.fr = newCountReader(rs)
}
r.off = off
return nil
}
// buffer is a flate.Reader used by a decompressor to store read-ahead data.
type buffer struct {
// Buffered compressed data from read ahead.
off int // Current position in buffered data.
size int // Total size of buffered data.
data [MaxBlockSize]byte
}
// Read provides the flate.Decompressor Read method.
func (r *buffer) Read(b []byte) (int, error) {
if r.off >= r.size {
return 0, io.EOF
}
if n := r.size - r.off; len(b) > n {
b = b[:n]
}
n := copy(b, r.data[r.off:])
r.off += n
return n, nil
}
// ReadByte provides the flate.Decompressor ReadByte method.
func (r *buffer) ReadByte() (byte, error) {
if r.off == r.size {
return 0, io.EOF
}
b := r.data[r.off]
r.off++
return b, nil
}
// reset makes the buffer available to store data.
func (r *buffer) reset() { r.size = 0 }
// hasData returns whether the buffer has any data buffered.
func (r *buffer) hasData() bool { return r.size != 0 }
// readLimited reads n bytes into the buffer from the given source.
func (r *buffer) readLimited(n int, src *countReader) error {
if r.hasData() {
panic("bgzf: read into non-empty buffer")
}
r.off = 0
var err error
r.size, err = io.ReadFull(src, r.data[:n])
return err
}
// equals returns a boolean indicating the equality between
// the buffered data and the given byte slice.
func (r *buffer) equals(b []byte) bool { return bytes.Equal(r.data[:r.size], b) }
// decompressor is a gzip member decompressor worker.
type decompressor struct {
owner *Reader
gz gzip.Reader
cr *countReader
// Current block size.
blockSize int
// Buffered compressed data from read ahead.
buf buffer
// Decompressed data.
wg sync.WaitGroup
blk Block
err error
}
// Read provides the Read method for the decompressor's gzip.Reader.
func (d *decompressor) Read(b []byte) (int, error) {
if d.buf.hasData() {
return d.buf.Read(b)
}
return d.cr.Read(b)
}
// ReadByte provides the ReadByte method for the decompressor's gzip.Reader.
func (d *decompressor) ReadByte() (byte, error) {
if d.buf.hasData() {
return d.buf.ReadByte()
}
return d.cr.ReadByte()
}
// lazyBlock conditionally creates a ready to use Block.
func (d *decompressor) lazyBlock() {
if d.blk == nil {
if w, ok := d.owner.cache.(Wrapper); ok {
d.blk = w.Wrap(&block{owner: d.owner})
} else {
d.blk = &block{owner: d.owner}
}
return
}
if !d.blk.ownedBy(d.owner) {
d.blk.setOwner(d.owner)
}
}
// acquireHead gains the read head from the decompressor's owner.
func (d *decompressor) acquireHead() {
d.wg.Add(1)
d.cr = <-d.owner.head
}
// releaseHead releases the read head back to the decompressor's owner.
func (d *decompressor) releaseHead() {
d.owner.head <- d.cr
d.cr = nil // Defensively zero the reader.
}
// wait waits for the current member to be decompressed or fail, and returns
// the resulting error state.
func (d *decompressor) wait() (Block, error) {
d.wg.Wait()
blk := d.blk
d.blk = nil
return blk, d.err
}
// using sets the Block for the decompressor to work with.
func (d *decompressor) using(b Block) *decompressor { d.blk = b; return d }
// nextBlockAt makes the decompressor ready for reading decompressed data
// from its Block. It checks if there is a cached Block for the nextBase,
// otherwise it seeks to the correct location if decompressor is not
// correctly positioned, and then reads the compressed data and fills
// the decompressed Block.
// After nextBlockAt returns without error, the decompressor's Block
// holds a valid gzip.Header and base offset.
func (d *decompressor) nextBlockAt(off int64, rs io.ReadSeeker) *decompressor {
d.err = nil
for {
exists, next := d.owner.cacheHasBlockFor(off)
if !exists {
break
}
off = next
}
d.lazyBlock()
d.acquireHead()
defer d.releaseHead()
if d.cr.offset() != off {
if rs == nil {
// It should not be possible for the expected next block base
// to be out of register with the count reader unless Seek
// has been called, so we know the base reader must be an
// io.ReadSeeker.
var ok bool
rs, ok = d.owner.r.(io.ReadSeeker)
if !ok {
panic("bgzf: unexpected offset without seek")
}
}
d.err = d.cr.seek(rs, off)
if d.err != nil {
d.wg.Done()
return d
}
}
d.blk.setBase(d.cr.offset())
d.err = d.readMember()
if d.err != nil {
d.wg.Done()
return d
}
d.blk.setHeader(d.gz.Header)
d.gz.Header = gzip.Header{} // Prevent retention of header field in next use.
// Decompress data into the decompressor's Block.
go func() {
d.err = d.blk.readFrom(&d.gz)
d.wg.Done()
}()
return d
}
// expectedMemberSize returns the size of the BGZF conformant gzip member.
// It returns -1 if no BGZF block size field is found.
func expectedMemberSize(h gzip.Header) int {
i := bytes.Index(h.Extra, bgzfExtraPrefix)
if i < 0 || i+5 >= len(h.Extra) {
return -1
}
return (int(h.Extra[i+4]) | int(h.Extra[i+5])<<8) + 1
}
// readMember buffers the gzip member starting the current decompressor offset.
func (d *decompressor) readMember() error {
// Set the decompressor to Read from the underlying flate.Reader
// and mark the starting offset from which the underlying reader
// was used.
d.buf.reset()
mark := d.cr.offset()
err := d.gz.Reset(d)
if err != nil {
d.blockSize = -1
return err
}
d.blockSize = expectedMemberSize(d.gz.Header)
if d.blockSize < 0 {
return ErrNoBlockSize
}
skipped := int(d.cr.offset() - mark)
need := d.blockSize - skipped
if need == 0 {
return io.EOF
} else if need < 0 {
return ErrCorrupt
}
// Read compressed data into the decompressor buffer until the
// underlying flate.Reader is positioned at the end of the gzip
// member in which the readMember call was made.
return d.buf.readLimited(need, d.cr)
}
// Offset is a BGZF virtual offset.
type Offset struct {
File int64
Block uint16
}
// Chunk is a region of a BGZF file.
type Chunk struct {
Begin Offset
End Offset
}
// Reader implements BGZF blocked gzip decompression.
type Reader struct {
gzip.Header
r io.Reader
// head serialises access to the underlying
// io.Reader.
head chan *countReader
// lastChunk is the virtual file offset
// interval of the last successful read
// or seek operation.
lastChunk Chunk
// Blocked specifies the behaviour of the
// Reader at the end of a BGZF member.
// If the Reader is Blocked, a Read that
// reaches the end of a BGZF block will
// return io.EOF. This error is not sticky,
// so a subsequent Read will progress to
// the next block if it is available.
Blocked bool
// Non-concurrent work decompressor.
dec *decompressor
// Concurrent work fields.
waiting chan *decompressor
working chan *decompressor
control chan int64
done chan struct{}
current Block
// cache is the Reader block cache. If Cache is not nil,
// the cache is queried for blocks before an attempt to
// read from the underlying io.Reader.
mu sync.RWMutex
cache Cache
err error
}
// NewReader returns a new BGZF reader.
//
// The number of concurrent read decompressors is specified by rd. If rd
// is 0, GOMAXPROCS concurrent will be created. If rd is 1, blocks will
// be read synchronously without readahead. The returned Reader should
// be closed after use to avoid leaking resources.
func NewReader(r io.Reader, rd int) (*Reader, error) {
if rd == 0 {
rd = runtime.GOMAXPROCS(0)
}
bg := &Reader{
r: r,
head: make(chan *countReader, 1),
}
bg.head <- newCountReader(r)
// Make work loop control structures.
if rd > 1 {
bg.waiting = make(chan *decompressor, rd)
bg.working = make(chan *decompressor, rd)
bg.control = make(chan int64, 1)
bg.done = make(chan struct{})
for ; rd > 1; rd-- {
bg.waiting <- &decompressor{owner: bg}
}
}
// Read the first block now so we can fail before
// the first Read call if there is a problem.
bg.dec = &decompressor{owner: bg}
blk, err := bg.dec.nextBlockAt(0, nil).wait()
if err != nil {
return nil, err
}
bg.current = blk
bg.Header = bg.current.header()
// Set up work loop if rd was > 1.
if bg.control != nil {
bg.waiting <- bg.dec
bg.dec = nil
next := blk.NextBase()
go func() {
defer func() {
bg.mu.Lock()
bg.cache = nil
bg.mu.Unlock()
close(bg.done)
}()
for dec := range bg.waiting {
var open bool
if next < 0 {
next, open = <-bg.control
if !open {
return
}
} else {
select {
case next, open = <-bg.control:
if !open {
return
}
default:
}
}
dec.nextBlockAt(next, nil)
next = dec.blk.NextBase()
bg.working <- dec
}
}()
}
return bg, nil
}
// SetCache sets the cache to be used by the Reader.
func (bg *Reader) SetCache(c Cache) {
bg.mu.Lock()
bg.cache = c
bg.mu.Unlock()
}
// Seek performs a seek operation to the given virtual offset.
func (bg *Reader) Seek(off Offset) error {
rs, ok := bg.r.(io.ReadSeeker)
if !ok {
return ErrNotASeeker
}
if off.File != bg.current.Base() || !bg.current.hasData() {
ok := bg.cacheSwap(off.File)
if !ok {
var dec *decompressor
if bg.dec != nil {
dec = bg.dec
} else {
select {
case dec = <-bg.waiting:
case dec = <-bg.working:
blk, err := dec.wait()
if err == nil {
bg.keep(blk)
if blk.Base() == off.File {
// This decompressor had the block we
// wanted.
bg.current = blk
bg.control <- bg.current.NextBase()
bg.waiting <- dec
dec = nil
}
}
}
}
if dec != nil {
// Synchronously decompress the requested block using
// the selected decompressor. Not necessary if that
// decompressor had the block ready, as determined
// above.
bg.current, bg.err = dec.
using(bg.current).
nextBlockAt(off.File, rs).
wait()
if bg.dec == nil {
select {
case <-bg.control:
default:
}
bg.control <- bg.current.NextBase()
bg.waiting <- dec
}
bg.Header = bg.current.header()
if bg.err != nil {
return bg.err
}
}
}
}
bg.err = bg.current.seek(int64(off.Block))
if bg.err == nil {
bg.lastChunk = Chunk{Begin: off, End: off}
}
return bg.err
}
// LastChunk returns the region of the BGZF file read by the last
// successful read operation or the resulting virtual offset of
// the last successful seek operation.
func (bg *Reader) LastChunk() Chunk { return bg.lastChunk }
// BlockLen returns the number of bytes remaining to be read from the
// current BGZF block.
func (bg *Reader) BlockLen() int { return bg.current.len() }
// Close closes the reader and releases resources.
func (bg *Reader) Close() error {
if bg.control != nil {
close(bg.control)
close(bg.waiting)
<-bg.done
}
if bg.err == io.EOF {
return nil
}
return bg.err
}
// Read implements the io.Reader interface.
func (bg *Reader) Read(p []byte) (int, error) {
if bg.err != nil {
return 0, bg.err
}
// Discard leading empty blocks. This is an indexing
// optimisation to avoid retaining useless members
// in a BAI/CSI.
for bg.current.len() == 0 {
bg.err = bg.nextBlock()
if bg.err != nil {
return 0, bg.err
}
}
bg.lastChunk.Begin = bg.current.txOffset()
var n int
for n < len(p) && bg.err == nil {
var _n int
_n, bg.err = bg.current.Read(p[n:])
n += _n
if bg.err == io.EOF {
if n == len(p) {
bg.err = nil
break
}
if bg.Blocked {
bg.err = nil
bg.lastChunk.End = bg.current.txOffset()
return n, io.EOF
}
bg.err = bg.nextBlock()
if bg.err != nil {
break
}
}
}
bg.lastChunk.End = bg.current.txOffset()
return n, bg.err
}
// ReadByte implements the io.ByteReader interface.
func (bg *Reader) ReadByte() (byte, error) {
if bg.err != nil {
return 0, bg.err
}
// Discard leading empty blocks. This is an indexing
// optimisation to avoid retaining useless members
// in a BAI/CSI.
for bg.current.len() == 0 {
bg.err = bg.nextBlock()
if bg.err != nil {
return 0, bg.err
}
}
bg.lastChunk.Begin = bg.current.txOffset()
var b byte
b, bg.err = bg.current.ReadByte()
if bg.err == io.EOF {
if bg.Blocked {
bg.err = nil
bg.lastChunk.End = bg.current.txOffset()
return b, io.EOF
}
bg.err = bg.nextBlock()
}
bg.lastChunk.End = bg.current.txOffset()
return b, bg.err
}
// nextBlock swaps the current decompressed block for the next
// in the stream. If the block is available from the cache
// no additional work is done, otherwise a decompressor is
// used or waited on.
func (bg *Reader) nextBlock() error {
base := bg.current.NextBase()
ok := bg.cacheSwap(base)
if ok {
bg.Header = bg.current.header()
return nil
}
var err error
if bg.dec != nil {
bg.dec.using(bg.current).nextBlockAt(base, nil)
bg.current, err = bg.dec.wait()
} else {
var ok bool
for i := 0; i < cap(bg.working); i++ {
dec := <-bg.working
bg.current, err = dec.wait()
bg.waiting <- dec
if bg.current.Base() == base {
ok = true
break
}
if err == nil {
bg.keep(bg.current)
bg.current = nil
}
}
if !ok {
panic("bgzf: unexpected block")
}
}
if err != nil {
return err
}
// Only set header if there was no error.
h := bg.current.header()
if bg.current.isMagicBlock() {
// TODO(kortschak): Do this more carefully. It may be that
// someone actually has extra data in this field that we are
// clobbering.
bg.Header.Extra = h.Extra
} else {
bg.Header = h
}
return nil
}
// cacheSwap attempts to swap the current Block for a cached Block
// for the given base offset. It returns true if successful.
func (bg *Reader) cacheSwap(base int64) bool {
bg.mu.RLock()
defer bg.mu.RUnlock()
if bg.cache == nil {
return false
}
blk, err := bg.cachedBlockFor(base)
if err != nil {
return false
}
if blk != nil {
// TODO(kortschak): Under some conditions, e.g. FIFO
// cache we will be discarding a non-nil evicted Block.
// Consider retaining these in a sync.Pool.
bg.cachePut(bg.current)
bg.current = blk
return true
}
var retained bool
bg.current, retained = bg.cachePut(bg.current)
if retained {
bg.current = nil
}
return false
}
// cacheHasBlockFor returns whether the Reader's cache has a block
// for the given base offset. If the requested Block exists, the base
// offset of the following Block is returned.
func (bg *Reader) cacheHasBlockFor(base int64) (exists bool, next int64) {
bg.mu.RLock()
defer bg.mu.RUnlock()
if bg.cache == nil {
return false, -1
}
return bg.cache.Peek(base)
}
// cachedBlockFor returns a non-nil Block if the Reader has access to a
// cache and the cache holds the block with the given base and the
// correct owner, otherwise it returns nil. If the Block's owner is not
// correct, or the Block cannot seek to the start of its data, a non-nil
// error is returned.
func (bg *Reader) cachedBlockFor(base int64) (Block, error) {
blk := bg.cache.Get(base)
if blk != nil {
if !blk.ownedBy(bg) {
return nil, ErrContaminatedCache
}
err := blk.seek(0)
if err != nil {
return nil, err
}
}
return blk, nil
}
// cachePut puts the given Block into the cache if it exists, it returns
// the Block that was evicted or b if it was not retained, and whether
// the Block was retained by the cache.
func (bg *Reader) cachePut(b Block) (evicted Block, retained bool) {
if b == nil || !b.hasData() {
return b, false
}
return bg.cache.Put(b)
}
// keep puts the given Block into the cache if it exists.
func (bg *Reader) keep(b Block) {
if b == nil || !b.hasData() {
return
}
bg.mu.RLock()
defer bg.mu.RUnlock()
if bg.cache != nil {
bg.cache.Put(b)
}
}
// Begin returns a Tx that starts at the current virtual offset.
func (bg *Reader) Begin() Tx { return Tx{begin: bg.lastChunk.Begin, r: bg} }
// Tx represents a multi-read transaction.
type Tx struct {
begin Offset
r *Reader
}
// End returns the Chunk spanning the transaction. After return the Tx is
// no longer valid.
func (t *Tx) End() Chunk {
c := Chunk{Begin: t.begin, End: t.r.lastChunk.End}
t.r = nil
return c
}
|