Skip to content

Commit 5a944e0

Browse files
committed
Implement cache item indexing using numeric sequence keys.
1 parent 6a6bda0 commit 5a944e0

3 files changed

Lines changed: 58 additions & 2 deletions

File tree

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,14 @@ dc = DataCache(joinpath(homedir(), ".datacaches", "project1"))
128128
dc["canidae_occs"] = pbdb_occurrences(base_name = "Canidae", show = "full")
129129
dc["dinosaur_taxa"] = pbdb_taxa(name = "Dinosauria", vocab = "pbdb")
130130

131-
# Retrieve
131+
# Retrieve by label
132132
occs = dc["canidae_occs"]
133133
taxa = dc["dinosaur_taxa"]
134134

135+
# Retrieve by sequence index (as shown in showcache output)
136+
occs = dc[1]
137+
taxa = dc[2]
138+
135139
# Conditionally fetch
136140
if !haskey(dc, "trilobites")
137141
dc["trilobites"] = pbdb_occurrences(base_name = "Trilobita")
@@ -156,6 +160,9 @@ showcache(dc)
156160
relabel!(dc, "canidae_occs", "canidae") # by label
157161
relabel!(dc, 2, "canidae") # by sequence index
158162

163+
# Retrieve by sequence index
164+
df = dc[1]
165+
159166
# Remove entries
160167
delete!(dc, "trilobites") # by label
161168
delete!(dc, 2) # by sequence index

src/DataCaches.jl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,11 @@ end
268268
"""
269269
read(cache::DataCache, key::CacheKey) → data
270270
read(cache::DataCache, label::AbstractString) → data
271+
read(cache::DataCache, n::Integer) → data
271272
272-
Retrieve a cached dataset by [`CacheKey`](@ref) or label string.
273+
Retrieve a cached dataset by [`CacheKey`](@ref), label string, or stable sequence
274+
index. The `Integer` form uses the sequence index shown in brackets by `showcache`
275+
(e.g. `[1]`, `[2]`). Use `reindexcache!` to compact gaps after many deletions.
273276
"""
274277
function Base.read(cache::DataCache, key::CacheKey)
275278
isfile(key.path) || error("Cache file missing: $(key.path)")
@@ -282,8 +285,15 @@ function Base.read(cache::DataCache, lbl::AbstractString)
282285
return Base.read(cache, cache._index[id])
283286
end
284287

288+
function Base.read(cache::DataCache, n::Integer)
289+
key = _resolve_by_seq(cache, Int(n))
290+
isnothing(key) && error("No cache entry with sequence index $n")
291+
return Base.read(cache, key)
292+
end
293+
285294
Base.getindex(cache::DataCache, lbl::AbstractString) = Base.read(cache, lbl)
286295
Base.getindex(cache::DataCache, key::CacheKey) = Base.read(cache, key)
296+
Base.getindex(cache::DataCache, n::Integer) = Base.read(cache, n)
287297
Base.setindex!(cache::DataCache, data, lbl::AbstractString) = write!(cache, data; label = lbl)
288298

289299
# --- Introspection -----------------------------------------------------------

test/runtests.jl

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,4 +240,43 @@ using TOML
240240
end
241241
end
242242

243+
@testset "read and getindex by sequence index" begin
244+
mktempdir() do dir
245+
c = DataCache(dir)
246+
write!(c, [1, 2, 3]; label = "first")
247+
write!(c, [4, 5, 6]; label = "second")
248+
249+
@test Base.read(c, 1) == [1, 2, 3]
250+
@test Base.read(c, 2) == [4, 5, 6]
251+
252+
@test c[1] == [1, 2, 3]
253+
@test c[2] == [4, 5, 6]
254+
255+
@test_throws ErrorException c[99]
256+
end
257+
end
258+
259+
@testset "seq index stable after deletion" begin
260+
mktempdir() do dir
261+
c = DataCache(dir)
262+
write!(c, "a"; label = "first")
263+
write!(c, "b"; label = "second")
264+
write!(c, "c"; label = "third")
265+
delete!(c, "second")
266+
@test c[1] == "a"
267+
@test c[3] == "c"
268+
@test_throws ErrorException c[2]
269+
end
270+
end
271+
272+
@testset "seq index survives reload" begin
273+
mktempdir() do dir
274+
c1 = DataCache(dir)
275+
write!(c1, [9, 8, 7]; label = "persist_seq")
276+
seq = only(values(c1._index)).seq
277+
c2 = DataCache(dir)
278+
@test c2[seq] == [9, 8, 7]
279+
end
280+
end
281+
243282
end

0 commit comments

Comments
 (0)