KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > controller > cache > result > ResultCacheColumnUnique


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Copyright (C) 2005 AmicoSoft, Inc. dba Emic Networks
6  * Contact: sequoia@continuent.org
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  * Initial developer(s): Emmanuel Cecchet.
21  * Contributor(s): Nicolas Modrzyk.
22  */

23
24 package org.continuent.sequoia.controller.cache.result;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.Iterator JavaDoc;
29
30 import org.continuent.sequoia.controller.backend.result.ControllerResultSet;
31 import org.continuent.sequoia.controller.cache.result.entries.AbstractResultCacheEntry;
32 import org.continuent.sequoia.controller.cache.result.schema.CacheDatabaseColumn;
33 import org.continuent.sequoia.controller.cache.result.schema.CacheDatabaseTable;
34 import org.continuent.sequoia.controller.requests.AbstractRequest;
35 import org.continuent.sequoia.controller.requests.DeleteRequest;
36 import org.continuent.sequoia.controller.requests.InsertRequest;
37 import org.continuent.sequoia.controller.requests.ParsingGranularities;
38 import org.continuent.sequoia.controller.requests.RequestType;
39 import org.continuent.sequoia.controller.requests.SelectRequest;
40 import org.continuent.sequoia.controller.requests.UpdateRequest;
41 import org.continuent.sequoia.controller.sql.schema.TableColumn;
42
43 /**
44  * This is a query cache implementation with a column unique granularity:
45  * <ul>
46  * <li><code>COLUMN_UNIQUE</code>: same as <code>COLUMN</code> except that
47  * <code>UNIQUE</code> queries that selects a single row based on a key are
48  * invalidated only when needed.
49  * </ul>
50  *
51  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
52  * @author <a HREF="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
53  * @version 1.0
54  * @deprecated Column parsing and caching is no more supported
55  */

