uk.ac.ic.doc.automed.qproc.cache
Class DefaultQueryCache

java.lang.Object
  extended by uk.ac.ic.doc.automed.qproc.cache.DefaultQueryCache
All Implemented Interfaces:
java.util.Iterator, IQueryCache, Cacheable, Persistence, QueryRewritable
Direct Known Subclasses:
QueryCache

public class DefaultQueryCache
extends java.lang.Object
implements IQueryCache, Persistence, java.util.Iterator


Constructor Summary
protected DefaultQueryCache(long ageTime)
          Creates a new instance of DefaultQueryCache (singleton)
 
Method Summary
 void addListener(CacheListener cacheListener)
          Register a listener for handling cache-related events
 java.lang.String addQuery(java.lang.String query, java.lang.Object metadata)
          Store reference to the AbstractQueryProcessor object that processes this query
so that we can in a later stage use the query analysis with query result
to build a semantic cache entry for the query
.
 void addQueryResult(java.lang.String queryID, java.lang.Object resultObj)
          Add a new query to cache.
 int addSubQueryResult(java.lang.String queryID, java.lang.String subQuery, java.lang.Object resultObj)
          Add result of a sub-query of a query in the cache.
 void clear()
          Clear the cache
 void deserialise()
          Read content from disk (file or database)
 java.util.List getCachedQueries()
          Return external queries of all cached items
protected  CacheContentDescription getContent()
          Implements interface Cacheable.
 CacheContentDescription getContentObject()
          Return the cache data bag
 ASG getExactQuery(java.lang.String query)
          Check in the cache for an exact query match.
 ASG getExactSubQuery(java.lang.String subQuery)
          Check sub-query existence in the whole cache.
 ASG getExactSubQuery(java.lang.String queryID, java.lang.String subQuery)
          Check if result of the sub-query of a query already exists in the cache.
protected static DefaultQueryCache getInstance(long ageTime)
          A singleton method to make sure that there is only one instance of query cache per peer
 AbstractObjectBinding getObjectBinding()
          Return an instance of object binding for serialisation
 PersistentHandler getPersistentHandler()
           
 java.util.List getQuery(ASG view)
           
 ASG[] getQuery(java.lang.String query)
          Check in the cache for a similar query match.
 java.lang.Object[] getQueryMetadataObjects()
          Return an array of metadata objects associated to the query objects
 boolean hasNext()
          Implements mandatory methods of the ListIterator interface
 boolean isEmpty()
           
 java.lang.Object next()
          Implements mandatory methods of the ListIterator interface Returns the metadata object of the next cache item
 void notifyListeners(Cacheable cacheObj)
          Notify listeners to handle a specific event
 void refresh()
          refresh cache
 void remove()
          Implements mandatory methods of the ListIterator interface
 QueryRewriterResult rewrite(ASG query, Schema querySchema, int type)
          Rewrite a query associated to a schema s using the local query cache
 void serialise(java.lang.Object obj)
          Save/synch a cache item object to disk (or database).
 void setPersistentHandler(PersistentHandler ph)
          Implement this method for serialising cache content
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DefaultQueryCache

protected DefaultQueryCache(long ageTime)
Creates a new instance of DefaultQueryCache (singleton)

Method Detail

getInstance

protected static DefaultQueryCache getInstance(long ageTime)
A singleton method to make sure that there is only one instance of query cache per peer


addQueryResult

public void addQueryResult(java.lang.String queryID,
                           java.lang.Object resultObj)
                    throws CacheIntegrityException
Add a new query to cache. The query is used as the key and result object
is the value. Often a query was pre-processed in query rewriting phase by an
instance of AbstractQueryProcessor whose result was added to this cache. Query result is often retrieved and later added to the cache at the execution phase
Hence, what we are doing here is to find the cache-item corresponding to the query and update it with

