Main Page | Packages | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

CacheDatabaseColumn.java

00001 /**
00002  * C-JDBC: Clustered JDBC.
00003  * Copyright (C) 2002-2004 French National Institute For Research In Computer
00004  * Science And Control (INRIA).
00005  * Contact: c-jdbc@objectweb.org
00006  * 
00007  * This library is free software; you can redistribute it and/or modify it
00008  * under the terms of the GNU Lesser General Public License as published by the
00009  * Free Software Foundation; either version 2.1 of the License, or any later
00010  * version.
00011  * 
00012  * This library is distributed in the hope that it will be useful, but WITHOUT
00013  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
00015  * for more details.
00016  * 
00017  * You should have received a copy of the GNU Lesser General Public License
00018  * along with this library; if not, write to the Free Software Foundation,
00019  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00020  *
00021  * Initial developer(s): Emmanuel Cecchet.
00022  * Contributor(s): Julie Marguerite, Sara Bouchenak.
00023  */
00024 
00025 package org.objectweb.cjdbc.controller.cache.result.schema;
00026 
00027 import java.io.Serializable;
00028 import java.util.ArrayList;
00029 import java.util.Hashtable;
00030 import java.util.Iterator;
00031 
00032 import org.objectweb.cjdbc.common.sql.RequestType;
00033 import org.objectweb.cjdbc.common.sql.SelectRequest;
00034 import org.objectweb.cjdbc.controller.cache.result.entries.CacheEntry;
00035 
00036 /**
00037  * A <code>CacheDatabaseColumn</code> represents a column of a database
00038  * table. It is composed of a <code>DatabaseColumn</code> object and an
00039  * <code>ArrayList</code> of cache entries.
00040  * 
00041  * @author <a href="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet</a>
00042  * @author <a href="mailto:Julie.Marguerite@inria.fr">Julie Marguerite</a>
00043  * @author <a href="mailto:Sara.Bouchenak@epfl.ch">Sara Bouchenak</a>
00044  * @version 1.0
00045  */
00046 public class CacheDatabaseColumn implements Serializable
00047 {
00048   private String name;
00049   private ArrayList cacheEntries;
00050 
00051   /**
00052    * Creates a new <code>CacheDatabaseColumn</code> instance.
00053    * 
00054    * @param name name of the column
00055    */
00056   public CacheDatabaseColumn(String name)
00057   {
00058     this.name = name;
00059     cacheEntries = new ArrayList();
00060   }
00061 
00062   /**
00063    * Gets the column name.
00064    * 
00065    * @return the column name
00066    */
00067   public String getName()
00068   {
00069     return name;
00070   }
00071 
00072   /**
00073    * Two <code>CacheDatabaseColumn</code> are equals if they have the same
00074    * <code>DatabaseColumn</code>.
00075    * 
00076    * @param other the object to compare with
00077    * @return <code>true</code> if the objects are the same
00078    */
00079   public boolean equals(Object other)
00080   {
00081     if (!(other instanceof CacheDatabaseColumn))
00082       return false;
00083 
00084     return name.equals(((CacheDatabaseColumn) other).getName());
00085   }
00086 
00087   /**
00088    * Adds a <code>CacheEntry</code> object whose consistency depends on this
00089    * column.
00090    * 
00091    * @param ce a <code>ResultCacheEntry</code> value
00092    */
00093   public synchronized void addCacheEntry(CacheEntry ce)
00094   {
00095     cacheEntries.add(ce);
00096   }
00097 
00098   /**
00099    * Marks dirty all valid cache entries depending on this colum that are non
00100    * unique.
00101    */
00102   public synchronized void markDirtyAllNonUnique()
00103   {
00104     // Do not try to optimize by moving cacheEntries.size()
00105     // out of the for statement
00106     for (int i = 0; i < cacheEntries.size(); i++)
00107     {
00108       CacheEntry ce = (CacheEntry) cacheEntries.get(i);
00109       if ((ce.getRequest().getCacheAbility() != RequestType.UNIQUE_CACHEABLE)
00110         && ce.isValid())
00111         ce.markDirty();
00112     }
00113   }
00114 
00115   /**
00116    * Invalidates all cache entries depending on this column.
00117    */
00118   public synchronized void invalidateAll()
00119   {
00120     for (Iterator i = cacheEntries.iterator(); i.hasNext();)
00121     {
00122       CacheEntry entry = (CacheEntry) i.next();
00123        entry.invalidate();
00124     }
00125     cacheEntries.clear();
00126   }
00127 
00128   /**
00129    * Invalidates all cache entries depending on this column that are non <code>UNIQUE</code>.
00130    */
00131   public synchronized void invalidateAllNonUnique()
00132   {
00133     // Do not try to optimize by moving cacheEntries.size()
00134     // out of the for statement
00135     for (int i = 0; i < cacheEntries.size();)
00136     {
00137       CacheEntry ce = (CacheEntry) cacheEntries.get(i);
00138       if (ce.getRequest().getCacheAbility() != RequestType.UNIQUE_CACHEABLE)
00139       {
00140         ce.invalidate();
00141         cacheEntries.remove(i);
00142       }
00143       else
00144       {
00145         i++;
00146       }
00147     }
00148   }
00149 
00150   /**
00151    * Invalidates all cache entries depending on this column that are either
00152    * non- unique or unique and associated with given values.
00153    * 
00154    * @param val a <code>String</code> representing the value of the current
00155    *          column.
00156    * @param columns an <code>ArrayList</code> of CacheDatabaseColumn objects
00157    * @param values an <code>ArrayList</code> of String objects representing
00158    *          values.
00159    */
00160   public synchronized void invalidateAllUniqueWithValuesAndAllNonUnique(
00161     String val,
00162     ArrayList columns,
00163     ArrayList values)
00164   {
00165     // Do not try to optimize by moving cacheEntries.size()
00166     // out of the for statement
00167     for (int i = 0; i < cacheEntries.size();)
00168     {
00169       CacheEntry ce = (CacheEntry) cacheEntries.get(i);
00170       if (ce.getRequest().getCacheAbility() == RequestType.UNIQUE_CACHEABLE)
00171       {
00172         Hashtable queryValues;
00173         String value, v;
00174         SelectRequest query;
00175         int size, j;
00176 
00177         query = ce.getRequest();
00178         queryValues = query.getWhereValues();
00179         // queryValues != null in a UNIQUE_CACHEABLE request
00180         value = (String) queryValues.get(this.name);
00181         if (value.compareToIgnoreCase(val) == 0)
00182         {
00183           // The value associated with this column in the WHERE clause
00184           // of the UNIQUE SELECT query equals val:
00185           // Check if the values associated with the other columns are equal.
00186           size = values.size();
00187           j = 0;
00188           for (Iterator it = columns.iterator();
00189             it.hasNext() && (j < size);
00190             j++)
00191           {
00192             CacheDatabaseColumn cdc = (CacheDatabaseColumn) it.next();
00193             if (!this.equals(cdc))
00194             {
00195               v = (String) values.get(j);
00196               value = (String) queryValues.get(cdc.getName());
00197               if (value.compareToIgnoreCase(v) != 0)
00198               {
00199                 // UNIQUE_CACHEABLE request with a different value
00200                 // Do not invalidate it
00201                 return;
00202               }
00203             }
00204           }
00205           // UNIQUE_CACHEABLE request with same values
00206           // Invalidate it
00207           ce.invalidate();
00208           cacheEntries.remove(i);
00209         }
00210         else
00211         {
00212           // UNIQUE_CACHEABLE request with a different value
00213           // Do not invalidate it
00214           i++;
00215         }
00216       }
00217       else
00218       {
00219         // NON UNIQUE_CACHEABLE request
00220         // Invalidate it
00221         ce.invalidate();
00222         cacheEntries.remove(i);
00223       }
00224     }
00225   }
00226 
00227   /**
00228    * Invalidates all cache entries depending on this column that are non <code>UNIQUE</code>
00229    * and mark dirty <code>UNIQUE</code> queries.
00230    */
00231   public synchronized void invalidateAllNonUniqueAndMarkDirtyUnique()
00232   {
00233     // Do not try to optimize by moving cacheEntries.size()
00234     // out of the for statement
00235     for (int i = 0; i < cacheEntries.size(); i++)
00236     {
00237       CacheEntry ce = (CacheEntry) cacheEntries.get(i);
00238       if ((ce.getRequest().getCacheAbility() != RequestType.UNIQUE_CACHEABLE)
00239         && ce.isValid())
00240         ce.markDirty();
00241       else
00242       {
00243         ce.invalidate();
00244         cacheEntries.remove(i);
00245       }
00246     }
00247   }
00248 
00249   /**
00250    * Returns the column name.
00251    * 
00252    * @return a <code>String</code> value
00253    */
00254   public String getInformation()
00255   {
00256     return name;
00257   }
00258 }

Generated on Mon Apr 11 22:01:29 2005 for C-JDBC by  doxygen 1.3.9.1