Skip to content

Commit

Permalink
Add BlobyRef + some rewriting + test passing
Browse files Browse the repository at this point in the history
  • Loading branch information
josePereiro committed Oct 14, 2024
1 parent 15b7e14 commit e63751f
Show file tree
Hide file tree
Showing 21 changed files with 389 additions and 180 deletions.
7 changes: 7 additions & 0 deletions notes/notes.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# TODO: create bloberia(::AbstractBlob), etc kind of accessor interfaces

# DONE: BlobyRef
# - An object containuing all data for pointing to a BlobyObject/Val
# - Its main goald is to be cheap to serialized
# - It must serialized the expected return type

# TODO: Work on type stability

# TODO: Dry the code: a lot of code repetition
Expand Down
23 changes: 20 additions & 3 deletions src/0.Base/0_types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ mutable struct Bloberia
temp::OrderedDict # RAM only
end

## .-- . -. - .--..- -- .- - --..-.-.- .- -.--
# TODO: dry some code using AbstractFramedBlob
# ex: getframe
abstract type AbstractFramedBlob end

## .-- . -. - .--..- -- .- - --..-.-.- .- -.--
# BlobBatch
# meta (file: meta.jls)
Expand All @@ -36,7 +41,7 @@ end
# |-- {"mtime" => 1.718214748856385e9, ...}

# TODO: define types
mutable struct BlobBatch
mutable struct BlobBatch <: AbstractFramedBlob
B::Bloberia # Parent folder
group::String # Batch group (defaul "0")
uuid::UInt128 # use UUIDs.uuid4().value
Expand All @@ -48,20 +53,25 @@ mutable struct BlobBatch
temp::OrderedDict # RAM only
end

## .-- . -. - .--..- -- .- - --..-.-.- .- -.--
# TODO: dry some code using AbstractBlob
# ex: getframe
abstract type AbstractBlob <: AbstractFramedBlob end

## .-- . -. - .--..- -- .- - --..-.-.- .- -.--
# btBlob (a blob in a batch)
# dat [dat/frame/key]
# |-- "0"
# |-- "A" => 1

struct btBlob
struct btBlob <: AbstractBlob
batch::BlobBatch # owner batch
uuid::UInt128 # unique universal id
end

## --.--. - .-. .- .--.-.- .- .---- ... . .-.-.-.-
# raBlob (random access blob)
struct raBlob
struct raBlob <: AbstractBlob
B::Bloberia # owner batch
id::String # user defined id
meta::OrderedDict{String, Any} # config/state/meta in general
Expand All @@ -70,5 +80,12 @@ struct raBlob
temp::OrderedDict # RAM only
end

## --.--. - .-. .- .--.-.- .- .---- ... . .-.-.-.-
# TODO: Complete signature BlobyRef{lT, rT} where {lT::Symbol, rT<:Any}
struct BlobyRef{lT, rT}
link::Dict{String, Any} # All coordinates
BlobyRef(ltype::Symbol, rtype::DataType) = new{ltype, rtype}(Dict())
end

## --.--. - .-. .- .--.-.- .- .---- ... . .-.-.-.-
nothing
4 changes: 3 additions & 1 deletion src/1.BloberiaBase/blobbatch.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## --.--. - .-. .- .--.-.- .- .---- ... . .-.-.-.-
# Create an empty blobbatch
blobbatch!(B::Bloberia, group::AbstractString ) = BlobBatch(B, group)
blobbatch!(B::Bloberia, group::AbstractString, uuid::UInt128) =
BlobBatch(B, group, uuid)
blobbatch!(B::Bloberia, group::AbstractString) = BlobBatch(B, group)
blobbatch!(B::Bloberia) = BlobBatch(B, BLOBERIA_DEFAULT_BATCH_GROUP)

function blobbatch(B::Bloberia, uuid0::UInt128) # existing batch
Expand Down
87 changes: 87 additions & 0 deletions src/3.AbstractBlobBase/base.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
## --.--. - .-. .- .--.-.- .- .---- ... . .-.-.-.-
# getindex
import Base.getindex
Base.getindex(b::AbstractBlob, frame::AbstractString, key) =
getindex(getframe(b, frame), key) # custom frame
Base.getindex(b::AbstractBlob, T::Type, frame::AbstractString, key) =
getindex(getframe(b, frame), key)::T # custom frame
Base.getindex(b::AbstractBlob, key) =
getindex(getframe(b), key) # default frame
Base.getindex(b::AbstractBlob, T::Type, key) =
getindex(getframe(b), key)::T # default frame

## --.--. - .-. .- .--.-.- .- .---- ... . .-.-.-.-
# TODO: Move to AbstractBlob
Base.setindex!(b::AbstractBlob, value, key) =
setindex!(b, value, BLOBBATCH_DEFAULT_FRAME_NAME, key)

