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

Make it easier to avoid the file system #33

Merged
merged 1 commit into from
Jul 12, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ function load_dir(dirpath::AbstractString)::ZGroup
reader = if isdir(dirpath)
DirectoryReader(dirpath)
elseif isfile(dirpath)
BufferedZipReader(dirpath)
ZarrZipReader(read(dirpath))
else
throw(ArgumentError("loading directory $(repr(dirpath)): No such file or directory"))
end
Expand Down
11 changes: 5 additions & 6 deletions src/readers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,17 @@ function read_key_idx(d::DirectoryReader, idx::Int)::Vector{UInt8}
end


struct BufferedZipReader <: AbstractReader
struct ZarrZipReader <: AbstractReader
zipfile::ZipBufferReader{Vector{UInt8}}
function BufferedZipReader(path)
@argcheck isfile(path)
new(ZipBufferReader(read(path)))
function ZarrZipReader(buffer::Vector{UInt8})
new(ZipBufferReader(buffer))
end
end

function key_names(d::BufferedZipReader)::Vector{String}
function key_names(d::ZarrZipReader)::Vector{String}
return zip_names(d.zipfile)
end

function read_key_idx(d::BufferedZipReader, idx::Int)::Vector{UInt8}
function read_key_idx(d::ZarrZipReader, idx::Int)::Vector{UInt8}
zip_readentry(d.zipfile, idx)
end
22 changes: 16 additions & 6 deletions src/saving.jl
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@


"""
If dirpath ends in .zip, save to a zip file, otherwise save to a directory.

Note this will delete pre existing data at dirpath
"""
function save_dir(dirpath::AbstractString, z::ZGroup)
writer = if endswith(dirpath, ".zip")
BufferedZipWriter(dirpath)
if endswith(dirpath, ".zip")
@argcheck !isdir(dirpath)
mkpath(dirname(dirpath))
open(dirpath; write=true) do io
writer = ZarrZipWriter(io)
try
save_dir(writer, z)
finally
closewriter(writer)
end
end
else
DirectoryWriter(dirpath)
save_dir(DirectoryWriter(dirpath), z)
end
save_dir(writer, z)
closewriter(writer)
nothing
end


"""
Note this will delete pre existing data at dirpath
"""
Expand Down
23 changes: 7 additions & 16 deletions src/writers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,28 +40,19 @@ end
Write to an in memory zipfile, that gets saved to disk on close.
This writer will overwrite any existing file at `path`
"""
struct BufferedZipWriter <: AbstractWriter
zipfile::ZipWriter{IOBuffer}
path::String
iobuffer::IOBuffer
function BufferedZipWriter(path)
@argcheck !isdir(path)
@argcheck !isdirpath(path)
mkpath(dirname(path))
iobuffer = IOBuffer()
zipfile = ZipWriter(iobuffer)
new(zipfile, abspath(path), iobuffer)
struct ZarrZipWriter{IO_TYPE<:IO} <: AbstractWriter
zipfile::ZipWriter{IO_TYPE}
function ZarrZipWriter(io)
new{typeof(io)}(ZipWriter(io))
end
end

function write_key(d::BufferedZipWriter, key::AbstractString, data)::Nothing
function write_key(d::ZarrZipWriter, key::AbstractString, data)::Nothing
zip_writefile(d.zipfile, key, data)
nothing
end

function closewriter(d::BufferedZipWriter)
function closewriter(d::ZarrZipWriter)
close(d.zipfile)
open(d.path, "w") do f
write(f, take!(d.iobuffer))
end
nothing
end