001: /**
002: * Sequoia: Database clustering technology.
003: * Copyright (C) 2002-2004 French National Institute For Research In Computer
004: * Science And Control (INRIA).
005: * Copyright (C) 2005 AmicoSoft, Inc. dba Emic Networks
006: * Contact: sequoia@continuent.org
007: *
008: * Licensed under the Apache License, Version 2.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: * Initial developer(s): Emmanuel Cecchet.
021: * Contributor(s): Nicolas Modrzyk.
022: */package org.continuent.sequoia.controller.cache.result;
023:
024: import java.util.ArrayList;
025: import java.util.Collection;
026: import java.util.Iterator;
027:
028: import org.continuent.sequoia.common.sql.schema.TableColumn;
029: import org.continuent.sequoia.controller.cache.result.entries.AbstractResultCacheEntry;
030: import org.continuent.sequoia.controller.cache.result.schema.CacheDatabaseColumn;
031: import org.continuent.sequoia.controller.cache.result.schema.CacheDatabaseTable;
032: import org.continuent.sequoia.controller.requests.AbstractWriteRequest;
033: import org.continuent.sequoia.controller.requests.ParsingGranularities;
034: import org.continuent.sequoia.controller.requests.SelectRequest;
035: import org.continuent.sequoia.controller.requests.UpdateRequest;
036:
037: /**
038: * This is a query cache implementation with a column granularity:
039: * <ul>
040: * <li><code>COLUMN</code>: column granularity, entries in the cache are
041: * invalidated based on column dependencies</li>
042: * </ul>
043: *
044: * @author <a href="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
045: * @author <a href="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
046: * @version 1.0
047: */
048:
049: public class ResultCacheColumn extends ResultCache {
050: /**
051: * Builds a new ResultCache with a Column granularity.
052: *
053: * @param maxEntries maximum number of entries
054: * @param pendingTimeout pending timeout for concurrent queries
055: */
056: public ResultCacheColumn(int maxEntries, int pendingTimeout) {
057: super (maxEntries, pendingTimeout);
058: parsingGranularity = ParsingGranularities.COLUMN;
059: }
060:
061: /**
062: * @see org.continuent.sequoia.controller.cache.result.ResultCache#processAddToCache(AbstractResultCacheEntry)
063: */
064: public void processAddToCache(AbstractResultCacheEntry qe) {
065: SelectRequest request = qe.getRequest();
066: ArrayList selectedColumns = request.getSelect();
067: // Update the tables columns dependencies
068: if (selectedColumns == null || selectedColumns.isEmpty()) {
069: logger
070: .warn("No parsing of select clause found - Fallback to table granularity");
071: Collection from = request.getFrom();
072: if (from == null)
073: return;
074: for (Iterator i = from.iterator(); i.hasNext();) {
075: CacheDatabaseTable table = cdbs.getTable((String) i
076: .next());
077: table.addCacheEntry(qe);
078: // Add all columns, entries will be added below.
079: ArrayList columns = table.getColumns();
080: for (int j = 0; j < columns.size(); j++) {
081: ((CacheDatabaseColumn) columns.get(j))
082: .addCacheEntry(qe);
083: }
084: return;
085: }
086: }
087: for (Iterator i = selectedColumns.iterator(); i.hasNext();) {
088: TableColumn tc = (TableColumn) i.next();
089: cdbs.getTable(tc.getTableName()).getColumn(
090: tc.getColumnName()).addCacheEntry(qe);
091: }
092: if (request.getWhere() != null)
093: for (Iterator i = request.getWhere().iterator(); i
094: .hasNext();) {
095: TableColumn tc = (TableColumn) i.next();
096: cdbs.getTable(tc.getTableName()).getColumn(
097: tc.getColumnName()).addCacheEntry(qe);
098: }
099: }
100:
101: /**
102: * @see org.continuent.sequoia.controller.cache.result.AbstractResultCache#isUpdateNecessary(org.continuent.sequoia.controller.requests.UpdateRequest)
103: */
104: public boolean isUpdateNecessary(UpdateRequest request) {
105: return true;
106: }
107:
108: /**
109: * @see org.continuent.sequoia.controller.cache.result.ResultCache#processWriteNotify(org.continuent.sequoia.controller.requests.AbstractWriteRequest)
110: */
111: protected void processWriteNotify(AbstractWriteRequest request) {
112: // Sanity check
113: if (request.getColumns() == null) {
114: logger
115: .warn("No column parsing found - Fallback to table granularity ("
116: + request.getUniqueKey() + ")");
117: cdbs.getTable(request.getTableName()).invalidateAll();
118: return;
119: }
120: if (request.isAlter()) {
121: cdbs.getTable(request.getTableName()).invalidateAll();
122: return;
123: }
124: for (Iterator i = request.getColumns().iterator(); i.hasNext();) {
125: TableColumn tc = (TableColumn) i.next();
126: cdbs.getTable(tc.getTableName()).getColumn(
127: tc.getColumnName()).invalidateAll();
128: }
129: }
130:
131: /**
132: * @see org.continuent.sequoia.controller.cache.result.ResultCache#getName()
133: */
134: public String getName() {
135: return "column";
136: }
137:
138: }
|