KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > amber > query > ResultSetCacheChunk


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free Software Foundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.amber.query;
30
31 import com.caucho.amber.expr.AmberExpr;
32 import com.caucho.util.Alarm;
33
34 import java.lang.ref.SoftReference JavaDoc;
35 import java.sql.SQLException JavaDoc;
36 import java.util.ArrayList JavaDoc;
37
38 /**
39  * The JDBC statement implementation.
40  */

41 public class ResultSetCacheChunk {
42   public static final int CACHE_CHUNK_SIZE = 64;
43
44   private SelectQuery _query;
45
46   private ArrayList JavaDoc<FromItem> _fromList;
47   private ArrayList JavaDoc<AmberExpr> _resultList;
48
49   private int _startRow;
50
51   private final ArrayList JavaDoc<Object JavaDoc[]> _results = new ArrayList JavaDoc<Object JavaDoc[]>();
52
53   private SoftReference JavaDoc<ResultSetCacheChunk> _next;
54   private boolean _isLast;
55
56   private long _expireTime;
57
58   public ResultSetCacheChunk()
59   {
60   }
61
62   public ResultSetCacheChunk(ResultSetCacheChunk prev)
63   {
64     _startRow = prev._startRow + CACHE_CHUNK_SIZE;
65     _query = prev._query;
66     _fromList = prev._fromList;
67     _resultList = prev._resultList;
68
69     _expireTime = prev._expireTime;
70   }
71
72   /**
73    * Sets the query.
74    */

75   public void setQuery(SelectQuery query)
76   {
77     _query = query;
78
79     _fromList = query.getFromList();
80     _resultList = query.getResultList();
81
82     _expireTime = Alarm.getCurrentTime() + query.getCacheMaxAge();
83   }
84
85   /**
86    * Gets the query.
87    */

88   public SelectQuery getQuery()
89   {
90     return _query;
91   }
92
93   /**
94    * Returns the expire time.
95    */

96   public long getExpireTime()
97   {
98     return _expireTime;
99   }
100
101   /**
102    * Return true if the chunk is still valid.
103    */

104   public boolean isValid()
105   {
106     return Alarm.getCurrentTime() <= _expireTime;
107   }
108
109   /**
110    * Invalidates the chunk.
111    */

112   public void invalidate()
113   {
114     _expireTime = 0;
115     _next = null;
116   }
117
118   /**
119    * Invalidates the chunk based on a table and key.
120    */

121   public boolean invalidate(String JavaDoc table, Object JavaDoc key)
122   {
123     if (getQuery().invalidateTable(table)) {
124       invalidate();
125
126       return true;
127     }
128     else
129       return false;
130   }
131
132   /**
133    * Returns the number of rows.
134    */

135   public int getRowCount()
136   {
137     return _startRow + _results.size();
138   }
139
140   /**
141    * Adds a new row.
142    */

143   public void newRow()
144   {
145     _results.add(new Object JavaDoc[_resultList.size()]);
146   }
147
148   /**
149    * Sets a row value.
150    */

151   public void setValue(int row, int column, Object JavaDoc value)
152   {
153     _results.get(row % CACHE_CHUNK_SIZE)[column] = value;
154   }
155
156   /**
157    * Gets the next chunk
158    */

159   public ResultSetCacheChunk getNext()
160   {
161     SoftReference JavaDoc<ResultSetCacheChunk> nextRef = _next;
162
163     if (nextRef != null)
164       return nextRef.get();
165     else
166       return null;
167   }
168
169   /**
170    * Gets the next chunk
171    */

172   public void setNext(ResultSetCacheChunk next)
173   {
174     _next = new SoftReference JavaDoc<ResultSetCacheChunk>(next);
175   }
176
177   /**
178    * Sets true for the last.
179    */

180   public void setLast(boolean isLast)
181   {
182     _isLast = isLast;
183   }
184
185   /**
186    * True for the last.
187    */

188   public boolean isLast()
189   {
190     return _isLast;
191   }
192
193   /**
194    * Returns true if the last column read was null.
195    */

196   public boolean isNull(int row, int column)
197   {
198     return getObject(row, column) == null;
199   }
200
201   /**
202    * Returns the boolean value for the column.
203    */

204   public boolean getBoolean(int row, int column)
205     throws SQLException JavaDoc
206   {
207     Object JavaDoc object = getObject(row, column);
208
209     if (object instanceof Boolean JavaDoc)
210       return ((Boolean JavaDoc) object).booleanValue();
211     else if (object instanceof Number JavaDoc)
212       return ((Number JavaDoc) object).intValue() != 0;
213     else if (object instanceof String JavaDoc) {
214       String JavaDoc s = (String JavaDoc) object;
215
216       return s.startsWith("t") || s.startsWith("y");
217     }
218     else
219       return object != null;
220   }
221
222   /**
223    * Returns the byte value for the column.
224    */

225   public byte getByte(int row, int column)
226     throws SQLException JavaDoc
227   {
228     Object JavaDoc object = getObject(row, column);
229
230     if (object instanceof Byte JavaDoc)
231       return ((Byte JavaDoc) object).byteValue();
232     else if (object instanceof String JavaDoc)
233       return Byte.parseByte((String JavaDoc) object);
234     else if (object == null)
235       return 0;
236     else
237       return Byte.parseByte(String.valueOf(object));
238   }
239
240   /**
241    * Returns the int value for the column.
242    */

243   public int getInt(int row, int column)
244     throws SQLException JavaDoc
245   {
246     Object JavaDoc object = getObject(row, column);
247
248     if (object instanceof Number JavaDoc)
249       return ((Number JavaDoc) object).intValue();
250     else if (object instanceof String JavaDoc)
251       return Integer.parseInt((String JavaDoc) object);
252     else if (object == null)
253       return 0;
254     else
255       return Integer.parseInt(String.valueOf(object));
256   }
257
258   /**
259    * Returns the short value for the column.
260    */

261   public short getShort(int row, int column)
262     throws SQLException JavaDoc
263   {
264     Object JavaDoc object = getObject(row, column);
265
266     if (object instanceof Number JavaDoc)
267       return ((Number JavaDoc) object).shortValue();
268     else if (object instanceof String JavaDoc)
269       return Short.parseShort((String JavaDoc) object);
270     else if (object == null)
271       return 0;
272     else
273       return Short.parseShort(String.valueOf(object));
274   }
275
276   /**
277    * Returns the long value for the column.
278    */

279   public long getLong(int row, int column)
280     throws SQLException JavaDoc
281   {
282     Object JavaDoc object = getObject(row, column);
283
284     if (object instanceof Number JavaDoc)
285       return ((Number JavaDoc) object).longValue();
286     else if (object instanceof String JavaDoc)
287       return Long.parseLong((String JavaDoc) object);
288     else if (object instanceof java.sql.Date JavaDoc)
289       return ((java.sql.Date JavaDoc) object).getTime();
290     else if (object == null)
291       return 0;
292     else
293       return Long.parseLong(String.valueOf(object));
294   }
295
296   /**
297    * Returns the double value for the column.
298    */

299   public double getDouble(int row, int column)
300     throws SQLException JavaDoc
301   {
302     Object JavaDoc object = getObject(row, column);
303
304     if (object instanceof Number JavaDoc)
305       return ((Number JavaDoc) object).doubleValue();
306     else if (object instanceof String JavaDoc)
307       return Double.parseDouble((String JavaDoc) object);
308     else if (object == null)
309       return 0;
310     else
311       return Double.parseDouble(String.valueOf(object));
312   }
313
314   /**
315    * Returns the float value for the column.
316    */

317   public float getFloat(int row, int column)
318     throws SQLException JavaDoc
319   {
320     Object JavaDoc object = getObject(row, column);
321
322     if (object instanceof Number JavaDoc)
323       return ((Number JavaDoc) object).floatValue();
324     else if (object instanceof String JavaDoc)
325       return Float.parseFloat((String JavaDoc) object);
326     else if (object == null)
327       return 0;
328     else
329       return Float.parseFloat(String.valueOf(object));
330   }
331
332   /**
333    * Returns the string value for the column.
334    */

335   public String JavaDoc getString(int row, int column)
336     throws SQLException JavaDoc
337   {
338     Object JavaDoc object = getObject(row, column);
339
340     if (object instanceof String JavaDoc)
341       return (String JavaDoc) object;
342     else if (object == null)
343       return null;
344     else
345       return String.valueOf(object);
346   }
347
348   /**
349    * Returns the value.
350    */

351   public final Object JavaDoc getObject(int row, int column)
352   {
353     return _results.get(row % CACHE_CHUNK_SIZE)[column];
354   }
355 }
356
Popular Tags