Skip to content

Commit

Permalink
WIP #403 - rename Elements->Blob
Browse files Browse the repository at this point in the history
  • Loading branch information
Affie committed Jul 2, 2020
1 parent e2ddf93 commit b09697f
Show file tree
Hide file tree
Showing 14 changed files with 367 additions and 156 deletions.
5 changes: 3 additions & 2 deletions src/BigData/BigData.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ include("services/FileDataStore.jl")

export AbstractDataStore

export AbstractBigDataEntry, GeneralBigDataEntry, MongodbBigDataEntry, FileBigDataEntry
export AbstractBigDataEntry, GeneralDataEntry, MongodbDataEntry, FileDataEntry
export InMemoryDataStore, FileDataStore

export getBigData, addBigData!, updateBigData!, deleteBigData!, listStoreEntries
export getData, addData!, updateData!, deleteData!, listStoreEntries
export getDataBlob, addDataBlob!, updateDataBlob!, deleteDataBlob!, listDataBlobs
export copyStore
23 changes: 15 additions & 8 deletions src/BigData/entities/AbstractBigDataEntries.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
GeneralBigDataEntry is a generic multipurpose data entry that creates a unique
reproducible key using userId_robotId_sessionId_variableId_key.
"""
mutable struct GeneralBigDataEntry <: AbstractBigDataEntry
mutable struct GeneralDataEntry <: AbstractBigDataEntry
key::Symbol
storeKey::Symbol # Could swap this to string, but using it as an index later, so better as a symbol I believe.
createdTimestamp::DateTime
Expand All @@ -21,32 +21,39 @@ function _uniqueKey(dfg::G, v::V, key::Symbol)::Symbol where {G <: AbstractDFG,
return Symbol(key)
end

GeneralBigDataEntry(key::Symbol, storeKey::Symbol;

GeneralDataEntry(key::Symbol, storeKey::Symbol;
mimeType::String="application/octet-stream") =
GeneralBigDataEntry(key, storeKey, now(), now(), mimeType)
GeneralDataEntry(key, storeKey, now(), now(), mimeType)

function GeneralBigDataEntry(dfg::G, var::V, key::Symbol;
function GeneralDataEntry(dfg::G, var::V, key::Symbol;
mimeType::String="application/octet-stream") where {G <: AbstractDFG, V <: AbstractDFGVariable}
return GeneralBigDataEntry(key, _uniqueKey(dfg, var, key), mimeType=mimeType)
return GeneralDataEntry(key, _uniqueKey(dfg, var, key), mimeType=mimeType)
end

@deprecate GeneralBigDataEntry(args...; kwargs...) GeneralDataEntry(args...; kwargs...)

"""
$(TYPEDEF)
BigDataEntry in MongoDB.
"""
struct MongodbBigDataEntry <: AbstractBigDataEntry
struct MongodbDataEntry <: AbstractBigDataEntry
key::Symbol
oid::NTuple{12, UInt8} #mongodb object id
#maybe other fields such as:
#flags::Bool ready, valid, locked, permissions
#MIMEType::Symbol
#MIMEType::String
end

@deprecate MongodbBigDataEntry(args...) MongodbDataEntry(args...)

"""
$(TYPEDEF)
BigDataEntry in a file.
"""
struct FileBigDataEntry <: AbstractBigDataEntry
struct FileDataEntry <: AbstractBigDataEntry
key::Symbol
filename::String
end

@deprecate FileBigDataEntry(args...) FileDataEntry(args...)
2 changes: 1 addition & 1 deletion src/BigData/entities/InMemoryDataStore.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ end
Create an in-memory store using binary data (UInt8) as a type.
"""
function InMemoryDataStore()
return InMemoryDataStore{Vector{UInt8}, GeneralBigDataEntry}()
return InMemoryDataStore{Vector{UInt8}, GeneralDataEntry}()
end
103 changes: 58 additions & 45 deletions src/BigData/services/AbstractBigDataEntries.jl
Original file line number Diff line number Diff line change
@@ -1,41 +1,42 @@
import Base: ==

function ==(a::GeneralBigDataEntry, b::GeneralBigDataEntry)
function ==(a::GeneralDataEntry, b::GeneralDataEntry)
return a.key == b.key &&
a.storeKey == b.storeKey &&
a.mimeType == b.mimeType &&
Dates.value(a.createdTimestamp - b.createdTimestamp) < 1000 &&
Dates.value(a.lastUpdatedTimestamp - b.lastUpdatedTimestamp) < 1000 #1 second
end

