Skip to content

Commit

Permalink
fix insert of FolderDict
Browse files Browse the repository at this point in the history
  • Loading branch information
dehann committed Feb 16, 2024
1 parent ac4c41c commit 51bddb7
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 19 deletions.
40 changes: 23 additions & 17 deletions src/dev/FolderDict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ import Base: getindex, setindex!
mkpath(wdir_)
wdir_
end
# serialization function
serialize::Function = Serialization.serialize
deserialize::Function = Serialization.deserialize
end

##
Expand All @@ -50,7 +53,7 @@ function Base.getindex(
flb = sd.keydict[f]
toload = joinpath(sd.wdir, "$flb")
# fetch from cold storage
Serialization.deserialize(toload)
sd.deserialize(toload)
end

# you've hit a cache miss, so start making space or new elements
Expand Down Expand Up @@ -88,15 +91,12 @@ end
function setindex!(
sd::FolderDict,
v,
k
k,
id::UUID = uuid4() # nonBase API, allows wider use-cases of FolderDict
)
# immediately/always insert new data into folder store with newly generated uuid
nid = uuid4()
flb = joinpath(sd.wdir, "$nid")
wtsk = @async begin
Serialization.serialize(flb, v) # for sluggish IO
@info "DOSE2T" flb
end
# immediately/always insert new data into folder store with a unique id
flb = joinpath(sd.wdir, "$id")
wtsk = @async sd.serialize(flb, v) # for sluggish IO

# should any obsolete files be deleted from the filesystem?
dtsk = if haskey(sd.keydict, k)
Expand All @@ -106,16 +106,22 @@ function setindex!(
@async Base.Filesystem.rm(joinpath(sd.wdir, "$dlb")) # for sluggish IO
end
# set new uuid in keydict only after potential overwrite delete
sd.keydict[k] = nid
sd.keydict[k] = id

# should the cache be updated? pqueue is arbitor over cache
if haskey(sd.pqueue, k)
sd.cache[k] = v
# maintain the old priority value
# ensure pqueue has correct value in all cases
prk = collect(keys(sd.pqueue))
maxpriority = 0
# truncate for cache_size, and also allow for cache_size==0
if 0 < sd.cache_size <= length(sd.pqueue)
maxpriority = sd.pqueue[prk[end]]
rmk = prk[1]
delete!(sd.pqueue,rmk)
delete!(sd.cache, rmk)
end

# write new info to FolderDict fields
sd.keydict[k] = nid
# assume middle of the pack priority for this cache-miss
sd.pqueue[k] = round(Int, maxpriority/2)
# Reminder, pqueue is arbitor over cache
sd.cache[k] = v

# wait for any disk mutations to finish
_wait(::Nothing) = nothing
Expand Down
24 changes: 22 additions & 2 deletions test/testFolderDict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
using Test

using Caesar
import Caesar: FolderStore

import Caesar: FolderDict

##

@testset "Basic functional tests of FolderDict" begin
##
Expand All @@ -23,6 +23,26 @@ fd[:a] = 1
@test 1 == fd.cache[:a]
@test 1 == fd[:a] # all up test for getindex when key in cache

fd[:b] = 2

@test 2 == length(fd.keydict)
@test fd.keydict[:a] != fd.keydict[:b]
@test 2 == length(fd.pqueue)
@test 2 == length(fd.cache)
@test 2 == fd.cache[:b]
@test 2 == fd[:b] # all up test for getindex when key in cache

fd[:c] = 3

@test 3 == length(fd.keydict)
@test fd.keydict[:a] != fd.keydict[:c]
@test 2 == length(fd.pqueue)
@test 2 == length(fd.cache)
@test 3 == fd.cache[:c]
@test 3 == fd[:c] # all up test for getindex when key in cache





##
Expand Down

0 comments on commit 51bddb7

Please sign in to comment.