Skip to content

Commit

Permalink
add better implementation for checking if collections are empty in ti…
Browse files Browse the repository at this point in the history
…me controller
  • Loading branch information
guilhermebodin committed Oct 9, 2024
1 parent af0169b commit 2aa28d5
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

0 comments on commit 2aa28d5

Please sign in to comment.