-
Notifications
You must be signed in to change notification settings - Fork 20
/
time_series_storage.jl
90 lines (75 loc) · 2.48 KB
/
time_series_storage.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
"""
Abstract type for time series storage implementations.
All subtypes must implement:
- clear_time_series!
- deserialize_time_series
- get_compression_settings
- get_num_time_series
- remove_time_series!
- serialize_time_series!
- Base.isempty
"""
abstract type TimeSeriesStorage end
const DEFAULT_COMPRESSION = false
@scoped_enum(CompressionTypes, BLOSC = 0, DEFLATE = 1,)
"""
Provides customization of HDF5 compression settings.
Refer to the HDF5.jl and HDF5 documention for more information.
"""
struct CompressionSettings
"Controls whether compression is enabled."
enabled::Bool
"Specifies the type of compression to use."
type::CompressionTypes
"Supported values are 0-9. Higher values deliver better compression ratios but take longer."
level::Int
"Controls whether to enable the shuffle filter. Used with DEFLATE."
shuffle::Bool
end
function CompressionSettings(;
enabled = DEFAULT_COMPRESSION,
type = CompressionTypes.DEFLATE,
level = 3,
shuffle = true,
)
return CompressionSettings(enabled, type, level, shuffle)
end
function make_time_series_storage(;
in_memory = false,
filename = nothing,
directory = nothing,
compression = CompressionSettings(),
)
if in_memory
storage = InMemoryTimeSeriesStorage()
elseif !isnothing(filename)
storage = Hdf5TimeSeriesStorage(; filename = filename, compression = compression)
else
storage =
Hdf5TimeSeriesStorage(true; directory = directory, compression = compression)
end
return storage
end
const COMPONENT_name_DELIMITER = "__"
function make_component_name(component_uuid::UUIDs.UUID, name::AbstractString)
return string(component_uuid) * COMPONENT_name_DELIMITER * name
end
function deserialize_component_name(component_name::AbstractString)
data = split(component_name, COMPONENT_name_DELIMITER)
component = UUIDs.UUID(data[1])
name = data[2]
return component, name
end
function serialize(storage::TimeSeriesStorage, file_path::AbstractString)
if storage isa Hdf5TimeSeriesStorage
if abspath(get_file_path(storage)) == abspath(file_path)
error("Attempting to overwrite identical time series file")
end
copy_h5_file(get_file_path(storage), file_path)
elseif storage isa InMemoryTimeSeriesStorage
convert_to_hdf5(storage, file_path)
else
error("unsupported type $(typeof(storage))")
end
@info "Serialized time series data to $file_path."
end