-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBlobEntry.jl
More file actions
365 lines (320 loc) · 11 KB
/
BlobEntry.jl
File metadata and controls
365 lines (320 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
##==============================================================================
## Blobentry - common
##==============================================================================
"""
$(SIGNATURES)
Function to generate source string - agentLabel|graphLabel|varLabel
"""
function buildSourceString(dfg::AbstractDFG, label::Symbol)
return "$(getAgentLabel(dfg))|$(getGraphLabel(dfg))|$label"
end
##==============================================================================
## Blobentry - Defined in src/entities/AbstractDFG.jl
##==============================================================================
# Fields to be implemented
# label
# id
getHash(entry::Blobentry) = hex2bytes(entry.hash)
getTimestamp(entry::Blobentry) = entry.timestamp
function assertHash(de::Blobentry, db; hashfunction::Function = sha256)
getHash(de) === nothing && @warn "Missing hash?" && return true
if hashfunction(db) == getHash(de)
return true #or nothing?
else
error("Stored hash and data blob hash do not match")
end
end
function Base.show(io::IO, ::MIME"text/plain", entry::Blobentry)
println(io, "Blobentry {")
println(io, " id: ", entry.id)
println(io, " blobId: ", entry.blobId)
println(io, " label: ", entry.label)
println(io, " blobstore: ", entry.blobstore)
println(io, " hash: ", entry.hash)
println(io, " origin: ", entry.origin)
println(io, " description: ", entry.description)
println(io, " mimeType: ", entry.mimeType)
println(io, " timestamp ", entry.timestamp)
println(io, " _version: ", entry._version)
return println(io, "}")
end
##==============================================================================
## Blobentry - CRUD
##==============================================================================
"""
$(SIGNATURES)
Get data entry
Also see: [`addBlobentry!`](@ref), [`getBlob`](@ref), [`listBlobentries`](@ref)
"""
function getBlobentry(var::AbstractGraphVariable, key::Symbol)
if !hasBlobentry(var, key)
throw(LabelNotFoundError("Blobentry", key, collect(keys(var.dataDict))))
end
return var.dataDict[key]
end
function getBlobentry(var::VariableDFG, key::Symbol)
if !hasBlobentry(var, key)
throw(LabelNotFoundError("Blobentry", key))
end
return var.blobEntries[findfirst(x -> x.label == key, var.blobEntries)]
end
"""
$(SIGNATURES)
Finds and returns the first blob entry that matches the filter.
The result is sorted by `sortby[=getLabel]` and `sortlt[=natural_lt]` before returning the first entry.
Also see: [`getBlobentry`](@ref)
"""
function getfirstBlobentry(
v::AbstractGraphVariable;
labelFilter::Union{Nothing, Function} = nothing,
blobIdFilter::Union{Nothing, Function} = nothing,
sortby::Function = getLabel,
sortlt::Function = natural_lt,
)
entries = getBlobentries(v; labelFilter, blobIdFilter)
if isempty(entries)
return nothing
else
return sort(entries; by = sortby, lt = sortlt)[1]
end
end
function getfirstBlobentry(
dfg::AbstractDFG,
label::Symbol;
labelFilter::Union{Nothing, Function} = nothing,
blobIdFilter::Union{Nothing, Function} = nothing,
)
return getfirstBlobentry(getVariable(dfg, label); labelFilter, blobIdFilter)
end
# TODO Consider autogenerating all methods of the form:
# verbNoun(dfg::VariableCompute, label::Symbol, args...; kwargs...) = verbNoun(getVariable(dfg, label), args...; kwargs...)
# with something like:
# getvariablemethod = [
# :getfirstBlobentry,
# ]
# for met in methodstooverload
# @eval DistributedFactorGraphs $met(dfg::AbstractDFG, label::Symbol, args...; kwargs...) = $met(getVariable(dfg, label), args...; kwargs...)
# end
function getBlobentry(dfg::AbstractDFG, varLabel::Symbol, label::Symbol)
return getBlobentry(getVariable(dfg, varLabel), label)
end
"""
$(SIGNATURES)
Add a `Blobentry` to a variable
Should be extended if DFG variable is not returned by reference.
Also see: [`getBlobentry`](@ref), [`addBlob!`](@ref), [`mergeBlobentry!`](@ref)
"""
function addBlobentry!(var::VariableCompute, entry::Blobentry)
haskey(var.dataDict, entry.label) && throw(LabelExistsError("Blobentry", entry.label))
var.dataDict[entry.label] = entry
return entry
end
function addBlobentry!(var::VariableDFG, entry::Blobentry)
entry.label in getproperty.(var.blobEntries, :label) &&
throw(LabelExistsError("Blobentry", entry.label))
push!(var.blobEntries, entry)
return entry
end
function addBlobentry!(dfg::AbstractDFG, vLbl::Symbol, entry::Blobentry)
return addBlobentry!(getVariable(dfg, vLbl), entry)
end
function addBlobentries!(dfg::AbstractDFG, vLbl::Symbol, entries::Vector{Blobentry})
return addBlobentry!.(dfg, vLbl, entries)
end
"""
$(SIGNATURES)
Update a Blobentry in the factor graph.
If the Blobentry does not exist, it will be added.
Notes:
"""
function mergeBlobentry!(var::AbstractGraphVariable, bde::Blobentry)
if !haskey(var.dataDict, bde.label)
addBlobentry!(var, bde)
else
var.dataDict[bde.label] = bde
end
return 1
end
function mergeBlobentry!(dfg::AbstractDFG, label::Symbol, bde::Blobentry)
# !isVariable(dfg, label) && return nothing
return mergeBlobentry!(getVariable(dfg, label), bde)
end
"""
$(SIGNATURES)
Delete a `Blobentry` from the factor graph variable.
Notes:
- This doesn't remove the associated `Blob` from any Blobstores.
"""
function deleteBlobentry!(var::VariableCompute, key::Symbol)
!hasBlobentry(var, key) && throw(LabelNotFoundError("Blobentry", key))
delete!(var.dataDict, key)
return 1
end
function deleteBlobentry!(var::VariableDFG, key::Symbol)
!hasBlobentry(var, key) && throw(LabelNotFoundError("Blobentry", key))
deleteat!(var.blobEntries, findfirst(x -> x.label == key, var.blobEntries))
return 1
end
function deleteBlobentry!(dfg::AbstractDFG, label::Symbol, key::Symbol)
return deleteBlobentry!(getVariable(dfg, label), key)
end
function deleteBlobentry!(var::AbstractGraphVariable, entry::Blobentry)
return deleteBlobentry!(var, entry.label)
end
##==============================================================================
## Default bulk Agent and Graph Blobentry operations
##==============================================================================
function deleteAgentBlobentries!(dfg::AbstractDFG, labels::Vector{Symbol})
cnts = map(labels) do label
return deleteAgentBlobentry!(dfg, label)
end
return sum(cnts)
end
function deleteGraphBlobentries!(dfg::AbstractDFG, labels::Vector{Symbol})
cnts = map(labels) do label
return deleteGraphBlobentry!(dfg, label)
end
return sum(cnts)
end
##==============================================================================
## Blobentry - Helper functions, Lists, etc
##==============================================================================
"""
$SIGNATURES
Does a blob entry exist with `blobLabel`.
"""
hasBlobentry(v::VariableCompute, blobLabel::Symbol) = haskey(v.dataDict, blobLabel)
function hasBlobentry(v::VariableDFG, label::Symbol)
return label in getproperty.(v.blobEntries, :label)
end
"""
$(SIGNATURES)
Get blob entries, returns a `Vector{Blobentry}`.
"""
function getBlobentries(v::VariableCompute)
return collect(values(v.dataDict))
end
function getBlobentries(v::VariableDFG)
return copy(v.blobEntries)
end
function getBlobentries(
v::AbstractGraphVariable;
labelFilter::Union{Nothing, Function} = nothing,
blobIdFilter::Union{Nothing, Function} = nothing,
)
entries = getBlobentries(v)
filterDFG!(entries, labelFilter, getLabel)
filterDFG!(entries, blobIdFilter, x -> string(x.blobId))
return entries
end
function getBlobentries(
dfg::AbstractDFG,
variableLabel::Symbol;
labelFilter::Union{Nothing, Function} = nothing,
blobIdFilter::Union{Nothing, Function} = nothing,
)
return getBlobentries(getVariable(dfg, variableLabel); labelFilter, blobIdFilter)
end
function gatherBlobentries(
dfg::AbstractDFG;
labelFilter::Union{Nothing, Function} = nothing,
blobIdFilter::Union{Nothing, Function} = nothing,
solvableFilter::Union{Nothing, Function} = nothing,
tagsFilter::Union{Nothing, Function} = nothing,
typeFilter::Union{Nothing, Function} = nothing,
variableLabelFilter::Union{Nothing, Function} = nothing,
)
vls = listVariables(
dfg;
solvableFilter,
tagsFilter,
typeFilter,
labelFilter = variableLabelFilter,
)
return map(vls) do vl
return vl => getBlobentries(dfg, vl; labelFilter, blobIdFilter)
end
end
const collectBlobentries = gatherBlobentries
"""
$(SIGNATURES)
List the blob entries associated with a particular variable.
"""
function listBlobentries(var::AbstractGraphVariable)
return collect(keys(var.dataDict))
end
function listBlobentries(var::VariableDFG)
return getproperty.(var.blobEntries, :label)
end
function listBlobentries(dfg::AbstractDFG, label::Symbol)
return listBlobentries(getVariable(dfg, label))
end
"""
$SIGNATURES
List a collection of blob entries per variable that match a particular `pattern::Regex`.
Notes
- Optional sort function argument, default is unsorted.
- Likely use of `sortDFG` for basic Symbol sorting.
Example
```julia
listBlobentrySequence(fg, :x0, r"IMG_CENTER", sortDFG)
15-element Vector{Symbol}:
:IMG_CENTER_21676
:IMG_CENTER_21677
:IMG_CENTER_21678
:IMG_CENTER_21679
...
```
"""
function listBlobentrySequence(
dfg::AbstractDFG,
lb::Symbol,
pattern::Regex,
_sort::Function = (x) -> x,
)
#
ents_ = listBlobentries(dfg, lb)
entReg = map(l -> match(pattern, string(l)), ents_)
entMsk = entReg .!== nothing
return ents_[findall(entMsk)] |> _sort
end
"""
$SIGNATURES
If the blob label `datalabel` already exists, then this function will return the name `datalabel_1`.
If the blob label `datalabel_1` already exists, then this function will return the name `datalabel_2`.
"""
function incrDataLabelSuffix(
dfg::AbstractDFG,
vla::Union{Symbol, <:AbstractString},
bllb::S;
datalabel = Ref(""),
) where {S <: Union{Symbol, <:AbstractString}}
count = 1
hasund = false
len = 0
try
de, _ = getData(dfg, Symbol(vla), bllb)
bllb = string(bllb)
# bllb *= bllb[end] != '_' ? "_" : ""
datalabel[] = string(de.label)
dlb = match(r"\d*", reverse(datalabel[]))
# slightly complicated search if blob name already has an underscore number suffix, e.g. `_4`
count, hasund, len = if occursin(Regex(dlb.match * "_"), reverse(datalabel[]))
parse(Int, dlb.match |> reverse) + 1, true, length(dlb.match)
else
1, datalabel[][end] == '_', 0
end
catch err
# append latest count
if !(err isa KeyError)
throw(err)
end
end
# the piece from old label without the suffix count number
bllb = datalabel[][1:(end - len)]
if !hasund || bllb[end] != '_'
bllb *= "_"
end
bllb *= string(count)
return S(bllb)
end