## --.--. - .-. .- .--.-.- .- .---- ... . .-.-.-.-
# import Base.get
Base.get(b::AbstractBlob, frame::AbstractString, key, default) =
Base.get(getframe(b, frame), key, default)
Base.get(f::Function, b::AbstractBlob, frame::AbstractString, key) =
Base.get(f, getframe(b, frame), key)
Base.get!(b::AbstractBlob, frame::AbstractString, key, default) =
Base.get!(getframe!(b, frame), key, default)
Base.get!(f::Function, b::AbstractBlob, frame::AbstractString, key) =
Base.get!(f, getframe!(b, frame), key)

# default
Base.get(b::AbstractBlob, key, default) = Base.get(getframe(b), key, default)
Base.get(f::Function, b::AbstractBlob, key) = Base.get(f, getframe(b), key)
Base.get!(b::AbstractBlob, key, default) = Base.get!(getframe!(b), key, default)
Base.get!(f::Function, b::AbstractBlob, key) = Base.get!(f, getframe!(b), key)

## --.--. - .-. .- .--.-.- .- .---- ... . .-.-.-.-
# TODO: Move to Absttract
# :set! :get :get! :dry
# all in ram
function withblob!(f::Function, rb::AbstractBlob, mode::Symbol, frame, key::String)
if mode == :set!
val = f()
setindex!(rb, val, frame, key)
return blobyref(rb, frame, key; rT = typeof(val))
end
if mode == :get
val = get(f, rb, frame, key)
return blobyref(rb, frame, key; rT = typeof(val))
end
if mode == :get!
val = get!(f, rb, frame, key)
return blobyref(rb, frame, key; rT = typeof(val))
end
if mode == :dry
val = f()
return blobyref(rb, frame, key; rT = typeof(val))
end
error("Unknown mode, ", mode, ". see withblob! src")
end
withblob!(f::Function, rb::AbstractBlob, mode::Symbol, key::String) =
withblob!(f, rb, mode, BLOBBATCH_DEFAULT_FRAME_NAME, key)


## --.--. - .-. .- .--.-.- .- .---- ... . .-.-.-.-
# getframe
getframe!(b::AbstractBlob) = getframe!(b, BLOBBATCH_DEFAULT_FRAME_NAME)
getframe(b::AbstractBlob) = getframe(b, BLOBBATCH_DEFAULT_FRAME_NAME)

## --.--. - .-. .- .--.-.- .- .---- ... . .-.-.-.-
# lock
import Base.lock
Base.lock(f::Function, rb::AbstractBlob; kwargs...) = _lock(f, rb; kwargs...)
Base.lock(rb::AbstractBlob; kwargs...) = _lock(rb; kwargs...)

import Base.islocked
Base.islocked(rb::AbstractBlob) = _islocked(rb)

import Base.unlock
Base.unlock(rb::AbstractBlob; force = false) = _unlock(rb; force)

## --.--. - .-. .- .--.-.- .- .---- ... . .-.-.-.-
# blobyref
blobyref(rb::AbstractBlob, key; rT = nothing) =
blobyref(rb, BLOBBATCH_DEFAULT_FRAME_NAME, key; rT)


## --.--. - .-. .- .--.-.- .- .---- ... . .-.-.-.-
Base.haskey(b::AbstractBlob, key) = haskey(b, BLOBBATCH_DEFAULT_FRAME_NAME, key)
4 changes: 0 additions & 4 deletions src/3.btBlobBase/lock.jl

This file was deleted.

42 changes: 10 additions & 32 deletions src/3.btBlobBase/base.jl → src/4.btBlobBase/base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ end

## --.--. - .-. .- .--.-.- .- .---- ... . .-.-.-.-
# getindex
import Base.getindex
Base.getindex(b::btBlob, frame::AbstractString, key) = getindex(getframe(b, frame), key) # custom frame
Base.getindex(b::btBlob, T::Type, frame::AbstractString, key) = getindex(getframe(b, frame), key)::T # custom frame
Base.getindex(b::btBlob, key) = getindex(getframe(b), key) # default frame
Base.getindex(b::btBlob, T::Type, key) = getindex(getframe(b), key)::T # default frame
# import Base.getindex
# Base.getindex(b::btBlob, frame::AbstractString, key) = getindex(getframe(b, frame), key) # custom frame
# Base.getindex(b::btBlob, T::Type, frame::AbstractString, key) = getindex(getframe(b, frame), key)::T # custom frame
# Base.getindex(b::btBlob, key) = getindex(getframe(b), key) # default frame
# Base.getindex(b::btBlob, T::Type, key) = getindex(getframe(b), key)::T # default frame

