KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > controller > cache > result > schema > CacheDatabaseTable


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): Sara Bouchenak.
22  */

23
24 package org.continuent.sequoia.controller.cache.result.schema;
25
26 import java.sql.SQLException JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Iterator JavaDoc;
30
31 import org.continuent.sequoia.controller.cache.result.entries.AbstractResultCacheEntry;
32 import org.continuent.sequoia.controller.requests.RequestType;
33 import org.continuent.sequoia.controller.sql.schema.DatabaseColumn;
34 import org.continuent.sequoia.controller.sql.schema.DatabaseTable;
35
36 /**
37  * A <code>CacheDatabaseTable</code> represents a database table and its
38  * associated cache entries. It has an array of <code>CacheDatabaseColumn</code>
39  * objects.
40  * <p>
41  * Keep it mind that <code>ArrayList</code> and <code>HashMap</code> are not
42  * synchronized...
43  *
44  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
45  * @author <a HREF="mailto:Sara.Bouchenak@epfl.ch">Sara Bouchenak </a>
46  * @version 1.0
47  */

48 public class CacheDatabaseTable
49 {
50   private String JavaDoc name;
51   private ArrayList JavaDoc columns;
52   private ArrayList JavaDoc cacheEntries; // Cache entries depending on this table
53
private HashMap JavaDoc pkCacheEntries; // Cache entries corresponding to a pk value
54

55   /**
56    * Creates a new <code>CacheDatabaseTable</code> instance.
57    *
58    * @param databaseTable the database table to cache
59    */

60   public CacheDatabaseTable(DatabaseTable databaseTable)
61   {
62     // Clone the name and the columns
63
name = databaseTable.getName();
64     ArrayList JavaDoc origColumns = databaseTable.getColumns();
65     int size = origColumns.size();
66     columns = new ArrayList JavaDoc(size);
67     for (int i = 0; i < size; i++)
68       columns.add(new CacheDatabaseColumn(((DatabaseColumn) origColumns.get(i))
69           .getName()));
70
71     // Create an empty cache
72
cacheEntries = new ArrayList JavaDoc();
73     pkCacheEntries = new HashMap JavaDoc();
74   }
75
76   /**
77    * Gets the name of the table.
78    *
79    * @return the table name
80    */

81   public String JavaDoc getName()
82   {
83     return name;
84   }
85
86   /**
87    * Adds a <code>CacheDatabaseColumn</code> object to this table.
88    * <p>
89    * Warning! The underlying <code>ArrayList</code> is not synchronized.
90    *
91    * @param column a <code>CacheDatabaseColumn</code> value
92    */

93   public void addColumn(CacheDatabaseColumn column)
94   {
95     columns.add(column);
96   }
97
98   /**
99    * Merge the given table's columns with the current table. All missing columns
100    * are added if no conflict is detected. An exception is thrown if the given
101    * table columns conflicts with the current one.
102    *
103    * @param t the table to merge
104    * @throws SQLException if the schemas conflict
105    */

106   public void mergeColumns(CacheDatabaseTable t) throws SQLException JavaDoc
107   {
108     if (t == null)
109       return;
110
111     ArrayList JavaDoc otherColumns = t.getColumns();
112     if (otherColumns == null)
113       return;
114
115     int size = otherColumns.size();
116     for (int i = 0; i < size; i++)
117     {
118       CacheDatabaseColumn c = (CacheDatabaseColumn) otherColumns.get(i);
119       CacheDatabaseColumn original = getColumn(c.getName());
120       if (original == null)
121         addColumn(c);
122       else
123       {
124         if (!original.equals(c))
125           throw new SQLException JavaDoc("Column " + c.getName()
126               + " definition mismatch.");
127       }
128     }
129   }
130
131   /**
132    * Returns a list of <code>CacheDatabaseColumn</code> objects describing the
133    * columns of this table.
134    * <p>
135    * Warning! The underlying <code>ArrayList</code> is not synchronized.
136    *
137    * @return an <code>ArrayList</code> of <code>CacheDatabaseColumn</code>
138    */

139   public ArrayList JavaDoc getColumns()
140   {
141     return columns;
142   }
143
144   /**
145    * Returns the <code>CacheDatabaseColumn</code> object matching the given
146    * column name or <code>null</code> if not found.
147    *
148    * @param columnName column name to look for
149    * @return a <code>CacheDatabaseColumn</code> value or <code>null</code>
150    */

151   public CacheDatabaseColumn getColumn(String JavaDoc columnName)
152   {
153     for (Iterator JavaDoc i = columns.iterator(); i.hasNext();)
154     {
155       CacheDatabaseColumn c = (CacheDatabaseColumn) i.next();
156       if (columnName.compareToIgnoreCase(c.getName()) == 0)
157         return c;
158     }
159     return null;
160   }
161
162   /**
163    * Two <code>CacheDatabaseColumn</code> are equals if they have the same
164    * name and the same columns.
165    *
166    * @param other the object to compare with
167    * @return true if the objects are the same
168    */

169   public boolean equals(Object JavaDoc other)
170   {
171     if (!(other instanceof CacheDatabaseTable))
172       return false;
173
174     CacheDatabaseTable t = (CacheDatabaseTable) other;
175     return t.getName().equals(name) && t.getColumns().equals(columns);
176   }
177
178   /**
179    * Adds an <code>AbstractResultCacheEntry</code> object whose consistency
180    * depends on this table.
181    *
182    * @param ce an <code>AbstractResultCacheEntry</code> value
183    */

184   public synchronized void addCacheEntry(AbstractResultCacheEntry ce)
185   {
186     cacheEntries.add(ce);
187   }
188
189   /**
190    * Adds an <code>AbstractResultCacheEntry</code> object associated to a pk
191    * entry.
192    *
193    * @param pk the pk entry
194    * @param ce an <code>AbstractResultCacheEntry</code> value
195    */

196   public void addPkCacheEntry(String JavaDoc pk, AbstractResultCacheEntry ce)
197   {
198     synchronized (pkCacheEntries)
199     {
200       pkCacheEntries.put(pk, ce);
201     }
202   }
203
204   /**
205    * Gets a <code>CacheEntry</code> object associated to a pk entry.
206    *
207    * @param pk the pk entry
208    * @return the corresponding cache entry if any or null if nothing is found
209    */

210   public AbstractResultCacheEntry getPkResultCacheEntry(String JavaDoc pk)
211   {
212     if (pk == null)
213       return null;
214     synchronized (pkCacheEntries)
215     {
216       return (AbstractResultCacheEntry) pkCacheEntries.get(pk);
217     }
218   }
219
220   /**
221    * Remove a <code>CacheEntry</code> object associated to a pk entry.
222    *
223    * @param pk the pk entry
224    */

225   public void removePkResultCacheEntry(Object JavaDoc pk)
226   {
227     synchronized (pkCacheEntries)
228     {
229       AbstractResultCacheEntry rce = (AbstractResultCacheEntry) pkCacheEntries
230           .remove(pk);
231       rce.invalidate();
232     }
233   }
234
235   /**
236    * Invalidates all cache entries of every column of this table. This does also
237    * affect the entries based on pk values.
238    */

239   public void invalidateAll()
240   {
241     synchronized (this)
242     {
243       for (Iterator JavaDoc i = cacheEntries.iterator(); i.hasNext();)
244         ((AbstractResultCacheEntry) i.next()).invalidate();
245       cacheEntries.clear();
246
247       for (int i = 0; i < columns.size(); i++)
248         ((CacheDatabaseColumn) columns.get(i)).invalidateAll();
249     }
250     synchronized (pkCacheEntries)
251     { // All pk cache entries have been invalidated as a side effect by the
252
// above loop.
253
pkCacheEntries.clear();
254     }
255   }
256
257   /**
258    * Invalidates all cache entries of every column of this table. This does not
259    * affect the entries based on pk values.
260    */

261   public synchronized void invalidateAllExceptPk()
262   {
263     for (Iterator JavaDoc i = cacheEntries.iterator(); i.hasNext();)
264     {
265       AbstractResultCacheEntry qce = (AbstractResultCacheEntry) i.next();
266       if (qce.getRequest().getCacheAbility() != RequestType.UNIQUE_CACHEABLE)
267         qce.invalidate();
268     }
269     cacheEntries.clear();
270   }
271
272   /**
273    * Returns information about the database table and its columns.
274    *
275    * @param longFormat true for a long format, false for a short summary
276    * @return String
277    */

278   public String JavaDoc getInformation(boolean longFormat)
279   {
280     String JavaDoc result = "Table " + name + ": ";
281     int size = columns.size();
282     for (int i = 0; i < size; i++)
283     {
284       CacheDatabaseColumn c = (CacheDatabaseColumn) columns.get(i);
285       if (longFormat)
286         result += "\n";
287       result += c.getInformation();
288       if (!longFormat && (i < size - 1))
289         result += ",";
290     }
291     return result;
292   }
293 }
Popular Tags