-
Notifications
You must be signed in to change notification settings - Fork 42
/
storage.jl
408 lines (332 loc) · 13.3 KB
/
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
@enum(UnitPermission, NOT_USED, READ_ONLY, READ_AND_WRITE)
"""
About storage units
-------------------
Storage units keep user data (a model) and computed data between different runs
of an algorithm or between runs of different algorithms.
Models are storage units themselves. Each unit is associated with a
model. Thus a unit adds computed data to a model.
Records are useful to store records of storage units at some point
of the calculation flow so that we can later return to this point and
restore the units. For example, the calculation flow may return to
some saved node in the search tree.
Some units can have different parts which are stored in different
records. Thus, we operate with triples (model, unit, record).
For every model there may be only one unit for each couple
(unit type, record type).
To store all storage units of a data, we use functions
"store_records!(::AbstractData)::RecordsVector" or
"copy_records(::RecordsVector)::RecordsVector"
Every stored record should be removed or restored using functions
"restore_from_records!(::RecordsVector,::UnitsUsageDict)"
and "remove_records!(::RecordsVector)"
After recording current records, if we write to some storage unit, we should restore
it for writing using "restore_from_records!(...)"
After recording current records, if we read from a storage unit,
no particular precautions should be taken.
"""
"""
AbstractStorageUnit
A storage unit contains information about a model or the execution of an algorithm.
For every unit a constructor should be defined which
takes a model as a parameter. This constructor is
called when the formulation is completely known so the data
can be safely computed.
"""
abstract type AbstractStorageUnit end
# this is the type of record associated to the storage unit
record_type(::Type{SU}) where {SU<:AbstractStorageUnit} =
error("Type of record contained by storage unit $(SU) not defined.")
"""
AbstractRecord
A record is the particular condition that a storage unit is in at a specific time
of the execution of Coluna.
For each record, a constructor should be defined which
takes a model and a unit as parameters. This constructor
is called during storing a unit.
"""
abstract type AbstractRecord end
"""
restore_from_record!(model, unit, record)
This method should be defined for every triple (model type, unit type, record type)
used by an algorithm.
"""
restore_from_record!(model::AbstractModel, unit::AbstractStorageUnit, record::AbstractRecord) =
error(string(
"restore_from_record! not defined for model type $(typeof(model)), ",
"unit type $(typeof(unit)), and record type $(typeof(record))"
))
# """
# EmptyRecord
# If a storage unit is not changed after initialization, then
# the empty record should be used with it.
# """
# struct EmptyRecord <: AbstractRecord end
# EmptyRecord(model::AbstractModel, unit::AbstractStorageUnit) = nothing
# restore_from_record!(::AbstractModel, ::AbstractStorageUnit, ::EmptyRecord) = nothing
# UnitType = Pair{Type{<:AbstractStorageUnit}, Type{<:AbstractRecord}}.
# see https://github.com/atoptima/Coluna.jl/pull/323#discussion_r418972805
const UnitType = DataType #Type{<:AbstractStorageUnit}
# TO DO : replace with the set of UnitType, should only contain records which should
# be restored for writing (all other records are restored anyway but just for reading)
#const UnitsUsageDict = Dict{Tuple{AbstractModel,UnitType},UnitPermission}
# TODO :
# function Base.show(io::IO, usagedict::UnitsUsageDict)
# print(io, "storage units usage dict [")
# for usage in usagedict
# print(io, " (", typeof(usage[1][1]), ", ", usage[1][2], ") => ", usage[2])
# end
# print(io, " ]")
# end
"""
RecordWrapper
It wraps and contains additional information about a record.
The participation is equal to the number of times the record has been stored.
When the participation drops to zero, the record can be deleted.
"""
const RecordId = Int
mutable struct RecordWrapper{R <: AbstractRecord}
id::RecordId
participation::Int
record::Union{R,Nothing}
end
RecordWrapper{R}(recordid::RecordId, participation::Int) where {R <: AbstractRecord} =
RecordWrapper{R}(recordid, participation, nothing)
getrecordid(rw::RecordWrapper) = rw.id
recordisempty(rw::RecordWrapper) = rw.record === nothing
getparticipation(rw::RecordWrapper) = rw.participation
getrecord(rw::RecordWrapper) = rw.record
increaseparticipation!(rw::RecordWrapper) = rw.participation += 1
decreaseparticipation!(rw::RecordWrapper) = rw.participation -= 1
function setrecord!(rw::RecordWrapper{R}, record_to_set::R) where {R <: AbstractRecord}
rw.record = record_to_set
end
function Base.show(io::IO, rw::RecordWrapper{R}) where {R <: AbstractRecord}
print(io, "record ", remove_until_last_point(string(R)))
print(io, " with id=", getrecordid(rw), " part=", getparticipation(rw))
if getrecord(rw) === nothing
print(io, " empty")
else
print(io, " ", getrecord(rw))
end
end
# """
# EmptyRecordWrapper
# """
# const EmptyRecordWrapper = RecordWrapper{EmptyRecord}
# EmptyRecordWrapper(recordid::RecordId, participation::Int) =
# EmptyRecordWrapper(1, 0)
# getrecordid(erw::EmptyRecordWrapper) = 1
# recordisempty(erw::EmptyRecordWrapper) = true
# getparticipation(erw::EmptyRecordWrapper) = 0
# increaseparticipation!(erw::EmptyRecordWrapper) = nothing
# decreaseparticipation!(erw::EmptyRecordWrapper) = nothing
"""
StorageUnitWrapper
This container keeps a storage unit and all records which have been
stored. It implements storing and restoring records of units in an
efficient way.
"""
mutable struct StorageUnitWrapper{M <: AbstractModel,SU <: AbstractStorageUnit,R <: AbstractRecord}
model::M
cur_record::RecordWrapper{R}
maxrecordid::RecordId
storage_unit::SU
typepair::UnitType
recordsdict::Dict{RecordId,RecordWrapper{R}}
end
function StorageUnitWrapper{M,SU,R}(model::M) where {M,SU,R}
return StorageUnitWrapper{M,SU,R}(
model, RecordWrapper{R}(1, 0), 1, SU(model),
SU, Dict{RecordId,RecordWrapper{R}}()
)
end
const RecordsVector = Vector{Pair{StorageUnitWrapper,RecordId}}
struct Storage
units::Dict{UnitType, StorageUnitWrapper}
end
Storage() = Storage(Dict{UnitType, StorageUnitWrapper}())
# TODO
# function Base.show(io::IO, storage::StorageUnitWrapper)
# println(io, "todo.")
# # print(io, "unit (")
# # print(IOContext(io, :compact => true), storage.model)
# # (StorageUnitType, RecordType) = storage.typepair
# # print(io, ", ", remove_until_last_point(string(StorageUnitType)))
# # print(io, ", ", remove_until_last_point(string(RecordType)), ")")
# end
function setcurrecord!(
storage::StorageUnitWrapper{M,SU,R}, record::RecordWrapper{R}
) where {M,SU,R}
# we delete the current record container from the dictionary if necessary
if !recordisempty(storage.cur_record) && getparticipation(storage.cur_record) == 0
delete!(storage.recordsdict, getrecordid(storage.cur_record))
end
storage.cur_record = record
if storage.maxrecordid < getrecordid(record)
storage.maxrecordid = getrecordid(record)
end
end
function _increaseparticipation!(storage::StorageUnitWrapper, recordid::RecordId)
record = if getrecordid(storage.cur_record) === recordid
storage.cur_record
else
get(storage.recordsdict, recordid, nothing)
end
if record === nothing
error(string("Record with id $recordid does not exist for ", storage))
end
increaseparticipation!(record)
return
end
# TODO : review
function retrieve_from_recordsdict(storage::StorageUnitWrapper, recordid::RecordId)
if !haskey(storage.recordsdict, recordid)
error(string("State with id $recordid does not exist for ", storage))
end
record = storage.recordsdict[recordid]
decreaseparticipation!(record)
if getparticipation(record) < 0
error(string("Participation is below zero for record with id $recordid of ", storage))
end
return record
end
# TODO : review / refactor
function save_to_recordsdict!(
storage::StorageUnitWrapper{M,SU,R}, record::RecordWrapper{R}
) where {M,SU,R}
if getparticipation(record) > 0 && recordisempty(record)
record_content = R(storage.model, storage.storage_unit)
# @logmsg LogLevel(-2) string("Created record with id ", getrecordid(record), " for ", storage)
setrecord!(record, record_content)
storage.recordsdict[getrecordid(record)] = record
end
end
# TODO : refactor
function store_record!(storage::StorageUnitWrapper)::RecordId
increaseparticipation!(storage.cur_record)
return getrecordid(storage.cur_record)
end
# TODO: refactor
function restore_from_record!(
storage::StorageUnitWrapper{M,SU,R}, recordid::RecordId, mode::UnitPermission
) where {M,SU,R}
record = storage.cur_record
if getrecordid(record) == recordid
decreaseparticipation!(record)
if getparticipation(record) < 0
error(string("Participation is below zero for record with id $recordid of ", getnicename(storage)))
end
if mode == READ_AND_WRITE
save_to_recordsdict!(storage, record)
record = RecordWrapper{R}(storage.maxrecordid + 1, 0)
setcurrecord!(storage, record)
end
return
elseif mode != NOT_USED
# we save current record to dictionary if necessary
save_to_recordsdict!(storage, record)
end
record = retrieve_from_recordsdict(storage, recordid)
if mode == NOT_USED
if !recordisempty(record) && getparticipation(record) == 0
delete!(storage.recordsdict, getrecordid(record))
# @logmsg LogLevel(-2) string("Removed record with id ", getrecordid(record), " for ", storage)
end
else
restore_from_record!(storage.model, storage.storage_unit, getrecord(record))
# @logmsg LogLevel(-2) string("Restored record with id ", getrecordid(record), " for ", storage)
if mode == READ_AND_WRITE
record = RecordWrapper{R}(storage.maxrecordid + 1, 0)
end
setcurrecord!(storage, record)
end
end
function check_records_participation(storage::StorageUnitWrapper)
if getparticipation(storage.cur_record) > 0
@warn string("Positive participation of record ", storage.cur_record)
end
for (_, record) in storage.recordsdict
if getparticipation(record) > 0
@warn string("Positive participation of record ", record)
end
end
end
#######
"""
UnitsUsage()
Stores the access rights to some storage units.
"""
struct UnitsUsage
permissions::Dict{StorageUnitWrapper,UnitPermission}
end
UnitsUsage() = UnitsUsage(Dict{StorageUnitWrapper,UnitPermission}())
"""
set_permission!(units_usage, storage_unit, access_right)
Set the permission to a storage unit.
"""
function set_permission!(usages::UnitsUsage, unit::StorageUnitWrapper, mode::UnitPermission)
current_mode = get(usages.permissions, unit, NOT_USED)
new_mode = max(current_mode, mode)
usages.permissions[unit] = new_mode
return new_mode
end
"""
get_permission(units_usage, storage_unit, default)
Return the permission to a storage unit or `default` if the storage unit has
no permission entered in `units_usage`.
"""
function get_permission(usages::UnitsUsage, unit::StorageUnitWrapper, default)
return get(usages.permissions, unit, default)
end
"""
Storage unit functions used by Coluna
"""
# this is a "lighter" alternative to `restore_from_records!` below
# not used for the moment as it has impact on the code readability
# we keep this function for a while for the case when `restore_from_records!`
# happens to be a bottleneck
# function reserve_for_writing!(storage::StorageUnitWrapper{M,SU,R}) where {M,SU,R}
# save_to_recordsdict!(storage, storage.cur_record)
# storage.cur_record = RecordWrapper{R}(storage.maxrecordid + 1, 0)
# setcurrecord!(storage, storage.cur_record)
# end
function restore_from_records!(units_to_restore::UnitsUsage, records::RecordsVector)
for (storage, recordid) in records
mode = get_permission(units_to_restore, storage, READ_ONLY)
restore_from_record!(storage, recordid, mode)
end
empty!(records)
return
end
function remove_records!(records::RecordsVector)
TO.@timeit Coluna._to "Restore/remove records" begin
for (storage, recordid) in records
restore_from_record!(storage, recordid, NOT_USED)
end
end
empty!(records) # vector of records should be emptied
end
function copy_records(records::RecordsVector)::RecordsVector
recordscopy = RecordsVector()
for (storage, recordid) in records
push!(recordscopy, storage => recordid)
_increaseparticipation!(storage, recordid)
end
return recordscopy
end
# store_records is missing.
"""
IMPORTANT!
Every stored or copied record should be either restored or removed so that it's
participation is correctly computed and memory correctly controlled
"""
#####
function getstorageunit(m::AbstractModel, SU::Type{<:AbstractStorageUnit})
return getstoragewrapper(m, SU).storage_unit
end
function getstoragewrapper(m::AbstractModel, SU::Type{<:AbstractStorageUnit})
storagecont = get(getstorage(m).units, SU, nothing)
storagecont === nothing && error("No storage unit of type $SU in $(typeof(m)) with id $(getuid(m)).")
return storagecont
end