KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > controller > cache > result > ResultCacheColumn


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2005 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Emmanuel Cecchet.
22  * Contributor(s): Nicolas Modrzyk.
23  */

24
25 package org.objectweb.cjdbc.controller.cache.result;
26
27 import java.util.ArrayList JavaDoc;
28 import java.util.Iterator JavaDoc;
29
30 import org.objectweb.cjdbc.common.sql.AbstractWriteRequest;
31 import org.objectweb.cjdbc.common.sql.ParsingGranularities;
32 import org.objectweb.cjdbc.common.sql.SelectRequest;
33 import org.objectweb.cjdbc.common.sql.UpdateRequest;
34 import org.objectweb.cjdbc.common.sql.schema.TableColumn;
35 import org.objectweb.cjdbc.controller.cache.result.entries.AbstractResultCacheEntry;
36 import org.objectweb.cjdbc.controller.cache.result.schema.CacheDatabaseColumn;
37 import org.objectweb.cjdbc.controller.cache.result.schema.CacheDatabaseTable;
38
39 /**
40  * This is a query cache implementation with a column granularity:
41  * <ul>
42  * <li><code>COLUMN</code>: column granularity, entries in the cache are
43  * invalidated based on column dependencies</li>
44  * </ul>
45  *
46  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
47  * @author <a HREF="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
48  * @version 1.0
49  */

50
51 public class ResultCacheColumn extends ResultCache
52 {
53   /**
54    * Builds a new ResultCache with a Column granularity.
55    *
56    * @param maxEntries maximum number of entries
57    * @param pendingTimeout pending timeout for concurrent queries
58    */

59   public ResultCacheColumn(int maxEntries, int pendingTimeout)
60   {
61     super(maxEntries, pendingTimeout);
62     parsingGranularity = ParsingGranularities.COLUMN;
63   }
64
65   /**
66    * @see org.objectweb.cjdbc.controller.cache.result.ResultCache#processAddToCache(AbstractResultCacheEntry)
67    */

68   public void processAddToCache(AbstractResultCacheEntry qe)
69   {
70     SelectRequest request = qe.getRequest();
71     ArrayList JavaDoc selectedColumns = request.getSelect();
72     // Update the tables columns dependencies
73
if (selectedColumns == null || selectedColumns.isEmpty())
74     {
75       logger
76           .warn("No parsing of select clause found - Fallback to table granularity");
77       for (Iterator JavaDoc i = request.getFrom().iterator(); i.hasNext();)
78       {
79         CacheDatabaseTable table = cdbs.getTable((String JavaDoc) i.next());
80         table.addCacheEntry(qe);
81         // Add all columns, entries will be added below.
82
ArrayList JavaDoc columns = table.getColumns();
83         for (int j = 0; j < columns.size(); j++)
84         {
85           ((CacheDatabaseColumn) columns.get(j)).addCacheEntry(qe);
86         }
87         return;
88       }
89     }
90     for (Iterator JavaDoc i = selectedColumns.iterator(); i.hasNext();)
91     {
92       TableColumn tc = (TableColumn) i.next();
93       cdbs.getTable(tc.getTableName()).getColumn(tc.getColumnName())
94           .addCacheEntry(qe);
95     }
96     if (request.getWhere() != null)
97       for (Iterator JavaDoc i = request.getWhere().iterator(); i.hasNext();)
98       {
99         TableColumn tc = (TableColumn) i.next();
100         cdbs.getTable(tc.getTableName()).getColumn(tc.getColumnName())
101             .addCacheEntry(qe);
102       }
103   }
104
105   /**
106    * @see org.objectweb.cjdbc.controller.cache.result.AbstractResultCache#isUpdateNecessary(org.objectweb.cjdbc.common.sql.UpdateRequest)
107    */

108   public boolean isUpdateNecessary(UpdateRequest request)
109   {
110     return true;
111   }
112
113   /**
114    * @see org.objectweb.cjdbc.controller.cache.result.ResultCache#processWriteNotify(org.objectweb.cjdbc.common.sql.AbstractWriteRequest)
115    */

116   protected void processWriteNotify(AbstractWriteRequest request)
117   {
118     // Sanity check
119
if (request.getColumns() == null)
120     {
121       logger.warn("No column parsing found - Fallback to table granularity ("
122           + request.getSQL() + ")");
123       cdbs.getTable(request.getTableName()).invalidateAll();
124       return;
125     }
126     if (request.isAlter())
127     {
128       cdbs.getTable(request.getTableName()).invalidateAll();
129       return;
130     }
131     for (Iterator JavaDoc i = request.getColumns().iterator(); i.hasNext();)
132     {
133       TableColumn tc = (TableColumn) i.next();
134       cdbs.getTable(tc.getTableName()).getColumn(tc.getColumnName())
135           .invalidateAll();
136     }
137   }
138
139   /**
140    * @see org.objectweb.cjdbc.controller.cache.result.ResultCache#getName()
141    */

142   public String JavaDoc getName()
143   {
144     return "column";
145   }
146
147 }
Popular Tags