Specified by:
addQueryResult in interface IQueryCache
Parameters:
queryID - the unique query identifier (generated by #addQuery(String query, String fQuery, String sQuery, String iQuery, Object metadata, Object qproc) method
resultObj - the result object from query execution
Throws:
CacheIntegrityException

addSubQueryResult

public int addSubQueryResult(java.lang.String queryID,
                             java.lang.String subQuery,
                             java.lang.Object resultObj)
                      throws CacheIntegrityException
Add result of a sub-query of a query in the cache. A query might have one or more intermediate results each of which is added as a sub_result item (List object) of the query cache item.

Each query's sub result is represented as a two-dimensional array as follows:
Object[] subResult = new Object[] {subQuery, subQueryResult}

Specified by:
addSubQueryResult in interface IQueryCache
Parameters:
queryID - the id of the parent query
subQuery - sub-query string
resultObj - sub-query result object
Returns:
int the sub-result id of the query identified by queryID
Throws:
CacheIntegrityException

addQuery

public java.lang.String addQuery(java.lang.String query,
                                 java.lang.Object metadata)
                          throws CacheIntegrityException
Store reference to the AbstractQueryProcessor object that processes this query
so that we can in a later stage use the query analysis with query result
to build a semantic cache entry for the query
.
A query processor object is constructed for each query in query rewriting phase carried out by the query mediator component.

Specified by:
addQuery in interface IQueryCache
Parameters:
query - the original (external) IQL query posed by user
fQuery - the formulated query
sQuery - the executional representation of the query
iQuery - the internal representation of the query. This is the primary query id.
metadata - the metadata object of the query (e.g. QueryAnalyser)
qproc - the AbstractQueryProcessor object
Returns:
String the unique query identifier
Throws:
CacheIntegrityException

getExactQuery

public ASG getExactQuery(java.lang.String query)
Check in the cache for an exact query match. Note that this method operates on the internal IQL representation of the query which must only result in at most one matched entry. In any case, this method returns an array of the possible query results as ASGs.

Specified by:
getExactQuery in interface IQueryCache
Parameters:
query - the query string must be in internal format
Returns:
ASG[] array of possible query results found in the cache

getExactSubQuery

public ASG getExactSubQuery(java.lang.String queryID,
                            java.lang.String subQuery)
                     throws CacheIntegrityException
Check if result of the sub-query of a query already exists in the cache. This method only checks sub-query existence among sub-queries of a given query instead of all queries in the cache

Specified by:
getExactSubQuery in interface IQueryCache
Parameters:
queryID - the ID of query to which the sub-query is associated
subQuery - sub-query to search for
Returns:
ASG sub-query result (or null if sub-query is not found
Throws:
CacheIntegrityException

getExactSubQuery

public ASG getExactSubQuery(java.lang.String subQuery)
Check sub-query existence in the whole cache. This method is preferred to throws CacheIntegrityException

Specified by:
getExactSubQuery in interface IQueryCache
Parameters:
subQuery - sub-query to search for
Returns:
ASG sub-query result (or null if sub-query is not found

getQuery

public ASG[] getQuery(java.lang.String query)
Check in the cache for a similar query match. Note that this method operates on the external IQL representation of the query which could result in more than one exact matches in the cache. In any case, this method returns an array of the possible query results as ASGs.

Specified by:
getQuery in interface IQueryCache
Parameters:
query - the query string must be in external format
Returns:
ASG[] array of possible query results found in the cache

getQuery

public java.util.List getQuery(ASG view)

getCachedQueries

public java.util.List getCachedQueries()
Return external queries of all cached items

Specified by:
getCachedQueries in interface IQueryCache
Returns:
List a list of all metadata objects in the cache OR null
if no queries are found

getQueryMetadataObjects

public java.lang.Object[] getQueryMetadataObjects()
Return an array of metadata objects associated to the query objects

Specified by:
getQueryMetadataObjects in interface IQueryCache

getContent

protected CacheContentDescription getContent()
Implements interface Cacheable. Returns a List object of cache content. Each item in the list is
a HashMap object containing id, query, result and timestamp values
of each cached query.


getContentObject

public CacheContentDescription getContentObject()
Return the cache data bag

Specified by:
getContentObject in interface Cacheable

setPersistentHandler

public void setPersistentHandler(PersistentHandler ph)
Description copied from interface: Persistence
Implement this method for serialising cache content

Specified by:
setPersistentHandler in interface Persistence

getPersistentHandler

public PersistentHandler getPersistentHandler()

serialise

public void serialise(java.lang.Object obj)
Save/synch a cache item object to disk (or database).

Specified by:
serialise in interface Persistence

deserialise

public void deserialise()
Read content from disk (file or database)

Specified by:
deserialise in interface Persistence

getObjectBinding

public AbstractObjectBinding getObjectBinding()
Description copied from interface: Persistence
Return an instance of object binding for serialisation

Specified by:
getObjectBinding in interface Persistence

clear

public void clear()
Clear the cache

Specified by:
clear in interface Cacheable

refresh

public void refresh()
refresh cache

Specified by:
refresh in interface Cacheable

addListener

public void addListener(CacheListener cacheListener)
Register a listener for handling cache-related events

Specified by:
addListener in interface Cacheable
Parameters:
cacheListener - a CacheListener object to add

notifyListeners

public void notifyListeners(Cacheable cacheObj)
Notify listeners to handle a specific event

Specified by:
notifyListeners in interface Cacheable
Parameters:
cacheObj - the Cacheable object to be used by listener to handle event

hasNext

public boolean hasNext()
Implements mandatory methods of the ListIterator interface

Specified by:
hasNext in interface java.util.Iterator

next

public java.lang.Object next()
Implements mandatory methods of the ListIterator interface Returns the metadata object of the next cache item

Specified by:
next in interface java.util.Iterator

remove

public void remove()
Implements mandatory methods of the ListIterator interface

Specified by:
remove in interface java.util.Iterator

rewrite

public QueryRewriterResult rewrite(ASG query,
                                   Schema querySchema,
                                   int type)
                            throws QueryRewriterException
Rewrite a query associated to a schema s using the local query cache

Specified by:
rewrite in interface QueryRewritable
Parameters:
query - the ASG query
querySchema - the source schema over which query is executed
QueryAnalyser[] - an array of QueryAnalyser objects of the cached views
type - specifies the type of query rewriting algorithm used (QueryRewriteProcessor for list of types)
Returns:
QueryRewriterResult a result object that can be used to manipulate the rewriting results
Throws:
QueryRewriterException

isEmpty

public boolean isEmpty()