Skip to content

Commit 35d13a3

Browse files
committed
Cleanup on packing and unpacking
1 parent e9e696b commit 35d13a3

File tree

8 files changed

+78
-96
lines changed

8 files changed

+78
-96
lines changed

src/CloudGraphsDFG/entities/CloudGraphsDFG.jl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,12 @@ mutable struct CloudGraphsDFG{T <: AbstractParams} <: AbstractDFG
1616
decodePackedTypeFunc
1717
rebuildFactorMetadata!
1818
labelDict::Dict{Symbol, Int64}
19-
variableCache::Dict{Symbol, DFGVariable}
20-
factorCache::Dict{Symbol, DFGFactor}
2119
addHistory::Vector{Symbol} #TODO: Discuss more - is this an audit trail?
2220
solverParams::T # Solver parameters
23-
useCache::Bool
2421
end
2522

2623
function show(io::IO, c::CloudGraphsDFG)
2724
println(io, "CloudGraphsDFG:")
2825
println(io, " - Neo4J instance: $(c.neo4jInstance.connection.host)")
2926
println(io, " - Session: $(c.userId):$(c.robotId):$(c.sessionId)")
30-
println(io, " - Caching: $(c.useCache)")
3127
end

src/CloudGraphsDFG/services/CGStructure.jl

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,6 @@ function clearSession!!(dfg::CloudGraphsDFG)::Nothing
235235

236236
# Clearing history
237237
dfg.addHistory = Symbol[]
238-
empty!(dfg.variableCache)
239-
empty!(dfg.factorCache)
240238
empty!(dfg.labelDict)
241239
return nothing
242240
end
@@ -251,8 +249,6 @@ function clearRobot!!(dfg::CloudGraphsDFG)::Nothing
251249

252250
# Clearing history
253251
dfg.addHistory = Symbol[]
254-
empty!(dfg.variableCache)
255-
empty!(dfg.factorCache)
256252
empty!(dfg.labelDict)
257253
return nothing
258254
end
@@ -267,8 +263,6 @@ function clearUser!!(dfg::CloudGraphsDFG)::Nothing
267263

268264
# Clearing history
269265
dfg.addHistory = Symbol[]
270-
empty!(dfg.variableCache)
271-
empty!(dfg.factorCache)
272266
empty!(dfg.labelDict)
273267
return nothing
274268
end

src/CloudGraphsDFG/services/CloudGraphsDFG.jl

