Skip to content

Commit 4b0de79

Browse files
committed
Fix test with small data tipes
1 parent e8e590b commit 4b0de79

File tree

7 files changed

+73
-10
lines changed

7 files changed

+73
-10
lines changed

attic/InMemoryDataStore.jl

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
"""
2+
$(TYPEDEF)
3+
Simple in-memory data store with a specified data type and a specified key type.
4+
"""
5+
struct InMemoryDataStore{T, E <: AbstractDataEntry} <: AbstractDataStore{T}
6+
data::Dict{Symbol, T}
7+
entries::Dict{Symbol, E}
8+
end
9+
10+
"""
11+
$(SIGNATURES)
12+
Create an in-memory store using a specific data type.
13+
"""
14+
function InMemoryDataStore{T, E}() where {T, E <: AbstractDataEntry}
15+
return InMemoryDataStore{T, E}(Dict{Symbol, T}(), Dict{Symbol, E}())
16+
end
17+
18+
"""
19+
$(SIGNATURES)
20+
Create an in-memory store using binary data (UInt8) as a type.
21+
"""
22+
function InMemoryDataStore()
23+
return InMemoryDataStore{Vector{UInt8}, GeneralDataEntry}()
24+
end
25+
26+
27+
##==============================================================================
28+
## InMemoryDataStore CRUD
29+
##==============================================================================
30+
function getDataBlob(store::InMemoryDataStore{T, E}, entry::E)::Union{T, Nothing} where {T, E <: AbstractDataEntry}
31+
!haskey(store.data, entry.storeKey) && return nothing
32+
return store.data[entry.storeKey]
33+
end
34+
35+
function addDataBlob!(store::InMemoryDataStore{T, E}, entry::E, data::T)::T where {T, E <: AbstractDataEntry}
36+
haskey(store.entries, entry.storeKey) && @warn "Key '$(entry.storeKey)' already exists in the data store, overwriting!"
37+
store.entries[entry.storeKey] = entry
38+
# Update timestamp
39+
entry.lastUpdatedTimestamp = now()
40+
return store.data[entry.storeKey] = data
41+
end
42+
43+
function updateDataBlob!(store::InMemoryDataStore{T, E}, entry::E, data::T)::Union{T, Nothing} where {T, E <: AbstractDataEntry}
44+
!haskey(store.entries, entry.storeKey) && (@error "Key '$(entry.storeKey)' doesn't exist in the data store!"; return nothing)
45+
store.entries[entry.storeKey] = entry
46+
# Update timestamp
47+
entry.lastUpdatedTimestamp = now()
48+
return store.data[entry.storeKey] = data
49+
end
50+
51+
function deleteDataBlob!(store::InMemoryDataStore{T, E}, entry::E)::T where {T, E <: AbstractDataEntry}
52+
data = getDataBlob(store, entry)
53+
data == nothing && return nothing
54+
delete!(store.data, entry.storeKey)
55+
delete!(store.entries, entry.storeKey)
56+
return data
57+
end
58+
59+
function listDataBlobs(store::InMemoryDataStore{T, E})::Vector{E} where {T, E <: AbstractDataEntry}
60+
return collect(values(store.entries))
61+
end

src/DistributedFactorGraphs.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ export getSolverData
123123

124124
export getVariableType
125125

126+
# Small Data CRUD
127+
export SmallDataTypes, getSmallData, addSmallData!, updateSmallData!, deleteSmallData!, clearSmallData!
126128
export getSmallData, setSmallData!
127129

128130

src/entities/DFGVariable.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ getEstimateFields(::MeanMaxPPE) = [:suggested, :max, :mean]
217217
## DFG Variables
218218
##==============================================================================
219219

220-
const SmallDataType = Union{Int, Float64, String, Bool, Vector{Int}, Vector{Float64}, Vector{String}, Vector{Bool}}
220+
const SmallDataTypes = Union{Int, Float64, String, Bool, Vector{Int}, Vector{Float64}, Vector{String}, Vector{Bool}}
221221

222222
##------------------------------------------------------------------------------
223223
## DFGVariable lv2
@@ -250,7 +250,7 @@ struct DFGVariable{T<:InferenceVariable} <: AbstractDFGVariable
250250
solverDataDict::Dict{Symbol, VariableNodeData{T}}
251251
"""Dictionary of small data associated with this variable.
252252
Accessors: [`getSmallData`](@ref), [`setSmallData!`](@ref)"""
253-
smallData::Dict{String, SmallDataType}
253+
smallData::Dict{Symbol, SmallDataTypes}
254254
"""Dictionary of large data associated with this variable.
255255
Accessors: [`addDataEntry!`](@ref), [`getDataEntry`](@ref), [`updateDataEntry!`](@ref), and [`deleteDataEntry!`](@ref)"""
256256
dataDict::Dict{Symbol, AbstractDataEntry}
@@ -272,7 +272,7 @@ function DFGVariable(label::Symbol, softtype::T;
272272
tags::Set{Symbol}=Set{Symbol}(),
273273
estimateDict::Dict{Symbol, <: AbstractPointParametricEst}=Dict{Symbol, MeanMaxPPE}(),
274274
solverDataDict::Dict{Symbol, VariableNodeData{T}}=Dict{Symbol, VariableNodeData{T}}(),
275-
smallData::Dict{String, SmallDataType}=Dict{String, SmallDataType}(),
275+
smallData::Dict{String, SmallDataTypes}=Dict{String, SmallDataTypes}(),
276276
dataDict::Dict{Symbol, AbstractDataEntry}=Dict{Symbol,AbstractDataEntry}(),
277277
solvable::Int=1) where {T <: InferenceVariable}
278278

