View Javadoc

1   /*
2    * Copyright (c) 2004-2005, University Health Network.  All rights reserved. Distributed under the BSD 
3    * license (see http://opensource.org/licenses/bsd-license.php).
4    *  
5    * Created on 18-Jan-2005
6    */
7   package ca.uhn.cache.util;
8   
9   import java.util.Date;
10  
11  import EDU.oswego.cs.dl.util.concurrent.Executor;
12  import ca.uhn.cache.IQuery;
13  import ca.uhn.cache.IQueryResult;
14  import ca.uhn.cache.ISemanticCache;
15  import ca.uhn.cache.exception.CacheException;
16  
17  /***
18   * An extension of QueryProcessor to handle unstationary data.  
19   * See IUnstationaryDataSource for discussion.  
20   * 
21   * @author <a href="mailto:bryan.tripp@uhn.on.ca">Bryan Tripp</a>
22   * @version $Revision: 1.1 $ updated on $Date: 2005/01/24 22:51:49 $ by $Author: bryan_tripp $
23   */
24  public class UnstationaryQueryProcessor extends QueryProcessor {
25  
26      private ISemanticCache myCache;
27      private IQuery myQuery;
28      
29      /***
30       * @param theQuery the query to process
31       * @param theCache the cache against which to process it 
32       * @param theMaxGroups maximum number of remainder queries
33       * @param theExecutor handler of concurrent tasks (defaults to DirectExecutor if null) 
34       * 
35       * @throws CacheException if there is a problem finding the query remainder
36       */
37      public UnstationaryQueryProcessor(IQuery theQuery, ISemanticCache theCache,
38              int theMaxGroups, Executor theExecutor) throws CacheException {
39          super(theQuery, theCache, theMaxGroups, theExecutor);
40          
41          myCache = theCache;
42          myQuery = theQuery;
43      }
44      
45      /***
46       * Does nothing (over-rides default of starting cache query ... this is deferred until 
47       * setUpdate() is called). 
48       */
49      protected void init() {
50      }
51      
52      /***
53       * @return the date since which updates are needed from the source system, for cached regions, 
54       *      in order to ensure cache consistency.  Should be used in a source system query (for 
55       *      example against an IUnstationaryDataSource) and the results should be passed to setUpdate(...).  
56       * @throws CacheException if unable to perform 
57       */
58      public Date getUpdateBoundary() throws CacheException {
59          return myCache.getEarliestCacheTime(myQuery);
60      }
61      
62      /***
63       * Must be called before getCombinedData().  The cache is not queried until it is updated 
64       * via this call.  
65       * 
66       * @param theUpdate results of an update query against the original source data.  The update query  
67       *      has the same scope as theQuery, except that it is limited to data that have been updated
68       *      since corresponding cached data were cached.  It is used to make cached regions current before 
69       *      performing the main query.  An IUnstationaryDataSource can be used as a source.  This is 
70       *      essential only for unstationary data, i.e. data that may move between semantic regions.   
71       *      Unstationary data, if not updated, may result in records missing from getCombinedResult().  
72       * @throws CacheException if unable to perform 
73       */
74      public void setUpdate(IQueryResult theUpdate) throws CacheException {
75          myCache.update(theUpdate);
76          
77          final QueryProcessor processor = this;
78          
79          Runnable getter = new Runnable() {
80              public void run() {
81                  try {
82                      processor.setCombinedResult(myCache.get(myQuery));
83                  } catch (CacheException e) {
84                      processor.declareException("Problem getting data from cache", e);
85                  }
86              }
87          };
88          
89          thread(getter);        
90      }
91  
92  }