Skip to content

Commit 7a81505

Browse files
authored
Merge pull request #601 from JuliaRobotics/3Q20/542/saving_serialized_version
DFG version in serialized data
2 parents ff56034 + f02b6cd commit 7a81505

File tree

7 files changed

+38
-5
lines changed

7 files changed

+38
-5
lines changed

Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ JSON2 = "2535ab7d-5cd8-5a07-80ac-9b1792aadce3"
1414
LightGraphs = "093fc24a-ae57-5d10-9952-331d41423f4d"
1515
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
1616
Neo4j = "d2adbeaf-5838-5367-8a2f-e46d570981db"
17+
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
1718
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
1819
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
1920
SHA = "ea8e919c-243c-51af-8825-aaa63cd721ce"
@@ -31,6 +32,7 @@ JSON = "0.21"
3132
JSON2 = "0.3.1"
3233
LightGraphs = "1.2, 1.3"
3334
Neo4j = "2"
35+
Pkg = "1.4, 1.5"
3436
Reexport = "0.2, 0.3, 0.4, 0.5, 1"
3537
Requires = "0.5, 0.6, 0.7, 0.8, 0.9, 0.10, 1"
3638
TimeZones = "1.3.1"

src/CloudGraphsDFG/CloudGraphsDFG.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ import ...DistributedFactorGraphs: setSolverParams!,
8686
getDataEntry,
8787
addDataEntry!,
8888
updateDataEntry!,
89-
deleteDataEntry!
89+
deleteDataEntry!,
90+
_getDFGVersion
9091

9192
using Neo4j
9293
using Base64

src/CloudGraphsDFG/services/CGStructure.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ function _convertNodeToDict(abstractNode::N)::Dict{String, Any} where N <: Abstr
1010
data = length(cp.data) != 0 ? JSON2.write(cp.data) : "{}"
1111
ser = JSON2.read(JSON2.write(abstractNode), Dict{String, Any})
1212
ser["data"] = base64encode(data)
13+
ser["_version"] = _getDFGVersion()
1314
return ser
1415
end
1516

src/CloudGraphsDFG/services/CloudGraphsDFG.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,8 @@ function updateFactor!(dfg::CloudGraphsDFG, factor::DFGFactor; skipAddError::Boo
296296
# NOTE: We are no merging the factor tags into the labels anymore. We can index by that but not
297297
# going to pollute the graph with unnecessary (and potentially dangerous) labels.
298298
fnctype = getSolverData(factor).fnc.usrfnc!
299-
addProps = Dict("fnctype" => "\"$(String(_getname(fnctype)))\"")
299+
addProps = Dict(
300+
"fnctype" => "\"$(String(_getname(fnctype)))\"")
300301
query = """
301302
MATCH (session:$(join(_getLabelsForType(dfg, Session), ":")))
302303
MERGE (node:$(join(_getLabelsForInst(dfg, factor), ":")))

src/CloudGraphsDFG/services/CommonFunctions.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,8 @@ function _structToNeo4jProps(inst::Union{User, Robot, Session, PVND, N, APPE, AB
199199
for (k, v) in addProps
200200
write(io, "$cypherNodeName.$k= $v,")
201201
end
202-
# The node type
202+
# Write in the version and node type
203+
write(io, "$cypherNodeName._version=\"$(_getDFGVersion())\",")
203204
write(io, "$cypherNodeName._type=\"$(typeof(inst))\"")
204205
# Ref String(): "When possible, the memory of v will be used without copying when the String object is created.
205206
# This is guaranteed to be the case for byte vectors returned by take!" # Apparent replacement for takebuf_string()

src/DistributedFactorGraphs.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ using JSON2 # JSON2 requires all properties to be in correct sequence, can't gua
2727
using LinearAlgebra
2828
using SparseArrays
2929
using UUIDs
30+
using Pkg
3031

3132

3233
##==============================================================================

src/services/Serialization.jl

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,33 @@
22
# For all types that pack their type into their own structure (e.g. PPE)
33
const TYPEKEY = "_type"
44

5-
# Custom serialization
5+
## Custom serialization
66
using JSON
77
import JSON.show_json
88
import JSON.Writer: StructuralContext, JSONContext, show_json
99
import JSON.Serializations: CommonSerialization, StandardSerialization
1010
JSON.show_json(io::JSONContext, serialization::CommonSerialization, uuid::UUID) = print(io.io, "\"$uuid\"")
1111

12+
## Version checking
13+
function _getDFGVersion()
14+
if haskey(Pkg.dependencies(), Base.UUID("b5cc3c7e-6572-11e9-2517-99fb8daf2f04"))
15+
return string(Pkg.dependencies()[Base.UUID("b5cc3c7e-6572-11e9-2517-99fb8daf2f04")].version)
16+
else
17+
# This is arguably slower, but needed for Travis.
18+
return Pkg.TOML.parse(read(joinpath(dirname(pathof(@__MODULE__)), "..", "Project.toml"), String))["version"]
19+
end
20+
end
21+
22+
function _versionCheck(props::Dict{String, Any})
23+
if haskey(props, "_version")
24+
if props["_version"] != _getDFGVersion()
25+
@warn "This data was serialized using DFG $(props["_version"]) but you have $(_getDFGVersion()) installed, there may be deserialization issues."
26+
end
27+
else
28+
@warn "There isn't a version tag in this data so it's older than v0.10, there may be deserialization issues."
29+
end
30+
end
31+
1232
## Utility functions for ZonedDateTime
1333

1434
# Regex parser that converts clauses like ":59.82-" to well formatted ":59.820-"
@@ -92,6 +112,7 @@ function packVariable(dfg::G, v::DFGVariable)::Dict{String, Any} where G <: Abst
92112
props["softtype"] = typeModuleName(getSofttype(v))
93113
props["dataEntry"] = JSON2.write(Dict(keys(v.dataDict) .=> map(bde -> JSON.json(bde), values(v.dataDict))))
94114
props["dataEntryType"] = JSON2.write(Dict(keys(v.dataDict) .=> map(bde -> typeof(bde), values(v.dataDict))))
115+
props["_version"] = _getDFGVersion()
95116
return props
96117
end
97118

@@ -101,6 +122,8 @@ function unpackVariable(dfg::G,
101122
unpackSolverData::Bool=true,
102123
unpackBigData::Bool=true)::DFGVariable where G <: AbstractDFG
103124
@debug "Unpacking variable:\r\n$packedProps"
125+
# Version checking.
126+
_versionCheck(packedProps)
104127
label = Symbol(packedProps["label"])
105128
# Make sure that the timestamp is correctly formatted with subseconds
106129
packedProps["timestamp"] = getStandardZDTString(packedProps["timestamp"])
@@ -228,7 +251,7 @@ function packFactor(dfg::G, f::DFGFactor)::Dict{String, Any} where G <: Abstract
228251
props["fnctype"] = String(_getname(fnctype))
229252
props["_variableOrderSymbols"] = JSON2.write(f._variableOrderSymbols)
230253
props["solvable"] = getSolvable(f)
231-
254+
props["_version"] = _getDFGVersion()
232255
return props
233256
end
234257

@@ -244,6 +267,9 @@ end
244267

245268

246269
function unpackFactor(dfg::G, packedProps::Dict{String, Any})::DFGFactor where G <: AbstractDFG
270+
# Version checking.
271+
_versionCheck(packedProps)
272+
247273
label = packedProps["label"]
248274
# Make sure that the timestamp is correctly formatted with subseconds
249275
packedProps["timestamp"] = getStandardZDTString(packedProps["timestamp"])

0 commit comments

Comments
 (0)