| 1 | // Copyright ©2012 The bíogo Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package bgzf |
| 6 | |
| 7 | import ( |
| 8 | "bytes" |
| 9 | "compress/gzip" |
| 10 | "io" |
| 11 | ) |
| 12 | |
| 13 | // Cache is a Block caching type. Basic cache implementations are provided |
| 14 | // in the cache package. A Cache must be safe for concurrent use. |
| 15 | // |
| 16 | // If a Cache is a Wrapper, its Wrap method is called on newly created blocks. |
| 17 | type Cache interface { |
| 18 | // Get returns the Block in the Cache with the specified |
| 19 | // base or a nil Block if it does not exist. The returned |
| 20 | // Block must be removed from the Cache. |
| 21 | Get(base int64) Block |
| 22 | |
| 23 | // Put inserts a Block into the Cache, returning the Block |
| 24 | // that was evicted or nil if no eviction was necessary and |
| 25 | // a boolean indicating whether the put Block was retained |
| 26 | // by the Cache. |
| 27 | Put(Block) (evicted Block, retained bool) |
| 28 | |
| 29 | // Peek returns whether a Block exists in the cache for the |
| 30 | // given base. If a Block satisfies the request, then exists |
| 31 | // is returned as true with the offset for the next Block in |
| 32 | // the stream, otherwise false and -1. |
| 33 | Peek(base int64) (exists bool, next int64) |
| 34 | } |
| 35 | |
| 36 | // Wrapper defines Cache types that need to modify a Block at its creation. |
| 37 | type Wrapper interface { |
| 38 | Wrap(Block) Block |
| 39 | } |
| 40 | |
| 41 | // Block wraps interaction with decompressed BGZF data blocks. |
| 42 | type Block interface { |
| 43 | // Base returns the file offset of the start of |
| 44 | // the gzip member from which the Block data was |
| 45 | // decompressed. |
| 46 | Base() int64 |
| 47 | |
| 48 | io.Reader |
| 49 | io.ByteReader |
| 50 | |
| 51 | // Used returns whether one or more bytes have |
| 52 | // been read from the Block. |
| 53 | Used() bool |
| 54 | |
| 55 | // header returns the gzip.Header of the gzip member |
| 56 | // from which the Block data was decompressed. |
| 57 | header() gzip.Header |
| 58 | |
| 59 | // isMagicBlock returns whether the Block is a BGZF |
| 60 | // magic EOF marker block. |
| 61 | isMagicBlock() bool |
| 62 | |
| 63 | // ownedBy returns whether the Block is owned by |
| 64 | // the given Reader. |
| 65 | ownedBy(*Reader) bool |
| 66 | |
| 67 | // setOwner changes the owner to the given Reader, |
| 68 | // reseting other data to its zero state. |
| 69 | setOwner(*Reader) |
| 70 | |
| 71 | // hasData returns whether the Block has read data. |
| 72 | hasData() bool |
| 73 | |
| 74 | // The following are unexported equivalents |
| 75 | // of the io interfaces. seek is limited to |
| 76 | // the file origin offset case and does not |
| 77 | // return the new offset. |
| 78 | seek(offset int64) error |
| 79 | readFrom(io.ReadCloser) error |
| 80 | |
| 81 | // len returns the number of remaining |
| 82 | // bytes that can be read from the Block. |
| 83 | len() int |
| 84 | |
| 85 | // setBase sets the file offset of the start |
| 86 | // and of the gzip member that the Block data |
| 87 | // was decompressed from. |
| 88 | setBase(int64) |
| 89 | |
| 90 | // NextBase returns the expected position of the next |
| 91 | // BGZF block. It returns -1 if the Block is not valid. |
| 92 | NextBase() int64 |
| 93 | |
| 94 | // setHeader sets the file header of of the gzip |
| 95 | // member that the Block data was decompressed from. |
| 96 | setHeader(gzip.Header) |
| 97 | |
| 98 | // txOffset returns the current vitual offset. |
| 99 | txOffset() Offset |
| 100 | } |
| 101 | |
| 102 | type block struct { |
| 103 | owner *Reader |
| 104 | used bool |
| 105 | |
| 106 | base int64 |
| 107 | h gzip.Header |
| 108 | magic bool |
| 109 | |
| 110 | offset Offset |
| 111 | |
| 112 | buf *bytes.Reader |
| 113 | data [MaxBlockSize]byte |
| 114 | } |
| 115 | |
| 116 | func (b *block) Base() int64 { return b.base } |
| 117 | |
| 118 | func (b *block) Used() bool { return b.used } |
| 119 | |
| 120 | func (b *block) Read(p []byte) (int, error) { |
| 121 | n, err := b.buf.Read(p) |
| 122 | b.offset.Block += uint16(n) |
| 123 | if n > 0 { |
| 124 | b.used = true |
| 125 | } |
| 126 | return n, err |
| 127 | } |
| 128 | |
| 129 | func (b *block) ReadByte() (byte, error) { |
| 130 | c, err := b.buf.ReadByte() |
| 131 | if err == nil { |
| 132 | b.offset.Block++ |
| 133 | b.used = true |
| 134 | } |
| 135 | return c, err |
| 136 | } |
| 137 | |
| 138 | // readToEOF will exhaust r or fill buf. |
| 139 | // If r does not EOF upon reading up to len(buf) bytes then readToEOF will return |
| 140 | // io.ErrShortBuffer and an additional byte will be discarded from the reader. |
| 141 | func readToEOF(r io.Reader, buf []byte) (n int, err error) { |
| 142 | for err == nil && n < len(buf) { |
| 143 | var nn int |
| 144 | nn, err = r.Read(buf[n:]) |
| 145 | n += nn |
| 146 | } |
| 147 | switch { |
| 148 | case err == io.EOF: |
| 149 | return n, nil |
| 150 | case n == MaxBlockSize && err == nil: |
| 151 | // This is paranoic, but some readers will return |
| 152 | // quickly when passed a zero-length byte slice. |
| 153 | var dummy [1]byte |
| 154 | _, err = r.Read(dummy[:]) |
| 155 | if err == nil { |
| 156 | return n, io.ErrShortBuffer |
| 157 | } |
| 158 | if err == io.EOF { |
| 159 | err = nil |
| 160 | } |
| 161 | } |
| 162 | return n, err |
| 163 | } |
| 164 | |
| 165 | func (b *block) readFrom(r io.ReadCloser) error { |
| 166 | o := b.owner |
| 167 | b.owner = nil |
| 168 | n, err := readToEOF(r, b.data[:]) |
| 169 | if err != nil { |
| 170 | return err |
| 171 | } |
| 172 | b.buf = bytes.NewReader(b.data[:n]) |
| 173 | b.owner = o |
| 174 | b.magic = b.magic && b.len() == 0 |
| 175 | return r.Close() |
| 176 | } |
| 177 | |
| 178 | func (b *block) seek(offset int64) error { |
| 179 | _, err := b.buf.Seek(offset, 0) |
| 180 | if err == nil { |
| 181 | b.offset.Block = uint16(offset) |
| 182 | } |
| 183 | return err |
| 184 | } |
| 185 | |
| 186 | func (b *block) len() int { |
| 187 | if b.buf == nil { |
| 188 | return 0 |
| 189 | } |
| 190 | return b.buf.Len() |
| 191 | } |
| 192 | |
| 193 | func (b *block) setBase(n int64) { |
| 194 | b.base = n |
| 195 | b.offset = Offset{File: n} |
| 196 | } |
| 197 | |
| 198 | func (b *block) NextBase() int64 { |
| 199 | size := int64(expectedMemberSize(b.h)) |
| 200 | if size == -1 { |
| 201 | return -1 |
| 202 | } |
| 203 | return b.base + size |
| 204 | } |
| 205 | |
| 206 | func (b *block) setHeader(h gzip.Header) { |
| 207 | b.h = h |
| 208 | b.magic = h.OS == 0xff && |
| 209 | // Test for zero time and old compress/gzip behaviour. |
| 210 | (h.ModTime.IsZero() || h.ModTime.Equal(unixEpoch)) && |
| 211 | h.Name == "" && |
| 212 | h.Comment == "" && |
| 213 | bytes.Equal(h.Extra, []byte("BC\x02\x00\x1b\x00")) |
| 214 | } |
| 215 | |
| 216 | func (b *block) header() gzip.Header { return b.h } |
| 217 | |
| 218 | func (b *block) isMagicBlock() bool { return b.magic } |
| 219 | |
| 220 | func (b *block) setOwner(r *Reader) { |
| 221 | b.owner = r |
| 222 | b.used = false |
| 223 | b.base = -1 |
| 224 | b.h = gzip.Header{} |
| 225 | b.offset = Offset{} |
| 226 | b.buf = nil |
| 227 | } |
| 228 | |
| 229 | func (b *block) ownedBy(r *Reader) bool { return b.owner == r } |
| 230 | |
| 231 | func (b *block) hasData() bool { return b.buf != nil } |
| 232 | |
| 233 | func (b *block) txOffset() Offset { return b.offset } |