Skip to content

Commit

Permalink
Merge pull request #227 from psrenergy/gb/speed_up_time_series_row_re…
Browse files Browse the repository at this point in the history
…ader

Add better implementation for checking if collections are empty in tme controllers
  • Loading branch information
guilhermebodin authored Oct 9, 2024
2 parents af0169b + 2aa28d5 commit 9d95af3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "PSRClassesInterface"
uuid = "1eab49e5-27d8-4905-b9f6-327b6ea666c4"
version = "0.17.1"
version = "0.17.2"

[deps]
DBInterface = "a10d1c49-ce27-4219-8d33-6db1a4562965"
Expand Down
6 changes: 1 addition & 5 deletions src/PSRDatabaseSQLite/read.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ function number_of_elements(db::DatabaseSQLite, collection_id::String)::Int
end
end

function _collection_has_any_data(db::DatabaseSQLite, collection_id::String)::Bool
return number_of_elements(db, collection_id) > 0
end

function _get_id(
db::DatabaseSQLite,
collection_id::String,
Expand Down Expand Up @@ -391,7 +387,7 @@ function read_time_series_row(

T = attribute.type

if !(_collection_has_any_data(db, collection_id))
if !(_time_controller_collection_has_any_data(db, collection_id))
return Vector{T}(undef, 0)
end
if !haskey(db._time_controller.cache, collection_attribute)
Expand Down
19 changes: 18 additions & 1 deletion src/PSRDatabaseSQLite/time_controller.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,35 @@ mutable struct TimeControllerCache{T}
_closest_global_previous_date_with_data::DateTime
_closest_global_next_date_with_data::DateTime

# Cache of collection_ids
# Cache of collection_ids, these are the ids of elements in a specific collection
_collection_ids::Vector{Int}
end

Base.@kwdef mutable struct TimeController
cache::Dict{CollectionAttribute, TimeControllerCache} = Dict{CollectionAttribute, TimeControllerCache}()

# Upon initialization the time controller will ask if a certain
# collection has any elements, if the collection has any elements it
# will be added to this cache. This cache will be used to avoid querying
# multiple times if a certain collection has any elements.
# This relies on the fact that the Time Controller only works in
# read only databases.
collection_has_any_data::Dict{String, Bool} = Dict{String, Bool}()
end

function _collection_attribute(collection_id::String, attribute_id::String)::CollectionAttribute
return (collection_id, attribute_id)
end

function _time_controller_collection_has_any_data(db, collection_id::String)::Bool
if haskey(db._time_controller.collection_has_any_data, collection_id)
return db._time_controller.collection_has_any_data[collection_id]
else
db._time_controller.collection_has_any_data[collection_id] = number_of_elements(db, collection_id) > 0
return db._time_controller.collection_has_any_data[collection_id]
end
end

function _update_time_controller_cache!(
cache::TimeControllerCache,
db,
Expand Down

2 comments on commit 9d95af3

@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/116933

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.17.2 -m "<description of version>" 9d95af31c1a1f4dce878413248c6df8c679ec6ad
git push origin v0.17.2

Please sign in to comment.