Class CacheManager

  • All Implemented Interfaces:
    ICacheManager
    Direct Known Subclasses:
    LRUCacheManager, PlainCacheManager

    public abstract class CacheManager
    extends java.lang.Object
    implements ICacheManager
    This class implemented the cache management, involving the cache status management on per MNode and cache eviction. All the nodes in memory are still basically organized as a trie via the container and parent reference in each node. Some extra data structure is used to help manage the node cached in memory.

    The cache eviction on node is actually evicting a subtree from the MTree.

    All the cached nodes are divided into two parts by their cache status, evictable nodes and none evictable nodes.

    1. Evictable nodes are all placed in nodeCache, which prepares the nodes be selected by cache eviction.
    2. None evictable nodes takes two parts:
      1. The volatile nodes, new added or updated, which means the data has not been synced to disk.
      2. The ancestors of the volatile nodes. If a node is not volatile but not in nodeCache, it means this node is an ancestor of some volatile node. Any volatile node is contained in the CachedContainer of its parent. The parent will be placed in nodeBuffer. If the parent is volatile as well, then the parent of the parent will be placed in nodeBuffer instead, which means the root node of a maximum volatile subtree will be placed in node buffer.
    • Constructor Detail

      • CacheManager

        public CacheManager()
    • Method Detail

      • updateCacheStatusAfterDiskRead

        public void updateCacheStatusAfterDiskRead​(IMNode node)
        The node read from disk should be cached and added to nodeCache and the cache of its belonging container.
        Specified by:
        updateCacheStatusAfterDiskRead in interface ICacheManager
        Parameters:
        node -
      • updateCacheStatusAfterAppend

        public void updateCacheStatusAfterAppend​(IMNode node)
        The new appended node should be cached and the volatile subtree it belonged should be added to nodeBuffer.
        Specified by:
        updateCacheStatusAfterAppend in interface ICacheManager
        Parameters:
        node -
      • updateCacheStatusAfterUpdate

        public void updateCacheStatusAfterUpdate​(IMNode node)
        The updated node should be marked volatile and removed from nodeCache if necessary and the volatile subtree it belonged should be added to nodeBuffer.
        Specified by:
        updateCacheStatusAfterUpdate in interface ICacheManager
        Parameters:
        node -
      • updateCacheStatusAfterPersist

        public void updateCacheStatusAfterPersist​(IMNode node)
        After flush the given node's container to disk, the cache status of the nodes in container and their ancestors should be updated. All the node should be added to nodeCache and the nodeBuffer should be cleared after finishing a MTree flush task.
        Specified by:
        updateCacheStatusAfterPersist in interface ICacheManager
        Parameters:
        node -
      • collectVolatileMNodes

        public java.util.List<IMNode> collectVolatileMNodes()
        Collect nodes in all volatile subtrees. All volatile nodes' parents will be collected. The ancestor will be in front of the descendent in returned list.
        Specified by:
        collectVolatileMNodes in interface ICacheManager
        Returns:
      • evict

        public boolean evict()
        Choose an evictable node from nodeCache and evicted all the cached node in the subtree it represented.
        Specified by:
        evict in interface ICacheManager
        Returns:
        whether evicted any MNode successfully
      • pinMNode

        public void pinMNode​(IMNode node)
                      throws MNodeNotPinnedException
        Pin a node in memory, and it will not be evicted. The pin is implemented as a multi mayer lock. When a thread/task pin the node, one mayer lock will be added to the node and its parent. This help guarantees the cache assumption that if a node is cached/pinned, its parent should be cached/pinned.
        Specified by:
        pinMNode in interface ICacheManager
        Parameters:
        node -
        Throws:
        MNodeNotPinnedException
      • unPinMNode

        public boolean unPinMNode​(IMNode node)
        Unpin a node, and if the lock mayer on this node is zero, it will be evictable. Unpin a node means decrease one mayer lock from the node. If the lock mayer reaches zero, the mayer lock of its parent should decrease.
        Specified by:
        unPinMNode in interface ICacheManager
        Parameters:
        node -
        Returns:
      • initCacheEntryForNode

        protected void initCacheEntryForNode​(IMNode node)
      • updateCacheStatusAfterAccess

        protected abstract void updateCacheStatusAfterAccess​(CacheEntry cacheEntry)
      • updateCacheStatusAfterUpdate

        protected abstract void updateCacheStatusAfterUpdate​(CacheEntry cacheEntry,
                                                             IMNode node)
      • isInNodeCache

        protected abstract boolean isInNodeCache​(CacheEntry cacheEntry)
      • addToNodeCache

        protected abstract void addToNodeCache​(CacheEntry cacheEntry,
                                               IMNode node)
      • removeFromNodeCache

        protected abstract void removeFromNodeCache​(CacheEntry cacheEntry)
      • getPotentialNodeTobeEvicted

        protected abstract IMNode getPotentialNodeTobeEvicted()
      • clearNodeCache

        protected abstract void clearNodeCache()