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): ______________________________________.
022: */package org.continuent.sequoia.controller.cache.result.entries;
023:
024: import org.continuent.sequoia.common.stream.DriverStream;
025: import org.continuent.sequoia.controller.backend.result.ControllerResultSet;
026: import org.continuent.sequoia.controller.requests.SelectRequest;
027:
028: /**
029: * A <code>CacheEntry</code> represents a SQL select request with its reponse.
030: * The cache entry can have 3 states:
031: * <p>
032: * <ul>
033: * <li><code>CACHE_VALID</code> when it is valid</li>
034: * <li><code>CACHE_DIRTY</code> when the result has been marked dirty (may be
035: * invalid)</li>
036: * <li><code>CACHE_INVALID</code> when there is no result (request has to be
037: * re-issued to the database)</li>
038: * </ul>
039: *
040: * @author <a href="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
041: * @version 1.0
042: */
043: public abstract class AbstractResultCacheEntry {
044: protected SelectRequest request;
045: protected ControllerResultSet result;
046: protected int state;
047:
048: //Chain for LRU
049: private AbstractResultCacheEntry next;
050: private AbstractResultCacheEntry prev;
051:
052: /** This entry has no deadline */
053: public static final int NO_DEADLINE = -1;
054: /** State this entry is valid */
055: public static final int CACHE_VALID = 0;
056: /** This entry is dirty */
057: public static final int CACHE_DIRTY = 1;
058: /** This entry is no more valid and is not consistent with real data. */
059: public static final int CACHE_INVALID = 2;
060:
061: /**
062: * Creates a new <code>CacheEntry</code> instance.
063: *
064: * @param request a <code>SelectRequest</code> value
065: * @param result a <code>ControllerResultSet</code> value
066: */
067: public AbstractResultCacheEntry(SelectRequest request,
068: ControllerResultSet result) {
069: this .request = request;
070: this .result = result;
071: state = CACHE_VALID;
072: next = null;
073: prev = null;
074: }
075:
076: /**
077: * Get the type of this entry as a string
078: *
079: * @return NoCache or Eager or Relaxed
080: */
081: public abstract String getType();
082:
083: /**
084: * Get the state of this entry as a string
085: *
086: * @return Valid or Dirty or Invalid
087: */
088: public String getState() {
089: if (isValid())
090: return "Valid";
091: if (isDirty())
092: return "Dirty";
093: else
094: return "Invalid";
095: }
096:
097: /**
098: * Return <code>true</code> if cache entry state is valid (state is
099: * {@link #CACHE_VALID}).
100: *
101: * @return a <code>boolean</code> value
102: */
103: public boolean isValid() {
104: return state == CACHE_VALID;
105: }
106:
107: /**
108: * Returns <code>true</code> if cache entry state is marked dirty (state is
109: * {@link #CACHE_DIRTY}).
110: *
111: * @return a <code>boolean</code> value
112: */
113: public boolean isDirty() {
114: return state == CACHE_DIRTY;
115: }
116:
117: /**
118: * Returns the <code>SELECT</code> request of this cache entry.
119: *
120: * @return a <code>SelectRequest</code> value
121: */
122: public SelectRequest getRequest() {
123: return request;
124: }
125:
126: /**
127: * Returns the <code>ControllerResultSet</code> of the cached select request
128: *
129: * @return a <code>ControllerResultSet</code> value
130: */
131: public ControllerResultSet getResult() {
132: return result;
133: }
134:
135: /**
136: * Set a new <code>ControllerResultSet</code> of the cached select request
137: * (cache update).
138: * <p>
139: * The cache state is automatically set to valid ({@link #CACHE_VALID}).
140: *
141: * @param result a <code>ControllerResultSet</code> value
142: */
143: public void setResult(ControllerResultSet result) {
144: this .result = result;
145: state = CACHE_VALID;
146: }
147:
148: /**
149: * Invalidates this cache entry (removes the <code>ResultSet</code> and turn
150: * state to {@link #CACHE_INVALID}).
151: */
152: public abstract void invalidate();
153:
154: /**
155: * Marks this entry dirty (state becomes {@link #CACHE_DIRTY}).
156: * <p>
157: * The <code>ResultSet</code> if not affected by this method.
158: */
159: public void markDirty() {
160: state = CACHE_DIRTY;
161: }
162:
163: /**
164: * Marks this entry valid (state becomes {@link #CACHE_VALID}).
165: */
166: public void setValid() {
167: state = CACHE_VALID;
168: }
169:
170: /**
171: * Gets the value of next <code>AbstractResultCacheEntry</code> in LRU.
172: *
173: * @return value of next.
174: */
175: public AbstractResultCacheEntry getNext() {
176: return next;
177: }
178:
179: /**
180: * Sets the value of next <code>AbstractResultCacheEntry</code> in LRU.
181: *
182: * @param next value to assign to next.
183: */
184: public void setNext(AbstractResultCacheEntry next) {
185: this .next = next;
186: }
187:
188: /**
189: * Gets the value of previous <code>AbstractResultCacheEntry</code> in LRU.
190: *
191: * @return value of previous.
192: */
193: public AbstractResultCacheEntry getPrev() {
194: return prev;
195: }
196:
197: /**
198: * Sets the value of previous <code>AbstractResultCacheEntry</code> in LRU.
199: *
200: * @param prev value to assign to prev.
201: */
202: public void setPrev(AbstractResultCacheEntry prev) {
203: this .prev = prev;
204: }
205:
206: /**
207: * Get data about this entry
208: *
209: * @return an array [request,type,status(valid,notvalid,dirty),deadLine]
210: */
211: public abstract String[] toStringTable();
212:
213: /**
214: * Size of the result in bytes
215: *
216: * @return an integer
217: */
218: public int getSizeOfResult() {
219: try {
220: return DriverStream.countBytes(result);
221: } catch (Exception e) {
222: return -1;
223: }
224: }
225:
226: }
|