Class SegmentCache


  • public abstract class SegmentCache
    extends Object
    A cache for data Segment instances by their SegmentId. This cache ignores bulk segments.

    Conceptually this cache serves as a 2nd level cache for segments. The 1st level cache is implemented by memoising the segment in its id (see SegmentId#segment. Every time an segment is evicted from this cache the memoised segment is discarded (see SegmentId#onAccess.

    • Field Detail

      • DEFAULT_SEGMENT_CACHE_MB

        public static final int DEFAULT_SEGMENT_CACHE_MB
        Default maximum weight of this cache in MB
        See Also:
        Constant Field Values
    • Constructor Detail

      • SegmentCache

        public SegmentCache()
    • Method Detail

      • newSegmentCache

        @NotNull
        public static @NotNull SegmentCache newSegmentCache​(long cacheSizeMB)
        Create a new segment cache of the given size. Returns an always empty cache for cacheSizeMB <= 0.
        Parameters:
        cacheSizeMB - size of the cache in megabytes.
      • getSegment

        @NotNull
        public abstract @NotNull Segment getSegment​(@NotNull
                                                    @NotNull SegmentId id,
                                                    @NotNull
                                                    @NotNull Callable<Segment> loader)
                                             throws ExecutionException
        Retrieve an segment from the cache or load it and cache it if not yet in the cache.
        Parameters:
        id - the id of the segment
        loader - the loader to load the segment if not yet in the cache
        Returns:
        the segment identified by id
        Throws:
        ExecutionException - when loader failed to load an segment
      • putSegment

        public abstract void putSegment​(@NotNull
                                        @NotNull Segment segment)
        Put a segment into the cache. This method does nothing for bulk segments.
        Parameters:
        segment - the segment to cache
      • clear

        public abstract void clear()
        Clear all segment from the cache
      • getCacheStats

        @NotNull
        public abstract @NotNull AbstractCacheStats getCacheStats()
        Returns:
        Statistics for this cache.
      • recordHit

        public abstract void recordHit()
        Record a hit in this cache's underlying statistics. See SegmentId#onAccess