function ==(a::MongodbBigDataEntry, b::MongodbBigDataEntry)
function ==(a::MongodbDataEntry, b::MongodbDataEntry)
return a.key == b.key && a.oid == b.oid
end

function ==(a::FileBigDataEntry, b::FileBigDataEntry)
function ==(a::FileDataEntry, b::FileDataEntry)
return a.key == b.key && a.filename == b.filename
end

"""
$(SIGNATURES)
Add Big Data Entry to a DFG variable
"""
function addBigDataEntry!(var::AbstractDFGVariable, bde::AbstractBigDataEntry)
function addDataEntry!(var::AbstractDFGVariable, bde::AbstractBigDataEntry)
haskey(var.bigData,bde.key) && error("BigData entry $(bde.key) already exists in variable")
var.bigData[bde.key] = bde
return bde
end


"""
$(SIGNATURES)
Add Big Data Entry to distributed factor graph.
Should be extended if DFG variable is not returned by reference.
"""
function addBigDataEntry!(dfg::AbstractDFG, label::Symbol, bde::AbstractBigDataEntry)
function addDataEntry!(dfg::AbstractDFG, label::Symbol, bde::AbstractBigDataEntry)
return addBigDataEntry!(getVariable(dfg, label), bde)
end

addDataEntry!(x...) = addBigDataEntry!(x...)
@deprecate addBigDataEntry!(args...) addDataEntry!(args...)

"""
$(SIGNATURES)
Expand All @@ -50,22 +51,22 @@ Related
addData!, getDataEntryElement, fetchData
"""
function addDataEntry!(dfg::AbstractDFG,
lbl::Symbol,
datastore::Union{FileDataStore, InMemoryDataStore},
descr::Symbol,
mimeType::AbstractString,
data::Vector{UInt8} )
function addData!(dfg::AbstractDFG,
lbl::Symbol,
datastore::Union{FileDataStore, InMemoryDataStore},
descr::Symbol,
mimeType::AbstractString,
data::Vector{UInt8} )
#
node = isVariable(dfg, lbl) ? getVariable(dfg, lbl) : getFactor(dfg, lbl)
# Make a big data entry in the graph - use JSON2 to just write this
entry = GeneralBigDataEntry(dfg, node, descr, mimeType=mimeType)
entry = GeneralDataEntry(dfg, node, descr, mimeType=mimeType)
# Set it in the store
addBigData!(datastore, entry, data)
addDataBlob!(datastore, entry, data)
# Add the entry to the graph
addBigDataEntry!(node, entry)
addDataEntry!(node, entry)
end
const addData! = addDataEntry!
# const addDataEntry! = addData!

"""
$SIGNATURES
Expand All @@ -79,15 +80,17 @@ const hasBigDataEntry = hasDataEntry
$(SIGNATURES)
Get big data entry
"""
function getBigDataEntry(var::AbstractDFGVariable, key::Symbol)
function getDataEntry(var::AbstractDFGVariable, key::Symbol)
!hasDataEntry(var, key) && (error("BigData entry $(key) does not exist in variable"); return nothing)
return var.bigData[key]
end

function getBigDataEntry(dfg::AbstractDFG, label::Symbol, key::Symbol)
function getDataEntry(dfg::AbstractDFG, label::Symbol, key::Symbol)
return getBigDataEntry(getVariable(dfg, label), key)
end

@deprecate getBigDataEntry(args...) getDataEntry(args...)

"""
$SIGNATURES
Get both the entry and raw data element from datastore returning as a tuple.
Expand Down Expand Up @@ -125,24 +128,26 @@ Related
addDataEntry!, addData!, fetchData, fetchDataEntryElement
"""
function getDataEntryElement(dfg::AbstractDFG,
dfglabel::Symbol,
datastore::Union{FileDataStore, InMemoryDataStore},
datalabel::Symbol)
function getDataEntryBlob(dfg::AbstractDFG,
dfglabel::Symbol,
datastore::Union{FileDataStore, InMemoryDataStore},
datalabel::Symbol)
#
vari = getVariable(dfg, dfglabel)
if !hasDataEntry(vari, datalabel)
@error "missing data entry $datalabel in $dfglabel"
return nothing, nothing
# current standards is to fail hard
error("missing data entry $datalabel in $dfglabel")
# return nothing, nothing
end
entry = getBigDataEntry(vari, datalabel)
element = getBigData(datastore, entry)
entry = getDataEntry(vari, datalabel)
element = getDataBlob(datastore, entry)
return entry, element
end
const fetchDataEntryElement = getDataEntryElement
const fetchData = getDataEntryElement