56 public class ResultCacheColumnUnique extends ResultCache
57 {
58
59   /**
60    * Builds a new ResultCache with a column unique granularity.
61    *
62    * @param maxEntries maximum number of entries
63    * @param pendingTimeout pending timeout for concurrent queries
64    */

65   public ResultCacheColumnUnique(int maxEntries, int pendingTimeout)
66   {
67     super(maxEntries, pendingTimeout);
68     parsingGranularity = ParsingGranularities.COLUMN_UNIQUE;
69   }
70
71   /**
72    * @see org.continuent.sequoia.controller.cache.result.ResultCache#processAddToCache(AbstractResultCacheEntry)
73    */

74   public void processAddToCache(AbstractResultCacheEntry qe)
75   {
76     SelectRequest request = qe.getRequest();
77     ArrayList JavaDoc selectedColumns = request.getSelect();
78     // Update the tables columns dependencies
79
Collection JavaDoc from = request.getFrom();
80     if (from == null)
81       return;
82     if (selectedColumns == null || selectedColumns.isEmpty())
83     {
84       logger
85           .warn("No parsing of select clause found - Fallback to table granularity");
86       for (Iterator JavaDoc i = from.iterator(); i.hasNext();)
87       {
88         CacheDatabaseTable table = cdbs.getTable((String JavaDoc) i.next());
89         table.addCacheEntry(qe);
90         // Add all columns, entries will be added below.
91
ArrayList JavaDoc columns = table.getColumns();
92         for (int j = 0; j < columns.size(); j++)
93         {
94           ((CacheDatabaseColumn) columns.get(j)).addCacheEntry(qe);
95         }
96         return;
97       }
98     }
99     for (Iterator JavaDoc i = request.getSelect().iterator(); i.hasNext();)
100     {
101       TableColumn tc = (TableColumn) i.next();
102       cdbs.getTable(tc.getTableName()).getColumn(tc.getColumnName())
103           .addCacheEntry(qe);
104     }
105     if (request.getWhere() != null)
106     { // Add all columns dependencies
107
for (Iterator JavaDoc i = request.getWhere().iterator(); i.hasNext();)
108       {
109         TableColumn tc = (TableColumn) i.next();
110         cdbs.getTable(tc.getTableName()).getColumn(tc.getColumnName())
111             .addCacheEntry(qe);
112       }
113       if (request.getCacheAbility() == RequestType.UNIQUE_CACHEABLE)
114       { // Add a specific entry for this pk
115
String JavaDoc tableName = (String JavaDoc) from.iterator().next();
116         AbstractResultCacheEntry entry = cdbs.getTable(tableName)
117             .getPkResultCacheEntry(request.getPkValue());
118         if (entry != null)
119         {
120           if (entry.isValid())
121           { // Do not add an entry which has a lower selection than the current
122
// one
123
if (entry.getRequest().getSelect().size() >= request.getSelect()
124                 .size())
125               return;
126           }
127         }
128         cdbs.getTable(tableName).addPkCacheEntry(request.getPkValue(), qe);
129       }
130     }
131   }
132
133   /**
134    * @see org.continuent.sequoia.controller.cache.result.AbstractResultCache#isUpdateNecessary(org.continuent.sequoia.controller.requests.UpdateRequest)
135    */

136   public boolean isUpdateNecessary(UpdateRequest request)
137   {
138     if (request.getCacheAbility() != RequestType.UNIQUE_CACHEABLE)
139       return true;
140     CacheDatabaseTable cacheTable = cdbs.getTable(request.getTableName());
141     if (request.getColumns() == null)
142       return true;
143     String JavaDoc pk = request.getPk();
144     AbstractResultCacheEntry qce = cacheTable.getPkResultCacheEntry(pk);
145     if (qce != null)
146     {
147       if (!qce.isValid())
148         return true;
149       ControllerResultSet rs = qce.getResult();
150       if (rs == null)
151         return true;
152       else
153         return needInvalidate(rs, request)[1];
154     }
155     else
156       return true;
157   }
158
159   /**
160    * @see org.continuent.sequoia.controller.cache.result.ResultCache#processWriteNotify(AbstractRequest)
161    */

162   protected void processWriteNotify(AbstractRequest request)
163   {
164     for (Iterator JavaDoc iter = request.getSemantic().getWriteSet().iterator(); iter
165         .hasNext();)
166     {
167       String JavaDoc tableName = (String JavaDoc) iter.next();
168       CacheDatabaseTable cacheTable = cdbs.getTable(tableName);
169       if (request.isInsert())
170       {
171         InsertRequest insert = (InsertRequest) request;
172         if (insert.getColumns() == null)
173         {
174           logger
175               .warn("No column parsing found - Fallback to table granularity ("
176                   + request.getUniqueKey() + ")");
177           cacheTable.invalidateAll();
178           return;
179         }
180         for (Iterator JavaDoc i = insert.getColumns().iterator(); i.hasNext();)
181         {
182           TableColumn tc = (TableColumn) i.next();
183           cdbs.getTable(tc.getTableName()).getColumn(tc.getColumnName())
184               .invalidateAllNonUnique();
185         }
186       }
187       else
188       {
189         if (request.getCacheAbility() == RequestType.UNIQUE_CACHEABLE)
190         {
191           if (request.isUpdate())
192           {
193             String JavaDoc pk = ((UpdateRequest) request).getPk();
194             AbstractResultCacheEntry qce = cacheTable.getPkResultCacheEntry(pk);
195             if (qce != null)
196             {
197               boolean[] invalidate = needInvalidate(qce.getResult(),
198                   (UpdateRequest) request);
199               if (invalidate[0])
200               { // We must invalidate this entry
201
cacheTable.removePkResultCacheEntry(pk);
202                 return;
203               }
204               else
205               {
206                 if (logger.isDebugEnabled())
207                   logger.debug("No invalidate needed for request:"
208                       + request.getSqlShortForm(20));
209                 return; // We don't need to invalidate
210
}
211             }
212           }
213           else if (request.isDelete())
214           { // Invalidate the corresponding cache entry
215
cacheTable.removePkResultCacheEntry(((DeleteRequest) request)
216                 .getPk());
217             return;
218           }
219         }
220         // At this point this is a non unique write query or a request
221
// we didn't handle properly (unknown request for example)
222
cacheTable.invalidateAll(); // Pk are associated to tables
223
}
224     }
225   }
226
227   /**
228    * @see org.continuent.sequoia.controller.cache.result.ResultCache#getName()
229    */

230   public String JavaDoc getName()
231   {
232     return "columnUnique";
233   }
234
235 }
Popular Tags