Skip to content

Commit 0d2b182

Browse files
authored
dont rely on readblock to return (#177)
1 parent 64f4de8 commit 0d2b182

File tree

1 file changed

+13
-18
lines changed

1 file changed

+13
-18
lines changed

src/cached.jl

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ struct CachedDiskArray{T,N,A<:AbstractArray{T,N},C} <: AbstractDiskArray{T,N}
1818
end
1919
function CachedDiskArray(A::AbstractArray{T,N}; maxsize=1000) where {T,N}
2020
by(x) = sizeof(x) ÷ 1_000_000 # In Megabytes
21-
CachedDiskArray(A, LRU{Tuple,Any}(; by, maxsize))
21+
CachedDiskArray(A, LRU{ChunkIndex{N,OffsetChunks},OffsetArray{T,N,Array{T,N}}}(; by, maxsize))
2222
end
2323

2424
Base.parent(A::CachedDiskArray) = A.parent
@@ -35,25 +35,20 @@ eachchunk(A::CachedDiskArray) = eachchunk(parent(A))
3535
function _readblock_cached!(A::CachedDiskArray{T,N}, data, I...) where {T,N}
3636
chunks = eachchunk(A)
3737
chunk_inds = findchunk.(chunks.chunks, I)
38-
needed_chunks = chunks[chunk_inds...]
39-
40-
chunk_arrays = map(needed_chunks) do c
41-
if haskey(A.cache, c)
42-
A.cache[c]
43-
else
44-
chunk_data = Array{T,N}(undef, length.(c)...)
45-
A.cache[c] = readblock!(parent(A), chunk_data, c...)
38+
data_offset = OffsetArray(data,map(i->first(i)-1,I)...)
39+
foreach(CartesianIndices(chunk_inds)) do ci
40+
chunkindex = ChunkIndex(ci,offset=true)
41+
chunk = get!(A.cache, chunkindex) do
42+
res = parent(A)[chunkindex]
43+
res
44+
end
45+
inner_indices = map(axes(chunk),axes(data_offset)) do ax1, ax2
46+
max(first(ax1),first(ax2)):min(last(ax1),last(ax2))
47+
end
48+
for ii in CartesianIndices(inner_indices)
49+
data_offset[ii] = chunk[ii]
4650
end
4751
end
48-
out = ConcatDiskArray(chunk_arrays)
49-
50-
out_inds = map(I, first(needed_chunks)) do i, nc
51-
i .- first(nc) .+ 1
52-
end
53-
54-
data .= view(out, out_inds...)
55-
56-
return data
5752
end
5853

5954
"""

0 commit comments

Comments
 (0)