|
2 | 2 |
|
3 | 3 | import java.io.IOException; |
4 | 4 | import java.util.ArrayList; |
5 | | -import java.util.Collection; |
6 | 5 | import java.util.List; |
7 | 6 | import org.janelia.saalfeldlab.n5.N5Exception.N5IOException; |
8 | 7 | import org.janelia.saalfeldlab.n5.readdata.LazyRead; |
9 | 8 | import org.janelia.saalfeldlab.n5.readdata.Range; |
10 | 9 | import org.janelia.saalfeldlab.n5.readdata.ReadData; |
11 | 10 |
|
12 | | -public abstract class SliceTrackingLazyRead implements LazyRead { |
| 11 | +/** |
| 12 | + * A {@link LazyRead} that wraps a delegate {@code LazyRead} and keeps track of |
| 13 | + * all slices that have been {@link #materialize materialized}. |
| 14 | + * <p> |
| 15 | + * When materializing a new slice, we first check whether it is completely |
| 16 | + * covered by a materialized slice that we already track. If so, then we just |
| 17 | + * return a slice on the existing materialized slice. If not, we materialize the |
| 18 | + * slice from the delegate track it. |
| 19 | + */ |
| 20 | +public class SliceTrackingLazyRead implements LazyRead { |
13 | 21 |
|
14 | 22 | protected static class Slice implements Range { |
15 | 23 |
|
@@ -75,39 +83,7 @@ public long size() throws N5IOException { |
75 | 83 | return delegate.size(); |
76 | 84 | } |
77 | 85 |
|
78 | | - /** |
79 | | - * Indicates that the given slices will be subsequently read. |
80 | | - * {@code LazyRead} implementations (optionally) may take steps to prepare |
81 | | - * for these subsequent slices. |
82 | | - * <p> |
83 | | - * Minimal implementation: Find offset and length covering all ranges that |
84 | | - * are not yet fully covered by existing slices. Then materialize the slice |
85 | | - * covering that range. |
86 | | - * |
87 | | - * @param ranges |
88 | | - * slice ranges to prefetch |
89 | | - * |
90 | | - * @throws N5IOException |
91 | | - * if any I/O error occurs |
92 | | - */ |
93 | | - @Override |
94 | | - public void prefetch(final Collection<? extends Range> ranges) throws N5IOException { |
95 | | - |
96 | | - long fromIndex = Long.MAX_VALUE; |
97 | | - long toIndex = Long.MIN_VALUE; |
98 | | - for (final Range slice : ranges) { |
99 | | - if (!isCovered(slice)) { |
100 | | - fromIndex = Math.min(fromIndex, slice.offset()); |
101 | | - toIndex = Math.max(toIndex, slice.end()); |
102 | | - } |
103 | | - } |
104 | | - |
105 | | - if (fromIndex < toIndex) { |
106 | | - materialize(fromIndex, toIndex - fromIndex); |
107 | | - } |
108 | | - } |
109 | | - |
110 | | - private boolean isCovered(final Range slice) { |
| 86 | + protected boolean isCovered(final Range slice) { |
111 | 87 |
|
112 | 88 | return Slices.findContainingSlice(slices, slice) != null; |
113 | 89 | } |
|
0 commit comments