00001 /** 00002 * C-JDBC: Clustered JDBC. 00003 * Copyright (C) 2002-2004 French National Institute For Research In Computer 00004 * Science And Control (INRIA). 00005 * Contact: c-jdbc@objectweb.org 00006 * 00007 * This library is free software; you can redistribute it and/or modify it 00008 * under the terms of the GNU Lesser General Public License as published by the 00009 * Free Software Foundation; either version 2.1 of the License, or any later 00010 * version. 00011 * 00012 * This library is distributed in the hope that it will be useful, but WITHOUT 00013 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00014 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 00015 * for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public License 00018 * along with this library; if not, write to the Free Software Foundation, 00019 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. 00020 * 00021 * Initial developer(s): Emmanuel Cecchet. 00022 * Contributor(s): ______________________________________. 00023 */ 00024 00025 package org.objectweb.cjdbc.controller.cache.result.entries; 00026 00027 import org.objectweb.cjdbc.common.sql.SelectRequest; 00028 import org.objectweb.cjdbc.common.stream.CJDBCStream; 00029 import org.objectweb.cjdbc.controller.virtualdatabase.ControllerResultSet; 00030 00031 /** 00032 * A <code>CacheEntry</code> represents a SQL select request with its reponse. 00033 * The cache entry can have 3 states: 00034 * <p> 00035 * <ul> 00036 * <li><code>CACHE_VALID</code> when it is valid</li> 00037 * <li><code>CACHE_DIRTY</code> when the result has been marked dirty (may be 00038 * invalid)</li> 00039 * <li><code>CACHE_INVALID</code> when there is no result (request has to be 00040 * re-issued to the database)</li> 00041 * </ul> 00042 * 00043 * @author <a href="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a> 00044 * @version 1.0 00045 */ 00046 public abstract class ResultCacheEntry implements CacheEntry 00047 { 00048 protected SelectRequest request; 00049 protected ControllerResultSet result; 00050 protected int state; 00051 00052 private CacheEntry next; // Chain for LRU 00053 private CacheEntry prev; 00054 00055 /** 00056 * Creates a new <code>CacheEntry</code> instance. 00057 * 00058 * @param request a <code>SelectRequest</code> value 00059 * @param result a <code>ControllerResultSet</code> value 00060 */ 00061 public ResultCacheEntry(SelectRequest request, ControllerResultSet result) 00062 { 00063 this.request = request; 00064 this.result = result; 00065 state = CACHE_VALID; 00066 next = null; 00067 prev = null; 00068 } 00069 00070 /** 00071 * Get the type of this entry as a string 00072 * 00073 * @return NoCache or Eager or Relaxed 00074 */ 00075 public abstract String getType(); 00076 00077 /** 00078 * Get the state of this entry as a string 00079 * 00080 * @return Valid or Dirty or Invalid 00081 */ 00082 public String getState() 00083 { 00084 if (isValid()) 00085 return "Valid"; 00086 if (isDirty()) 00087 return "Dirty"; 00088 else 00089 return "Invalid"; 00090 } 00091 00092 /** 00093 * Return <code>true</code> if cache entry state is valid (state is 00094 * {@link #CACHE_VALID}). 00095 * 00096 * @return a <code>boolean</code> value 00097 */ 00098 public boolean isValid() 00099 { 00100 return state == CACHE_VALID; 00101 } 00102 00103 /** 00104 * Returns <code>true</code> if cache entry state is marked dirty (state is 00105 * {@link #CACHE_DIRTY}). 00106 * 00107 * @return a <code>boolean</code> value 00108 */ 00109 public boolean isDirty() 00110 { 00111 return state == CACHE_DIRTY; 00112 } 00113 00114 /** 00115 * Returns the <code>SELECT</code> request of this cache entry. 00116 * 00117 * @return a <code>SelectRequest</code> value 00118 */ 00119 public SelectRequest getRequest() 00120 { 00121 return request; 00122 } 00123 00124 /** 00125 * Returns the <code>ControllerResultSet</code> of the cached select request 00126 * 00127 * @return a <code>ControllerResultSet</code> value 00128 */ 00129 public ControllerResultSet getResult() 00130 { 00131 return result; 00132 } 00133 00134 /** 00135 * Set a new <code>ControllerResultSet</code> of the cached select request 00136 * (cache update). 00137 * <p> 00138 * The cache state is automatically set to valid ({@link #CACHE_VALID}). 00139 * 00140 * @param result a <code>ControllerResultSet</code> value 00141 */ 00142 public void setResult(ControllerResultSet result) 00143 { 00144 this.result = result; 00145 state = CACHE_VALID; 00146 } 00147 00148 /** 00149 * Invalidates this cache entry (removes the <code>ResultSet</code> and turn 00150 * state to {@link #CACHE_INVALID}). 00151 */ 00152 public abstract void invalidate(); 00153 00154 /** 00155 * Marks this entry dirty (state becomes {@link #CACHE_DIRTY}). 00156 * <p> 00157 * The <code>ResultSet</code> if not affected by this method. 00158 */ 00159 public void markDirty() 00160 { 00161 state = CACHE_DIRTY; 00162 } 00163 00164 /** 00165 * Marks this entry valid (state becomes {@link #CACHE_VALID}). 00166 */ 00167 public void setValid() 00168 { 00169 state = CACHE_VALID; 00170 } 00171 00172 /** 00173 * Gets the value of next <code>ResultCacheEntry</code> in LRU. 00174 * 00175 * @return value of next. 00176 */ 00177 public CacheEntry getNext() 00178 { 00179 return next; 00180 } 00181 00182 /** 00183 * Sets the value of next <code>ResultCacheEntry</code> in LRU. 00184 * 00185 * @param next value to assign to next. 00186 */ 00187 public void setNext(CacheEntry next) 00188 { 00189 this.next = next; 00190 } 00191 00192 /** 00193 * Gets the value of previous <code>ResultCacheEntry</code> in LRU. 00194 * 00195 * @return value of previous. 00196 */ 00197 public CacheEntry getPrev() 00198 { 00199 return prev; 00200 } 00201 00202 /** 00203 * Sets the value of previous <code>ResultCacheEntry</code> in LRU. 00204 * 00205 * @param prev value to assign to prev. 00206 */ 00207 public void setPrev(CacheEntry prev) 00208 { 00209 this.prev = prev; 00210 } 00211 00212 /** 00213 * Get data about this entry 00214 * 00215 * @return an array [request,type,status(valid,notvalid,dirty),deadLine] 00216 */ 00217 public abstract String[] toStringTable(); 00218 00219 /** 00220 * Size of the result in bytes 00221 * 00222 * @return an integer 00223 */ 00224 public int getSizeOfResult() 00225 { 00226 try 00227 { 00228 return CJDBCStream.countBytes(result); 00229 } 00230 catch (Exception e) 00231 { 00232 return -1; 00233 } 00234 } 00235 00236 }