diff --git a/src/hdf5_time_series_storage.jl b/src/hdf5_time_series_storage.jl index 1ce754877..60b3a2858 100644 --- a/src/hdf5_time_series_storage.jl +++ b/src/hdf5_time_series_storage.jl @@ -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 diff --git a/src/in_memory_time_series_storage.jl b/src/in_memory_time_series_storage.jl index e733c32b7..65aa97fc3 100644 --- a/src/in_memory_time_series_storage.jl +++ b/src/in_memory_time_series_storage.jl @@ -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 diff --git a/test/test_time_series.jl b/test/test_time_series.jl index 07331c07a..9aa1646df 100644 --- a/test/test_time_series.jl +++ b/test/test_time_series.jl @@ -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