Skip to content

Commit 3c44f92

Browse files
committed
Add BigData to DFGVariable
1 parent 4d75d0e commit 3c44f92

File tree

5 files changed

+205
-5
lines changed

5 files changed

+205
-5
lines changed

src/BigData.jl

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
2+
# TODO move to ...
3+
4+
5+
# """
6+
# $(TYPEDEF)
7+
# Abstract parent struct for big data entry.
8+
# """
9+
# abstract type AbstractBigDataEntry end
10+
11+
# --- AbstractBigData Interfaces ---
12+
# fields to implement:
13+
# - key::Symbol
14+
# available methods:
15+
# - addBigDataEntry!
16+
# - getBigDataEntry
17+
# - updateBigDataEntry!
18+
# - deleteBigDataEntry!
19+
# - getBigDataEntries
20+
# - getBigDataKeys
21+
22+
export addBigDataEntry!,
23+
getBigDataEntry,
24+
updateBigDataEntry!,
25+
deleteBigDataEntry!,
26+
getBigDataEntries,
27+
getBigDataKeys,
28+
MongodbBigDataEntry,
29+
FileBigDataEntry
30+
31+
# Methods
32+
33+
#TODO should this return Bool or the modified Variable?
34+
"""
35+
$(SIGNATURES)
36+
Add Big Data Entry to a DFG variable
37+
"""
38+
function addBigDataEntry!(var::AbstractDFGVariable, bde::AbstractBigDataEntry)::Bool
39+
haskey(var.bigData,bde.key) && @warn "$(bde.key) already exists in variable, overwriting!"
40+
var.bigData[bde.key] = bde
41+
return true
42+
end
43+
44+
"""
45+
$(SIGNATURES)
46+
Add Big Data Entry to distrubuted factor graph.
47+
Should be extended if DFG variable is not returned by by reference.
48+
"""
49+
function addBigDataEntry!(dfg::AbstractDFG, label::Symbol, bde::AbstractBigDataEntry)::Bool
50+
return addBigDataEntry!(getVariable(dfg, label), bde)
51+
end
52+
53+
"""
54+
$(SIGNATURES)
55+
Get big data entry
56+
"""
57+
function getBigDataEntry(var::AbstractDFGVariable, key::Symbol)::AbstractBigDataEntry
58+
return var.bigData[key]
59+
end
60+
function getBigDataEntry(dfg::AbstractDFG, label::Symbol, key::Symbol)::AbstractBigDataEntry
61+
return getBigDataEntry(getVariable(dfg, label), key)
62+
end
63+
64+
"""
65+
$(SIGNATURES)
66+
Update big data entry
67+
"""
68+
function updateBigDataEntry!(var::AbstractDFGVariable, bde::AbstractBigDataEntry)::Bool#TODO should this return Bool?
69+
!haskey(var.bigData,bde.key) && (@error "$(bde.key) does not exist in variable!"; return false)
70+
var.bigData[bde.key] = bde
71+
return true
72+
end
73+
function updateBigDataEntry!(dfg::AbstractDFG, label::Symbol, bde::AbstractBigDataEntry)::Bool
74+
updateBigDataEntry!(getVariable(dfg, label), bde)
75+
end
76+
77+
"""
78+
$(SIGNATURES)
79+
Delete big data entry
80+
"""
81+
function deleteBigDataEntry!(var::AbstractDFGVariable, key::Symbol)::AbstractBigDataEntry #users responsibility to delete big data in db before deleting entry
82+
bde = getBigDataEntry(var, key)
83+
delete!(var.bigData, key)
84+
return bde
85+
end
86+
87+
function deleteBigDataEntry!(dfg::AbstractDFG, label::Symbol, key::Symbol)::AbstractBigDataEntry #users responsibility to delete big data in db before deleting entry
88+
deleteBigDataEntry!(getVariable(dfg, label), key)
89+
end
90+
91+
"""
92+
$(SIGNATURES)
93+
Get big data entries, Vector{AbstractBigDataEntry}
94+
"""
95+
function getBigDataEntries(var::AbstractDFGVariable)::Vector{AbstractBigDataEntry}
96+
#or should we return the iterator, Base.ValueIterator{Dict{Symbol,AbstractBigDataEntry}}?
97+
collect(values(var.bigData))
98+
end
99+
function getBigDataEntries(dfg::AbstractDFG, label::Symbol)::Vector{AbstractBigDataEntry}
100+
#or should we return the iterator, Base.ValueIterator{Dict{Symbol,AbstractBigDataEntry}}?
101+
getBigDataEntries(getVariable(dfg, label))
102+
end
103+
104+
105+
"""
106+
$(SIGNATURES)
107+
getBigDataKeys
108+
"""
109+
function getBigDataKeys(var::AbstractDFGVariable)::Vector{Symbol}
110+
collect(keys(var.bigData))
111+
end
112+
function getBigDataKeys(dfg::AbstractDFG, label::Symbol)::Vector{Symbol}
113+
getBigDataKeys(getVariable(dfg, label))
114+
end
115+
116+
117+
118+
# Types <: AbstractBigDataEntry
119+
"""
120+
$(TYPEDEF)
121+
BigDataEntry in MongoDB.
122+
"""
123+
struct MongodbBigDataEntry <: AbstractBigDataEntry
124+
key::Symbol
125+
oid::NTuple{12, UInt8} #mongodb object id
126+
#maybe other fields such as:
127+
#flags::Bool ready, valid, locked, permissions
128+
#MIMEType::Symbol
129+
end
130+
131+
132+
"""
133+
$(TYPEDEF)
134+
BigDataEntry in a file.
135+
"""
136+
struct FileBigDataEntry <: AbstractBigDataEntry
137+
key::Symbol
138+
filename::String
139+
end