const fetchDataEntryElement = getDataEntryBlob
const fetchData = getDataEntryBlob

#
@deprecate getDataEntryElement(args...) getDataEntryBlob(args...)

"""
$(SIGNATURES)
Expand All @@ -151,16 +156,18 @@ Update big data entry
DevNote
- DF, unclear if `update` verb is applicable in this case, see #404
"""
function updateBigDataEntry!(var::AbstractDFGVariable, bde::AbstractBigDataEntry)
function updateDataEntry!(var::AbstractDFGVariable, bde::AbstractBigDataEntry)
!haskey(var.bigData,bde.key) && (@warn "$(bde.key) does not exist in variable, adding")
var.bigData[bde.key] = bde
return bde
end
function updateBigDataEntry!(dfg::AbstractDFG, label::Symbol, bde::AbstractBigDataEntry)
function updateDataEntry!(dfg::AbstractDFG, label::Symbol, bde::AbstractBigDataEntry)
# !isVariable(dfg, label) && return nothing
return updateBigDataEntry!(getVariable(dfg, label), bde)
return updateDataEntry!(getVariable(dfg, label), bde)
end

@deprecate updateBigDataEntry!(args...) updateDataEntry!(args...)

"""
$(SIGNATURES)
Delete big data entry from the factor graph.
Expand All @@ -169,46 +176,52 @@ Note this doesn't remove it from any data stores.
Notes:
- users responsibility to delete big data in db before deleting entry
"""
function deleteBigDataEntry!(var::AbstractDFGVariable, key::Symbol)
bde = getBigDataEntry(var, key)
function deleteDataEntry!(var::AbstractDFGVariable, key::Symbol)
bde = getDataEntry(var, key)
bde == nothing && return nothing
delete!(var.bigData, key)
return var
end
function deleteBigDataEntry!(dfg::AbstractDFG, label::Symbol, key::Symbol)
function deleteDataEntry!(dfg::AbstractDFG, label::Symbol, key::Symbol)
#users responsibility to delete big data in db before deleting entry
!isVariable(dfg, label) && return nothing
return deleteBigDataEntry!(getVariable(dfg, label), key)
return deleteDataEntry!(getVariable(dfg, label), key)
end

function deleteBigDataEntry!(var::AbstractDFGVariable, entry::AbstractBigDataEntry)
function deleteDataEntry!(var::AbstractDFGVariable, entry::AbstractBigDataEntry)
#users responsibility to delete big data in db before deleting entry
return deleteBigDataEntry!(var, entry.key)
return deleteDataEntry!(var, entry.key)
end

@deprecate deleteBigDataEntry!(args...) deleteDataEntry!(args...)

"""
$(SIGNATURES)
Get big data entries, Vector{AbstractBigDataEntry}
"""
function getBigDataEntries(var::AbstractDFGVariable)
function getDataEntries(var::AbstractDFGVariable)
#or should we return the iterator, Base.ValueIterator{Dict{Symbol,AbstractBigDataEntry}}?
collect(values(var.bigData))
end
function getBigDataEntries(dfg::AbstractDFG, label::Symbol)
function getDataEntries(dfg::AbstractDFG, label::Symbol)
!isVariable(dfg, label) && return nothing
#or should we return the iterator, Base.ValueIterator{Dict{Symbol,AbstractBigDataEntry}}?
getBigDataEntries(getVariable(dfg, label))
getDataEntries(getVariable(dfg, label))
end

@deprecate getBigDataEntries(args...) getDataEntries(args...)


"""
$(SIGNATURES)
getBigDataKeys
listDataEntries
"""
function getBigDataKeys(var::AbstractDFGVariable)
function listDataEntries(var::AbstractDFGVariable)
collect(keys(var.bigData))
end
function getBigDataKeys(dfg::AbstractDFG, label::Symbol)
function listDataEntries(dfg::AbstractDFG, label::Symbol)
!isVariable(dfg, label) && return nothing
getBigDataKeys(getVariable(dfg, label))
listDataEntries(getVariable(dfg, label))
end

