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

Updated writewithschema in tables.jl #20

Merged
merged 2 commits into from
Apr 20, 2024
Merged
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
20 changes: 13 additions & 7 deletions src/tables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,23 +80,29 @@ function writewithschema(io, parts, rows, st, sch, dictrow, compress, kw)
pos = 0
else
row, rowst = rowsstate
# calc nbytes on first row, then allocate bytes
# calc nbytes on all rows to find max, then allocate bytes
bytesperrow = nbytes(schtyp, row)
while true
rowsstate = iterate(rows, rowst)
rowsstate === nothing && break
row, rowst = rowsstate
nb = nbytes(schtyp, row)
if nb > bytesperrow
bytesperrow = nb
end
end
rowsstate = iterate(rows)
row, rowst = rowsstate
blen = trunc(Int, nrow * bytesperrow * 1.05) # add 5% cushion
bytes = Vector{UInt8}(undef, blen)
n = 1
nb = nbytes(schtyp, row)
while true
pos = writevalue(Binary(), schtyp, row, bytes, pos, blen, kw)
rowsstate = iterate(rows, rowst)
rowsstate === nothing && break
row, rowst = rowsstate
nb = nbytes(schtyp, row)
if (pos + nb - 1) > blen
# unlikely, but need to resize our buffer
rowslefttowrite = nrow - n
avgbytesperrowwritten = div(bytesperrow, n)
resize!(bytes, pos + (rowslefttowrite * avgbytesperrowwritten))
end
bytesperrow += nb
n += 1
end
Expand Down
Loading