Skip to content

Commit

Permalink
Merge pull request #242 from daniel-thom/optimize-deepcopy
Browse files Browse the repository at this point in the history
Optimize deepcopy of system
  • Loading branch information
daniel-thom authored Jun 14, 2021
2 parents 64a2255 + 49c6c7a commit f9212ff
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
21 changes: 21 additions & 0 deletions src/hdf5_time_series_storage.jl
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,27 @@ function from_file(
return storage
end

"""
Copy the time series data to a new file. This should get called when the system is
undergoing a deepcopy.
# Arguments
- `storage::Hdf5TimeSeriesStorage`: storage instance
- `directory::String`: If nothing, use tempdir
"""
function copy_to_new_file!(storage::Hdf5TimeSeriesStorage, directory = nothing)
if directory === nothing
directory = tempdir()
end

# If we ever choose to keep the HDF5 file open then this will break.
# Any open buffers will need to be flushed.
filename, io = mktemp(directory)
close(io)
copy_file(get_file_path(storage), filename)
storage.file_path = filename
end

get_compression_settings(storage::Hdf5TimeSeriesStorage) = storage.compression

get_file_path(storage::Hdf5TimeSeriesStorage) = storage.file_path
Expand Down
4 changes: 2 additions & 2 deletions src/in_memory_time_series_storage.jl
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,11 @@ function compare_values(x::InMemoryTimeSeriesStorage, y::InMemoryTimeSeriesStora
@error "component_names don't match" record_x.component_names record_y.component_names
return false
end
if TimeSeries.timestamp(record_x.ta.data) != TimeSeries.timestamp(record_y.ta.data)
if TimeSeries.timestamp(record_x.ts.data) != TimeSeries.timestamp(record_y.ts.data)
@error "timestamps don't match" record_x record_y
return false
end
if TimeSeries.values(record_x.ta.data) != TimeSeries.values(record_y.ta.data)
if TimeSeries.values(record_x.ts.data) != TimeSeries.values(record_y.ts.data)
@error "values don't match" record_x record_y
return false
end
Expand Down
29 changes: 29 additions & 0 deletions test/test_time_series.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1985,3 +1985,32 @@ end
count = 3,
)
end

@testset "Test copy_to_new_file! on HDF5" begin
sys = IS.SystemData(time_series_in_memory = false)
name = "Component1"
name = "val"
component = IS.TestComponent(name, 5)
IS.add_component!(sys, component)

initial_timestamp = Dates.DateTime("2020-01-01T00:00:00")
horizon = 24
resolution = Dates.Hour(1)
data_input = rand(horizon)
data = SortedDict(initial_timestamp => data_input)
time_series = IS.Deterministic(name = name, resolution = resolution, data = data)
fdata = IS.get_data(time_series)
@test initial_timestamp == first(keys((fdata)))
@test data_input == first(values((fdata)))

IS.add_time_series!(sys, component, time_series)
orig_file = IS.get_file_path(sys.time_series_storage)
IS.copy_to_new_file!(sys.time_series_storage)
@test orig_file != IS.get_file_path(sys.time_series_storage)

time_series2 = IS.get_time_series(IS.Deterministic, component, name)
@test time_series2 isa IS.Deterministic
fdata2 = IS.get_data(time_series2)
@test initial_timestamp == first(keys((fdata2)))
@test data_input == first(values((fdata2)))
end

0 comments on commit f9212ff

Please sign in to comment.