com.syncsoft.plainsql
Class StmtCache

java.lang.Object
  extended by com.syncsoft.plainsql.StmtCache

public class StmtCache
extends java.lang.Object

This class creates an object which maintains a cache of recently executed statements. When a statement is executed by other methods in this package, the statement is stored here and retreived when subsequently executed. Statements age out of the cache on an LRU basis, although the insertion point need not be at the top of the LRU list - probably works a bit better if you insert statemnets in the middle of the list (so that a storm of one-off statements do not totally empty the cache)


Constructor Summary
StmtCache(int cachesize, int insertionPoint)
          Initialize the Statment cache.
 
Method Summary
 void add(PlainSql sqlStmt)
          Add the specified SQL statement to the cache
 void display()
          List out the contents of the cache and the cache hit rates
 PlainSql get(java.lang.String sql)
          Get the cached statement corresponding to the nominated SQL You should call "isCached" first to make sure that the statement is actuall in there.
 int getCachesize()
          Returns the size of the cache: the maximum number of Sqls which can be cached.
 int getElements()
          Returns the number of SQLs in the cache.
 int getHits()
          Returns the number of times a statement was found in the cache
 int getInsertionPoint()
          Returns the insertion point - spot on the LRU chain where new entries will be added.
 int getMisses()
          Returns the number of times a statement was sought but not found
 java.util.HashMap<java.lang.String,PlainSql> getStatementCache()
          Return the statement cache object
 java.util.ArrayList<java.lang.String> getStatementLRU()
          Returns the LRU list
 boolean isCached(java.lang.String sql)
          Return TRUE if we have the SQL in the cache
 int position(java.lang.String sql)
          Returns the position in the LRU chain of the specified SQL
 void setInsertionPoint(int insertionPoint)
          Set the insertion point.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StmtCache

public StmtCache(int cachesize,
                 int insertionPoint)
Initialize the Statment cache.

Parameters:
cachesize - Size of the cache (# of statements)
insertionPoint - Where to put new statements (half way is probably good)
Method Detail

add

public void add(PlainSql sqlStmt)
Add the specified SQL statement to the cache

Parameters:
sqlStmt - : A PlainSql object

display

public void display()
List out the contents of the cache and the cache hit rates


get

public PlainSql get(java.lang.String sql)
Get the cached statement corresponding to the nominated SQL You should call "isCached" first to make sure that the statement is actuall in there.

Parameters:
sql - text of the Sql statement
Returns:
PlainSql object

isCached

public boolean isCached(java.lang.String sql)
Return TRUE if we have the SQL in the cache

Parameters:
sql -
Returns:
True if cached, False if not

position

public int position(java.lang.String sql)
Returns the position in the LRU chain of the specified SQL

Parameters:
sql -
Returns:
the position in the LRU list (0=MRU end of the list)

getInsertionPoint

public int getInsertionPoint()
Returns the insertion point - spot on the LRU chain where new entries will be added.

Returns:
the insertion point

setInsertionPoint

public void setInsertionPoint(int insertionPoint)
Set the insertion point. This is the index in the LRU chain where a new statement is first created. Adding statements mid-way in the chain prevents a flood of one-off statements from wiping out the top end of the cache.

Parameters:
insertionPoint -

getCachesize

public int getCachesize()
Returns the size of the cache: the maximum number of Sqls which can be cached.

Returns:
the maximum number of Sqls that can be stored in the cache

getElements

public int getElements()
Returns the number of SQLs in the cache.

Returns:
Number of elements currently in the cache.

getHits

public int getHits()
Returns the number of times a statement was found in the cache

Returns:
Hit count

getMisses

public int getMisses()
Returns the number of times a statement was sought but not found

Returns:
number of misses

getStatementCache

public java.util.HashMap<java.lang.String,PlainSql> getStatementCache()
Return the statement cache object

Returns:
HashMap: key is SQL statement, value is PlainSql object

getStatementLRU

public java.util.ArrayList<java.lang.String> getStatementLRU()
Returns the LRU list

Returns:
ArrayList: index is position in LRU; value is SQL text