Skip to content

Commit 0a43a5d

Browse files
committed
save and load to gzip as default
1 parent e73e71d commit 0a43a5d

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

src/FileDFG/services/FileDFG.jl

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ v1 = addVariable!(dfg, :a, ContinuousScalar, labels = [:POSE], solvable=0)
1414
saveDFG(dfg, "/tmp/saveDFG")
1515
```
1616
"""
17-
function saveDFG(dfg::G, folder::String) where G <: AbstractDFG
17+
function saveDFG(dfg::AbstractDFG, folder::String; compress::Symbol=:gzip)
1818
variables = getVariables(dfg)
1919
factors = getFactors(dfg)
2020
varFolder = "$folder/variables"
@@ -43,6 +43,19 @@ function saveDFG(dfg::G, folder::String) where G <: AbstractDFG
4343
JSON2.write(io, fPacked)
4444
close(io)
4545
end
46+
47+
# compress newly saved folder, skip if not supported format
48+
!(compress in [:gzip]) && return
49+
savepath = folder[end] == '/' ? folder[1:end-1] : folder
50+
savedir = dirname(savepath)
51+
savename = splitpath(string(savepath))[end]
52+
@assert savename != ""
53+
# temporarily change working directory to get correct zipped path
54+
here = Base.pwd()
55+
Base.cd(savedir)
56+
run(`tar -zcf $savepath.tar.gz $savename`)
57+
Base.rm(savename, recursive=true)
58+
Base.cd(here)
4659
end
4760

4861
"""
@@ -56,12 +69,25 @@ using DistributedFactorGraphs, IncrementalInference
5669
# Create a DFG - can make one directly, e.g. GraphsDFG{NoSolverParams}() or use IIF:
5770
dfg = initfg()
5871
# Load the graph
59-
loadDFG("/tmp/savedgraph", IncrementalInference, dfg)
72+
loadDFG("/tmp/savedgraph.tar.gz", IncrementalInference, dfg)
73+
loadDFG("/tmp/savedgraph", IncrementalInference, dfg) # alternative
6074
# Use the DFG as you do normally.
6175
ls(dfg)
6276
```
6377
"""
64-
function loadDFG(folder::String, iifModule, dfgLoadInto::G) where G <: AbstractDFG
78+
function loadDFG(dst::String, iifModule, dfgLoadInto::G) where G <: AbstractDFG
79+
# check if zipped dst first
80+
folder = dst
81+
sdist = split(dst, '.')
82+
if sdist[end] == "gz" && sdist[end-1] == "tar"
83+
caesardir = joinpath("/tmp","caesar","random")
84+
Base.mkpath(caesardir)
85+
folder = joinpath(caesardir, splitpath(string(sdist[end-2]))[end] )
86+
@info "loadDF detected a gzip tarball -- unpacking via $folder now..."
87+
Base.rm(folder, recursive=true, force=true)
88+
# unzip the tar file
89+
run(`tar -zxf $dst -C $caesardir`)
90+
end
6591
variables = DFGVariable[]
6692
factors = DFGFactor[]
6793
varFolder = "$folder/variables"

0 commit comments

Comments
 (0)