Environment for
DeveLoping
KDD-Applications
Supported by Index-Structures

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

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

public class LRUCache<P extends Page<P>>
extends Object
implements Cache<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.

Author:
Elke Achtert

Field Summary
protected  int cacheSize
          The maximum number of objects in this cache.
protected  CachedFile<P> file
          The underlying file of this cache.
private  LinkedHashMap<Integer,P> map
          The map holding the objects of this cache.
private  long pageAccess
          The number of read accesses
 
Constructor Summary
LRUCache()
          Creates a new empty LRU cache.
 
Method Summary
 void clear()
          Clears this cache.
 void flush()
          Flushes this caches by writing any entry to the underlying file.
 P get(int pageID)
          Retrieves a page from the cache.
 long getPageAccess()
          Returns the number of page accesses.
 void initialize(int cacheSize, CachedFile<P> file)
          Initializes this cache with the specified parameters.
 void put(P page)
          Adds a page to this cache.
 P remove(int pageID)
          Removes a page from this cache.
 void resetPageAccess()
          Resets the pages access of this cache.
 void setCacheSize(int cacheSize)
          Sets the maximum size of this cache.
 String toString()
          Returns a string representation of this cache.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

cacheSize

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


map

private LinkedHashMap<Integer,P extends Page<P>> map
The map holding the objects of this cache.


file

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


pageAccess

private long pageAccess
The number of read accesses

Constructor Detail

LRUCache

public LRUCache()
Creates a new empty LRU cache.

Method Detail

initialize

public void initialize(int cacheSize,
                       CachedFile<P> file)
Initializes this cache with the specified parameters.

Specified by:
initialize in interface Cache<P extends Page<P>>
Parameters:
cacheSize - 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

get

public P get(int pageID)
Retrieves a page from the cache. The retrieved page becomes the MRU (most recently used) page.

Specified by:
get in interface Cache<P extends Page<P>>
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

put

public void put(P page)
Adds a page to this cache. If the cache is full, the LRU (least recently used) page is dropped and written to file.

Specified by:
put in interface Cache<P extends Page<P>>
Parameters:
page - the page to be added

remove

public P remove(int pageID)
Removes a page from this cache.

Specified by:
remove in interface Cache<P extends Page<P>>
Parameters:
pageID - the number of the node to be removed.
Returns:
the removed page

flush

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

Specified by:
flush in interface Cache<P extends Page<P>>

getPageAccess

public long getPageAccess()
Returns the number of page accesses.

Specified by:
getPageAccess in interface Cache<P extends Page<P>>
Returns:
the number of page accesses

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.

Specified by:
clear in interface Cache<P extends Page<P>>

setCacheSize

public void setCacheSize(int cacheSize)
Sets the maximum size of this cache.

Specified by:
setCacheSize in interface Cache<P extends Page<P>>
Parameters:
cacheSize - the cache size to be set

resetPageAccess

public void resetPageAccess()
Resets the pages access of this cache.

Specified by:
resetPageAccess in interface Cache<P extends Page<P>>

Release 0.2.1 (2009-07-13_1605)