Class CacheLongKeyLIRS.Segment<V>

java.lang.Object
org.h2.mvstore.cache.CacheLongKeyLIRS.Segment<V>
Type Parameters:
V - the value type
Enclosing class:
CacheLongKeyLIRS<V>

private static class CacheLongKeyLIRS.Segment<V> extends Object
A cache segment
  • Field Details

    • mapSize

      int mapSize
      The number of (hot, cold, and non-resident) entries in the map.
    • queueSize

      int queueSize
      The size of the LIRS queue for resident cold entries.
    • queue2Size

      int queue2Size
      The size of the LIRS queue for non-resident cold entries.
    • hits

      long hits
      The number of cache hits.
    • misses

      long misses
      The number of cache misses.
    • entries

      final CacheLongKeyLIRS.Entry<V>[] entries
      The map array. The size is always a power of 2.
    • usedMemory

      long usedMemory
      The currently used memory.
    • stackMoveDistance

      private final int stackMoveDistance
      How many other item are to be moved to the top of the stack before the current item is moved.
    • maxMemory

      private long maxMemory
      The maximum memory this cache should use in bytes.
    • mask

      private final int mask
      The bit mask that is applied to the key hash code to get the index in the map array. The mask is the length of the array minus one.
    • nonResidentQueueSize

      private final int nonResidentQueueSize
      Low watermark for the number of entries in the non-resident queue, as a factor of the number of entries in the map.
    • nonResidentQueueSizeHigh

      private final int nonResidentQueueSizeHigh
      High watermark for the number of entries in the non-resident queue, as a factor of the number of entries in the map.
    • stack

      private final CacheLongKeyLIRS.Entry<V> stack
      The stack of recently referenced elements. This includes all hot entries, and the recently referenced cold entries. Resident cold entries that were not recently referenced, as well as non-resident cold entries, are not in the stack.

      There is always at least one entry: the head entry.

    • stackSize

      private int stackSize
      The number of entries in the stack.
    • queue

      private final CacheLongKeyLIRS.Entry<V> queue
      The queue of resident cold entries.

      There is always at least one entry: the head entry.

    • queue2

      private final CacheLongKeyLIRS.Entry<V> queue2
      The queue of non-resident cold entries.

      There is always at least one entry: the head entry.

    • stackMoveCounter

      private int stackMoveCounter
      The number of times any item was moved to the top of the stack.
  • Constructor Details

    • Segment

      Segment(long maxMemory, int stackMoveDistance, int len, int nonResidentQueueSize, int nonResidentQueueSizeHigh)
      Create a new cache segment.
      Parameters:
      maxMemory - the maximum memory to use
      stackMoveDistance - the number of other entries to be moved to the top of the stack before moving an entry to the top
      len - the number of hash table buckets (must be a power of 2)
      nonResidentQueueSize - the non-resident queue size low watermark factor
      nonResidentQueueSizeHigh - the non-resident queue size high watermark factor
    • Segment

      Segment(CacheLongKeyLIRS.Segment<V> old, int len)
      Create a new cache segment from an existing one. The caller must synchronize on the old segment, to avoid concurrent modifications.
      Parameters:
      old - the old segment
      len - the number of hash table buckets (must be a power of 2)
  • Method Details

    • getNewMapLen

      int getNewMapLen()
      Calculate the new number of hash table buckets if the internal map should be re-sized.
      Returns:
      0 if no resizing is needed, or the new length
    • addToMap

      private void addToMap(CacheLongKeyLIRS.Entry<V> e)
    • get

      Get the value from the given entry. This method adjusts the internal state of the cache sometimes, to ensure commonly used entries stay in the cache.
      Parameters:
      e - the entry
      Returns:
      the value, or null if there is no resident entry
    • access

      private void access(CacheLongKeyLIRS.Entry<V> e)
      Access an item, moving the entry to the top of the stack or front of the queue if found.
      Parameters:
      e - entry to record access for
    • put

      V put(long key, int hash, V value, int memory)
      Add an entry to the cache. The entry may or may not exist in the cache yet. This method will usually mark unknown entries as cold and known entries as hot.
      Parameters:
      key - the key (may not be null)
      hash - the hash
      value - the value (may not be null)
      memory - the memory used for the given entry
      Returns:
      the old value, or null if there was no resident entry
    • remove

      V remove(long key, int hash)
      Remove an entry. Both resident and non-resident entries can be removed.
      Parameters:
      key - the key (may not be null)
      hash - the hash
      Returns:
      the old value, or null if there was no resident entry
    • evict

      private void evict()
      Evict cold entries (resident and non-resident) until the memory limit is reached. The new entry is added as a cold entry, except if it is the only entry.
    • evictBlock

      private void evictBlock()
    • trimNonResidentQueue

      void trimNonResidentQueue()
    • convertOldestHotToCold

      private void convertOldestHotToCold()
    • pruneStack

      private void pruneStack()
      Ensure the last entry of the stack is cold.
    • find

      CacheLongKeyLIRS.Entry<V> find(long key, int hash)
      Try to find an entry in the map.
      Parameters:
      key - the key
      hash - the hash
      Returns:
      the entry (might be a non-resident)
    • addToStack

      private void addToStack(CacheLongKeyLIRS.Entry<V> e)
    • addToStackBottom

      private void addToStackBottom(CacheLongKeyLIRS.Entry<V> e)
    • removeFromStack

      private void removeFromStack(CacheLongKeyLIRS.Entry<V> e)
      Remove the entry from the stack. The head itself must not be removed.
      Parameters:
      e - the entry
    • addToQueue

      private void addToQueue(CacheLongKeyLIRS.Entry<V> q, CacheLongKeyLIRS.Entry<V> e)
    • removeFromQueue

      private void removeFromQueue(CacheLongKeyLIRS.Entry<V> e)
    • keys

      List<Long> keys(boolean cold, boolean nonResident)
      Get the list of keys. This method allows to read the internal state of the cache.
      Parameters:
      cold - if true, only keys for the cold entries are returned
      nonResident - true for non-resident entries
      Returns:
      the key list
    • keySet

      Set<Long> keySet()
      Get the set of keys for resident entries.
      Returns:
      the set of keys
    • setMaxMemory

      void setMaxMemory(long maxMemory)
      Set the maximum memory this cache should use. This will not immediately cause entries to get removed however; it will only change the limit. To resize the internal array, call the clear method.
      Parameters:
      maxMemory - the maximum size (1 or larger) in bytes