Skip to content

Commit

Permalink
Merge pull request #18 from psrenergy/gb/better-handling-for-closing-csv
Browse files Browse the repository at this point in the history
Open the file manually and pass it to the CSV.Rows iterator
  • Loading branch information
guilhermebodin authored Aug 23, 2024
2 parents 8f5884d + 2e61493 commit 978619c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Quiver"
uuid = "cdbb3f72-2527-4dbd-9d0e-93533a5519ac"
authors = ["raphasampaio", "guilhermebodin"]
version = "0.1.1"
version = "0.1.2"

[deps]
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
Expand Down
25 changes: 15 additions & 10 deletions src/csv.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
file_extension(::Type{csv}) = "csv"

mutable struct QuiverCSVRowReader
io
iterator
next
end
Expand Down Expand Up @@ -59,6 +60,9 @@ function Reader{csv}(
labels_to_read::Vector{String} = String[],
carrousel::Bool = false,
)
# Note to the future https://discourse.julialang.org/t/closing-files-when-using-csv-rows/29169/2
# CSV.jl mmaps the file and keeps it open until the rows object is garbage collected
# To avoid this we can follow the recommendation in the link above

filename_with_extensions = add_extension_to_file(filename, file_extension(csv))
if !isfile(filename_with_extensions)
Expand All @@ -67,7 +71,14 @@ function Reader{csv}(

metadata = from_toml("$filename.toml")

rows = CSV.Rows(filename_with_extensions; types = [fill(Int32, metadata.number_of_dimensions); fill(Float32, metadata.number_of_time_series)])
io = open(filename_with_extensions, "r")

rows = CSV.Rows(
io;
types = [fill(Int32, metadata.number_of_dimensions); fill(Float32, metadata.number_of_time_series)],
buffer_in_memory = true,
reusebuffer = true,
)

last_dimension_read = zeros(Int, metadata.number_of_dimensions)

Expand All @@ -76,7 +87,7 @@ function Reader{csv}(


reader = try
row_reader = QuiverCSVRowReader(rows, next)
row_reader = QuiverCSVRowReader(io, rows, next)
Quiver.Reader{csv}(
row_reader,
filename,
Expand All @@ -86,12 +97,7 @@ function Reader{csv}(
carrousel = carrousel,
)
catch e
row_reader = nothing
rows = nothing
next = nothing
row = nothing
state = nothing
GC.gc()
close(io)
rethrow(e)
end

Expand Down Expand Up @@ -121,8 +127,7 @@ function _quiver_goto!(reader::Quiver.Reader{csv}, dims...)
end

function _quiver_close!(reader::Quiver.Reader{csv})
reader.reader.iterator = nothing
GC.gc()
close(reader.reader.io)
return nothing
end

Expand Down

2 comments on commit 978619c

@guilhermebodin
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/113743

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 v0.1.2 -m "<description of version>" 978619c6d99b129eeac316726a6c3ad2baeba850
git push origin v0.1.2

Please sign in to comment.