@@ -289,7 +289,7 @@ function DFGVariable(label::Symbol,
289289
nstime::Nanosecond = Nanosecond(0),
290290
tags::Set{Symbol}=Set{Symbol}(),
291291
estimateDict::Dict{Symbol, <: AbstractPointParametricEst}=Dict{Symbol, MeanMaxPPE}(),
292-
smallData::Dict{String, SmallDataType}=Dict{String, SmallDataType}(),
292+
smallData::Dict{String, SmallDataTypes}=Dict{String, SmallDataTypes}(),
293293
dataDict::Dict{Symbol, AbstractDataEntry}=Dict{Symbol,AbstractDataEntry}(),
294294
solvable::Int=1) where {T <: InferenceVariable}
295295
if timestamp isa DateTime

src/services/DFGVariable.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,14 +273,14 @@ setSolverData!(v::DFGVariable, data::VariableNodeData, key::Symbol=:default) = v
273273
$(SIGNATURES)
274274
Get the small data for a variable.
275275
"""
276-
getSmallData(v::DFGVariable)::Dict{String, SmallDataType} = v.smallData
276+
getSmallData(v::DFGVariable)::Dict{Symbol, SmallDataTypes} = v.smallData
277277

278278
"""
279279
$(SIGNATURES)
280280
Set the small data for a variable.
281281
This will overwrite old smallData.
282282
"""
283-
function setSmallData!(v::DFGVariable, smallData::Dict{String, SmallDataType})
283+
function setSmallData!(v::DFGVariable, smallData::Dict{Symbol, SmallDataTypes})
284284
empty!(v.smallData)
285285
merge!(v.smallData, smallData)
286286
end

src/services/Serialization.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ function unpackVariable(dfg::G,
3939
tags = Symbol.(packedProps["tags"])
4040
end
4141
ppeDict = unpackPPEs ? JSON2.read(packedProps["ppeDict"], Dict{Symbol, MeanMaxPPE}) : Dict{Symbol, MeanMaxPPE}()
42-
smallData = JSON2.read(packedProps["smallData"], Dict{String, SmallDataType})
42+
smallData = JSON2.read(packedProps["smallData"], Dict{Symbol, SmallDataTypes})
4343

4444
softtypeString = packedProps["softtype"]
4545
softtype = getTypeFromSerializationModule(dfg, Symbol(softtypeString))

test/interfaceTests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ end
5656
@test printFactor(fac1) == nothing
5757

5858
@test printVariable(iobuf, var1, skipfields=[:timestamp, :solver, :ppe, :nstime]) == nothing
59-
@test String(take!(iobuf)) == "DFGVariable{TestSofttype1}\nlabel:\n:a\ntags:\nSet([:VARIABLE, :POSE])\nsmallData:\nDict(\"small\"=>\"data\")\ndataDict:\nDict{Symbol,AbstractDataEntry}()\nsolvable:\n0\n"
59+
@test String(take!(iobuf)) == "DFGVariable{TestSofttype1}\nlabel:\n:a\ntags:\nSet([:VARIABLE, :POSE])\nsmallData:\nDict{Symbol,Union{Bool, Float64, Int64, Array{Bool,1}, Array{Float64,1}, Array{Int64,1}, Array{String,1}, String}}(:small=>\"data\")\ndataDict:\nDict{Symbol,AbstractDataEntry}()\nsolvable:\n0\n"
6060

6161
@test printVariable(iobuf, var1, short=true) == nothing
6262
@test String(take!(iobuf)) == "DFGVariable{TestSofttype1}\nlabel: a\ntags: Set([:VARIABLE, :POSE])\nsize marginal samples: (1, 1)\nkde bandwidths: [0.0]\nNo PPEs\n"

test/testBlocks.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ function DFGVariableSCA()
238238

239239
v1_lbl = :a
240240
v1_tags = Set([:VARIABLE, :POSE])
241-
small = Dict("small"=>"data")
241+
small = Dict{Symbol, SmallDataTypes}(:small=>"data")
242242
testTimestamp = now(localzone())
243243
# Constructors
244244
v1 = DFGVariable(v1_lbl, TestSofttype1(), tags=v1_tags, solvable=0, solverDataDict=Dict(:default=>VariableNodeData{TestSofttype1}()))
@@ -262,7 +262,7 @@ function DFGVariableSCA()
262262

263263
@test getPPEDict(v1) == v1.ppeDict
264264

265-
@test getSmallData(v1) == Dict{String,String}()
265+
@test getSmallData(v1) == Dict{Symbol,SmallDataTypes}()
266266

267267
@test getSofttype(v1) == TestSofttype1()
268268

0 commit comments

Comments
 (0)