@deprecate getBigDataKeys(args...) listDataEntries(args...)
24 changes: 12 additions & 12 deletions src/BigData/services/AbstractDataStore.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,43 @@
$(SIGNATURES)
Get the data for the specified entry, returns the data or Nothing.
"""
function getBigData(store::D, entry::E)::Union{Nothing, T} where {T, D <: AbstractDataStore{T}, E <: AbstractBigDataEntry}
error("$(typeof(store)) doesn't override 'getData'.")
function getDataBlob(store::D, entry::E)::Union{Nothing, T} where {T, D <: AbstractDataStore{T}, E <: AbstractBigDataEntry}
error("$(typeof(store)) doesn't override 'getDataBlob'.")
end

"""
$(SIGNATURES)
Adds the data to the store with the given entry. The function will warn if the entry already
exists and will overwrite it.
"""
function addBigData!(store::D, entry::E, data::T)::Union{Nothing, T} where {T, D <: AbstractDataStore{T}, E <: AbstractBigDataEntry}
error("$(typeof(store)) doesn't override 'addData!'.")
function addDataBlob!(store::D, entry::E, data::T)::Union{Nothing, T} where {T, D <: AbstractDataStore{T}, E <: AbstractBigDataEntry}
error("$(typeof(store)) doesn't override 'addDataBlob!'.")
end

"""
$(SIGNATURES)
Update the data in the store. The function will error and return nothing if
the entry does not exist.
"""
function updateBigData!(store::D, entry::E, data::T)::Union{Nothing, T} where {T, D <: AbstractDataStore{T}, E <: AbstractBigDataEntry}
error("$(typeof(store)) doesn't override 'updateData!'.")
function updateDataBlob!(store::D, entry::E, data::T)::Union{Nothing, T} where {T, D <: AbstractDataStore{T}, E <: AbstractBigDataEntry}
error("$(typeof(store)) doesn't override 'updateDataBlob!'.")
end

"""
$(SIGNATURES)
Delete the data in the store for the given entry. The function will error and return nothing if
the entry does not exist.
"""
function deleteBigData!(store::D, entry::E)::Union{Nothing, T} where {T, D <: AbstractDataStore{T}, E <: AbstractBigDataEntry}
error("$(typeof(store)) doesn't override 'deleteData!'.")
function deleteDataBlob!(store::D, entry::E)::Union{Nothing, T} where {T, D <: AbstractDataStore{T}, E <: AbstractBigDataEntry}
error("$(typeof(store)) doesn't override 'deleteDataBlob!'.")
end

"""
$(SIGNATURES)
List all entries in the data store.
"""
function listStoreEntries(store::D)::Vector{E} where {D <: AbstractDataStore, E <: AbstractBigDataEntry}
error("$(typeof(store)) doesn't override 'listEntries'.")
function listDataBlobs(store::D) where D <: AbstractDataStore
error("$(typeof(store)) doesn't override 'listDataBlobs'.")
end

"""
Expand All @@ -49,13 +49,13 @@ Returns the list of copied entries.
"""
function copyStore(sourceStore::D1, destStore::D2; sourceEntries=listEntries(sourceStore))::Vector{E} where {T, D1 <: AbstractDataStore{T}, D2 <: AbstractDataStore{T}, E <: AbstractBigDataEntry}
# Quick check
destEntries = listStoreEntries(destStore)
destEntries = listDataBlobs(destStore)
typeof(sourceEntries) != typeof(destEntries) && error("Can't copy stores, source has entries of type $(typeof(sourceEntries)), destination has entries of type $(typeof(destEntries)).")
# Same source/destination check
sourceStore == destStore && error("Can't specify same store for source and destination.")
# Otherwise, continue
for sourceEntry in sourceEntries
addBigData!(destStore, deepcopy(sourceEntry), getBigData(sourceStore, sourceEntry))
addDataBlob!(destStore, deepcopy(sourceEntry), getDataBlob(sourceStore, sourceEntry))
end
return sourceEntries
end
Loading

0 comments on commit b09697f

Please sign in to comment.