1 24 25 package org.objectweb.cjdbc.controller.cache.result.schema; 26 27 import java.sql.SQLException ; 28 import java.util.ArrayList ; 29 import java.util.HashMap ; 30 import java.util.Iterator ; 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 49 public class CacheDatabaseTable 50 { 51 private String name; 52 private ArrayList columns; 53 private ArrayList cacheEntries; private HashMap pkCacheEntries; 56 61 public CacheDatabaseTable(DatabaseTable databaseTable) 62 { 63 name = databaseTable.getName(); 65 ArrayList origColumns = databaseTable.getColumns(); 66 int size = origColumns.size(); 67 columns = new ArrayList (size); 68 for (int i = 0; i < size; i++) 69 columns.add(new CacheDatabaseColumn(((DatabaseColumn) origColumns.get(i)) 70 .getName())); 71 72 cacheEntries = new ArrayList (); 74 pkCacheEntries = new HashMap (); 75 } 76 77 82 public String getName() 83 { 84 return name; 85 } 86 87 94 public void addColumn(CacheDatabaseColumn column) 95 { 96 columns.add(column); 97 } 98 99 107 public void mergeColumns(CacheDatabaseTable t) throws SQLException 108 { 109 if (t == null) 110 return; 111 112 ArrayList 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 ("Column " + c.getName() 127 + " definition mismatch."); 128 } 129 } 130 } 131 132 140 public ArrayList getColumns() 141 { 142 return columns; 143 } 144 145 152 public CacheDatabaseColumn getColumn(String columnName) 153 { 154 for (Iterator 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 170 public boolean equals(Object 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 185 public synchronized void addCacheEntry(AbstractResultCacheEntry ce) 186 { 187 cacheEntries.add(ce); 188 } 189 190 197 public void addPkCacheEntry(String pk, AbstractResultCacheEntry ce) 198 { 199 synchronized (pkCacheEntries) 200 { 201 pkCacheEntries.put(pk, ce); 202 } 203 } 204 205 211 public AbstractResultCacheEntry getPkResultCacheEntry(String pk) 212 { 213 if (pk == null) 214 return null; 215 synchronized (pkCacheEntries) 216 { 217 return (AbstractResultCacheEntry) pkCacheEntries.get(pk); 218 } 219 } 220 221 226 public void removePkResultCacheEntry(Object pk) 227 { 228 synchronized (pkCacheEntries) 229 { 230 AbstractResultCacheEntry rce = (AbstractResultCacheEntry) pkCacheEntries 231 .remove(pk); 232 rce.invalidate(); 233 } 234 } 235 236 240 public void invalidateAll() 241 { 242 synchronized (this) 243 { 244 for (Iterator 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 { pkCacheEntries.clear(); 255 } 256 } 257 258 262 public synchronized void invalidateAllExceptPk() 263 { 264 for (Iterator 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 279 public String getInformation(boolean longFormat) 280 { 281 String 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 |