Skip to content

Commit

Permalink
Merge pull request #29 from CiaranOMara/hotfix/ensure_buffered_data
Browse files Browse the repository at this point in the history
Correct `ensure_buffered_data`
  • Loading branch information
CiaranOMara committed Jul 25, 2023
2 parents 51af9df + 6bc27f9 commit bf71956
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/UnitTests.yml
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
@@ -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
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
@@ -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

2 comments on commit bf71956

@CiaranOMara
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/88279

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.3.2 -m "<description of version>" bf71956a81a7d4affdcb422bd2686f119a900e1b
git push origin v0.3.2

Please sign in to comment.