From 58aa1bfcb493fdb075513984f6c21054b53a1bca Mon Sep 17 00:00:00 2001 From: untoreh Date: Wed, 15 Jan 2025 08:58:58 +0100 Subject: [PATCH] feat: implement abstract dict store which allows to use any dict as a DictStore --- src/Storage/dictstore.jl | 81 ++++++++++++++++++++++------------------ 1 file changed, 45 insertions(+), 36 deletions(-) diff --git a/src/Storage/dictstore.jl b/src/Storage/dictstore.jl index 7815ed2..97938ed 100644 --- a/src/Storage/dictstore.jl +++ b/src/Storage/dictstore.jl @@ -1,54 +1,63 @@ # Stores data in a simple dict in memory -struct DictStore <: AbstractStore - a::Dict{String,Vector{UInt8}} +abstract type AbstractDictStore <: AbstractStore end + +struct DictStore <: AbstractDictStore + a::Dict{String,Vector{UInt8}} end DictStore() = DictStore(Dict{String,Vector{UInt8}}()) -Base.show(io::IO,d::DictStore) = print(io,"Dictionary Storage") -function _pdict(d::DictStore,p) - p = (isempty(p) || endswith(p,'/')) ? p : p*'/' - filter(((k,v),)->startswith(k,p),d.a) +Base.show(io::IO, d::AbstractDictStore) = print(io, "Dictionary Storage") +function _pdict(d::AbstractDictStore, p) + p = (isempty(p) || endswith(p, '/')) ? p : p * '/' + filter(((k, v),) -> startswith(k, p), d.a) +end +function _pkeys(d::AbstractDictStore, p) + p = (isempty(p) || endswith(p, '/')) ? p : p * '/' + filter((k) -> startswith(k, p), keys(d.a)) end -function storagesize(d::DictStore,p) - sum(i->last(split(i[1],'/')) ∉ (".zattrs",".zarray") ? sizeof(i[2]) : zero(sizeof(i[2])), _pdict(d,p)) +function storagesize(d::AbstractDictStore, p) + sum(i -> if last(split(i[1], '/')) ∉ (".zattrs", ".zarray") + sizeof(i[2]) + else + zero(sizeof(i[2])) + end, _pdict(d, p)) end -function Base.getindex(d::DictStore,i::AbstractString) - get(d.a,i,nothing) +function Base.getindex(d::AbstractDictStore, i::AbstractString) + get(d.a, i, nothing) end -function Base.setindex!(d::DictStore,v,i::AbstractString) - d.a[i] = v +function Base.setindex!(d::AbstractDictStore, v, i::AbstractString) + d.a[i] = v end -Base.delete!(d::DictStore, i::AbstractString) = delete!(d.a,i) +Base.delete!(d::AbstractDictStore, i::AbstractString) = delete!(d.a, i) -function subdirs(d::DictStore,p) - d2 = _pdict(d,p) - _searchsubdict(d2,p,(sp,lp)->length(sp) > lp+1) +function subdirs(d::AbstractDictStore, p) + d2 = _pkeys(d, p) + _searchsubdict(d2, p, (sp, lp) -> length(sp) > lp + 1) end -function subkeys(d::DictStore,p) - d2 = _pdict(d,p) - _searchsubdict(d2,p,(sp,lp)->length(sp) == lp+1) +function subkeys(d::AbstractDictStore, p) + d2 = _pkeys(d, p) + _searchsubdict(d2, p, (sp, lp) -> length(sp) == lp + 1) end -function _searchsubdict(d2,p,condition) - o = Set{String}() - pspl = split(rstrip(p,'/'),'/') - lp = if length(pspl) == 1 && isempty(pspl[1]) - 0 - else - length(pspl) - end - for k in keys(d2) - sp = split(k,'/') - if condition(sp,lp) - push!(o,sp[lp+1]) +function _searchsubdict(d2, p, condition) + o = Set{String}() + pspl = split(rstrip(p, '/'), '/') + lp = if length(pspl) == 1 && isempty(pspl[1]) + 0 + else + length(pspl) end - end - collect(o) + for k in d2 + sp = split(k, '/') + if condition(sp, lp) + push!(o, sp[lp + 1]) + end + end + collect(o) end +#getsub(d::AbstractDictStore, p, n) = _substore(d,p).subdirs[n] -#getsub(d::DictStore, p, n) = _substore(d,p).subdirs[n] - -#path(d::DictStore) = "" +#path(d::AbstractDictStore) = ""