001: /**
002: * Sequoia: Database clustering technology.
003: * Copyright (C) 2002-2004 French National Institute For Research In Computer
004: * Science And Control (INRIA).
005: * Contact: sequoia@continuent.org
006: *
007: * Licensed under the Apache License, Version 2.0 (the "License");
008: * you may not use this file except in compliance with the License.
009: * You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: *
019: * Initial developer(s): Nicolas Modrzyk.
020: * Contributor(s):
021: */package org.continuent.sequoia.controller.cache.result;
022:
023: import java.util.Hashtable;
024:
025: import org.continuent.sequoia.common.xml.DatabasesXmlTags;
026: import org.continuent.sequoia.controller.cache.result.rules.EagerCaching;
027: import org.continuent.sequoia.controller.cache.result.rules.NoCaching;
028: import org.continuent.sequoia.controller.cache.result.rules.RelaxedCaching;
029:
030: /**
031: * Create a cache that conforms to AbstractResultCache, that is implementation
032: * independant
033: *
034: * @author <a href="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
035: */
036: public class ResultCacheFactory {
037: /**
038: * Get an instance of the current cache implementation
039: *
040: * @param granularityValue of the parsing
041: * @param maxEntries to the cache
042: * @param pendingTimeout before pending query timeout
043: * @return <code>ResultCache</code> implementation of the
044: * <code>AbstractResultCache</code>
045: * @throws InstantiationException if parsing granularity is not valid
046: */
047: public static AbstractResultCache getCacheInstance(
048: int granularityValue, int maxEntries, int pendingTimeout)
049: throws InstantiationException {
050: AbstractResultCache currentRequestCache = null;
051: switch (granularityValue) {
052: case CachingGranularities.TABLE:
053: currentRequestCache = new ResultCacheTable(maxEntries,
054: pendingTimeout);
055: break;
056: case CachingGranularities.DATABASE:
057: currentRequestCache = new ResultCacheDatabase(maxEntries,
058: pendingTimeout);
059: break;
060: case CachingGranularities.COLUMN:
061: currentRequestCache = new ResultCacheColumn(maxEntries,
062: pendingTimeout);
063: break;
064: case CachingGranularities.COLUMN_UNIQUE:
065: currentRequestCache = new ResultCacheColumnUnique(
066: maxEntries, pendingTimeout);
067: break;
068: default:
069: throw new InstantiationException(
070: "Invalid Granularity Value");
071: }
072: return currentRequestCache;
073: }
074:
075: /**
076: * Get an instance of a cache behavior for this cache
077: *
078: * @param behaviorString representation of this cache behavior, xml tag
079: * @param options for different cache rules
080: * @return an instance of a cache behavior
081: */
082: public static CacheBehavior getCacheBehaviorInstance(
083: String behaviorString, Hashtable options) {
084: if (behaviorString
085: .equalsIgnoreCase(DatabasesXmlTags.ELT_NoCaching))
086: return new NoCaching();
087: if (behaviorString.equals(DatabasesXmlTags.ELT_EagerCaching)) {
088: // Timeout is in seconds: *1000
089: // 0, is no timeout, and 0x1000=0 !
090: long timeout = 1000 * Long.parseLong((String) options
091: .get(DatabasesXmlTags.ATT_timeout));
092: return new EagerCaching(timeout);
093: }
094: if (behaviorString.equals(DatabasesXmlTags.ELT_RelaxedCaching)) {
095: // Timeout is in seconds: *1000
096: long timeout = 1000 * Long.parseLong((String) options
097: .get(DatabasesXmlTags.ATT_timeout));
098: boolean keepIfNotDirty = new Boolean((String) options
099: .get(DatabasesXmlTags.ATT_keepIfNotDirty))
100: .booleanValue();
101: return new RelaxedCaching(keepIfNotDirty, timeout);
102: } else
103: return null;
104: }
105: }
|