Skip to content

Commit 2ad66fc

Browse files
authored
Immutable Folderstore id=>blob pair (#1160)
* Immutable Folderstore id=>blob pair * updateBlob! is obsolete * test listBlobs
1 parent 97044de commit 2ad66fc

File tree

4 files changed

+40
-34
lines changed

4 files changed

+40
-34
lines changed

src/DataBlobs/services/BlobStores.jl

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,6 @@ end
7676
# also creates an blobid as uuid4
7777
addBlob!(store::AbstractBlobstore, data) = addBlob!(store, uuid4(), data)
7878

79-
#update
80-
function updateBlob!(dfg::AbstractDFG, entry::Blobentry, data)
81-
return updateBlob!(getBlobstore(dfg, entry.blobstore), entry.blobid, data)
82-
end
83-
84-
function updateBlob!(store::AbstractBlobstore, entry::Blobentry, data)
85-
return updateBlob!(store, entry.blobid, data)
86-
end
8779
#delete
8880
function deleteBlob!(dfg::AbstractDFG, entry::Blobentry)
8981
return deleteBlob!(getBlobstore(dfg, entry.blobstore), entry)
@@ -147,7 +139,10 @@ end
147139

148140
function getBlob(store::FolderStore{T}, blobid::UUID) where {T}
149141
blobfilename = joinpath(store.folder, string(store.label), string(blobid))
150-
if isfile(blobfilename)
142+
tombstonefile = blobfilename * ".deleted"
143+
if isfile(tombstonefile)
144+
throw(IdNotFoundError("Blob (deleted)", blobid))
145+
elseif isfile(blobfilename)
151146
open(blobfilename) do f
152147
return read(f)
153148
end
@@ -168,25 +163,25 @@ function addBlob!(store::FolderStore{T}, blobid::UUID, data::T) where {T}
168163
end
169164
end
170165

171-
function updateBlob!(store::FolderStore{T}, blobid::UUID, data::T) where {T}
172-
blobfilename = joinpath(store.folder, string(store.label), string(blobid))
173-
if !isfile(blobfilename)
174-
@warn "Key '$blobid' doesn't exist."
175-
else
176-
open(blobfilename, "w") do f
177-
return write(f, data)
178-
end
179-
return data
180-
end
181-
end
182-
183166
function deleteBlob!(store::FolderStore{T}, blobid::UUID) where {T}
167+
# Tombstone pattern: instead of deleting the file, create a tombstone marker file
184168
blobfilename = joinpath(store.folder, string(store.label), string(blobid))
185-
if !isfile(blobfilename)
169+
tombstonefile = blobfilename * ".deleted"
170+
if isfile(blobfilename)
171+
# Remove the actual blob file
172+
rm(blobfilename)
173+
# Create a tombstone marker
174+
open(tombstonefile, "w") do f
175+
return write(f, "deleted")
176+
end
177+
return 1
178+
elseif isfile(tombstonefile)
179+
# Already deleted
180+
return 0
181+
else
182+
# Not found
186183
throw(IdNotFoundError("Blob", blobid))
187184
end
188-
rm(blobfilename)
189-
return 1
190185
end
191186

192187
function hasBlob(store::FolderStore, blobid::UUID)
@@ -196,7 +191,18 @@ end
196191

197192
hasBlob(store::FolderStore, entry::Blobentry) = hasBlob(store, entry.blobid)
198193

199-
listBlobs(store::FolderStore) = readdir(store.folder)
194+
function listBlobs(store::FolderStore)
195+
folder = joinpath(store.folder, string(store.label))
196+
# Parse folder to only include UUIDs automatically excluding tombstone files this way.
197+
blobids = UUID[]
198+
for filename in readdir(folder)
199+
id = tryparse(UUID, filename)
200+
isnothing(id) && continue
201+
push!(blobids, id)
202+
end
203+
return blobids
204+
end
205+
200206
##==============================================================================
201207
## InMemoryBlobstore
202208
##==============================================================================
@@ -228,13 +234,6 @@ function addBlob!(store::InMemoryBlobstore{T}, blobid::UUID, data::T) where {T}
228234
return blobid
229235
end
230236

231-
function updateBlob!(store::InMemoryBlobstore{T}, blobid::UUID, data::T) where {T}
232-
if haskey(store.blobs, blobid)
233-
@warn "Key '$blobid' doesn't exist."
234-
end
235-
return store.blobs[blobid] = data
236-
end
237-
238237
function deleteBlob!(store::InMemoryBlobstore, blobid::UUID)
239238
if !haskey(store.blobs, blobid)
240239
throw(IdNotFoundError("Blob", blobid))

src/Deprecated.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,10 @@ function getFactorState(args...)
344344
)
345345
end
346346

347+
function updateBlob!(args...)
348+
return error("updateBlob! is obsolete as blobid=>Blob pairs are immutable.")
349+
end
350+
347351
## ================================================================================
348352
## Deprecated in v0.28
349353
##=================================================================================

src/DistributedFactorGraphs.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -427,8 +427,6 @@ const unstable_functions::Vector{Symbol} = [
427427
:mergeGraph!,
428428
:buildSubgraph,
429429
:incrDataLabelSuffix,# TODO somewhat used, do we deprecate?
430-
:updateMetadata!,## TODO deprecated or obsolete
431-
:updateBlob!,## TODO deprecated or obsolete
432430

433431
# set # TODO what to do here, maybe `ref` verb + setproperty.
434432
:setSolverParams!,
@@ -440,6 +438,8 @@ const unstable_functions::Vector{Symbol} = [
440438
# no set on these
441439

442440
#deprecated in v0.29
441+
:updateMetadata!,## TODO deprecated or obsolete
442+
:updateBlob!,## TODO deprecated or obsolete
443443
:getFactorState, # FIXME getFactorState were questioned and being reviewed again for name, other than that they are checked.
444444
:packDistribution,
445445
:unpackDistribution,

test/testBlocks.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -911,12 +911,15 @@ function blobsStoresTestBlock!(fg)
911911
blobid = addBlob!(fs, testData)
912912
@test blobid isa UUID
913913
@test hasBlob(fs, blobid)
914+
@test listBlobs(fs) == [blobid]
914915
@test_throws DFG.IdExistsError addBlob!(fs, blobid, testData)
915916
@test getBlob(fs, blobid) == testData
916917
@test_throws DFG.IdNotFoundError getBlob(fs, uuid4())
917918
@test_throws DFG.IdNotFoundError deleteBlob!(fs, uuid4())
918919
@test deleteBlob!(fs, blobid) == 1
919920
@test_throws DFG.IdNotFoundError getBlob(fs, blobid)
921+
@test deleteBlob!(fs, blobid) == 0
922+
@test listBlobs(fs) == UUID[]
920923

921924
# Blob Wrappers
922925
# on Variable

0 commit comments

Comments
 (0)