CachedRegionTracker implements Cache.Listener { public synchronized int getRegionEndTimeMs(long byteOffset) { lookupRegion.startOffset = byteOffset; Region floorRegion = regions.floor(lookupRegion); if (floorRegion == null || byteOffset > floorRegion.endOffset || floorRegion.endOffsetIndex == -1) { return NOT_CACHED; } int index = floorRegion.endOffsetIndex; if (index == chunkIndex.length - 1 && floorRegion.endOffset == (chunkIndex.offsets[index] + chunkIndex.sizes[index])) { return CACHED_TO_END; } long segmentFractionUs = (chunkIndex.durationsUs[index] * (floorRegion.endOffset - chunkIndex.offsets[index])) / chunkIndex.sizes[index]; return (int) ((chunkIndex.timesUs[index] + segmentFractionUs) / 1000); } CachedRegionTracker(Cache cache, String cacheKey, ChunkIndex chunkIndex); void release(); synchronized int getRegionEndTimeMs(long byteOffset); @Override synchronized void onSpanAdded(Cache cache, CacheSpan span); @Override synchronized void onSpanRemoved(Cache cache, CacheSpan span); @Override void onSpanTouched(Cache cache, CacheSpan oldSpan, CacheSpan newSpan); static final int NOT_CACHED; static final int CACHED_TO_END; }
@Test public void testGetRegion_noSpansInCache() { assertThat(tracker.getRegionEndTimeMs(100)).isEqualTo(CachedRegionTracker.NOT_CACHED); assertThat(tracker.getRegionEndTimeMs(150)).isEqualTo(CachedRegionTracker.NOT_CACHED); }