|
| 1 | +export BlobStoreEntry |
| 2 | +""" |
| 3 | + $(TYPEDEF) |
| 4 | +Genaral Data Store Entry. |
| 5 | +""" |
| 6 | +struct BlobStoreEntry <: AbstractDataEntry |
| 7 | + label::Symbol |
| 8 | + id::UUID |
| 9 | + blobstore::Symbol |
| 10 | + hash::String # Probably https://docs.julialang.org/en/v1/stdlib/SHA |
| 11 | + origin::String # E.g. user|robot|session|varlabel |
| 12 | + description::String |
| 13 | + mimeType::String |
| 14 | + createdTimestamp::ZonedDateTime # of when the entry was created |
| 15 | +end |
| 16 | + |
| 17 | +abstract type AbstractBlobStore{T} end |
| 18 | + |
| 19 | +# AbstractBlobStore should have key or overwrite getKey |
| 20 | +getKey(store::AbstractBlobStore) = store.key |
| 21 | + |
| 22 | +#TODO consider adding this to dfg |
| 23 | +const BlobStores = Dict{Symbol, AbstractBlobStore} |
| 24 | + |
| 25 | + |
| 26 | +##============================================================================== |
| 27 | +## AbstractBlobStore CRUD Interface |
| 28 | +##============================================================================== |
| 29 | +function getDataBlob(store::AbstractBlobStore, entry::BlobStoreEntry) |
| 30 | + error("$(typeof(store)) doesn't override 'getDataBlob'.") |
| 31 | +end |
| 32 | + |
| 33 | +function addDataBlob!(store::AbstractBlobStore{T}, entry::BlobStoreEntry, data::T) where T |
| 34 | + error("$(typeof(store)) doesn't override 'addDataBlob!'.") |
| 35 | +end |
| 36 | + |
| 37 | +function updateDataBlob!(store::AbstractBlobStore{T}, entry::BlobStoreEntry, data::T) where T |
| 38 | + error("$(typeof(store)) doesn't override 'updateDataBlob!'.") |
| 39 | +end |
| 40 | + |
| 41 | +function deleteDataBlob!(store::AbstractBlobStore, entry::BlobStoreEntry) |
| 42 | + error("$(typeof(store)) doesn't override 'deleteDataBlob!'.") |
| 43 | +end |
| 44 | + |
| 45 | +function listDataBlobs(store::AbstractBlobStore) |
| 46 | + error("$(typeof(store)) doesn't override 'listDataBlobs'.") |
| 47 | +end |
| 48 | + |
| 49 | +##============================================================================== |
| 50 | +## Store and Entry Data CRUD |
| 51 | +##============================================================================== |
| 52 | + |
| 53 | +function getData(dfg::AbstractDFG, blobstore::AbstractBlobStore, label::Symbol, key::Symbol; hashfunction = sha256) |
| 54 | + de = getDataEntry(dfg, label, key) |
| 55 | + db = getDataBlob(blobstore, de) |
| 56 | + assertHash(de, db, hashfunction=hashfunction) |
| 57 | + return de=>db |
| 58 | +end |
| 59 | + |
| 60 | +function addData!(dfg::AbstractDFG, blobstore::AbstractBlobStore, label::Symbol, entry::AbstractDataEntry, blob::Vector{UInt8}; hashfunction = sha256) |
| 61 | + assertHash(entry, blob, hashfunction=hashfunction) |
| 62 | + de = addDataEntry!(dfg, label, entry) |
| 63 | + db = addDataBlob!(blobstore, de, blob) |
| 64 | + return de=>db |
| 65 | +end |
| 66 | + |
| 67 | +function updateData!(dfg::AbstractDFG, blobstore::AbstractBlobStore, label::Symbol, entry::AbstractDataEntry, blob::Vector{UInt8}) |
| 68 | + assertHash(entry, blob, hashfunction=hashfunction) |
| 69 | + de = updateDataEntry!(dfg, label, entry) |
| 70 | + db = updateDataBlob!(blobstore, de, blob) |
| 71 | + return de=>db |
| 72 | +end |
| 73 | + |
| 74 | +deleteData!(dfg::AbstractDFG, blobstore::AbstractBlobStore, label::Symbol, entry::AbstractDataEntry) = |
| 75 | + deleteData!(dfg, blobstore, label, entry.label) |
| 76 | + |
| 77 | +function deleteData!(dfg::AbstractDFG, blobstore::AbstractBlobStore, label::Symbol, key::Symbol) |
| 78 | + de = deleteDataEntry!(dfg, label, key) |
| 79 | + db = deleteDataBlob!(blobstore, de) |
| 80 | + return de=>db |
| 81 | +end |
| 82 | + |
| 83 | + |
| 84 | +function addData!(dfg::AbstractDFG, blobstore::AbstractBlobStore, label::Symbol, key::Symbol, |
| 85 | + blob::Vector{UInt8}, timestamp=now(localzone()); description="", mimeType = "", id::UUID = uuid4(), hashfunction = sha256) |
| 86 | + |
| 87 | + |
| 88 | + entry = BlobStoreEntry(key, id, blobstore.key, bytes2hex(hashfunction(blob)), |
| 89 | + "$(dfg.userId)|$(dfg.robotId)|$(dfg.sessionId)|$(dfg.label)", |
| 90 | + description, mimeType, timestamp) |
| 91 | + |
| 92 | + addData!(dfg, blobstore, label, entry, blob; hashfunction = hashfunction) |
| 93 | +end |
| 94 | + |
| 95 | + |
| 96 | +##============================================================================== |
| 97 | +## FolderStore |
| 98 | +##============================================================================== |
| 99 | +export FolderStore |
| 100 | + |
| 101 | +struct FolderStore{T} <: AbstractBlobStore{T} |
| 102 | + key::Symbol |
| 103 | + folder::String |
| 104 | +end |
| 105 | + |
| 106 | +blobfilename(store::FolderStore, entry::FileDataEntry) = joinpath(store.folder,"$(entry.id).dat") |
| 107 | +entryfilename(store::FolderStore, entry::FileDataEntry) = joinpath(store.folder,"$(entry.id).json") |
| 108 | + |
| 109 | +function getDataBlob(store::FolderStore{T}, entry::BlobStoreEntry) where T |
| 110 | + blobfilename = joinpath(store.folder,"$(entry.id).dat") |
| 111 | + # entryfilename = "$(store.folder)/$(entry.id).json" |
| 112 | + if isfile(blobfilename) |
| 113 | + open(blobfilename) do f |
| 114 | + return read(f) |
| 115 | + end |
| 116 | + else |
| 117 | + error("Could not find file '$(blobfilename)'.") |
| 118 | + # return nothing |
| 119 | + end |
| 120 | +end |
| 121 | + |
| 122 | +function addDataBlob!(store::FolderStore{T}, entry::BlobStoreEntry, data::T) where T |
| 123 | + blobfilename = joinpath(store.folder,"$(entry.id).dat") |
| 124 | + entryfilename = joinpath(store.folder,"$(entry.id).json") |
| 125 | + if isfile(blobfilename) |
| 126 | + error("Key '$(id)' blob already exists.") |
| 127 | + elseif isfile(entryfilename) |
| 128 | + error("Key '$(id)' entry already exists, but no blob.") |
| 129 | + else |
| 130 | + open(blobfilename, "w") do f |
| 131 | + write(f, data) |
| 132 | + end |
| 133 | + open(entryfilename, "w") do f |
| 134 | + JSON.print(f, entry) |
| 135 | + end |
| 136 | + return data |
| 137 | + end |
| 138 | +end |
| 139 | + |
| 140 | +function deleteDataBlob!(store::FolderStore{T}, entry::BlobStoreEntry) where T |
| 141 | + blobfilename = joinpath(store.folder,"$(entry.id).dat") |
| 142 | + entryfilename = joinpath(store.folder,"$(entry.id).json") |
| 143 | + |
| 144 | + data = getDataBlob(store, entry) |
| 145 | + rm(blobfilename) |
| 146 | + rm(entryfilename) |
| 147 | + return data |
| 148 | +end |
0 commit comments