Skip to content

Commit 940898a

Browse files
committed
use TimesDates and Variable serialization WIP
1 parent bbde17c commit 940898a

18 files changed

Lines changed: 362 additions & 281 deletions

Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
1616
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
1717
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
1818
ManifoldsBase = "3362f125-f0bb-47a3-aa74-596ffd7ef2fb"
19-
NanoDates = "46f1a544-deae-4307-8689-c12aa3c955c6"
2019
OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
2120
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
2221
ProgressMeter = "92933f4c-e287-5a05-a399-4b506db050ca"
@@ -29,6 +28,7 @@ Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
2928
Tar = "a4e569a6-e804-4fa4-b0f3-eef7a1d5b13e"
3029
TensorCast = "02d47bb6-7ce6-556a-be16-bb1710789e2b"
3130
TimeZones = "f269a46b-ccf7-5d73-abea-4c690281aa53"
31+
TimesDates = "bdfc003b-8df8-5c39-adcd-3a9087f5df4a"
3232
UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"
3333

3434
[weakdeps]
@@ -57,7 +57,6 @@ JSON = "1.0.0"
5757
LieGroups = "0.1"
5858
LinearAlgebra = "1.10"
5959
ManifoldsBase = "1, 2"
60-
NanoDates = "1.0.3"
6160
OrderedCollections = "1.4"
6261
Pkg = "1.4, 1.5"
6362
ProgressMeter = "1"
@@ -72,6 +71,7 @@ Tar = "1.9"
7271
TensorCast = "0.3.3, 0.4"
7372
Test = "1.10"
7473
TimeZones = "1.3.1"
74+
TimesDates = "0.3.3"
7575
UUIDs = "1.10"
7676
julia = "1.10"
7777

src/DataBlobs/entities/BlobEntry.jl

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,21 @@ StructUtils.@kwarg struct Blobentry
4040
""" Additional information that can help a different user of the Blob. """
4141
description::String = ""
4242
""" MIME description describing the format of binary data in the `Blob`, e.g. 'image/png' or 'application/json'. """
43-
mimetype::String = "application/octet-stream" #FIXME ::MIME = MIME("application/octet-stream")
43+
mimetype::MIME = MIME("application/octet-stream")
4444
""" Storage for a couple of bytes directly in the graph. Use with caution and keep it small and simple."""
4545
metadata::JSONText = JSONText("{}")
4646
""" When the Blob itself was first created. Serialized as an ISO 8601 string."""
47-
timestamp::NanoDate = ndnow(UTC) & (json = (lower = timestamp,),)
47+
timestamp::TimeDateZone = TimeDateZone(now(localzone()))
4848
""" Type version of this Blobentry."""
49-
version::VersionNumber = version(Blobentry)
49+
version::VersionNumber = DFG.version(Blobentry)
5050
end
5151
version(::Type{Blobentry}) = v"0.1.0"
5252

