@@ -55,34 +55,38 @@ function listBlobs end
55
55
# # AbstractBlobStore CRUD Interface
56
56
# #==============================================================================
57
57
58
- function getBlob (store:: AbstractBlobStore , entry :: BlobEntry )
58
+ function getBlob (store:: AbstractBlobStore , :: UUID )
59
59
error (" $(typeof (store)) doesn't override 'getBlob'." )
60
60
end
61
61
62
- function addBlob! (store:: AbstractBlobStore{T} , entry :: BlobEntry , data :: T ) where T
62
+ function addBlob! (store:: AbstractBlobStore{T} , :: UUID , :: T ) where {T}
63
63
error (" $(typeof (store)) doesn't override 'addBlob!'." )
64
64
end
65
65
66
- function updateBlob! (store:: AbstractBlobStore{T} , entry :: BlobEntry , data :: T ) where T
66
+ function updateBlob! (store:: AbstractBlobStore{T} , :: UUID , :: T ) where {T}
67
67
error (" $(typeof (store)) doesn't override 'updateBlob!'." )
68
68
end
69
69
70
- function deleteBlob! (store:: AbstractBlobStore , entry :: BlobEntry )
70
+ function deleteBlob! (store:: AbstractBlobStore , :: UUID )
71
71
error (" $(typeof (store)) doesn't override 'deleteBlob!'." )
72
72
end
73
73
74
74
function listBlobs (store:: AbstractBlobStore )
75
75
error (" $(typeof (store)) doesn't override 'listBlobs'." )
76
76
end
77
77
78
+ function hasBlob (store:: AbstractBlobStore , :: UUID )
79
+ error (" $(typeof (store)) doesn't override 'hasBlob'." )
80
+ end
81
+
78
82
# #==============================================================================
79
83
# # AbstractBlobStore derived CRUD for Blob
80
84
# #==============================================================================
81
85
82
86
function getBlob (dfg:: AbstractDFG , entry:: BlobEntry )
83
87
# cannot use entry.blobstore because the blob can be in any one of the blobstores
84
88
stores = getBlobStores (dfg)
85
- for (k,store) in stores
89
+ for (k, store) in stores
86
90
try
87
91
blob = getBlob (store, entry)
88
92
return blob
@@ -94,21 +98,46 @@ function getBlob(dfg::AbstractDFG, entry::BlobEntry)
94
98
end
95
99
throw (
96
100
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]) )"
98
102
)
99
103
)
100
104
end
101
105
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)
104
126
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)
107
129
130
+ # delete
108
131
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)
110
136
111
137
138
+ # has
139
+ hasBlob (dfg:: AbstractDFG , entry:: BlobEntry ) = hasBlob (getBlobStore (dfg, entry. blobstore), entry. originId)
140
+
112
141
# TODO
113
142
# """
114
143
# $(SIGNATURES)
@@ -136,7 +165,7 @@ deleteBlob!(dfg::AbstractDFG, entry::BlobEntry) =
136
165
struct FolderStore{T} <: AbstractBlobStore{T}
137
166
key:: Symbol
138
167
folder:: String
139
- function FolderStore {T} (key, folder) where T
168
+ function FolderStore {T} (key, folder) where {T}
140
169
if ! isdir (folder)
141
170
@info " Folder '$folder ' doesn't exist - creating."
142
171
# create new folder
@@ -148,103 +177,95 @@ end
148
177
149
178
FolderStore (foldername:: String ) = FolderStore {Vector{UInt8}} (:default_folder_store , foldername)
150
179
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" )
153
181
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" )
157
184
if isfile (blobfilename)
158
185
open (blobfilename) do f
159
186
return read (f)
160
187
end
161
188
else
162
189
throw (KeyError (" Could not find file '$(blobfilename) '." ))
163
- # return nothing
164
190
end
165
191
end
166
192
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" )
170
195
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." ))
174
197
else
175
198
open (blobfilename, " w" ) do f
176
199
write (f, data)
177
200
end
178
- open (entryfilename, " w" ) do f
179
- JSON3. write (f, entry)
180
- end
181
201
# return data
182
- # FIXME update for entry.blobId vs. entry.originId
183
- return UUID (entry. originId)
202
+ return blobId
184
203
end
185
204
end
186
205
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" )
190
208
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."
194
210
else
195
211
open (blobfilename, " w" ) do f
196
212
write (f, data)
197
213
end
198
- open (entryfilename, " w" ) do f
199
- JSON3. write (f, entry)
200
- end
201
214
return data
202
215
end
203
216
end
204
217
205
218
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
+
210
222
data = getBlob (store, entry)
211
223
rm (blobfilename)
212
- rm (entryfilename)
213
224
return data
214
225
end
215
226
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
+
216
236
# #==============================================================================
217
237
# # InMemoryBlobStore
218
238
# #==============================================================================
219
239
220
240
struct InMemoryBlobStore{T} <: AbstractBlobStore{T}
221
241
key:: Symbol
222
- blobs:: Dict{UUID, T}
242
+ blobs:: Dict{UUID,T}
223
243
end
224
244
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} ())
226
246
InMemoryBlobStore (storeKey:: Symbol = :default_inmemory_store ) = InMemoryBlobStore {Vector{UInt8}} (storeKey)
227
247
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 ]
230
250
end
231
251
232
- function addBlob! (store:: InMemoryBlobStore{T} , entry :: BlobEntry , data:: T ) where T
252
+ function addBlob! (store:: InMemoryBlobStore{T} , blobId :: UUID , data:: T ) where {T}
233
253
if haskey (store. blobs, entry. originId)
234
- error (" Key '$(entry . originId) ' blob already exists." )
254
+ error (" Key '$blobId ' blob already exists." )
235
255
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
239
258
end
240
259
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."
244
263
end
245
- return store. blobs[entry . originId ] = data
264
+ return store. blobs[blobId ] = data
246
265
end
247
266
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 )
250
269
end
270
+
271
+ hasBlob (store:: InMemoryBlobStore , blobId:: UUID ) = haskey (store. blobs, blobId)
0 commit comments