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 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.
-
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 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:AbstractPageFile
Perform the actual page write operation.- Specified by:
writePage
in classAbstractPageFile<P extends Page>
- Parameters:
pageID
- Page idpage
- 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.
-
close
public void close()
Description copied from interface:PageFile
Closes 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:
toString
in 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:PageFile
Log some statistics to the appropriate logger.- Specified by:
logStatistics
in interfacePageFile<P extends Page>
- Overrides:
logStatistics
in classAbstractPageFile<P extends Page>
-
getLogger
protected Logging getLogger()
Description copied from class:AbstractPageFile
Get the class logger.- Specified by:
getLogger
in classAbstractPageFile<P extends Page>
- Returns:
- Logger
-
-