Skip to content

Commit

Permalink
Merge pull request #16 from ChevronETC/contiguous
Browse files Browse the repository at this point in the history
better support for not DenseArrays's
  • Loading branch information
samtkaplan authored May 21, 2024
2 parents 5c9fdad + bfadaa6 commit 7f2177e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "FolderStorage"
uuid = "37e990fb-a500-53d7-846b-4c8aba66999e"
version = "1.6.0"
version = "1.6.1"

[deps]
AbstractStorage = "14dbef02-f468-5f15-853e-5ec8dee7b899"
Expand Down
28 changes: 20 additions & 8 deletions src/FolderStorage.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,20 @@ function Base.write(c::Folder, o::AbstractString, data::AbstractString)
write(joinpath(c.foldername, o), data)
end

function Base.write(c::Folder, o::AbstractString, data::DenseArray{T}) where {T<:Number}
databytes = unsafe_wrap(Array, convert(Ptr{UInt8}, pointer(data)), sizeof(data), own=false)
writebytes(c, o, databytes)
_iscontiguous(data::DenseArray) = isbitstype(eltype(data))
_iscontiguous(data::SubArray) = isbitstype(eltype(data)) && Base.iscontiguous(data)
_iscontiguous(data::AbstractArray) = false

function Base.write(c::Folder, o::AbstractString, data::AbstractArray{T}) where {T<:Number}
if _iscontiguous(data)
databytes = unsafe_wrap(Array, convert(Ptr{UInt8}, pointer(data)), sizeof(data), own=false)
writebytes(c, o, databytes)
else
error("FolderStorage: `write` is not supported for non-contiguous arrays")
end
end

function Base.write(c::Folder, o::AbstractString, data::DenseArray)
function Base.write(c::Folder, o::AbstractString, data::AbstractArray)
io = IOBuffer()
serialize(io, data)
databytes = take!(io)
Expand Down Expand Up @@ -103,13 +111,17 @@ end
Equivalent to `read!(joinpath(c.foldername, filename), String, data).`
"""
function Base.read!(c::Folder, o::AbstractString, data::DenseArray{T}; offset=0) where {T<:Number}
databytes = unsafe_wrap(Array, convert(Ptr{UInt8}, pointer(data)), (sizeof(data),))
readbytes!(c, o, databytes; offset=offset*sizeof(T))
function Base.read!(c::Folder, o::AbstractString, data::AbstractArray{T}; offset=0) where {T<:Number}
if _iscontiguous(data)
databytes = unsafe_wrap(Array, convert(Ptr{UInt8}, pointer(data)), (sizeof(data),))
readbytes!(c, o, databytes; offset=offset*sizeof(T))
else
error("FolderStorage: `read` is not supported for non-contiguous arrays.")
end
data
end

function Base.read!(c::Folder, o::AbstractString, data::DenseArray{T}; offset=0) where {T}
function Base.read!(c::Folder, o::AbstractString, data::AbstractArray{T}; offset=0) where {T}
databytes = Vector{UInt8}(undef, filesize(c, o))
readbytes!(c, o, databytes)
io = IOBuffer(databytes)
Expand Down

2 comments on commit 7f2177e

@samtkaplan
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/107309

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

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 v1.6.1 -m "<description of version>" 7f2177e1390115ca8f6c41257b944ea1c94981d8
git push origin v1.6.1

Please sign in to comment.