Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct ensure_buffered_data #29

Merged
merged 4 commits into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/UnitTests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "BGZFStreams"
uuid = "28d598bf-9b8f-59f1-b38c-5a06b4a0f5e6"
authors = ["Sabrina Jaye Ward <sabrinajward@protonmail.com>", "Kenta Sato <bicycle1885@gmail.com>", "Daniel C. Jones <dcjones@cs.washington.edu>", "Tony Kelman <tony@kelman.net>", "Elliot Saba <staticfloat@gmail.com>", "Christian Theil Have"]
version = "0.3.1"
version = "0.3.2"

[deps]
CodecZlib = "944b1d66-785c-5afd-91f1-9de20f533193"
Expand Down
19 changes: 15 additions & 4 deletions src/bgzfstream.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,20 @@ 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

# callback function called when closing the stream
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)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using BGZFStreams
using Test

@info "Environment" Threads.nthreads()

@testset "VirtualOffset" begin
voff = VirtualOffset(0, 0)
@test voff == VirtualOffset(0, 0)
Expand Down Expand Up @@ -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[]

Expand Down