Skip to content

Commit a5d43e8

Browse files
committed
wip on aligning blob with sdk
1 parent 866808a commit a5d43e8

File tree

6 files changed

+143
-106
lines changed

6 files changed

+143
-106
lines changed

src/DataBlobs/DataBlobs.jl

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/DataBlobs/services/BlobEntry.jl

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@ end
8181

8282
function getBlobEntry(var::AbstractDFGVariable, blobId::UUID)
8383
for (k,v) in var.dataDict
84-
# FIXME stop using v.id since that has been repurposed for unique BlobEntry indexing
85-
if v.originId == blobId || v.blobId == blobId || v.id == blobId
84+
if blobId in [v.originId, v.blobId]
8685
return v
8786
end
8887
end
@@ -93,7 +92,9 @@ end
9392

9493
#TODO consider changing this to use `first` as in julia's findfirst - as in findfirstBlobEntry->label::Symbol
9594
# or getBlobEntryFirst, btw this returns a vector in some other places
96-
function getBlobEntry(var::AbstractDFGVariable, key::Regex)
95+
@deprecate getBlobEntry(var::AbstractDFGVariable, key::Regex) getfirstBlobEntry(var, key)
96+
97+
function getfirstBlobEntry(var::AbstractDFGVariable, key::Regex)
9798
for (k,v) in var.dataDict
9899
if occursin(key, string(v.label))
99100
return v
@@ -106,7 +107,7 @@ end
106107

107108
getBlobEntry(var::AbstractDFGVariable, key::AbstractString) = getBlobEntry(var,Regex(key))
108109

109-
110+
#TODO split betweeen getfirstBlobEntry and getBlobEntry
110111
getBlobEntry(dfg::AbstractDFG, label::Symbol, key::Union{Symbol, UUID, <:AbstractString, Regex}) = getBlobEntry(getVariable(dfg, label), key)
111112
# getBlobEntry(dfg::AbstractDFG, label::Symbol, key::Symbol) = getBlobEntry(getVariable(dfg, label), key)
112113

