1 24 25 package org.objectweb.cjdbc.controller.cache.result; 26 27 import java.util.ArrayList ; 28 import java.util.Iterator ; 29 30 import org.objectweb.cjdbc.common.sql.AbstractWriteRequest; 31 import org.objectweb.cjdbc.common.sql.DeleteRequest; 32 import org.objectweb.cjdbc.common.sql.ParsingGranularities; 33 import org.objectweb.cjdbc.common.sql.RequestType; 34 import org.objectweb.cjdbc.common.sql.SelectRequest; 35 import org.objectweb.cjdbc.common.sql.UpdateRequest; 36 import org.objectweb.cjdbc.common.sql.schema.TableColumn; 37 import org.objectweb.cjdbc.controller.cache.result.entries.AbstractResultCacheEntry; 38 import org.objectweb.cjdbc.controller.cache.result.schema.CacheDatabaseColumn; 39 import org.objectweb.cjdbc.controller.cache.result.schema.CacheDatabaseTable; 40 import org.objectweb.cjdbc.controller.virtualdatabase.ControllerResultSet; 41 42 54 public class ResultCacheColumnUnique extends ResultCache 55 { 56 57 63 public ResultCacheColumnUnique(int maxEntries, int pendingTimeout) 64 { 65 super(maxEntries, pendingTimeout); 66 parsingGranularity = ParsingGranularities.COLUMN_UNIQUE; 67 } 68 69 72 public void processAddToCache(AbstractResultCacheEntry qe) 73 { 74 SelectRequest request = qe.getRequest(); 75 ArrayList selectedColumns = request.getSelect(); 76 if (selectedColumns == null || selectedColumns.isEmpty()) 78 { 79 logger 80 .warn("No parsing of select clause found - Fallback to table granularity"); 81 for (Iterator i = request.getFrom().iterator(); i.hasNext();) 82 { 83 CacheDatabaseTable table = cdbs.getTable((String ) i.next()); 84 table.addCacheEntry(qe); 85 ArrayList columns = table.getColumns(); 87 for (int j = 0; j < columns.size(); j++) 88 { 89 ((CacheDatabaseColumn) columns.get(j)).addCacheEntry(qe); 90 } 91 return; 92 } 93 } 94 for (Iterator i = request.getSelect().iterator(); i.hasNext();) 95 { 96 TableColumn tc = (TableColumn) i.next(); 97 cdbs.getTable(tc.getTableName()).getColumn(tc.getColumnName()) 98 .addCacheEntry(qe); 99 } 100 if (request.getWhere() != null) 101 { for (Iterator i = request.getWhere().iterator(); i.hasNext();) 103 { 104 TableColumn tc = (TableColumn) i.next(); 105 cdbs.getTable(tc.getTableName()).getColumn(tc.getColumnName()) 106 .addCacheEntry(qe); 107 } 108 if (request.getCacheAbility() == RequestType.UNIQUE_CACHEABLE) 109 { String tableName = (String ) request.getFrom().get(0); 111 AbstractResultCacheEntry entry = cdbs.getTable(tableName) 112 .getPkResultCacheEntry(request.getPkValue()); 113 if (entry != null) 114 { 115 if (entry.isValid()) 116 { if (entry.getRequest().getSelect().size() >= request.getSelect() 119 .size()) 120 return; 121 } 122 } 123 cdbs.getTable(tableName).addPkCacheEntry(request.getPkValue(), qe); 124 } 125 } 126 } 127 128 131 public boolean isUpdateNecessary(UpdateRequest request) 132 { 133 if (request.getCacheAbility() != RequestType.UNIQUE_CACHEABLE) 134 return true; 135 CacheDatabaseTable cacheTable = cdbs.getTable(request.getTableName()); 136 if (request.getColumns() == null) 137 return true; 138 String pk = request.getPk(); 139 AbstractResultCacheEntry qce = cacheTable.getPkResultCacheEntry(pk); 140 if (qce != null) 141 { 142 if (!qce.isValid()) 143 return true; 144 ControllerResultSet rs = qce.getResult(); 145 if (rs == null) 146 return true; 147 else 148 return needInvalidate(rs, request)[1]; 149 } 150 else 151 return true; 152 } 153 154 157 protected void processWriteNotify(AbstractWriteRequest request) 158 { 159 CacheDatabaseTable cacheTable = cdbs.getTable(request.getTableName()); 161 if (request.getColumns() == null) 162 { 163 logger.warn("No column parsing found - Fallback to table granularity (" 164 + request.getSQL() + ")"); 165 cacheTable.invalidateAll(); 166 return; 167 } 168 if (request.isInsert()) 169 { 170 for (Iterator i = request.getColumns().iterator(); i.hasNext();) 171 { 172 TableColumn tc = (TableColumn) i.next(); 173 cdbs.getTable(tc.getTableName()).getColumn(tc.getColumnName()) 174 .invalidateAllNonUnique(); 175 } 176 } 177 else 178 { 179 if (request.getCacheAbility() == RequestType.UNIQUE_CACHEABLE) 180 { 181 if (request.isUpdate()) 182 { 183 String pk = ((UpdateRequest) request).getPk(); 184 AbstractResultCacheEntry qce = cacheTable.getPkResultCacheEntry(pk); 185 if (qce != null) 186 { 187 boolean[] invalidate = needInvalidate(qce.getResult(), 188 (UpdateRequest) request); 189 if (invalidate[0]) 190 { cacheTable.removePkResultCacheEntry(pk); 192 return; 193 } 194 else 195 { 196 if (logger.isDebugEnabled()) 197 logger.debug("No invalidate needed for request:" 198 + request.getSQLShortForm(20)); 199 return; } 201 } 202 } 203 else if (request.isDelete()) 204 { cacheTable 206 .removePkResultCacheEntry(((DeleteRequest) request).getPk()); 207 return; 208 } 209 } 210 for (Iterator i = request.getColumns().iterator(); i.hasNext();) 213 { 214 TableColumn tc = (TableColumn) i.next(); 215 CacheDatabaseTable table = cdbs.getTable(tc.getTableName()); 216 table.invalidateAll(); if (!request.isAlter()) 218 table.getColumn(tc.getColumnName()).invalidateAll(); 219 } 220 } 221 } 222 223 226 public String getName() 227 { 228 return "columnUnique"; 229 } 230 231 } | Popular Tags |