src/DistributedFactorGraphs.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ include("services/AbstractDFG.jl")
4848
include("services/DFGVariable.jl")
4949
include("services/DFGFactor.jl")
5050

51+
include("BigData.jl")
52+
5153
# Include the Graphs.jl API.
5254
include("GraphsDFG/GraphsDFG.jl")
5355

src/entities/AbstractDFG.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,9 @@ Empty structure for solver parameters.
3939
"""
4040
mutable struct NoSolverParams <: AbstractParams
4141
end
42+
43+
"""
44+
$(TYPEDEF)
45+
Abstract parent struct for big data entry.
46+
"""
47+
abstract type AbstractBigDataEntry end

src/entities/DFGVariable.jl

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,9 @@ struct VariableEstimate
8080
end
8181

8282
"""
83-
$(SIGNATURES)
84-
Fundamental structure for a DFG variable.
83+
$(TYPEDEF)
84+
Fundamental structure for a DFG variable with fields:
85+
$(TYPEDFIELDS)
8586
"""
8687
mutable struct DFGVariable <: AbstractDFGVariable
8788
label::Symbol
@@ -90,14 +91,22 @@ mutable struct DFGVariable <: AbstractDFGVariable
9091
estimateDict::Dict{Symbol, Dict{Symbol, VariableEstimate}}
9192
solverDataDict::Dict{Symbol, VariableNodeData}
9293
smallData::Dict{String, String}
93-
bigData::Any
94+
bigData::Dict{Symbol, AbstractBigDataEntry}
9495
ready::Int
9596
backendset::Int
9697
_internalId::Int64
97-
DFGVariable(label::Symbol, _internalId::Int64) = new(label, now(), Symbol[], Dict{Symbol, Dict{Symbol, VariableEstimate}}(), Dict{Symbol, VariableNodeData}(:default => VariableNodeData()), Dict{String, String}(), nothing, 0, 0, _internalId)
98-
DFGVariable(label::Symbol) = new(label, now(), Symbol[], Dict{Symbol, VariableEstimate}(), Dict{Symbol, VariableNodeData}(:default => VariableNodeData()), Dict{String, String}(), nothing, 0, 0, 0)
9998
end
10099

100+
"""
101+
$SIGNATURES
102+
DFGVariable constructors.
103+
"""
104+
DFGVariable(label::Symbol, _internalId::Int64) =
105+
DFGVariable(label, now(), Symbol[], Dict{Symbol, Dict{Symbol, VariableEstimate}}(), Dict{Symbol, VariableNodeData}(:default => VariableNodeData()), Dict{String, String}(), Dict{Symbol,AbstractBigDataEntry}(), 0, 0, _internalId)
106+
107+
DFGVariable(label::Symbol) =
108+
DFGVariable(label, now(), Symbol[], Dict{Symbol, VariableEstimate}(), Dict{Symbol, VariableNodeData}(:default => VariableNodeData()), Dict{String, String}(), Dict{Symbol,AbstractBigDataEntry}(), 0, 0, 0)
109+
101110
# Accessors
102111
label(v::DFGVariable) = v.label
103112
timestamp(v::DFGVariable) = v.timestamp

test/interfaceTests.jl

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,50 @@ end
116116

117117
end
118118

119+
@testset "BigData" begin
120+
oid = zeros(UInt8,12); oid[12] = 0x01
121+
de1 = MongodbBigDataEntry(:key1, NTuple{12,UInt8}(oid))
122+
123+
oid = zeros(UInt8,12); oid[12] = 0x02
124+
de2 = MongodbBigDataEntry(:key2, NTuple{12,UInt8}(oid))
125+
126+
oid = zeros(UInt8,12); oid[12] = 0x03
127+
de2_update = MongodbBigDataEntry(:key2, NTuple{12,UInt8}(oid))
128+
129+
#add
130+
v1 = getVariable(dfg, :a)
131+
@test addBigDataEntry!(v1, de1)
132+
@test addBigDataEntry!(dfg, :a, de2)
133+
@test addBigDataEntry!(v1, de1)
134+
135+
#get
136+
@test deepcopy(de1) == getBigDataEntry(v1, :key1)
137+
@test deepcopy(de2) == getBigDataEntry(dfg, :a, :key2)
138+
@test_throws Any getBigDataEntry(v2, :key1)
139+
@test_throws Any getBigDataEntry(dfg, :b, :key1)
140+
141+
#update
142+
@test updateBigDataEntry!(dfg, :a, de2_update)
143+
@test deepcopy(de2_update) == getBigDataEntry(dfg, :a, :key2)
144+
@test !updateBigDataEntry!(dfg, :b, de2_update)
145+
146+
#list
147+
entries = getBigDataEntries(dfg, :a)
148+
@test length(entries) == 2
149+
@test symdiff(map(e->e.key, entries), [:key1, :key2]) == Symbol[]
150+
@test length(getBigDataEntries(dfg, :b)) == 0
151+
152+
@test symdiff(getBigDataKeys(dfg, :a), [:key1, :key2]) == Symbol[]
153+
@test getBigDataKeys(dfg, :b) == Symbol[]
154+
155+
#delete
156+
@test deepcopy(de1) == deleteBigDataEntry!(v1, :key1)
157+
@test getBigDataKeys(v1) == Symbol[:key2]
158+
#delete from dfg
159+
@test deepcopy(de2_update) == deleteBigDataEntry!(dfg, :a, :key2)
160+
@test getBigDataKeys(v1) == Symbol[]
161+
end
162+
119163
@testset "Updating Nodes" begin
120164
global dfg
121165
#get the variable

0 commit comments

Comments
 (0)