Skip to content

Commit a78b6d9

Browse files
authored
Merge pull request #986 from JuliaRobotics/23Q1/enh/blobhelpers
fixes to Blob and BlobEntry helpers, entry.originId
2 parents 3e2eca0 + 141f58e commit a78b6d9

File tree

10 files changed

+202
-107
lines changed

10 files changed

+202
-107
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ Listing news on any major breaking changes in DFG. For regular changes, see int
22

33
# v0.20
44

5+
- Throw `KeyError` if `getBlobEntry` is not found, previously was `ErrorException`.
6+
- Change return type on `addData!` convenience wrappers to only return new `BlobEntry`.
7+
- Fix `addBlob!` calls for `FolderStore` and `InMemoryBlobStore` to use `BlobEntry.originId` and not previous bug `entry.id`.
58
- Close long running serialization redo (#590) using only JSON3.jl and StructTypes.jl going forward.
69
- Standardize BlobEntry=>Blob naming of functions, and keeping convenience wrappers `{get,add,update,delete}Data[!]`.
710
- Consolidate to only one `BlobEntry` definition, dropping use of `AbstractBlobEntry`.

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "DistributedFactorGraphs"
22
uuid = "b5cc3c7e-6572-11e9-2517-99fb8daf2f04"
3-
version = "0.20.0"
3+
version = "0.20.1"
44

55
[deps]
66
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"

attic/DataBlobs/FileDataEntryBlob.jl

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ function addBlob!(dfg::AbstractDFG, entry::BlobEntry, data::Vector{UInt8})
4242
open(entryfilename(entry), "w") do f
4343
JSON.print(f, entry)
4444
end
45-
return getBlob(dfg, entry)::Vector{UInt8}
45+
# FIXME update for entry.blobId vs entry.originId
46+
return UUID(entry.id)
47+
# return getBlob(dfg, entry)::Vector{UInt8}
4648
end
4749
end
4850

@@ -72,7 +74,8 @@ end
7274
function addData!(::Type{BlobEntry}, dfg::AbstractDFG, label::Symbol, key::Symbol, folder::String, blob::Vector{UInt8}, timestamp=now(localzone());
7375
id::UUID = uuid4(), hashfunction = sha256)
7476
fde = BlobEntry(key, id, folder, bytes2hex(hashfunction(blob)), timestamp)
75-
de = addBlobEntry!(dfg, label, fde)
76-
db = addBlob!(dfg, fde, blob)
77-
return de=>db
77+
blobId = addBlob!(dfg, fde, blob) |> UUID
78+
newEntry = BlobEntry(fde; id=blobId, blobId)
79+
de = addBlobEntry!(dfg, label, newEntry)
80+
return de # de=>db
7881
end

src/DataBlobs/services/BlobEntry.jl

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,16 @@ Get data entry
7777
Also see: [`addBlobEntry`](@ref), [`getBlob`](@ref), [`listBlobEntries`](@ref)
7878
"""
7979
function getBlobEntry(var::AbstractDFGVariable, key::Symbol)
80-
!hasBlobEntry(var, key) && error("No dataEntry label $(key) found in variable $(getLabel(var))")
80+
if !hasBlobEntry(var, key)
81+
throw(KeyError("No dataEntry label $(key) found in variable $(getLabel(var)). Available keys: $(keys(var.dataDict))"))
82+
end
8183
return var.dataDict[key]
8284
end
8385

8486
function getBlobEntry(var::AbstractDFGVariable, blobId::UUID)
8587
for (k,v) in var.dataDict
86-
if v.id == blobId
88+
# FIXME stop using v.id since that has been repurposed for unique BlobEntry indexing
89+
if v.originId == blobId || v.blobId == blobId || v.id == blobId
8790
return v
8891
end
8992
end
@@ -117,16 +120,27 @@ getBlobEntry(dfg::AbstractDFG, label::Symbol, key::Union{Symbol, UUID, <:Abstrac
117120
Add Data Entry to a DFG variable
118121
Should be extended if DFG variable is not returned by reference.
119122
120-
Also see: [`getBlobEntry`](@ref), [`addBlob`](@ref), [`mergeBlobEntries!`](@ref)
123+
Also see: [`getBlobEntry`](@ref), [`addBlob!`](@ref), [`mergeBlobEntries!`](@ref)
121124
"""
122-
function addBlobEntry!(var::AbstractDFGVariable, bde::BlobEntry)
123-
haskey(var.dataDict, bde.label) && error("blobEntry $(bde.label) already exists on variable $(getLabel(var))")
124-
var.dataDict[bde.label] = bde
125-
return bde
125+
function addBlobEntry!(
126+
var::AbstractDFGVariable,
127+
entry::BlobEntry;
128+
blobId::Union{UUID,Nothing} = (isnothing(entry.blobId) ? entry.id : entry.blobId),
129+
blobSize::Int = (hasfield(BlobEntry, :size) ? entry.size : -1)
130+
)
131+
# see https://github.com/JuliaRobotics/DistributedFactorGraphs.jl/issues/985
132+
haskey(var.dataDict, entry.label) && error("blobEntry $(entry.label) already exists on variable $(getLabel(var))")
133+
var.dataDict[entry.label] = entry
134+
return entry
126135
end
127136

128-
function addBlobEntry!(dfg::AbstractDFG, label::Symbol, bde::BlobEntry)
129-
return addBlobEntry!(getVariable(dfg, label), bde)
137+
function addBlobEntry!(
138+
dfg::AbstractDFG,
139+
vLbl::Symbol,
140+
entry::BlobEntry;
141+
kw...
142+
)
143+
return addBlobEntry!(getVariable(dfg, vLbl), entry; kw...)
130144
end
131145

132146

src/DataBlobs/services/BlobStores.jl

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -165,30 +165,32 @@ function getBlob(store::FolderStore{T}, entry::BlobEntry) where T
165165
end
166166

167167
function addBlob!(store::FolderStore{T}, entry::BlobEntry, data::T) where T
168-
blobfilename = joinpath(store.folder,"$(entry.id).dat")
169-
entryfilename = joinpath(store.folder,"$(entry.id).json")
168+
blobfilename = joinpath(store.folder,"$(entry.originId).dat")
169+
entryfilename = joinpath(store.folder,"$(entry.originId).json")
170170
if isfile(blobfilename)
171-
error("Key '$(entry.id)' blob already exists.")
171+
error("Key '$(entry.originId)' blob already exists.")
172172
elseif isfile(entryfilename)
173-
error("Key '$(entry.id)' entry already exists, but no blob.")
173+
error("Key '$(entry.originId)' entry already exists, but no blob.")
174174
else
175175
open(blobfilename, "w") do f
176176
write(f, data)
177177
end
178178
open(entryfilename, "w") do f
179179
JSON3.write(f, entry)
180180
end
181-
return data
181+
# return data
182+
# FIXME update for entry.blobId vs. entry.originId
183+
return UUID(entry.originId)
182184
end
183185
end
184186

185187
function updateBlob!(store::FolderStore{T}, entry::BlobEntry, data::T) where T
186-
blobfilename = joinpath(store.folder,"$(entry.id).dat")
187-
entryfilename = joinpath(store.folder,"$(entry.id).json")
188+
blobfilename = joinpath(store.folder,"$(entry.originId).dat")
189+
entryfilename = joinpath(store.folder,"$(entry.originId).json")
188190
if !isfile(blobfilename)
189-
@warn "Key '$(entry.id)' doesn't exist."
191+
@warn "Key '$(entry.originId)' doesn't exist."
190192
elseif !isfile(entryfilename)
191-
@warn "Key '$(entry.id)' doesn't exist."
193+
@warn "Key '$(entry.originId)' doesn't exist."
192194
else
193195
open(blobfilename, "w") do f
194196
write(f, data)
@@ -228,19 +230,21 @@ function getBlob(store::InMemoryBlobStore{T}, entry::BlobEntry) where T
228230
end
229231

230232
function addBlob!(store::InMemoryBlobStore{T}, entry::BlobEntry, data::T) where T
231-
if haskey(store.blobs, entry.id)
232-
error("Key '$(entry.id)' blob already exists.")
233+
if haskey(store.blobs, entry.originId)
234+
error("Key '$(entry.originId)' blob already exists.")
233235
end
234-
return store.blobs[entry.id] = data
236+
# FIXME update for entry.originId vs .blobId
237+
store.blobs[entry.originId] = data
238+
return UUID(entry.originId)
235239
end
236240

237241
function updateBlob!(store::InMemoryBlobStore{T}, entry::BlobEntry, data::T) where T
238-
if haskey(store.blobs, entry.id)
239-
@warn "Key '$(entry.id)' doesn't exist."
242+
if haskey(store.blobs, entry.originId)
243+
@warn "Key '$(entry.originId)' doesn't exist."
240244
end
241-
return store.blobs[entry.id] = data
245+
return store.blobs[entry.originId] = data
242246
end
243247

244248
function deleteBlob!(store::InMemoryBlobStore{T}, entry::BlobEntry) where T
245-
return pop!(store.blobs, entry.id)
249+
return pop!(store.blobs, entry.originId)
246250
end

0 commit comments

Comments
 (0)