KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > controller > cache > result > entries > AbstractResultCacheEntry


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2005 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Emmanuel Cecchet.
22  * Contributor(s): ______________________________________.
23  */

24
25 package org.objectweb.cjdbc.controller.cache.result.entries;
26
27 import org.objectweb.cjdbc.common.sql.SelectRequest;
28 import org.objectweb.cjdbc.common.stream.CJDBCStream;
29 import org.objectweb.cjdbc.controller.virtualdatabase.ControllerResultSet;
30
31 /**
32  * A <code>CacheEntry</code> represents a SQL select request with its reponse.
33  * The cache entry can have 3 states:
34  * <p>
35  * <ul>
36  * <li><code>CACHE_VALID</code> when it is valid</li>
37  * <li><code>CACHE_DIRTY</code> when the result has been marked dirty (may be
38  * invalid)</li>
39  * <li><code>CACHE_INVALID</code> when there is no result (request has to be
40  * re-issued to the database)</li>
41  * </ul>
42  *
43  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
44  * @version 1.0
45  */

46 public abstract class AbstractResultCacheEntry
47 {
48   protected SelectRequest request;
49   protected ControllerResultSet result;
50   protected int state;
51
52   //Chain for LRU
53
private AbstractResultCacheEntry next;
54   private AbstractResultCacheEntry prev;
55
56   /** This entry has no deadline */
57   public static final int NO_DEADLINE = -1;
58   /** State this entry is valid */
59   public static final int CACHE_VALID = 0;
60   /** This entry is dirty */
61   public static final int CACHE_DIRTY = 1;
62   /** This entry is no more valid and is not consistent with real data. */
63   public static final int CACHE_INVALID = 2;
64
65   /**
66    * Creates a new <code>CacheEntry</code> instance.
67    *
68    * @param request a <code>SelectRequest</code> value
69    * @param result a <code>ControllerResultSet</code> value
70    */

71   public AbstractResultCacheEntry(SelectRequest request,
72       ControllerResultSet result)
73   {
74     this.request = request;
75     this.result = result;
76     state = CACHE_VALID;
77     next = null;
78     prev = null;
79   }
80
81   /**
82    * Get the type of this entry as a string
83    *
84    * @return NoCache or Eager or Relaxed
85    */

86   public abstract String JavaDoc getType();
87
88   /**
89    * Get the state of this entry as a string
90    *
91    * @return Valid or Dirty or Invalid
92    */

93   public String JavaDoc getState()
94   {
95     if (isValid())
96       return "Valid";
97     if (isDirty())
98       return "Dirty";
99     else
100       return "Invalid";
101   }
102
103   /**
104    * Return <code>true</code> if cache entry state is valid (state is
105    * {@link #CACHE_VALID}).
106    *
107    * @return a <code>boolean</code> value
108    */

109   public boolean isValid()
110   {
111     return state == CACHE_VALID;
112   }
113
114   /**
115    * Returns <code>true</code> if cache entry state is marked dirty (state is
116    * {@link #CACHE_DIRTY}).
117    *
118    * @return a <code>boolean</code> value
119    */

120   public boolean isDirty()
121   {
122     return state == CACHE_DIRTY;
123   }
124
125   /**
126    * Returns the <code>SELECT</code> request of this cache entry.
127    *
128    * @return a <code>SelectRequest</code> value
129    */

130   public SelectRequest getRequest()
131   {
132     return request;
133   }
134
135   /**
136    * Returns the <code>ControllerResultSet</code> of the cached select request
137    *
138    * @return a <code>ControllerResultSet</code> value
139    */

140   public ControllerResultSet getResult()
141   {
142     return result;
143   }
144
145   /**
146    * Set a new <code>ControllerResultSet</code> of the cached select request
147    * (cache update).
148    * <p>
149    * The cache state is automatically set to valid ({@link #CACHE_VALID}).
150    *
151    * @param result a <code>ControllerResultSet</code> value
152    */

153   public void setResult(ControllerResultSet result)
154   {
155     this.result = result;
156     state = CACHE_VALID;
157   }
158
159   /**
160    * Invalidates this cache entry (removes the <code>ResultSet</code> and turn
161    * state to {@link #CACHE_INVALID}).
162    */

163   public abstract void invalidate();
164
165   /**
166    * Marks this entry dirty (state becomes {@link #CACHE_DIRTY}).
167    * <p>
168    * The <code>ResultSet</code> if not affected by this method.
169    */

170   public void markDirty()
171   {
172     state = CACHE_DIRTY;
173   }
174
175   /**
176    * Marks this entry valid (state becomes {@link #CACHE_VALID}).
177    */

178   public void setValid()
179   {
180     state = CACHE_VALID;
181   }
182
183   /**
184    * Gets the value of next <code>AbstractResultCacheEntry</code> in LRU.
185    *
186    * @return value of next.
187    */

188   public AbstractResultCacheEntry getNext()
189   {
190     return next;
191   }
192
193   /**
194    * Sets the value of next <code>AbstractResultCacheEntry</code> in LRU.
195    *
196    * @param next value to assign to next.
197    */

198   public void setNext(AbstractResultCacheEntry next)
199   {
200     this.next = next;
201   }
202
203   /**
204    * Gets the value of previous <code>AbstractResultCacheEntry</code> in LRU.
205    *
206    * @return value of previous.
207    */

208   public AbstractResultCacheEntry getPrev()
209   {
210     return prev;
211   }
212
213   /**
214    * Sets the value of previous <code>AbstractResultCacheEntry</code> in LRU.
215    *
216    * @param prev value to assign to prev.
217    */

218   public void setPrev(AbstractResultCacheEntry prev)
219   {
220     this.prev = prev;
221   }
222
223   /**
224    * Get data about this entry
225    *
226    * @return an array [request,type,status(valid,notvalid,dirty),deadLine]
227    */

228   public abstract String JavaDoc[] toStringTable();
229
230   /**
231    * Size of the result in bytes
232    *
233    * @return an integer
234    */

235   public int getSizeOfResult()
236   {
237     try
238     {
239       return CJDBCStream.countBytes(result);
240     }
241     catch (Exception JavaDoc e)
242     {
243       return -1;
244     }
245   }
246
247 }
Popular Tags