Skip to content

Commit f2b3b40

Browse files
authored
Merge pull request #430 from JuliaRobotics/feat/2Q20/392
fetchData and docs
2 parents 37bb5f4 + b1b3892 commit f2b3b40

File tree

2 files changed

+76
-4
lines changed

2 files changed

+76
-4
lines changed

src/BigData/services/AbstractBigDataEntries.jl

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ addDataEntry!(x...) = addBigDataEntry!(x...)
4141
$(SIGNATURES)
4242
Add Big Data Entry to distributed factor graph.
4343
Should be extended if DFG variable is not returned by reference.
44+
45+
Example
46+
47+
See docs for `getDataEntryElement`.
48+
49+
Related
50+
51+
addData!, getDataEntryElement, fetchData
4452
"""
4553
function addDataEntry!(dfg::AbstractDFG,
4654
lbl::Symbol,
@@ -51,12 +59,13 @@ function addDataEntry!(dfg::AbstractDFG,
5159
#
5260
node = isVariable(dfg, lbl) ? getVariable(dfg, lbl) : getFactor(dfg, lbl)
5361
# Make a big data entry in the graph - use JSON2 to just write this
54-
element = GeneralBigDataEntry(dfg, node, descr, mimeType=mimeType)
62+
entry = GeneralBigDataEntry(dfg, node, descr, mimeType=mimeType)
5563
# Set it in the store
56-
addBigData!(datastore, element, data)
64+
addBigData!(datastore, entry, data)
5765
# Add the entry to the graph
58-
addBigDataEntry!(node, element)
66+
addBigDataEntry!(node, entry)
5967
end
68+
const addData! = addDataEntry!
6069

6170
"""
6271
$SIGNATURES
@@ -79,9 +88,68 @@ function getBigDataEntry(dfg::AbstractDFG, label::Symbol, key::Symbol)
7988
return getBigDataEntry(getVariable(dfg, label), key)
8089
end
8190

91+
"""
92+
$SIGNATURES
93+
Get both the entry and raw data element from datastore returning as a tuple.
94+
95+
Notes:
96+
- This is the counterpart to `addDataEntry!`.
97+
- Data is identified by the node in the DFG object `dfglabel::Symbol` as well as `datalabel::Symbol`.
98+
- The data should have been stored along with a `entry.mimeType::String` which describes the format of the data.
99+
- ImageMagick.jl is useful for storing images in png or jpg compressed format.
100+
101+
Example
102+
103+
```julia
104+
# some dfg object
105+
fg = initfg()
106+
addVariable!(fg, :x0, IIF.ContinuousScalar) # using IncrementalInference
107+
# FileDataStore (as example)
108+
datastore = FileDataStore(joinLogPath(fg,"datastore"))
109+
110+
# now some data comes in
111+
mydata = Dict(:soundBite => randn(Float32, 10000), :meta => "something about lazy foxes and fences.")
112+
113+
# store the data
114+
addDataEntry!( fg, :x0, datastore, :SOUND_DATA, "application/json", Vector{UInt8}(JSON2.write( mydata )) )
115+
116+
# get/fetch the data
117+
entry, rawData = fetchData(fg, :x0, datastore, :SOUND_DATA)
118+
119+
# unpack data to original format (this must be done by the user)
120+
@show entry.mimeType # "applicatio/json"
121+
userFormat = JSON2.read(IOBuffer(rawData))
122+
```
123+
124+
Related
125+
126+
addDataEntry!, addData!, fetchData, fetchDataEntryElement
127+
"""
128+
function getDataEntryElement(dfg::AbstractDFG,
129+
dfglabel::Symbol,
130+
datastore::Union{FileDataStore, InMemoryDataStore},
131+
datalabel::Symbol)
132+
#
133+
vari = getVariable(dfg, dfglabel)
134+
if !hasDataEntry(vari, datalabel)
135+
@error "missing data entry $datalabel in $dfglabel"
136+
return nothing, nothing
137+
end
138+
entry = getBigDataEntry(vari, datalabel)
139+
element = getBigData(datastore, entry)
140+
return entry, element
141+
end
142+
const fetchDataEntryElement = getDataEntryElement
143+
const fetchData = getDataEntryElement
144+
145+
146+
82147
"""
83148
$(SIGNATURES)
84149
Update big data entry
150+
151+
DevNote
152+
- DF, unclear if `update` verb is applicable in this case, see #404
85153
"""
86154
function updateBigDataEntry!(var::AbstractDFGVariable, bde::AbstractBigDataEntry)
87155
!haskey(var.bigData,bde.key) && (@warn "$(bde.key) does not exist in variable, adding")

src/DistributedFactorGraphs.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,11 @@ export getNeighborhood, getNeighbors, _getDuplicatedEmptyDFG
165165
export copyGraph!, deepcopyGraph, deepcopyGraph!, buildSubgraph, mergeGraph!
166166
# Big Data
167167
##------------------------------------------------------------------------------
168-
export addBigDataEntry!, addDataEntry!, getBigDataEntry, updateBigDataEntry!, deleteBigDataEntry!, getBigDataEntries, getBigDataKeys, hasDataEntry, hasBigDataEntry
168+
export addBigDataEntry!, getBigDataEntry, updateBigDataEntry!, deleteBigDataEntry!, getBigDataEntries, getBigDataKeys, hasDataEntry, hasBigDataEntry
169+
# convenience wrappers
170+
export addDataEntry!, getDataEntryElement
171+
# aliases
172+
export addData!, fetchData, fetchDataEntryElement
169173

170174

171175
##------------------------------------------------------------------------------

0 commit comments

Comments
 (0)