Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement abstract dict store #173

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 45 additions & 36 deletions src/Storage/dictstore.jl
Original file line number Diff line number Diff line change
@@ -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) = ""
Loading