53-
function Blobentry(label::Symbol, blobstore = :default; kwargs...)
54-
return Blobentry(; label, blobstore, kwargs...)
53+
function Blobentry(label::Symbol, blobstore = :default; metadata, kwargs...)
54+
if !(metadata isa JSONText)
55+
metadata = JSONText(JSON.json(metadata))
56+
end
57+
return Blobentry(; label, blobstore, metadata, kwargs...)
5558
end
5659
# construction helper from existing Blobentry for user overriding via kwargs
5760
function Blobentry(
@@ -123,3 +126,25 @@ function Base.setproperty!(x::Blobentry, f::Symbol, val)
123126
end
124127

125128
const Blobentries = OrderedDict{Symbol, Blobentry}
129+
130+
#TODO write Blobentries as array
131+
# StructUtils.dictlike(::Type{Blobentries}) = false
132+
# StructUtils.structlike(::Type{Blobentries}) = false
133+
# StructUtils.arraylike(::Type{Blobentries}) = false
134+
135+
# function StructUtils.lower(entries::Blobentries)
136+
# return map(collect(values(entries))) do (entry)
137+
# return StructUtils.lower(entry)
138+
# end
139+
# end
140+
141+
# function StructUtils.lift(s::StructUtils.StructStyle, T::Type{Blobentries}, json_vector)
142+
# entries = T()
143+
# @warn "lifting" s T json_vector
144+
# foreach(json_vector) do obj
145+
# global gobj = obj
146+
# return push!(entries, Symbol(obj.label) => StructUtils.make(s, Blobentry, obj))
147+
# end
148+
# @warn "lifted Blobentries" entries
149+
# return entries, nothing
150+
# end

src/DataBlobs/services/BlobEntry.jl

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,17 @@ Also see: [`addBlobentry!`](@ref), [`getBlob`](@ref), [`listBlobentries`](@ref)
6767
"""
6868
function getBlobentry(var::AbstractGraphVariable, key::Symbol)
6969
if !hasBlobentry(var, key)
70-
throw(LabelNotFoundError("Blobentry", key, collect(keys(var.dataDict))))
70+
throw(LabelNotFoundError("Blobentry", key, collect(keys(var.blobentries))))
7171
end
72-
return var.dataDict[key]
72+
return var.blobentries[key]
7373
end
7474

75-
function getBlobentry(var::VariableDFG, key::Symbol)
76-
if !hasBlobentry(var, key)
77-
throw(LabelNotFoundError("Blobentry", key))
78-
end
79-
return var.blobEntries[findfirst(x -> x.label == key, var.blobEntries)]
80-
end
75+
# function getBlobentry(var::VariableDFG, key::Symbol)
76+
# if !hasBlobentry(var, key)
77+
# throw(LabelNotFoundError("Blobentry", key))
78+
# end
79+
# return var.blobEntries[findfirst(x -> x.label == key, var.blobEntries)]
80+
# end
8181

8282
"""
8383
$(SIGNATURES)
@@ -131,17 +131,17 @@ Should be extended if DFG variable is not returned by reference.
131131
Also see: [`getBlobentry`](@ref), [`addBlob!`](@ref), [`mergeBlobentry!`](@ref)
132132
"""
133133
function addBlobentry!(var::VariableCompute, entry::Blobentry)
134-
haskey(var.dataDict, entry.label) && throw(LabelExistsError("Blobentry", entry.label))
135-
var.dataDict[entry.label] = entry
134+
haskey(var.blobentries, entry.label) && throw(LabelExistsError("Blobentry", entry.label))
135+
var.blobentries[entry.label] = entry
136136
return entry
137137
end
138138

139-
function addBlobentry!(var::VariableDFG, entry::Blobentry)
140-
entry.label in getproperty.(var.blobEntries, :label) &&
141-
throw(LabelExistsError("Blobentry", entry.label))
142-
push!(var.blobEntries, entry)
143-
return entry
144-
end
139+
# function addBlobentry!(var::VariableDFG, entry::Blobentry)
140+
# entry.label in getproperty.(var.blobEntries, :label) &&
141+
# throw(LabelExistsError("Blobentry", entry.label))
142+
# push!(var.blobEntries, entry)
143+
# return entry
144+
# end
145145

146146
function addBlobentry!(dfg::AbstractDFG, vLbl::Symbol, entry::Blobentry)
147147
return addBlobentry!(getVariable(dfg, vLbl), entry)
@@ -158,10 +158,10 @@ If the Blobentry does not exist, it will be added.
158158
Notes:
159159
"""
160160
function mergeBlobentry!(var::AbstractGraphVariable, bde::Blobentry)
161-
if !haskey(var.dataDict, bde.label)
161+
if !haskey(var.blobentries, bde.label)
162162
addBlobentry!(var, bde)
163163
else
164-
var.dataDict[bde.label] = bde
164+
var.blobentries[bde.label] = bde
165165
end
166166
return 1
167167
end
@@ -179,15 +179,15 @@ Notes:
179179
"""
180180
function deleteBlobentry!(var::VariableCompute, key::Symbol)
181181
!hasBlobentry(var, key) && throw(LabelNotFoundError("Blobentry", key))
182-
delete!(var.dataDict, key)
182+
delete!(var.blobentries, key)
183183
return 1
184184
end
185185

186-
function deleteBlobentry!(var::VariableDFG, key::Symbol)
187-
!hasBlobentry(var, key) && throw(LabelNotFoundError("Blobentry", key))
188-
deleteat!(var.blobEntries, findfirst(x -> x.label == key, var.blobEntries))
189-
return 1
190-
end
186+
# function deleteBlobentry!(var::VariableDFG, key::Symbol)
187+
# !hasBlobentry(var, key) && throw(LabelNotFoundError("Blobentry", key))
188+
# deleteat!(var.blobEntries, findfirst(x -> x.label == key, var.blobEntries))
189+
# return 1
190+
# end
191191

192192
function deleteBlobentry!(dfg::AbstractDFG, label::Symbol, key::Symbol)
193193
return deleteBlobentry!(getVariable(dfg, label), key)
@@ -223,24 +223,24 @@ end
223223
224224
Does a blob entry exist with `blobLabel`.
225225
"""
226-
hasBlobentry(v::VariableCompute, blobLabel::Symbol) = haskey(v.dataDict, blobLabel)
226+
hasBlobentry(v::VariableCompute, blobLabel::Symbol) = haskey(v.blobentries, blobLabel)
227227

228-
function hasBlobentry(v::VariableDFG, label::Symbol)
229-
return label in getproperty.(v.blobEntries, :label)
230-
end
228+
# function hasBlobentry(v::VariableDFG, label::Symbol)
229+
# return label in getproperty.(v.blobEntries, :label)
230+
# end
231231

232232
"""
233233
$(SIGNATURES)
234234
235235
Get blob entries, returns a `Vector{Blobentry}`.
236236
"""
237237
function getBlobentries(v::VariableCompute)
238-
return collect(values(v.dataDict))
238+
return collect(values(v.blobentries))
239239
end
240240

241-
function getBlobentries(v::VariableDFG)
242-
return copy(v.blobEntries)
243-
end
241+
# function getBlobentries(v::VariableDFG)
242+
# return copy(v.blobEntries)
243+
# end
244244

245245
function getBlobentries(
246246
v::AbstractGraphVariable;
@@ -289,12 +289,12 @@ const collectBlobentries = gatherBlobentries
289289
List the blob entries associated with a particular variable.
290290
"""
291291
function listBlobentries(var::AbstractGraphVariable)
292-
return collect(keys(var.dataDict))
292+
return collect(keys(var.blobentries))
293293
end
294294

295-
function listBlobentries(var::VariableDFG)
296-
return getproperty.(var.blobEntries, :label)
297-
end
295+
# function listBlobentries(var::VariableDFG)
296+
# return getproperty.(var.blobEntries, :label)
297+
# end
298298

299299
function listBlobentries(dfg::AbstractDFG, label::Symbol)
300300
return listBlobentries(getVariable(dfg, label))

src/Deprecated.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ function setSolverData!(v::VariableCompute, data::State, key::Symbol = :default)
193193
:setSolverData!,
194194
)
195195
@assert key == data.solveKey "State.solveKey=:$(data.solveKey) does not match requested :$(key)"
196-
return v.solverDataDict[key] = data
196+
return v.states[key] = data
197197
end
198198

199199
@deprecate mergeVariableSolverData!(args...; kwargs...) mergeState!(args...; kwargs...)

src/DistributedFactorGraphs.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ using DocStringExtensions
2020
using Dates
2121
using Random
2222
using TimeZones
23-
using NanoDates
23+
using TimesDates
2424
using JSON
2525
export StructUtils # export for use in macros
2626
using LinearAlgebra
@@ -83,7 +83,7 @@ export FactorDFG, FactorSummary, FactorSkeleton
8383

8484
export Blobentry
8585

86-
export State, PackedState
86+
export State
8787

8888
##------------------------------------------------------------------------------
8989
## Functions
@@ -531,6 +531,7 @@ include("entities/Bloblet.jl")
531531
include("DataBlobs/entities/BlobEntry.jl")
532532
include("DataBlobs/entities/BlobStores.jl")
533533

534+
include("serialization/DFGStructStyles.jl")
534535
include("serialization/PackedSerialization.jl")
535536
include("serialization/DistributionSerialization.jl")
536537

src/FileDFG/services/FileDFG.jl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,19 @@ function saveDFG(folder::AbstractString, dfg::AbstractDFG; saveMetadata::Bool =
4040
map(f -> rm("$factorFolder/$f"), readdir(factorFolder))
4141
# Variables
4242
@showprogress "saving variables" for v in variables
43-
vPacked = packVariable(v)
44-
JSON.json("$varFolder/$(v.label).json", vPacked)
43+
# vPacked = packVariable(v)
44+
JSON.json("$varFolder/$(v.label).json", v; style = DFGJSONStyle())
4545
end
4646
# Factors
4747
@showprogress "saving factors" for f in factors
48-
JSON.json("$factorFolder/$(f.label).json", f)
48+
JSON.json("$factorFolder/$(f.label).json", f; style = DFGJSONStyle())
4949
end
5050
#GraphsDFG metadata
5151
if saveMetadata
5252
@assert isa(dfg, GraphsDFG) "only metadata for GraphsDFG are supported"
5353
@info "saving dfg metadata"
5454
fgPacked = GraphsDFGs.packDFGMetadata(dfg)
55-
JSON.json("$savepath/dfg.json", fgPacked)
55+
JSON.json("$savepath/dfg.json", fgPacked; style = DFGJSONStyle())
5656
end
5757

5858
savedir = dirname(savepath) # is this a path of just local name? #344 -- workaround with unique names
@@ -138,7 +138,7 @@ function loadDFG!(
138138
@assert isa(dfgLoadInto, GraphsDFG) "Only GraphsDFG metadata are supported"
139139
@info "loading dfg metadata"
140140
jstr = read("$folder/dfg.json", String)
141-
fgPacked = JSON.parse(jstr, GraphsDFGs.PackedGraphsDFG)
141+
fgPacked = JSON.parse(jstr, GraphsDFGs.PackedGraphsDFG; style = DFGJSONStyle())
142142
GraphsDFGs.unpackDFGMetadata!(dfgLoadInto, fgPacked)
143143
end
144144

@@ -164,7 +164,7 @@ function loadDFG!(
164164
# type instability on `variables` as either `::Vector{Variable}` or `::Vector{VariableCompute{<:}}` (vector of abstract)
165165
variables = @showprogress 1 "loading variables" asyncmap(varFiles) do varFile
166166
jstr = read("$varFolder/$varFile", String)
167-
packedvar = JSON.parse(jstr, VariableDFG)
167+
packedvar = JSON.parse(jstr, VariableDFG; style = DFGJSONStyle())
168168
v = usePackedVariable ? packedvar : unpackVariable(packedvar)
169169
return addVariable!(dfgLoadInto, v)
170170
end
@@ -176,7 +176,7 @@ function loadDFG!(
176176

177177
# `factors` is not type stable `::Vector{Factor}` or `::Vector{FactorCompute{<:}}` (vector of abstract)
178178
factors = @showprogress 1 "loading factors" asyncmap(factorFiles) do factorFile
179-
f = JSON.parsefile("$factorFolder/$factorFile", FactorDFG)
179+
f = JSON.parsefile("$factorFolder/$factorFile", FactorDFG; style = DFGJSONStyle())
180180
return addFactor!(dfgLoadInto, f)
181181
end
182182

@@ -228,12 +228,12 @@ function loadDFG(file::AbstractString)
228228
#TODO deprecate old format, v0.28
229229
local fgPacked
230230
try
231-
fgPacked = JSON.parse(jstr, GraphsDFGs.PackedGraphsDFG)
231+
fgPacked = JSON.parse(jstr, GraphsDFGs.PackedGraphsDFG; style = DFGJSONStyle())
232232
catch e
233233
if e isa MethodError
234234
@warn "Deprecated serialization: Failed to read DFG metadata. Attempting to load using the old format. Error:" e
235235
fgPacked =
236-
GraphsDFGs.PackedGraphsDFG(JSON.parse(jstr, GraphsDFGs._OldPackedGraphsDFG))
236+
GraphsDFGs.PackedGraphsDFG(JSON.parse(jstr, GraphsDFGs._OldPackedGraphsDFG; style = DFGJSONStyle()))
237237
else
238238
rethrow(e)
239239
end

src/entities/Bloblet.jl

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,46 @@ function StructUtils.lift(::Type{Bloblets}, json_vector::Vector)
2121
Symbol(x["label"]) => Bloblet(Symbol(x["label"]), x["val"]) for x in json_vector
2222
)
2323
end
24+
25+
"""
26+
$(SIGNATURES)
27+
"""
28+
function getBloblet(node, label::Symbol)
29+
!haskey(refBloblets(node), label) && throw(LabelNotFoundError("Bloblet", label))
30+
return refBloblets(node)[label]
31+
end
32+
33+
"""
34+
$(SIGNATURES)
35+
"""
36+
function addBloblet!(node, bloblet::Bloblet)
37+
label = getLabel(bloblet)
38+
haskey(refBloblets(node), label) && throw(LabelExistsError("Bloblet", label))
39+
refBloblets(node)[label] = bloblet
40+
return bloblet
41+
end
42+
43+
"""
44+
$(SIGNATURES)
45+
"""
46+
function mergeBloblet!(node, bloblet::Bloblet)
47+
refBloblets(node)[getLabel(bloblet)] = bloblet
48+
return 1
49+
end
50+
51+
"""
52+
$(SIGNATURES)
53+
"""
54+
function deleteBloblet!(node, label::Symbol)
55+
!haskey(refBloblets(node), label) && throw(LabelNotFoundError("Bloblet", label))
56+
pop!(refBloblets(node), label)
57+
return 1
58+
end
59+
60+
"""
61+
$(SIGNATURES)
62+
List all Bloblet keys for a variable `label` in `dfg`
63+
"""
64+
function listBloblets(node)
65+
return collect(keys(refBloblets(node)))
66+
end

0 commit comments

Comments
 (0)