KickJava   Java API By Example, From Geeks To Geeks.

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


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

24
25 package org.objectweb.cjdbc.controller.cache.result.schema;
26
27 import java.util.ArrayList JavaDoc;
28 import java.util.Hashtable JavaDoc;
29 import java.util.Iterator JavaDoc;
30
31 import org.objectweb.cjdbc.common.sql.RequestType;
32 import org.objectweb.cjdbc.common.sql.SelectRequest;
33 import org.objectweb.cjdbc.controller.cache.result.entries.AbstractResultCacheEntry;
34
35 /**
36  * A <code>CacheDatabaseColumn</code> represents a column of a database table.
37  * It is composed of a <code>DatabaseColumn</code> object and an
38  * <code>ArrayList</code> of cache entries.
39  *
40  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
41  * @author <a HREF="mailto:Julie.Marguerite@inria.fr">Julie Marguerite </a>
42  * @author <a HREF="mailto:Sara.Bouchenak@epfl.ch">Sara Bouchenak </a>
43  * @version 1.0
44  */

45 public class CacheDatabaseColumn
46 {
47   private String JavaDoc name;
48   private ArrayList JavaDoc cacheEntries;
49
50   /**
51    * Creates a new <code>CacheDatabaseColumn</code> instance.
52    *
53    * @param name name of the column
54    */

55   public CacheDatabaseColumn(String JavaDoc name)
56   {
57     this.name = name;
58     cacheEntries = new ArrayList JavaDoc();
59   }
60
61   /**
62    * Gets the column name.
63    *
64    * @return the column name
65    */

66   public String JavaDoc getName()
67   {
68     return name;
69   }
70
71   /**
72    * Two <code>CacheDatabaseColumn</code> are equals if they have the same
73    * <code>DatabaseColumn</code>.
74    *
75    * @param other the object to compare with
76    * @return <code>true</code> if the objects are the same
77    */

78   public boolean equals(Object JavaDoc other)
79   {
80     if (!(other instanceof CacheDatabaseColumn))
81       return false;
82
83     return name.equals(((CacheDatabaseColumn) other).getName());
84   }
85
86   /**
87    * Adds an <code>AbstractResultCacheEntry</code> object whose consistency
88    * depends on this column.
89    *
90    * @param ce a <code>AbstractResultCacheEntry</code> value
91    */

92   public synchronized void addCacheEntry(AbstractResultCacheEntry ce)
93   {
94     cacheEntries.add(ce);
95   }
96
97   /**
98    * Marks dirty all valid cache entries depending on this colum that are non
99    * unique.
100    */

101   public synchronized void markDirtyAllNonUnique()
102   {
103     // Do not try to optimize by moving cacheEntries.size()
104
// out of the for statement
105
for (int i = 0; i < cacheEntries.size(); i++)
106     {
107       AbstractResultCacheEntry ce = (AbstractResultCacheEntry) cacheEntries
108           .get(i);
109       if ((ce.getRequest().getCacheAbility() != RequestType.UNIQUE_CACHEABLE)
110           && ce.isValid())
111         ce.markDirty();
112     }
113   }
114
115   /**
116    * Invalidates all cache entries depending on this column.
117    */

118   public synchronized void invalidateAll()
119   {
120     for (Iterator JavaDoc i = cacheEntries.iterator(); i.hasNext();)
121     {
122       AbstractResultCacheEntry entry = (AbstractResultCacheEntry) i.next();
123       entry.invalidate();
124     }
125     cacheEntries.clear();
126   }
127
128   /**
129    * Invalidates all cache entries depending on this column that are non
130    * <code>UNIQUE</code>.
131    */

132   public synchronized void invalidateAllNonUnique()
133   {
134     // Do not try to optimize by moving cacheEntries.size()
135
// out of the for statement
136
for (int i = 0; i < cacheEntries.size();)
137     {
138       AbstractResultCacheEntry ce = (AbstractResultCacheEntry) cacheEntries
139           .get(i);
140       if (ce.getRequest().getCacheAbility() != RequestType.UNIQUE_CACHEABLE)
141       {
142         ce.invalidate();
143         cacheEntries.remove(i);
144       }
145       else
146       {
147         i++;
148       }
149     }
150   }
151
152   /**
153    * Invalidates all cache entries depending on this column that are either non-
154    * unique or unique and associated with given values.
155    *
156    * @param val a <code>String</code> representing the value of the current
157    * column.
158    * @param columns an <code>ArrayList</code> of CacheDatabaseColumn objects
159    * @param values an <code>ArrayList</code> of String objects representing
160    * values.
161    */

162   public synchronized void invalidateAllUniqueWithValuesAndAllNonUnique(
163       String JavaDoc val, ArrayList JavaDoc columns, ArrayList JavaDoc values)
164   {
165     // Do not try to optimize by moving cacheEntries.size()
166
// out of the for statement
167
for (int i = 0; i < cacheEntries.size();)
168     {
169       AbstractResultCacheEntry ce = (AbstractResultCacheEntry) cacheEntries
170           .get(i);
171       if (ce.getRequest().getCacheAbility() == RequestType.UNIQUE_CACHEABLE)
172       {
173         Hashtable JavaDoc queryValues;
174         String JavaDoc value, v;
175         SelectRequest query;
176         int size, j;
177
178         query = ce.getRequest();
179         queryValues = query.getWhereValues();
180         // queryValues != null in a UNIQUE_CACHEABLE request
181
value = (String JavaDoc) queryValues.get(this.name);
182         if (value.compareToIgnoreCase(val) == 0)
183         {
184           // The value associated with this column in the WHERE clause
185
// of the UNIQUE SELECT query equals val:
186
// Check if the values associated with the other columns are equal.
187
size = values.size();
188           j = 0;
189           for (Iterator JavaDoc it = columns.iterator(); it.hasNext() && (j < size); j++)
190           {
191             CacheDatabaseColumn cdc = (CacheDatabaseColumn) it.next();
192             if (!this.equals(cdc))
193             {
194               v = (String JavaDoc) values.get(j);
195               value = (String JavaDoc) queryValues.get(cdc.getName());
196               if (value.compareToIgnoreCase(v) != 0)
197               {
198                 // UNIQUE_CACHEABLE request with a different value
199
// Do not invalidate it
200
return;
201               }
202             }
203           }
204           // UNIQUE_CACHEABLE request with same values
205
// Invalidate it
206
ce.invalidate();
207           cacheEntries.remove(i);
208         }
209         else
210         {
211           // UNIQUE_CACHEABLE request with a different value
212
// Do not invalidate it
213
i++;
214         }
215       }
216       else
217       {
218         // NON UNIQUE_CACHEABLE request
219
// Invalidate it
220
ce.invalidate();
221         cacheEntries.remove(i);
222       }
223     }
224   }
225
226   /**
227    * Invalidates all cache entries depending on this column that are non
228    * <code>UNIQUE</code> and mark dirty <code>UNIQUE</code> queries.
229    */

230   public synchronized void invalidateAllNonUniqueAndMarkDirtyUnique()
231   {
232     // Do not try to optimize by moving cacheEntries.size()
233
// out of the for statement
234
for (int i = 0; i < cacheEntries.size(); i++)
235     {
236       AbstractResultCacheEntry ce = (AbstractResultCacheEntry) cacheEntries
237           .get(i);
238       if ((ce.getRequest().getCacheAbility() != RequestType.UNIQUE_CACHEABLE)
239           && ce.isValid())
240         ce.markDirty();
241       else
242       {
243         ce.invalidate();
244         cacheEntries.remove(i);
245       }
246     }
247   }
248
249   /**
250    * Returns the column name.
251    *
252    * @return a <code>String</code> value
253    */

254   public String JavaDoc getInformation()
255   {
256     return name;
257   }
258 }
259
Popular Tags