Package elki.persistent
Class LRUCache<P extends Page>
- java.lang.Object
-
- elki.persistent.AbstractPageFile<P>
-
- elki.persistent.LRUCache<P>
-
- Type Parameters:
P- Page type
- All Implemented Interfaces:
PageFile<P>
public class LRUCache<P extends Page> extends AbstractPageFile<P>
An LRU cache, based onLinkedHashMap.
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 intcacheSizeThe maximum number of objects in this cache.protected intcacheSizeBytesCache size in bytes.protected PageFile<P>fileThe underlying file of this cache.private static LoggingLOGOur class logger.private java.util.LinkedHashMap<java.lang.Integer,P>mapThe map holding the objects of this cache.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()Clears this cache.voidclose()Closes this file.voiddeletePage(int pageID)Deletes the node with the specified id from this file.protected voidexpirePage(P page)Write page through to disk.voidflush()Flushes this caches by writing any entry to the underlying file.protected LogginggetLogger()Get the class logger.intgetNextPageID()Returns the next page id.intgetPageSize()Get the page size of this page file.booleaninitialize(PageHeader header)Initialize the page file with the given header - return "true" if the file already existed.voidlogStatistics()Log some statistics to the appropriate logger.PreadPage(int pageID)Retrieves a page from the cache.voidsetCacheSize(int cacheSize)Sets the maximum size of this cache.voidsetNextPageID(int nextPageID)Sets the next page id.intsetPageID(P page)Sets the id of the given page.java.lang.StringtoString()Returns a string representation of this cache.voidwritePage(int pageID, P page)Perform the actual page write operation.-
Methods inherited from class elki.persistent.AbstractPageFile
countRead, countWrite, writePage
-
-
-
-
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.
-
-
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:AbstractPageFilePerform the actual page write operation.- Specified by:
writePagein classAbstractPageFile<P extends Page>- Parameters:
pageID- Page idpage- Page to write
-
deletePage
public void deletePage(int pageID)
Description copied from interface:PageFileDeletes 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:PageFileSets 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:PageFileReturns the next page id.- Returns:
- the next page id
-
setNextPageID
public void setNextPageID(int nextPageID)
Description copied from interface:PageFileSets the next page id.- Parameters:
nextPageID- the next page id to be set
-
getPageSize
public int getPageSize()
Description copied from interface:PageFileGet the page size of this page file.- Returns:
- page size
-
initialize
public boolean initialize(PageHeader header)
Description copied from interface:PageFileInitialize the page file with the given header - return "true" if the file already existed.- Parameters:
header- Header- Returns:
- true when the file already existed.
-
close
public void close()
Description copied from interface:PageFileCloses this file.
-
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:
toStringin classjava.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
-
logStatistics
public void logStatistics()
Description copied from interface:PageFileLog some statistics to the appropriate logger.- Specified by:
logStatisticsin interfacePageFile<P extends Page>- Overrides:
logStatisticsin classAbstractPageFile<P extends Page>
-
getLogger
protected Logging getLogger()
Description copied from class:AbstractPageFileGet the class logger.- Specified by:
getLoggerin classAbstractPageFile<P extends Page>- Returns:
- Logger
-
-