Skip to content

Commit d3fb2fa

Browse files
authored
More standardization and improvements towards v1.0 (#1165)
* Standardize / check some of the core functions * improve lsPriors * getBlobentriesVariables -> gatherBlobentries * standard getfirstBlobentry
1 parent 3201b6c commit d3fb2fa

File tree

16 files changed

+405
-369
lines changed

16 files changed

+405
-369
lines changed

Project.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,18 @@ Test = "1.10"
7575
TimeZones = "1.3.1"
7676
UUIDs = "1.10"
7777
julia = "1.10"
78+
LieGroups = "0.1"
7879

7980
[extras]
8081
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
8182
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
8283
GraphMakie = "1ecd5474-83a3-4783-bb4f-06765db800d2"
84+
LieGroups = "6774de46-80ba-43f8-ba42-e41071ccfc5f"
8385
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
8486
Manifolds = "1cead3c2-87b3-11e9-0ccd-23c62b72b94e"
8587
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
8688
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
8789
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
8890

8991
[targets]
90-
test = ["Aqua", "Test", "DataStructures", "GraphMakie", "LinearAlgebra", "Manifolds", "Pkg", "Statistics"]
92+
test = ["Aqua", "Test", "DataStructures", "GraphMakie", "LinearAlgebra", "Manifolds", "Pkg", "Statistics", "LieGroups"]

src/DataBlobs/entities/BlobStores.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,4 @@ Design goal: all `Blobstore`s with the same `label` can contain the same `blobId
2929
3030
"""
3131
abstract type AbstractBlobstore{T} end
32+
const Blobstore = AbstractBlobstore

src/DataBlobs/services/BlobEntry.jl

Lines changed: 67 additions & 182 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,3 @@
1-
2-
##==============================================================================
3-
## Blobentry - compare
4-
##==============================================================================
5-
6-
import Base: ==
7-
8-
@generated function ==(x::T, y::T) where {T <: Blobentry}
9-
return mapreduce(n -> :(x.$n == y.$n), (a, b) -> :($a && $b), fieldnames(x))
10-
end
11-
121
##==============================================================================
132
## Blobentry - common
143
##==============================================================================
@@ -85,49 +74,26 @@ Finds and returns the first blob entry that matches the filter.
8574
8675
Also see: [`getBlobentry`](@ref)
8776
"""
88-
function getfirstBlobentry(var::AbstractGraphVariable, blobId::UUID)
89-
for (k, v) in var.dataDict
90-
if blobId == v.blobId
91-
return v
92-
end
93-
end
94-
throw(KeyError("No blobEntry with blobId $(blobId) found in variable $(getLabel(var))"))
95-
end
96-
97-
function getfirstBlobentry(dfg::AbstractDFG, label::Symbol, blobId::UUID)
98-
return getfirstBlobentry(getVariable(dfg, label), blobId)
99-
end
100-
101-
function getfirstBlobentry(var::AbstractGraphVariable, key::Regex)
102-
for (k, v) in var.dataDict
103-
if occursin(key, string(v.label))
104-
return v
105-
end
106-
end
107-
throw(
108-
KeyError(
109-
"No blobEntry with label matching regex $(key) found in variable $(getLabel(var))",
110-
),
111-
)
112-
end
113-
114-
function getfirstBlobentry(var::VariableDFG, key::Regex)
115-
firstIdx = findfirst(x -> contains(string(x.label), key), var.blobEntries)
116-
if isnothing(firstIdx)
117-
throw(KeyError("$key"))
77+
function getfirstBlobentry(
78+
v::AbstractGraphVariable;
79+
labelFilter::Union{Nothing, Function} = nothing,
80+
blobIdFilter::Union{Nothing, Function} = nothing,
81+
)
82+
entries = getBlobentries(v; labelFilter, blobIdFilter)
83+
if isempty(entries)
84+
return nothing
85+
else
86+
return entries[1]
11887
end
119-
return var.blobEntries[firstIdx]
12088
end
12189

122-
function getfirstBlobentry(dfg::AbstractDFG, label::Symbol, key::Regex)
123-
els = listBlobentries(dfg, label)
124-
firstIdx = findfirst(contains(key), string.(els))
125-
isnothing(firstIdx) && throw(
126-
KeyError(
127-
"No blobEntry with label matching regex $(key) found in variable $(label)",
128-
),
129-
)
130-
return getBlobentry(dfg, label, els[firstIdx])
90+
function getfirstBlobentry(
91+
dfg::AbstractDFG,
92+
label::Symbol;
93+
labelFilter::Union{Nothing, Function} = nothing,
94+
blobIdFilter::Union{Nothing, Function} = nothing,
95+
)
96+
return getfirstBlobentry(getVariable(dfg, label); labelFilter, blobIdFilter)
13197
end
13298

13399
# TODO Consider autogenerating all methods of the form:
@@ -140,22 +106,18 @@ end
140106
# @eval DistributedFactorGraphs $met(dfg::AbstractDFG, label::Symbol, args...; kwargs...) = $met(getVariable(dfg, label), args...; kwargs...)
141107
# end
142108

143-
function getBlobentry(dfg::AbstractDFG, label::Symbol, key::Symbol)
144-
return getBlobentry(getVariable(dfg, label), key)
109+
function getBlobentry(dfg::AbstractDFG, varLabel::Symbol, label::Symbol)
110+
return getBlobentry(getVariable(dfg, varLabel), label)
145111
end
146-
# getBlobentry(dfg::AbstractDFG, label::Symbol, key::Symbol) = getBlobentry(getVariable(dfg, label), key)
147112

148113
"""
149114
$(SIGNATURES)
150-
Add Data Entry to a DFG variable
115+
Add a `Blobentry` to a variable
151116
Should be extended if DFG variable is not returned by reference.
152117
153-
Also see: [`getBlobentry`](@ref), [`addBlob!`](@ref), [`mergeBlobentries!`](@ref)
118+
Also see: [`getBlobentry`](@ref), [`addBlob!`](@ref), [`mergeBlobentry!`](@ref)
154119
"""
155-
function addBlobentry!(var::AbstractGraphVariable, entry::Blobentry)
156-
# see https://github.com/JuliaRobotics/DistributedFactorGraphs.jl/issues/985
157-
# blobId::Union{UUID,Nothing} = (isnothing(entry.blobId) ? entry.id : entry.blobId),
158-
# blobSize::Int = (hasfield(Blobentry, :size) ? entry.size : -1)
120+
function addBlobentry!(var::VariableCompute, entry::Blobentry)
159121
haskey(var.dataDict, entry.label) && throw(LabelExistsError("Blobentry", entry.label))
160122
var.dataDict[entry.label] = entry
161123
return entry
@@ -197,37 +159,28 @@ end
197159

198160
"""
199161
$(SIGNATURES)
200-
Delete a blob entry from the factor graph.
201-
Note this doesn't remove it from any data stores.
162+
Delete a `Blobentry` from the factor graph variable.
202163
203164
Notes:
204-
- users responsibility to delete data in db before deleting entry
165+
- This doesn't remove the associated `Blob` from any Blobstores.
205166
"""
206-
function deleteBlobentry!(var::AbstractGraphVariable, key::Symbol)
207-
pop!(var.dataDict, key)
167+
function deleteBlobentry!(var::VariableCompute, key::Symbol)
168+
!hasBlobentry(var, key) && throw(LabelNotFoundError("Blobentry", key))
169+
delete!(var.dataDict, key)
208170
return 1
209171
end
210172

211173
function deleteBlobentry!(var::VariableDFG, key::Symbol)
212-
if !hasBlobentry(var, key)
213-
throw(
214-
KeyError(
215-
"No dataEntry label $(key) found in variable $(getLabel(var)). Available keys: $(keys(var.dataDict))",
216-
),
217-
)
218-
end
174+
!hasBlobentry(var, key) && throw(LabelNotFoundError("Blobentry", key))
219175
deleteat!(var.blobEntries, findfirst(x -> x.label == key, var.blobEntries))
220176
return 1
221177
end
222178

223179
function deleteBlobentry!(dfg::AbstractDFG, label::Symbol, key::Symbol)
224-
#users responsibility to delete data in db before deleting entry
225-
# !isVariable(dfg, label) && return nothing
226180
return deleteBlobentry!(getVariable(dfg, label), key)
227181
end
228182

229183
function deleteBlobentry!(var::AbstractGraphVariable, entry::Blobentry)
230-
#users responsibility to delete data in db before deleting entry
231184
return deleteBlobentry!(var, entry.label)
232185
end
233186

@@ -240,73 +193,66 @@ end
240193
241194
Does a blob entry exist with `blobLabel`.
242195
"""
243-
hasBlobentry(var::AbstractGraphVariable, blobLabel::Symbol) =
244-
haskey(var.dataDict, blobLabel)
196+
hasBlobentry(v::VariableCompute, blobLabel::Symbol) = haskey(v.dataDict, blobLabel)
245197

246-
function hasBlobentry(var::VariableDFG, label::Symbol)
247-
return label in getproperty.(var.blobEntries, :label)
198+
function hasBlobentry(v::VariableDFG, label::Symbol)
199+
return label in getproperty.(v.blobEntries, :label)
248200
end
249201

250202
"""
251203
$(SIGNATURES)
252204
253-
Get blob entries, Vector{Blobentry}
205+
Get blob entries, returns a `Vector{Blobentry}`.
254206
"""
255-
function getBlobentries(var::AbstractGraphVariable)
256-
#or should we return the iterator, Base.ValueIterator{Dict{Symbol,Blobentry}}?
257-
return collect(values(var.dataDict))
207+
function getBlobentries(v::VariableCompute)
208+
return collect(values(v.dataDict))
258209
end
259210

260-
function getBlobentries(var::VariableDFG)
261-
return var.blobEntries
211+
function getBlobentries(v::VariableDFG)
212+
return copy(v.blobEntries)
262213
end
263214

264-
function getBlobentries(dfg::AbstractDFG, label::Symbol)
265-
# !isVariable(dfg, label) && return nothing
266-
#or should we return the iterator, Base.ValueIterator{Dict{Symbol,Blobentry}}?
267-
return getBlobentries(getVariable(dfg, label))
268-
end
269-
270-
function getBlobentries(dfg::AbstractDFG, label::Symbol, regex::Regex)
271-
entries = getBlobentries(dfg, label)
272-
return filter(entries) do e
273-
return occursin(regex, string(e.label))
274-
end
215+
function getBlobentries(
216+
v::AbstractGraphVariable;
217+
labelFilter::Union{Nothing, Function} = nothing,
218+
blobIdFilter::Union{Nothing, Function} = nothing,
219+
)
220+
entries = getBlobentries(v)
221+
filterDFG!(entries, labelFilter, x -> string(x.label))
222+
filterDFG!(entries, blobIdFilter, x -> string(x.blobId))
223+
return entries
275224
end
276225

277226
function getBlobentries(
278227
dfg::AbstractDFG,
279-
label::Symbol,
280-
skey::Union{Symbol, <:AbstractString},
228+
variableLabel::Symbol;
229+
labelFilter::Union{Nothing, Function} = nothing,
230+
blobIdFilter::Union{Nothing, Function} = nothing,
281231
)
282-
return getBlobentries(dfg, label, Regex(string(skey)))
232+
return getBlobentries(getVariable(dfg, variableLabel); labelFilter, blobIdFilter)
283233
end
284234

285-
"""
286-
$(SIGNATURES)
287-
288-
Get all blob entries matching a Regex pattern over variables
289-
290-
Notes
291-
- Use `dropEmpties=true` to not include empty lists in result.
292-
- Use keyword `varList` for which variables to search through.
293-
"""
294-
function getBlobentriesVariables(
295-
dfg::AbstractDFG,
296-
bLblPattern::Regex;
297-
varList::AbstractVector{Symbol} = sort(listVariables(dfg); lt = natural_lt),
298-
dropEmpties::Bool = false,
235+
function gatherBlobentries(
236+
dfg::AbstractDFG;
237+
labelFilter::Union{Nothing, Function} = nothing,
238+
blobIdFilter::Union{Nothing, Function} = nothing,
239+
solvableFilter::Union{Nothing, Function} = nothing,
240+
tagsFilter::Union{Nothing, Function} = nothing,
241+
typeFilter::Union{Nothing, Function} = nothing,
242+
variableLabelFilter::Union{Nothing, Function} = nothing,
299243
)
300-
RETLIST = Vector{Vector{Blobentry}}()
301-
@showprogress "Get entries matching $bLblPattern" for vl in varList
302-
bes = filter(s -> occursin(bLblPattern, string(s.label)), listBlobentries(dfg, vl))
303-
# only push to list if there are entries on this variable
304-
(!dropEmpties || 0 < length(bes)) ? nothing : continue
305-
push!(RETLIST, bes)
244+
vls = listVariables(
245+
dfg;
246+
solvableFilter,
247+
tagsFilter,
248+
typeFilter,
249+
labelFilter = variableLabelFilter,
250+
)
251+
return map(vls) do vl
252+
return vl => getBlobentries(dfg, vl; labelFilter, blobIdFilter)
306253
end
307-
308-
return RETLIST
309254
end
255+
const collectBlobentries = gatherBlobentries
310256

311257
"""
312258
$(SIGNATURES)
@@ -321,7 +267,6 @@ function listBlobentries(var::VariableDFG)
321267
end
322268

323269
function listBlobentries(dfg::AbstractDFG, label::Symbol)
324-
# !isVariable(dfg, label) && return nothing
325270
return listBlobentries(getVariable(dfg, label))
326271
end
327272

@@ -357,66 +302,6 @@ function listBlobentrySequence(
357302
return ents_[findall(entMsk)] |> _sort
358303
end
359304

360-
"""
361-
$SIGNATURES
362-
363-
Add a blob entry into the destination variable which already exists
364-
in a source variable.
365-
366-
See also: [`addBlobentry!`](@ref), [`getBlobentry`](@ref), [`listBlobentries`](@ref), [`getBlob`](@ref)
367-
"""
368-
function mergeBlobentries!(
369-
dst::AbstractDFG,
370-
dlbl::Symbol,
371-
src::AbstractDFG,
372-
slbl::Symbol,
373-
bllb::Union{Symbol, UUID, <:AbstractString, Regex},
374-
)
375-
#
376-
_makevec(s) = [s;]
377-
_makevec(s::AbstractVector) = s
378-
des_ = getBlobentry(src, slbl, bllb)
379-
des = _makevec(des_)
380-
# don't add data entries that already exist
381-
dde = listBlobentries(dst, dlbl)
382-
# HACK, verb list should just return vector of Symbol. NCE36
383-
_getid(s) = s
384-
_getid(s::Blobentry) = s.id
385-
uids = _getid.(dde) # (s->s.id).(dde)
386-
filter!(s -> !(_getid(s) in uids), des)
387-
# add any data entries not already in the destination variable, by uuid
388-
return addBlobentry!.(dst, dlbl, des)
389-
end
390-
391-
function mergeBlobentries!(
392-
dst::AbstractDFG,
393-
dlbl::Symbol,
394-
src::AbstractDFG,
395-
slbl::Symbol,
396-
::Colon = :,
397-
)
398-
des = listBlobentries(src, slbl)
399-
# don't add data entries that already exist
400-
uids = listBlobentries(dst, dlbl)
401-
# verb list should just return vector of Symbol. NCE36
402-
filter!(s -> !(s in uids), des)
403-
if 0 < length(des)
404-
union(((s -> mergeBlobentries!(dst, dlbl, src, slbl, s)).(des))...)
405-
end
406-
end
407-
408-
function mergeBlobentries!(
409-
dest::AbstractDFG,
410-
src::AbstractDFG,
411-
w...;
412-
varList::AbstractVector = listVariables(dest) |> sortDFG,
413-
)
414-
@showprogress 1 "merging data entries" for vl in varList
415-
mergeBlobentries!(dest, vl, src, vl, w...)
416-
end
417-
return varList
418-
end
419-
420305
"""
421306
$SIGNATURES
422307

src/DataBlobs/services/BlobStores.jl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,10 +275,6 @@ function getBlob(store::LinkStore, blobId::UUID)
275275
return read(fname)
276276
end
277277

278-
function addBlob!(store::LinkStore, entry::Blobentry, linkfile::String)
279-
return addBlob!(store, entry.blobId, linkfile)
280-
end
281-
282278
function addBlob!(store::LinkStore, blobId::UUID, linkfile::String)
283279
if haskey(store.cache, blobId)
284280
throw(IdExistsError("Blob", blobId))

0 commit comments

Comments
 (0)