diff --git a/.github/workflows/UnitTests.yml b/.github/workflows/UnitTests.yml index 2e5befe..f73eeb4 100644 --- a/.github/workflows/UnitTests.yml +++ b/.github/workflows/UnitTests.yml @@ -40,6 +40,8 @@ jobs: uses: julia-actions/julia-buildpkg@v1 - name: Run Tests uses: julia-actions/julia-runtest@v1 + env: + JULIA_NUM_THREADS: "2" - name: Create CodeCov uses: julia-actions/julia-processcoverage@v1 - name: Upload CodeCov diff --git a/Project.toml b/Project.toml index b887ee7..3b04983 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "BGZFStreams" uuid = "28d598bf-9b8f-59f1-b38c-5a06b4a0f5e6" authors = ["Sabrina Jaye Ward ", "Kenta Sato ", "Daniel C. Jones ", "Tony Kelman ", "Elliot Saba ", "Christian Theil Have"] -version = "0.3.1" +version = "0.3.2" [deps] CodecZlib = "944b1d66-785c-5afd-91f1-9de20f533193" diff --git a/src/bgzfstream.jl b/src/bgzfstream.jl index a2421ee..07ae41b 100644 --- a/src/bgzfstream.jl +++ b/src/bgzfstream.jl @@ -84,6 +84,9 @@ mutable struct BGZFStream{T<:IO} <: IO # current block index block_index::Int + # index of the last block loaded + last_block_index::Int + # whether stream is open isopen::Bool @@ -91,6 +94,10 @@ mutable struct BGZFStream{T<:IO} <: IO onclose::Function end +function BGZFStream(io, mode, blocks, block_index, isopen, onclose) # This method maintains compatibility after the inclusion of the `last_block_index` field to `BGZFStream`. + return BGZFStream(io, mode, blocks, block_index, 0, isopen, onclose) +end + # BGZF blocks are no larger than 64 KiB before and after compression. const BGZF_MAX_BLOCK_SIZE = UInt(64 * 1024) @@ -315,16 +322,19 @@ end @inline function ensure_buffered_data(stream) #@assert stream.mode == READ_MODE @label doit - while stream.block_index ≤ lastindex(stream.blocks) + while stream.block_index < stream.last_block_index @inbounds block = stream.blocks[stream.block_index] - if is_eof_block(block.compressed_block) # Note: `read_blocks!` does not necessarily fill/overwrite blocks till `lastindex(stream.blocks)`, we need to stop incrementing `stream.block_index` when an eof block is encountered. - break - end if block.position ≤ block.size return stream.block_index end stream.block_index += 1 end + if stream.block_index == stream.last_block_index + @inbounds block = stream.blocks[stream.block_index] + if block.position ≤ block.size + return stream.block_index + end + end if !eof(stream.io) read_blocks!(stream) @goto doit @@ -364,6 +374,7 @@ function read_blocks!(stream) end while n_blocks < length(stream.blocks) && !eof(stream.io) block = stream.blocks[n_blocks += 1] + stream.last_block_index = n_blocks if has_position block.block_offset = position(stream.io) end diff --git a/test/runtests.jl b/test/runtests.jl index afc1de6..231db6c 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,6 +1,8 @@ using BGZFStreams using Test +@info "Environment" Threads.nthreads() + @testset "VirtualOffset" begin voff = VirtualOffset(0, 0) @test voff == VirtualOffset(0, 0) @@ -171,8 +173,8 @@ end @test stream.blocks |> length == 1 # Note: only one block when writing, @test stream.blocks[1].size == BGZFStreams.BGZF_SAFE_BLOCK_SIZE - # Generate n blocks of data. - data = rand(0x00:0xf0, (n*BGZFStreams.BGZF_SAFE_BLOCK_SIZE) ) + # Generate n+1 blocks of data. + data = rand(0x00:0xf0, ((n+1)*BGZFStreams.BGZF_SAFE_BLOCK_SIZE) ) write_offsets = BGZFStreams.VirtualOffset[]