KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > controller > cache > result > schema > CacheDatabaseTable


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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