de.lmu.ifi.dbs.elki.persistent
Class LRUCache<P extends Page>

java.lang.Object
  extended by de.lmu.ifi.dbs.elki.persistent.AbstractPageFile<P>
      extended by de.lmu.ifi.dbs.elki.persistent.LRUCache<P>
Type Parameters:
P - Page type
All Implemented Interfaces:
PageFile<P>, PageFileStatistics

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.


Field Summary
protected  long cacheSize
          The maximum number of objects in this cache.
protected  long cacheSizeBytes
          Cache size in bytes.
protected  PageFile<P> file
          The underlying file of this cache.
private static Logging logger
          Our logger
private  LinkedHashMap<Integer,P> map
          The map holding the objects of this cache.
 
Fields inherited from class de.lmu.ifi.dbs.elki.persistent.AbstractPageFile
readAccess, writeAccess
 
Constructor Summary
LRUCache(long cacheSizeBytes, PageFile<P> file)
          Initializes this cache with the specified parameters.
 
Method Summary
 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.
 PageFileStatistics getInnerStatistics()
          Get statistics for the inner page file, if present.
 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.
 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.
 Integer setPageID(P page)
          Sets the id of the given page.
 String toString()
          Returns a string representation of this cache.
 void writePage(Integer pageID, P page)
          Perform the actual page write operation.
 
Methods inherited from class de.lmu.ifi.dbs.elki.persistent.AbstractPageFile
getReadOperations, getWriteOperations, resetPageAccess, writePage
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

logger

private static final Logging logger
Our logger


cacheSizeBytes

protected long cacheSizeBytes
Cache size in bytes.


cacheSize

protected long cacheSize
The maximum number of objects in this cache.


map

private LinkedHashMap<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(long cacheSizeBytes,
                PageFile<P> file)
Initializes this cache with the specified parameters.

Parameters:
cacheSizeBytes - the maximum number of pages in 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(Integer 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 Integer 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.

Specified by:
close in interface PageFile<P extends Page>
Overrides:
close in class AbstractPageFile<P extends Page>

flush

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


toString

public String toString()
Returns a string representation of this cache.

Overrides:
toString in class 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

getInnerStatistics

public PageFileStatistics getInnerStatistics()
Description copied from interface: PageFileStatistics
Get statistics for the inner page file, if present.

Returns:
Inner page file statistics, or null.

Release 0.4.0 (2011-09-20_1324)