Class LRUCache<P extends Page>

  • Type Parameters:
    P - Page type
    All Implemented Interfaces:
    PageFile<P>

    public class LRUCache<P extends Page>
    extends AbstractPageFile<P>
    An LRU cache, based on LinkedHashMap.
    This cache has a fixed maximum number of objects (cacheSize). If the cache is full and another object is added, the LRU (least recently used) object is dropped.
    Since:
    0.1
    Author:
    Elke Achtert
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected int cacheSize
      The maximum number of objects in this cache.
      protected int cacheSizeBytes
      Cache size in bytes.
      protected PageFile<P> file
      The underlying file of this cache.
      private static Logging LOG
      Our class logger.
      private java.util.LinkedHashMap<java.lang.Integer,​P> map
      The map holding the objects of this cache.
    • Constructor Summary

      Constructors 
      Constructor Description
      LRUCache​(int cacheSizeBytes, PageFile<P> file)
      Initializes this cache with the specified parameters.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()
      Clears this cache.
      void close()
      Closes this file.
      void deletePage​(int pageID)
      Deletes the node with the specified id from this file.
      protected void expirePage​(P page)
      Write page through to disk.
      void flush()
      Flushes this caches by writing any entry to the underlying file.
      protected Logging getLogger()
      Get the class logger.
      int getNextPageID()
      Returns the next page id.
      int getPageSize()
      Get the page size of this page file.
      boolean initialize​(PageHeader header)
      Initialize the page file with the given header - return "true" if the file already existed.
      void logStatistics()
      Log some statistics to the appropriate logger.
      P readPage​(int pageID)
      Retrieves a page from the cache.
      void setCacheSize​(int cacheSize)
      Sets the maximum size of this cache.
      void setNextPageID​(int nextPageID)
      Sets the next page id.
      int setPageID​(P page)
      Sets the id of the given page.
      java.lang.String toString()
      Returns a string representation of this cache.
      void writePage​(int pageID, P page)
      Perform the actual page write operation.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • LOG

        private static final Logging LOG
        Our class logger.
      • cacheSizeBytes

        protected int cacheSizeBytes
        Cache size in bytes.
      • cacheSize

        protected int cacheSize
        The maximum number of objects in this cache.
      • map

        private java.util.LinkedHashMap<java.lang.Integer,​P extends Page> map
        The map holding the objects of this cache.
      • file

        protected PageFile<P extends Page> file
        The underlying file of this cache. If an object is dropped it is written to the file.
    • Constructor Detail

      • LRUCache

        public LRUCache​(int cacheSizeBytes,
                        PageFile<P> file)
        Initializes this cache with the specified parameters.
        Parameters:
        cacheSizeBytes - the maximum number of bytes for this cache
        file - the underlying file of this cache, if a page is dropped it is written to the file
    • Method Detail

      • readPage

        public P readPage​(int pageID)
        Retrieves a page from the cache. The retrieved page becomes the MRU (most recently used) page.
        Parameters:
        pageID - the id of the page to be returned
        Returns:
        the page associated to the id or null if no value with this key exists in the cache
      • writePage

        public void writePage​(int pageID,
                              P page)
        Description copied from class: AbstractPageFile
        Perform the actual page write operation.
        Specified by:
        writePage in class AbstractPageFile<P extends Page>
        Parameters:
        pageID - Page id
        page - Page to write
      • deletePage

        public void deletePage​(int pageID)
        Description copied from interface: PageFile
        Deletes the node with the specified id from this file.
        Parameters:
        pageID - the id of the node to be deleted
      • expirePage

        protected void expirePage​(P page)
        Write page through to disk.
        Parameters:
        page - page
      • setPageID

        public int setPageID​(P page)
        Description copied from interface: PageFile
        Sets the id of the given page.
        Parameters:
        page - the page to set the id
        Returns:
        the page id
      • getNextPageID

        public int getNextPageID()
        Description copied from interface: PageFile
        Returns the next page id.
        Returns:
        the next page id
      • setNextPageID

        public void setNextPageID​(int nextPageID)
        Description copied from interface: PageFile
        Sets the next page id.
        Parameters:
        nextPageID - the next page id to be set
      • getPageSize

        public int getPageSize()
        Description copied from interface: PageFile
        Get the page size of this page file.
        Returns:
        page size
      • initialize

        public boolean initialize​(PageHeader header)
        Description copied from interface: PageFile
        Initialize the page file with the given header - return "true" if the file already existed.
        Parameters:
        header - Header
        Returns:
        true when the file already existed.
      • flush

        public void flush()
        Flushes this caches by writing any entry to the underlying file.
      • toString

        public java.lang.String toString()
        Returns a string representation of this cache.
        Overrides:
        toString in class java.lang.Object
        Returns:
        a string representation of this cache
      • clear

        public void clear()
        Clears this cache.
      • setCacheSize

        public void setCacheSize​(int cacheSize)
        Sets the maximum size of this cache.
        Parameters:
        cacheSize - the cache size to be set