# # TODO: think about it
# function Base.getindex(b::btBlob, framev::Vector) # get frame interface b[["bla"]]
Expand All @@ -49,9 +49,9 @@ function Base.get(dflt::Function, b::btBlob, frame::AbstractString, key)
_b_frame = _bb_frame[b.uuid]
return get(dflt, _b_frame, key)
end
Base.get(dflt::Function, b::btBlob, key) = get(dflt, b, BLOBBATCH_DEFAULT_FRAME_NAME, key)
Base.get(b::btBlob, key, frame::AbstractString, dflt) = get(()-> dflt, b, frame, key)
Base.get(b::btBlob, key, dflt) = get(b, BLOBBATCH_DEFAULT_FRAME_NAME, key, dflt)
# Base.get(dflt::Function, b::btBlob, key) = get(dflt, b, BLOBBATCH_DEFAULT_FRAME_NAME, key)
# Base.get(b::btBlob, key, frame::AbstractString, dflt) = get(()-> dflt, b, frame, key)
# Base.get(b::btBlob, key, dflt) = get(b, BLOBBATCH_DEFAULT_FRAME_NAME, key, dflt)

import Base.get!
function Base.get!(dflt::Function, b::btBlob, frame::AbstractString, key)
Expand All @@ -65,28 +65,6 @@ Base.get!(dflt::Function, b::btBlob, key) = get!(dflt, b, BLOBBATCH_DEFAULT_FRAM
Base.get!(b::btBlob, frame::AbstractString, key, dflt) = get!(()-> dflt, b, frame, key)
Base.get!(b::btBlob, key, dflt) = get!(b, BLOBBATCH_DEFAULT_FRAME_NAME, key, dflt)

## --.--. - .-. .- .--.-.- .- .---- ... . .-.-.-.-
# :set! :get :get! :dry
# all in ram
function withblob!(f::Function, b::btBlob, mode::Symbol, frame, key::String)
if mode == :set!
return setindex!(b, f(), frame, key)
end
if mode == :get
return get(f, b, frame, key)
end
if mode == :get!
return get!(f, b, frame, key)
end
if mode == :dry
return f()
end
error("Unknown mode, ", mode, ". see withblob! src")
end
withblob!(f::Function, b::btBlob, mode::Symbol, key::String) =
withblob!(f, b, mode, BLOBBATCH_DEFAULT_FRAME_NAME, key)


import Base.haskey
function Base.haskey(b::btBlob, frame::AbstractString, key)
# frame == "temp" && return haskey(b.temp, key)
Expand All @@ -97,7 +75,6 @@ function Base.haskey(b::btBlob, frame::AbstractString, key)
_b_frame = _bb_frame[b.uuid]
return haskey(_b_frame, key)
end
Base.haskey(b::btBlob, key) = haskey(b, BLOBBATCH_DEFAULT_FRAME_NAME, key)

## --.--. - .-. .- .--.-.- .- .---- ... . .-.-.-.-
# merge!
Expand All @@ -113,4 +90,5 @@ function Base.merge!(b::btBlob, frame::String, dat; force = true, prefix = "")
return b
end
Base.merge!(b::btBlob, dat; force = true, prefix = "") =
merge!(b, BLOBBATCH_DEFAULT_FRAME_NAME, dat; force, prefix)
merge!(b, BLOBBATCH_DEFAULT_FRAME_NAME, dat; force, prefix)

2 changes: 0 additions & 2 deletions src/3.btBlobBase/getframe.jl → src/4.btBlobBase/getframe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ function getframe(b::btBlob, frame::AbstractString)
_ondemand_loaddat!(b.batch, frame) # loaded on batch
return b.batch.frames[frame][b.uuid]
end
getframe(b::btBlob) = getframe(b, BLOBBATCH_DEFAULT_FRAME_NAME)

function getframe!(b::btBlob, frame::AbstractString)
_ondemand_loaddat!(b.batch, frame) # loaded on batch
_bb_frame = get!(OrderedDict, b.batch.frames, frame)
_b_frame = get!(OrderedDict, _bb_frame, b.uuid)
return _b_frame
end
getframe!(b::btBlob) = getframe!(b, BLOBBATCH_DEFAULT_FRAME_NAME)

## --.--. - .-. .- .--.-.- .- .---- ... . .-.-.-.-
hasframe(b::btBlob, frame::String) = hasframe(b.batch, frame)
3 changes: 3 additions & 0 deletions src/4.btBlobBase/lock.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
_pidfile(rb::btBlob) = _pidfile(rb.batch)
_getlock(rb::btBlob) = _getlock(rb.batch)
_setlock!(rb::btBlob, lk) = _setlock!(rb.batch)
File renamed without changes.
98 changes: 0 additions & 98 deletions src/4.raBlobBase/base.jl

This file was deleted.

13 changes: 0 additions & 13 deletions src/4.raBlobBase/lock.jl

This file was deleted.

Loading

0 comments on commit e63751f

Please sign in to comment.