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): Emmanuel Cecchet.
021: */package org.continuent.sequoia.controller.cache.result;
022:
023: import java.util.regex.Pattern;
024:
025: import org.continuent.sequoia.common.log.Trace;
026: import org.continuent.sequoia.common.xml.DatabasesXmlTags;
027: import org.continuent.sequoia.common.xml.XmlComponent;
028: import org.continuent.sequoia.controller.requests.AbstractRequest;
029:
030: /**
031: * This is the to define cache rules in the cache. A
032: * <code>ResultCacheRule</code> is defined by a queryPattern, set to 'default'
033: * if default rule, and a <code>CacheBehavior</code>.
034: *
035: * @author <a href="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk</a>
036: * @author <a href="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet</a>
037: * @version 1.0
038: */
039: public class ResultCacheRule implements XmlComponent {
040: Trace logger = Trace.getLogger(ResultCacheRule.class.getName());
041: private Pattern queryPattern;
042: private String queryString;
043: private boolean isCaseSensitive;
044: private boolean applyToSkeleton;
045: private long timestampResolution;
046: private CacheBehavior behavior;
047:
048: /**
049: * Creates a new <code>ResultCacheRule</code>
050: *
051: * @param queryString for this rule
052: * @param caseSensitive true if matching is case sensitive
053: * @param applyToSkeleton true if rule apply to query skeleton
054: * @param timestampResolution timestamp resolution for NOW() macro
055: */
056: public ResultCacheRule(String queryString, boolean caseSensitive,
057: boolean applyToSkeleton, long timestampResolution) {
058: this .queryString = queryString;
059: queryPattern = Pattern.compile(queryString);
060: this .isCaseSensitive = caseSensitive;
061: this .applyToSkeleton = applyToSkeleton;
062: this .timestampResolution = timestampResolution;
063: }
064:
065: /**
066: * Get the query pattern
067: *
068: * @return the queryPattern for this <code>ResultCacheRule</code>
069: */
070: public Pattern getQueryPattern() {
071: return this .queryPattern;
072: }
073:
074: /**
075: * Get the cache behavior
076: *
077: * @return the <code>CacheBehavior</code> for this
078: * <code>ResultCacheRule</code>
079: */
080: public CacheBehavior getCacheBehavior() {
081: return behavior;
082: }
083:
084: /**
085: * Set the cache behavior
086: *
087: * @param behavior behavior for this rule
088: */
089: public void setCacheBehavior(CacheBehavior behavior) {
090: this .behavior = behavior;
091: }
092:
093: /**
094: * Retrieve the timestamp resolution of this scheduler
095: *
096: * @return timestampResolution
097: */
098: public long getTimestampResolution() {
099: return this .timestampResolution;
100: }
101:
102: /**
103: * @param request we may want to add to the cache
104: * @return the behavior to get the entry
105: */
106: public CacheBehavior matches(AbstractRequest request) {
107: if (queryPattern.matcher(request.getSqlOrTemplate()).matches())
108: return behavior;
109: else
110: return null;
111: }
112:
113: /**
114: * @see org.continuent.sequoia.common.xml.XmlComponent#getXml()
115: */
116: public String getXml() {
117: StringBuffer info = new StringBuffer();
118: info.append("<" + DatabasesXmlTags.ELT_ResultCacheRule + " "
119: + DatabasesXmlTags.ATT_queryPattern + "=\""
120: + queryString + "\" "
121: + DatabasesXmlTags.ATT_caseSensitive + "=\""
122: + isCaseSensitive + "\" "
123: + DatabasesXmlTags.ATT_applyToSkeleton + "=\""
124: + applyToSkeleton + "\" "
125: + DatabasesXmlTags.ATT_timestampResolution + "=\""
126: + timestampResolution / 1000 + "\" >");
127: info.append(behavior.getXml());
128: info.append("</" + DatabasesXmlTags.ELT_ResultCacheRule + ">");
129: return info.toString();
130: }
131:
132: }
|