Lines changed: 30 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,39 @@
33
$(SIGNATURES)
44
Create a new CloudGraphs-based DFG factor graph using a Neo4j.Connection.
55
"""
6-
function CloudGraphsDFG{T}(neo4jConnection::Neo4j.Connection, userId::String, robotId::String, sessionId::String, encodePackedTypeFunc, getPackedTypeFunc, decodePackedTypeFunc, rebuildFactorMetadata!; description::String="CloudGraphs DFG", solverParams::T=NoSolverParams(), useCache::Bool=false) where T <: AbstractParams
6+
function CloudGraphsDFG{T}(neo4jConnection::Neo4j.Connection,
7+
userId::String,
8+
robotId::String,
9+
sessionId::String,
10+
encodePackedTypeFunc,
11+
getPackedTypeFunc,
12+
decodePackedTypeFunc,
13+
rebuildFactorMetadata!;
14+
description::String="CloudGraphs DFG",
15+
solverParams::T=NoSolverParams()) where T <: AbstractParams
716
graph = Neo4j.getgraph(neo4jConnection)
817
neo4jInstance = Neo4jInstance(neo4jConnection, graph)
9-
return CloudGraphsDFG{T}(neo4jInstance, description, userId, robotId, sessionId, encodePackedTypeFunc, getPackedTypeFunc, decodePackedTypeFunc, rebuildFactorMetadata!, Dict{Symbol, Int64}(), Dict{Symbol, DFGVariable}(), Dict{Symbol, DFGFactor}(), Symbol[], solverParams, useCache)
18+
return CloudGraphsDFG{T}(neo4jInstance, description, userId, robotId, sessionId, encodePackedTypeFunc, getPackedTypeFunc, decodePackedTypeFunc, rebuildFactorMetadata!, Dict{Symbol, Int64}(), Symbol[], solverParams)
1019
end
1120
"""
1221
$(SIGNATURES)
1322
Create a new CloudGraphs-based DFG factor graph by specifying the Neo4j connection information.
1423
"""
15-
function CloudGraphsDFG{T}(host::String, port::Int, dbUser::String, dbPassword::String, userId::String, robotId::String, sessionId::String, encodePackedTypeFunc, getPackedTypeFunc, decodePackedTypeFunc, rebuildFactorMetadata!; description::String="CloudGraphs DFG", solverParams::T=NoSolverParams(), useCache::Bool=false) where T <: AbstractParams
24+
function CloudGraphsDFG{T}(host::String,
25+
port::Int,
26+
dbUser::String,
27+
dbPassword::String,
28+
userId::String,
29+
robotId::String,
30+
sessionId::String,
31+
encodePackedTypeFunc,
32+
getPackedTypeFunc,
33+
decodePackedTypeFunc,
34+
rebuildFactorMetadata!;
35+
description::String="CloudGraphs DFG",
36+
solverParams::T=NoSolverParams()) where T <: AbstractParams
1637
neo4jConnection = Neo4j.Connection(host, port=port, user=dbUser, password=dbPassword);
17-
return CloudGraphsDFG{T}(neo4jConnection, userId, robotId, sessionId, encodePackedTypeFunc, getPackedTypeFunc, decodePackedTypeFunc, rebuildFactorMetadata!, description=description, solverParams=solverParams, useCache=useCache)
38+
return CloudGraphsDFG{T}(neo4jConnection, userId, robotId, sessionId, encodePackedTypeFunc, getPackedTypeFunc, decodePackedTypeFunc, rebuildFactorMetadata!, description=description, solverParams=solverParams)
1839
end
1940

2041
"""
@@ -30,7 +51,7 @@ function _getDuplicatedEmptyDFG(dfg::CloudGraphsDFG)::CloudGraphsDFG
3051
length(_getLabelsFromCyphonQuery(dfg.neo4jInstance, "(node:$(dfg.userId):$(dfg.robotId):$(sessionId))")) == 0 && break
3152
end
3253
@debug "Unique+empty copy session name: $sessionId"
33-
return CloudGraphsDFG{typeof(dfg.solverParams)}(dfg.neo4jInstance.connection, dfg.userId, dfg.robotId, sessionId, dfg.encodePackedTypeFunc, dfg.getPackedTypeFunc, dfg.decodePackedTypeFunc, dfg.rebuildFactorMetadata!, solverParams=deepcopy(dfg.solverParams), description="(Copy of) $(dfg.description)", useCache=dfg.useCache)
54+
return CloudGraphsDFG{typeof(dfg.solverParams)}(dfg.neo4jInstance.connection, dfg.userId, dfg.robotId, sessionId, dfg.encodePackedTypeFunc, dfg.getPackedTypeFunc, dfg.decodePackedTypeFunc, dfg.rebuildFactorMetadata!, solverParams=deepcopy(dfg.solverParams), description="(Copy of) $(dfg.description)")
3455
end
3556

3657
# Accessors
@@ -49,8 +70,6 @@ function getSerializationModule(dfg::CloudGraphsDFG)::Module where G <: Abstract
4970
end
5071

5172
function exists(dfg::CloudGraphsDFG, nId::Symbol)
52-
# If in the dictionary, then shortcut return true
53-
dfg.useCache && haskey(dfg.labelDict, nId) && return true
5473
# Otherwise try get it
5574
nodeId = _tryGetNeoNodeIdFromNodeLabel(dfg.neo4jInstance, dfg.userId, dfg.robotId, dfg.sessionId, nId)
5675
if nodeId != nothing
@@ -80,12 +99,10 @@ function addVariable!(dfg::CloudGraphsDFG, variable::DFGVariable)::DFGVariable
8099
Neo4j.updatenodelabels(neo4jNode, union([string(variable.label), "VARIABLE", dfg.userId, dfg.robotId, dfg.sessionId], variable.tags))
81100

