KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > controller > cache > result > entries > AbstractResultCacheEntry


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Copyright (C) 2005 AmicoSoft, Inc. dba Emic Networks
6  * Contact: sequoia@continuent.org
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  * Initial developer(s): Emmanuel Cecchet.
21  * Contributor(s): ______________________________________.
22  */

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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