Skip to content

Commit 0a65ef2

Browse files
committed
better consolidated _unpackPPE
1 parent 1a333fe commit 0a65ef2

File tree

1 file changed

+40
-25
lines changed

1 file changed

+40
-25
lines changed

src/services/Serialization.jl

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,11 @@ end
7979

8080
typeModuleName(varT::Type{<:InferenceVariable}) = typeModuleName(varT())
8181

82-
function getTypeFromSerializationModule(variableTypeString::String)
82+
function getTypeFromSerializationModule(_typeString::AbstractString)
83+
@debug "DFG converting type string to Julia type" _typeString
8384
try
8485
# split the type at last `.`
85-
split_st = split(variableTypeString, r"\.(?!.*\.)")
86+
split_st = split(_typeString, r"\.(?!.*\.)")
8687
#if module is specified look for the module in main, otherwise use Main
8788
if length(split_st) == 2
8889
m = getfield(Main, Symbol(split_st[1]))
@@ -102,7 +103,7 @@ function getTypeFromSerializationModule(variableTypeString::String)
102103
return ret
103104

104105
catch ex
105-
@error "Unable to deserialize soft type $(variableTypeString)"
106+
@error "Unable to deserialize type $(_typeString)"
106107
io = IOBuffer()
107108
showerror(io, ex, catch_backtrace())
108109
err = String(take!(io))
@@ -152,11 +153,12 @@ end
152153

153154
"""
154155
$(SIGNATURES)
155-
Unpack a Dict{String, Any} into a PPE.
156+
157+
Common unpack a Dict{String, Any} into a PPE.
156158
"""
157159
function _unpackPPE(
158160
packedPPE::Dict{String, Any};
159-
_type = pop!(packedPPE, "_type")
161+
_type = pop!(packedPPE, "_type") # required for generic use
160162
)
161163
#
162164
# Cleanup Zoned timestamp, which is always UTC
@@ -165,18 +167,32 @@ function _unpackPPE(
165167
end
166168

167169
# !haskey(packedPPE, "_type") && error("Cannot find type key '_type' in packed PPE data")
168-
(_type === nothing || _type == "") && error("Cannot deserialize PPE, type key is empty")
170+
if (_type === nothing || _type == "")
171+
@warn "Cannot deserialize PPE, unknown type key, trying DistributedFactorGraphs.MeanMaxPPE" _type
172+
_type = "DistributedFactorGraphs.MeanMaxPPE"
173+
end
174+
ppeType = DistributedFactorGraphs.getTypeFromSerializationModule(_type)
175+
169176
ppe = Unmarshal.unmarshal(
170-
DistributedFactorGraphs.getTypeFromSerializationModule(_type),
171-
packedPPE
172-
)
177+
ppeType,
178+
packedPPE
179+
)
180+
# _pk = Symbol(packedPPE["solveKey"])
181+
# ppe = MeanMaxPPE(;
182+
# solveKey=_pk,
183+
# suggested=float.(pd["suggested"]),
184+
# max=float.(pd["max"]),
185+
# mean=float.(pd["mean"]),
186+
# lastUpdatedTimestamp=DateTime(string(pd["lastUpdatedTimestamp"]))
187+
# )
188+
173189
return ppe
174190
end
175191

176192
# returns a DFGVariable
177193
function unpackVariable(dfg::G,
178194
packedProps::Dict{String, Any};
179-
unpackPPEs::Bool=haskey(packedProps,"ppeDict"),
195+
unpackPPEs::Bool=true,
180196
unpackSolverData::Bool=true,
181197
unpackBigData::Bool=true) where G <: AbstractDFG
182198
@debug "Unpacking variable:\r\n$packedProps"
@@ -188,32 +204,31 @@ function unpackVariable(dfg::G,
188204
# Parse it
189205
timestamp = ZonedDateTime(packedProps["timestamp"])
190206
nstime = Nanosecond(get(packedProps, "nstime", 0))
191-
# Supporting string serialization using packVariable and CGDFG serialization (Vector{String})
192-
if packedProps["tags"] isa String
193-
tags = JSON2.read(packedProps["tags"], Vector{Symbol})
207+
208+
# FIXME, drop nested packing, see DFG #867
209+
# string serialization using packVariable and CGDFG serialization (Vector{String})
210+
tags = if packedProps["tags"] isa String
211+
JSON2.read(packedProps["tags"], Vector{Symbol})
194212
else
195-
tags = Symbol.(packedProps["tags"])
213+
Symbol.(packedProps["tags"])
196214
end
197-
ppeDict = if unpackPPEs
215+
216+
ppeDict = if haskey(packedProps,"ppesDict")
217+
# FIXME, drop nested packing, see DFG #867
198218
JSON2.read(packedProps["ppeDict"], Dict{Symbol, MeanMaxPPE})
199219
elseif haskey(packedProps,"ppes") && packedProps["ppes"] isa AbstractVector
220+
# these different cases are not well covered in tests, but first fix #867
221+
# TODO dont hardcode the ppeType (which is already discovered for each entry in _updatePPE)
200222
ppedict = Dict{Symbol, MeanMaxPPE}()
201223
for pd in packedProps["ppes"]
202-
# FIXME unmarshalling, consolidate with _unpackPPE
203-
pk = pd["solveKey"]
204-
_pk = Symbol(pk)
205-
ppedict[_pk] = MeanMaxPPE(;
206-
solveKey=_pk,
207-
suggested=float.(pd["suggested"]),
208-
max=float.(pd["max"]),
209-
mean=float.(pd["mean"]),
210-
lastUpdatedTimestamp=DateTime(string(pd["lastUpdatedTimestamp"]))
211-
)
224+
_type = haskey(pd, "_type") ? pd["_type"] : "DistributedFactorGraphs.MeanMaxPPE"
225+
ppedict[Symbol(pd["solveKey"])] = _unpackPPE(pd; _type)
212226
end
213227
ppedict
214228
else
215229
Dict{Symbol, MeanMaxPPE}()
216230
end
231+
217232
smallData = JSON2.read(packedProps["smallData"], Dict{Symbol, SmallDataTypes})
218233

219234
variableTypeString = if haskey(packedProps, "softtype")

0 commit comments

Comments
 (0)