82101
# Make sure that if there exists a SESSION sentinel that it is attached.
83-
# TODO: Optimize this.
84102
_bindSessionNodeToInitialVariable(dfg.neo4jInstance, dfg.userId, dfg.robotId, dfg.sessionId, string(variable.label))
85103

86104
# Update our internal dictionaries.
87105
push!(dfg.labelDict, variable.label=>variable._internalId)
88-
push!(dfg.variableCache, variable.label=>variable)
89106
# Track insertion
90107
push!(dfg.addHistory, variable.label)
91108

@@ -120,7 +137,6 @@ function addFactor!(dfg::CloudGraphsDFG, variables::Vector{<:DFGVariable}, facto
120137

121138
# Graphs.add_vertex!(dfg.g, v)
122139
push!(dfg.labelDict, factor.label=>factor._internalId)
123-
push!(dfg.factorCache, factor.label=>factor)
124140
# Track insertion only for variables
125141
# push!(dfg.addHistory, factor.label
126142

@@ -137,9 +153,6 @@ function getVariable(dfg::CloudGraphsDFG, variableId::Int64)::DFGVariable
137153
variable = unpackVariable(dfg, props)
138154
variable._internalId = variableId
139155

140-
# Add to cache
141-
push!(dfg.variableCache, variable.label=>variable)
142-
143156
return variable
144157
end
145158

@@ -148,8 +161,6 @@ function getVariable(dfg::CloudGraphsDFG, label::Union{Symbol, String})::DFGVari
148161
if typeof(label) == String
149162
label = Symbol(label)
150163
end
151-
dfg.useCache && haskey(dfg.variableCache, label) && return dfg.variableCache[label]
152-
# Else try get it
153164
nodeId = _tryGetNeoNodeIdFromNodeLabel(dfg.neo4jInstance, dfg.userId, dfg.robotId, dfg.sessionId, label)
154165
if nodeId == nothing
155166
error("Unable to retrieve the ID for variable '$label'. Please check your connection to the database and that the variable exists.")
@@ -160,7 +171,7 @@ end
160171

161172
function getFactor(dfg::CloudGraphsDFG, factorId::Int64)::DFGFactor
162173
props = getnodeproperties(dfg.neo4jInstance.graph, factorId)
163-
factor = unpackFactor(dfg, props, getSerializationModule(dfg))
174+
factor = unpackFactor(dfg, props)
164175
factor._internalId = factorId
165176

166177
# Lastly, rebuild the metadata
@@ -169,18 +180,13 @@ function getFactor(dfg::CloudGraphsDFG, factorId::Int64)::DFGFactor
169180
# ... TODO: refactor if changed: https://github.com/JuliaRobotics/IncrementalInference.jl/issues/350
170181
getSolverData(factor).fncargvID = factor._variableOrderSymbols
171182

172-
# Add to cache
173-
push!(dfg.factorCache, factor.label=>factor)
174-
175183
return factor
176184
end
177185

178186
function getFactor(dfg::CloudGraphsDFG, label::Union{Symbol, String})::DFGFactor
179187
if typeof(label) == String
180188
label = Symbol(label)
181189
end
182-
dfg.useCache && haskey(dfg.factorCache, label) && return dfg.factorCache[label]
183-
# Else try get it
184190
nodeId = _tryGetNeoNodeIdFromNodeLabel(dfg.neo4jInstance, dfg.userId, dfg.robotId, dfg.sessionId, label)
185191
if nodeId == nothing
186192
error("Unable to retrieve the ID for factor '$label'. Please check your connection to the database and that the factor exists.")
@@ -216,7 +222,7 @@ function mergeUpdateVariableSolverData!(dfg::CloudGraphsDFG, sourceVariable::DFG
216222
Neo4j.setnodeproperty(dfg.neo4jInstance.graph, var._internalId, "ppeDict",
217223
JSON2.write(newEsts))
218224
Neo4j.setnodeproperty(dfg.neo4jInstance.graph, var._internalId, "solverDataDict",
219-
JSON2.write(Dict(keys(newSolveData) .=> map(vnd -> pack(dfg, vnd), values(newSolveData)))))
225+
JSON2.write(Dict(keys(newSolveData) .=> map(vnd -> packVariableNodeData(dfg, vnd), values(newSolveData)))))
220226
return sourceVariable
221227
end
222228

@@ -272,13 +278,7 @@ function updateFactor!(dfg::CloudGraphsDFG, variableIds::Vector{Symbol}, factor:
272278
end
273279

274280
function deleteVariable!(dfg::CloudGraphsDFG, label::Symbol)::DFGVariable
275-
variable = nothing
276-
if dfg.useCache && haskey(dfg.variableCache, label)
277-
variable = dfg.variableCache[label]
278-
else
279-
# Else try get it
280-
variable = getVariable(dfg, label)
281-
end
281+
variable = getVariable(dfg, label)
282282
if variable == nothing
283283
error("Unable to retrieve the ID for variable '$label'. Please check your connection to the database and that the variable exists.")
284284
end
@@ -288,7 +288,6 @@ function deleteVariable!(dfg::CloudGraphsDFG, label::Symbol)::DFGVariable
288288

289289
# Clearing history
290290
dfg.addHistory = symdiff(dfg.addHistory, [label])
291-
haskey(dfg.variableCache, label) && delete!(dfg.variableCache, label)
292291
haskey(dfg.labelDict, label) && delete!(dfg.labelDict, label)
293292
return variable
294293
end
@@ -297,13 +296,7 @@ end
297296
deleteVariable!(dfg::CloudGraphsDFG, variable::DFGVariable)::DFGVariable = deleteVariable!(dfg, variable.label)
298297

299298
function deleteFactor!(dfg::CloudGraphsDFG, label::Symbol)::DFGFactor
300-
factor = nothing
301-
if dfg.useCache && haskey(dfg.factoreCache, label)
302-
factor = dfg.factorCache[label]
303-
else
304-
# Else try get it
305-
factor = getFactor(dfg, label)
306-
end
299+
factor = getFactor(dfg, label)
307300
if factor == nothing
308301
error("Unable to retrieve the ID for factor '$label'. Please check your connection to the database and that the factor exists.")
309302
end
@@ -313,7 +306,6 @@ function deleteFactor!(dfg::CloudGraphsDFG, label::Symbol)::DFGFactor
313306

314307
# Clearing history
315308
dfg.addHistory = symdiff(dfg.addHistory, [label])
316-
haskey(dfg.factorCache, label) && delete!(dfg.factorCache, label)
317309
haskey(dfg.labelDict, label) && delete!(dfg.labelDict, label)
318310
return factor
319311
end

src/DistributedFactorGraphs.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ export mergeUpdateVariableSolverData!, mergeUpdateGraphSolverData!
7878
export VariableNodeData, PackedVariableNodeData
7979
export GenericFunctionNodeData
8080
export getSerializationModule, setSerializationModule!
81-
export pack, unpack
82-
# Resolve with above
81+
# Packing and unpacking
8382
export packVariable, unpackVariable, packFactor, unpackFactor
83+
export packVariableNodeData, unpackVariableNodeData
8484

8585
# TODO TAGS
8686
export listTags, mergeTags!, removeTags!, emptyTags!

src/FileDFG/services/FileDFG.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ function loadDFG(dst::String, iifModule, dfgLoadInto::G; loaddir=joinpath("/","t
124124
for factorFile in factorFiles
125125
io = open("$factorFolder/$factorFile")
126126
packedData = JSON2.read(io, Dict{String, Any})
127-
push!(factors, unpackFactor(dfgLoadInto, packedData, iifModule))
127+
push!(factors, unpackFactor(dfgLoadInto, packedData))
128128
end
129129
@info "Loaded $(length(variables)) factors - $(map(f->f.label, factors))"
130130
@info "Inserting factors into graph..."

src/services/AbstractDFG.jl

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,25 @@ function getSerializationModule(dfg::G)::Module where G <: AbstractDFG
177177
return Main
178178
end
179179

180-
180+
"""
181+
$(SIGNATURES)
182+
Get a type from the serialization module inside DFG.
183+
"""
184+
function getTypeFromSerializationModule(dfg::G, moduleType::Symbol) where G <: AbstractDFG
185+
st = nothing
186+
mainmod = getSerializationModule(dfg)
187+
mainmod == nothing && error("Serialization module is null - please call setSerializationNamespace!(\"Main\" => Main) in your main program.")
188+
try
189+
st = getfield(mainmod, Symbol(moduleType))
190+
catch ex
191+
@error "Unable to deserialize soft type $(d.softtype)"
192+
io = IOBuffer()
193+
showerror(io, ex, catch_backtrace())
194+
err = String(take!(io))
195+
@error err
196+
end
197+
return st
198+
end
181199

182200
"""
183201
$(SIGNATURES)

src/services/DFGFactor.jl

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,26 @@ function packFactor(dfg::G, f::DFGFactor)::Dict{String, Any} where G <: Abstract
2626
# Include the type
2727
props["fnctype"] = String(_getname(fnctype))
2828
props["_variableOrderSymbols"] = JSON2.write(f._variableOrderSymbols)
29-
props["solvable"] = f.solvable
29+
props["solvable"] = getSolvable(f)
3030

3131
return props
3232
end
3333

34-
function unpackFactor(dfg::G, packedProps::Dict{String, Any}, iifModule)::DFGFactor where G <: AbstractDFG
34+
function unpackFactor(dfg::G, packedProps::Dict{String, Any})::DFGFactor where G <: AbstractDFG
3535
label = packedProps["label"]
3636
timestamp = DateTime(packedProps["timestamp"])
3737
tags = JSON2.read(packedProps["tags"], Vector{Symbol})
3838

3939
data = packedProps["data"]
40-
@debug "Decoding $label..."
4140
datatype = packedProps["fnctype"]
42-
packtype = getfield(Main, Symbol("Packed"*datatype))
41+
@debug "DECODING Softtype = '$unpackedTypeName' for factor '$label'"
42+
packtype = getTypeFromSerializationModule(dfg, Symbol("Packed"*datatype))
43+
4344
packed = nothing
4445
fullFactor = nothing
4546
try
4647
packed = JSON2.read(data, GenericFunctionNodeData{packtype,String})
47-
fullFactor = iifModule.decodePackedType(dfg, packed)
48+
fullFactor = getSerializationModule(dfg).decodePackedType(dfg, packed)
4849
catch ex
4950
io = IOBuffer()
5051
showerror(io, ex, catch_backtrace())
@@ -57,13 +58,13 @@ function unpackFactor(dfg::G, packedProps::Dict{String, Any}, iifModule)::DFGFac
5758
_variableOrderSymbols = JSON2.read(packedProps["_variableOrderSymbols"], Vector{Symbol})
5859
solvable = packedProps["solvable"]
5960

60-
# Rebuild DFGVariable
61+
# Rebuild DFGFactor
6162
factor = DFGFactor{typeof(fullFactor.fnc), Symbol}(Symbol(label), 0, timestamp)
6263

6364
union!(factor.tags, tags)
6465
factor.data = fullFactor #TODO setSolverData!(factor, fullFactor)
6566
factor._variableOrderSymbols = _variableOrderSymbols
66-
factor.solvable = solvable
67+
setSolvable!(factor, solvable)
6768

6869
# GUARANTEED never to bite us in the ass in the future...
6970
# ... TODO: refactor if changed: https://github.com/JuliaRobotics/IncrementalInference.jl/issues/350

0 commit comments

Comments
 (0)