private DiskRangeList addCachedBufferToIter(
      DiskRangeList currentNotCached, DiskRangeList currentCached, BooleanRef gotAllData) {
    if (currentNotCached.getOffset() >= currentCached.getOffset()) {
      // Cached buffer has the same (or lower) offset as the requested buffer.
      if (currentNotCached.getEnd() <= currentCached.getEnd()) {
        // Replace the entire current DiskRange with new cached range.
        // In case of an inexact match in either of the below it may throw. We do not currently
        // support the case where the caller requests a single cache buffer via multiple smaller
        // sub-ranges; if that happens, this may throw. Noone does it now, though.
        // TODO: should we actively assert here for cache buffers larger than range?
        currentNotCached.replaceSelfWith(currentCached);
        return null;
      } else {
        // This cache range is a prefix of the requested one; the above also applies.
        // The cache may still contain the rest of the requested range, so don't set gotAllData.
        currentNotCached.insertPartBefore(currentCached);
        return currentNotCached;
      }
    }

    // Some part of the requested range is not cached - the cached offset is past the requested.
    if (gotAllData != null) {
      gotAllData.value = false;
    }
    if (currentNotCached.getEnd() <= currentCached.getEnd()) {
      // The cache buffer comprises the tail of the requested range (and possibly overshoots it).
      // The same as above applies - may throw if cache buffer is larger than the requested range,
      // and there's another range after this that starts in the middle of this cache buffer.
      // Currently, we cache at exact offsets, so the latter should never happen.
      currentNotCached.insertPartAfter(currentCached);
      return null;  // No more matches expected.
    } else {
      // The cached buffer is in the middle of the requested range.
      // The remaining tail of the latter may still be available further.
      DiskRangeList tail = new DiskRangeList(currentCached.getEnd(), currentNotCached.getEnd());
      currentNotCached.insertPartAfter(currentCached);
      currentCached.insertAfter(tail);
      return tail;
    }
  }
Adds cached buffer to buffer list.
@param currentNotCached Pointer to the list node where we are inserting.
@param currentCached The cached buffer found for this node, to insert.
@return The new currentNotCached pointer, following the cached buffer insertion.
llap-server/src/java/org/apache/hadoop/hive/llap/cache/LowLevelCacheImpl.java