@@ -121,22 +122,30 @@ Also see: [`getBlobEntry`](@ref), [`addBlob!`](@ref), [`mergeBlobEntries!`](@ref
121122
function addBlobEntry!(
122123
var::AbstractDFGVariable,
123124
entry::BlobEntry;
124-
blobId::Union{UUID,Nothing} = (isnothing(entry.blobId) ? entry.id : entry.blobId),
125-
blobSize::Int = (hasfield(BlobEntry, :size) ? entry.size : -1)
126125
)
127126
# see https://github.com/JuliaRobotics/DistributedFactorGraphs.jl/issues/985
127+
# blobId::Union{UUID,Nothing} = (isnothing(entry.blobId) ? entry.id : entry.blobId),
128+
# blobSize::Int = (hasfield(BlobEntry, :size) ? entry.size : -1)
128129
haskey(var.dataDict, entry.label) && error("blobEntry $(entry.label) already exists on variable $(getLabel(var))")
129130
var.dataDict[entry.label] = entry
130131
return entry
131132
end
132133

134+
function addBlobEntry!(
135+
var::PackedVariable,
136+
entry::BlobEntry,
137+
)
138+
entry.label in getproperty.(var.blobEntries,:label) && error("blobEntry $(entry.label) already exists on variable $(getLabel(var))")
139+
push!(var.blobEntries, entry)
140+
return entry
141+
end
142+
133143
function addBlobEntry!(
134144
dfg::AbstractDFG,
135145
vLbl::Symbol,
136146
entry::BlobEntry;
137-
kw...
138147
)
139-
return addBlobEntry!(getVariable(dfg, vLbl), entry; kw...)
148+
return addBlobEntry!(getVariable(dfg, vLbl), entry)
140149
end
141150

142151

@@ -203,12 +212,25 @@ function getBlobEntries(var::AbstractDFGVariable)
203212
#or should we return the iterator, Base.ValueIterator{Dict{Symbol,BlobEntry}}?
204213
collect(values(var.dataDict))
205214
end
215+
216+
function getBlobEntries(var::PackedVariable)
217+
var.blobEntries
218+
end
219+
206220
function getBlobEntries(dfg::AbstractDFG, label::Symbol)
207221
# !isVariable(dfg, label) && return nothing
208222
#or should we return the iterator, Base.ValueIterator{Dict{Symbol,BlobEntry}}?
209223
getBlobEntries(getVariable(dfg, label))
210224
end
211225

226+
function getBlobEntries(dfg::AbstractDFG, label::Symbol, regex::Regex)
227+
entries = getBlobEntries(dfg, label)
228+
return filter(entries) do e
229+
occursin(regex, string(e.label))
230+
end
231+
end
232+
233+
212234
"""
213235
$(SIGNATURES)
214236

src/DataBlobs/services/BlobStores.jl

Lines changed: 79 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -55,34 +55,38 @@ function listBlobs end
5555
## AbstractBlobStore CRUD Interface
5656
##==============================================================================
5757

58-
function getBlob(store::AbstractBlobStore, entry::BlobEntry)
58+
function getBlob(store::AbstractBlobStore, ::UUID)
5959
error("$(typeof(store)) doesn't override 'getBlob'.")
6060
end
6161

62-
function addBlob!(store::AbstractBlobStore{T}, entry::BlobEntry, data::T) where T
62+
function addBlob!(store::AbstractBlobStore{T}, ::UUID, ::T) where {T}
6363
error("$(typeof(store)) doesn't override 'addBlob!'.")
6464
end
6565

66-
function updateBlob!(store::AbstractBlobStore{T}, entry::BlobEntry, data::T) where T
66+
function updateBlob!(store::AbstractBlobStore{T}, ::UUID, ::T) where {T}
6767
error("$(typeof(store)) doesn't override 'updateBlob!'.")
6868
end
6969

70-
function deleteBlob!(store::AbstractBlobStore, entry::BlobEntry)
70+
function deleteBlob!(store::AbstractBlobStore, ::UUID)
7171
error("$(typeof(store)) doesn't override 'deleteBlob!'.")
7272
end
7373

7474
function listBlobs(store::AbstractBlobStore)
7575
error("$(typeof(store)) doesn't override 'listBlobs'.")
7676
end
7777

78+
function hasBlob(store::AbstractBlobStore, ::UUID)
79+
error("$(typeof(store)) doesn't override 'hasBlob'.")
80+
end
81+
7882
##==============================================================================
7983
## AbstractBlobStore derived CRUD for Blob
8084
##==============================================================================
8185

8286
function getBlob(dfg::AbstractDFG, entry::BlobEntry)
8387
# cannot use entry.blobstore because the blob can be in any one of the blobstores
8488
stores = getBlobStores(dfg)
85-
for (k,store) in stores
89+
for (k, store) in stores
8690
try
8791
blob = getBlob(store, entry)
8892
return blob
@@ -94,21 +98,46 @@ function getBlob(dfg::AbstractDFG, entry::BlobEntry)
9498
end
9599
throw(
96100
KeyError(
97-
"could not find $(entry.label), uuid $(entry.id)) in any of the listed blobstores:\n $([s->getKey(s) for (s,v) in stores]))"
101+
"could not find $(entry.label), uuid $blobId) in any of the listed blobstores:\n $([s->getKey(s) for (s,v) in stores]))"
98102
)
99103
)
100104
end
101105

102-
addBlob!(dfg::AbstractDFG, entry::BlobEntry, data::T) where T =
103-
addBlob!(getBlobStore(dfg, entry.blobstore), entry, data)
106+
getBlob(store::AbstractBlobStore, entry::BlobEntry) =
107+
getBlob(store, entry.originId)
108+
109+
#add
110+
addBlob!(dfg::AbstractDFG, entry::BlobEntry, data) =
111+
addBlob!(getBlobStore(dfg, entry.blobstore), entry, data)
112+
113+
addBlob!(store::AbstractBlobStore, entry::BlobEntry, data) =
114+
addBlob!(store, entry.originId, data)
115+
116+
addBlob!(store::AbstractBlobStore, data) =
117+
addBlob!(store, uuid4(), data)
118+
119+
#fallback as not all blobStores use filename
120+
addBlob!(store::AbstractBlobStore, blobId::UUID, data, ::String) =
121+
addBlob!(store, blobId, data)
122+
123+
#update
124+
updateBlob!(dfg::AbstractDFG, entry::BlobEntry, data::T) where {T} =
125+
updateBlob!(getBlobStore(dfg, entry.blobstore), entry, data)
104126

105-
updateBlob!(dfg::AbstractDFG, entry::BlobEntry, data::T) where T =
106-
updateBlob!(getBlobStore(dfg, entry.blobstore), entry, data)
127+
updateBlob!(store::AbstractBlobStore, entry::BlobEntry, data) =
128+
updateBlob!(store, entry.originId, data)
107129

130+
#delete
108131
deleteBlob!(dfg::AbstractDFG, entry::BlobEntry) =
109-
deleteBlob!(getBlobStore(dfg, entry.blobstore), entry)
132+
deleteBlob!(getBlobStore(dfg, entry.blobstore), entry)
133+
134+
deleteBlob!(store::AbstractBlobStore, entry::BlobEntry) =
135+
deleteBlob!(store, entry.originId)
110136

111137

138+
#has
139+
hasBlob(dfg::AbstractDFG, entry::BlobEntry) = hasBlob(getBlobStore(dfg, entry.blobstore), entry.originId)
140+
112141
#TODO
113142
# """
114143
# $(SIGNATURES)
@@ -136,7 +165,7 @@ deleteBlob!(dfg::AbstractDFG, entry::BlobEntry) =
136165
struct FolderStore{T} <: AbstractBlobStore{T}
137166
key::Symbol
138167
folder::String
139-
function FolderStore{T}(key, folder) where T
168+
function FolderStore{T}(key, folder) where {T}
140169
if !isdir(folder)
141170
@info "Folder '$folder' doesn't exist - creating."
142171
# create new folder
@@ -148,103 +177,95 @@ end
148177

149178
FolderStore(foldername::String) = FolderStore{Vector{UInt8}}(:default_folder_store, foldername)
150179

151-
blobfilename(store::FolderStore, entry::BlobEntry) = joinpath(store.folder,"$(entry.id).dat")
152-
entryfilename(store::FolderStore, entry::BlobEntry) = joinpath(store.folder,"$(entry.id).json")
180+
blobfilename(store::FolderStore, blobId::UUID) = joinpath(store.folder, "$blobId.dat")
153181

154-
function getBlob(store::FolderStore{T}, entry::BlobEntry) where T
155-
blobfilename = joinpath(store.folder,"$(entry.id).dat")
156-
# entryfilename = "$(store.folder)/$(entry.id).json"
182+
function getBlob(store::FolderStore{T}, blobId::UUID) where {T}
183+
blobfilename = joinpath(store.folder, "$blobId.dat")
157184
if isfile(blobfilename)
158185
open(blobfilename) do f
159186
return read(f)
160187
end
161188
else
162189
throw(KeyError("Could not find file '$(blobfilename)'."))
163-
# return nothing
164190
end
165191
end
166192

167-
function addBlob!(store::FolderStore{T}, entry::BlobEntry, data::T) where T
168-
blobfilename = joinpath(store.folder,"$(entry.originId).dat")
169-
entryfilename = joinpath(store.folder,"$(entry.originId).json")
193+
function addBlob!(store::FolderStore{T}, blobId::UUID, data::T) where {T}
194+
blobfilename = joinpath(store.folder, "$blobId.dat")
170195
if isfile(blobfilename)
171-
error("Key '$(entry.originId)' blob already exists.")
172-
elseif isfile(entryfilename)
173-
error("Key '$(entry.originId)' entry already exists, but no blob.")
196+
throw(KeyError("Key '$blobId' blob already exists."))
174197
else
175198
open(blobfilename, "w") do f
176199
write(f, data)
177200
end
178-
open(entryfilename, "w") do f
179-
JSON3.write(f, entry)
180-
end
181201
# return data
182-
# FIXME update for entry.blobId vs. entry.originId
183-
return UUID(entry.originId)
202+
return blobId
184203
end
185204
end
186205

187-
function updateBlob!(store::FolderStore{T}, entry::BlobEntry, data::T) where T
188-
blobfilename = joinpath(store.folder,"$(entry.originId).dat")
189-
entryfilename = joinpath(store.folder,"$(entry.originId).json")
206+
function updateBlob!(store::FolderStore{T}, blobId::UUID, data::T) where {T}
207+
blobfilename = joinpath(store.folder, "$blobId.dat")
190208
if !isfile(blobfilename)
191-
@warn "Key '$(entry.originId)' doesn't exist."
192-
elseif !isfile(entryfilename)
193-
@warn "Key '$(entry.originId)' doesn't exist."
209+
@warn "Key '$blobId' doesn't exist."
194210
else
195211
open(blobfilename, "w") do f
196212
write(f, data)
197213
end
198-
open(entryfilename, "w") do f
199-
JSON3.write(f, entry)
200-
end
201214
return data
202215
end
203216
end
204217

205218

206-
function deleteBlob!(store::FolderStore{T}, entry::BlobEntry) where T
207-
blobfilename = joinpath(store.folder,"$(entry.id).dat")
208-
entryfilename = joinpath(store.folder,"$(entry.id).json")
209-
219+
function deleteBlob!(store::FolderStore{T}, blobId::UUID) where {T}
220+
blobfilename = joinpath(store.folder, "$blobId.dat")
221+
210222
data = getBlob(store, entry)
211223
rm(blobfilename)
212-
rm(entryfilename)
213224
return data
214225
end
215226

227+
#hasBlob or existsBlob?
228+
function hasBlob(store::FolderStore, blobId::UUID)
229+
blobfilename = joinpath(store.folder, "$blobId.dat")
230+
isfile(blobfilename)
231+
end
232+
233+
hasBlob(store::FolderStore, entry::BlobEntry) = hasBlob(store, entry.originId)
234+
235+
216236
##==============================================================================
217237
## InMemoryBlobStore
218238
##==============================================================================
219239

220240
struct InMemoryBlobStore{T} <: AbstractBlobStore{T}
221241
key::Symbol
222-
blobs::Dict{UUID, T}
242+
blobs::Dict{UUID,T}
223243
end
224244

225-
InMemoryBlobStore{T}(storeKey::Symbol) where T = InMemoryBlobStore{Vector{UInt8}}(storeKey, Dict{UUID, T}())
245+
InMemoryBlobStore{T}(storeKey::Symbol) where {T} = InMemoryBlobStore{Vector{UInt8}}(storeKey, Dict{UUID,T}())
226246
InMemoryBlobStore(storeKey::Symbol=:default_inmemory_store) = InMemoryBlobStore{Vector{UInt8}}(storeKey)
227247

228-
function getBlob(store::InMemoryBlobStore{T}, entry::BlobEntry) where T
229-
return store.blobs[entry.id]
248+
function getBlob(store::InMemoryBlobStore, blobId::UUID)
249+
return store.blobs[blobId]
230250
end
231251

232-
function addBlob!(store::InMemoryBlobStore{T}, entry::BlobEntry, data::T) where T
252+
function addBlob!(store::InMemoryBlobStore{T}, blobId::UUID, data::T) where {T}
233253
if haskey(store.blobs, entry.originId)
234-
error("Key '$(entry.originId)' blob already exists.")
254+
error("Key '$blobId' blob already exists.")
235255
end
236-
# FIXME update for entry.originId vs .blobId
237-
store.blobs[entry.originId] = data
238-
return UUID(entry.originId)
256+
store.blobs[blobId] = data
257+
return UUIDblobId
239258
end
240259

241-
function updateBlob!(store::InMemoryBlobStore{T}, entry::BlobEntry, data::T) where T
242-
if haskey(store.blobs, entry.originId)
243-
@warn "Key '$(entry.originId)' doesn't exist."
260+
function updateBlob!(store::InMemoryBlobStore{T}, blobId::UUID, data::T) where {T}
261+
if haskey(store.blobs, blobId)
262+
@warn "Key '$blobId' doesn't exist."
244263
end
245-
return store.blobs[entry.originId] = data
264+
return store.blobs[blobId] = data
246265
end
247266

248-
function deleteBlob!(store::InMemoryBlobStore{T}, entry::BlobEntry) where T
249-
return pop!(store.blobs, entry.originId)
267+
function deleteBlob!(store::InMemoryBlobStore, blobId::UUID)
268+
return pop!(store.blobs, blobId)
250269
end
270+
271+
hasBlob(store::InMemoryBlobStore, blobId::UUID) = haskey(store.blobs, blobId)

0 commit comments

Comments
 (0)