KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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

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

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

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

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

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

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

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

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

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

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