diff --git a/Project.toml b/Project.toml index 6c393552..f54a4c26 100644 --- a/Project.toml +++ b/Project.toml @@ -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" diff --git a/src/PSRDatabaseSQLite/read.jl b/src/PSRDatabaseSQLite/read.jl index 116c5d65..69150403 100644 --- a/src/PSRDatabaseSQLite/read.jl +++ b/src/PSRDatabaseSQLite/read.jl @@ -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, @@ -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) diff --git a/src/PSRDatabaseSQLite/time_controller.jl b/src/PSRDatabaseSQLite/time_controller.jl index 152c22a7..c7d43556 100644 --- a/src/PSRDatabaseSQLite/time_controller.jl +++ b/src/PSRDatabaseSQLite/time_controller.jl @@ -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,