1 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 ; 35 import java.sql.SQLException ; 36 import java.util.ArrayList ; 37 38 41 public class ResultSetCacheChunk { 42 public static final int CACHE_CHUNK_SIZE = 64; 43 44 private SelectQuery _query; 45 46 private ArrayList <FromItem> _fromList; 47 private ArrayList <AmberExpr> _resultList; 48 49 private int _startRow; 50 51 private final ArrayList <Object []> _results = new ArrayList <Object []>(); 52 53 private SoftReference <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 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 88 public SelectQuery getQuery() 89 { 90 return _query; 91 } 92 93 96 public long getExpireTime() 97 { 98 return _expireTime; 99 } 100 101 104 public boolean isValid() 105 { 106 return Alarm.getCurrentTime() <= _expireTime; 107 } 108 109 112 public void invalidate() 113 { 114 _expireTime = 0; 115 _next = null; 116 } 117 118 121 public boolean invalidate(String table, Object key) 122 { 123 if (getQuery().invalidateTable(table)) { 124 invalidate(); 125 126 return true; 127 } 128 else 129 return false; 130 } 131 132 135 public int getRowCount() 136 { 137 return _startRow + _results.size(); 138 } 139 140 143 public void newRow() 144 { 145 _results.add(new Object [_resultList.size()]); 146 } 147 148 151 public void setValue(int row, int column, Object value) 152 { 153 _results.get(row % CACHE_CHUNK_SIZE)[column] = value; 154 } 155 156 159 public ResultSetCacheChunk getNext() 160 { 161 SoftReference <ResultSetCacheChunk> nextRef = _next; 162 163 if (nextRef != null) 164 return nextRef.get(); 165 else 166 return null; 167 } 168 169 172 public void setNext(ResultSetCacheChunk next) 173 { 174 _next = new SoftReference <ResultSetCacheChunk>(next); 175 } 176 177 180 public void setLast(boolean isLast) 181 { 182 _isLast = isLast; 183 } 184 185 188 public boolean isLast() 189 { 190 return _isLast; 191 } 192 193 196 public boolean isNull(int row, int column) 197 { 198 return getObject(row, column) == null; 199 } 200 201 204 public boolean getBoolean(int row, int column) 205 throws SQLException 206 { 207 Object object = getObject(row, column); 208 209 if (object instanceof Boolean ) 210 return ((Boolean ) object).booleanValue(); 211 else if (object instanceof Number ) 212 return ((Number ) object).intValue() != 0; 213 else if (object instanceof String ) { 214 String s = (String ) object; 215 216 return s.startsWith("t") || s.startsWith("y"); 217 } 218 else 219 return object != null; 220 } 221 222 225 public byte getByte(int row, int column) 226 throws SQLException 227 { 228 Object object = getObject(row, column); 229 230 if (object instanceof Byte ) 231 return ((Byte ) object).byteValue(); 232 else if (object instanceof String ) 233 return Byte.parseByte((String ) object); 234 else if (object == null) 235 return 0; 236 else 237 return Byte.parseByte(String.valueOf(object)); 238 } 239 240 243 public int getInt(int row, int column) 244 throws SQLException 245 { 246 Object object = getObject(row, column); 247 248 if (object instanceof Number ) 249 return ((Number ) object).intValue(); 250 else if (object instanceof String ) 251 return Integer.parseInt((String ) object); 252 else if (object == null) 253 return 0; 254 else 255 return Integer.parseInt(String.valueOf(object)); 256 } 257 258 261 public short getShort(int row, int column) 262 throws SQLException 263 { 264 Object object = getObject(row, column); 265 266 if (object instanceof Number ) 267 return ((Number ) object).shortValue(); 268 else if (object instanceof String ) 269 return Short.parseShort((String ) object); 270 else if (object == null) 271 return 0; 272 else 273 return Short.parseShort(String.valueOf(object)); 274 } 275 276 279 public long getLong(int row, int column) 280 throws SQLException 281 { 282 Object object = getObject(row, column); 283 284 if (object instanceof Number ) 285 return ((Number ) object).longValue(); 286 else if (object instanceof String ) 287 return Long.parseLong((String ) object); 288 else if (object instanceof java.sql.Date ) 289 return ((java.sql.Date ) object).getTime(); 290 else if (object == null) 291 return 0; 292 else 293 return Long.parseLong(String.valueOf(object)); 294 } 295 296 299 public double getDouble(int row, int column) 300 throws SQLException 301 { 302 Object object = getObject(row, column); 303 304 if (object instanceof Number ) 305 return ((Number ) object).doubleValue(); 306 else if (object instanceof String ) 307 return Double.parseDouble((String ) object); 308 else if (object == null) 309 return 0; 310 else 311 return Double.parseDouble(String.valueOf(object)); 312 } 313 314 317 public float getFloat(int row, int column) 318 throws SQLException 319 { 320 Object object = getObject(row, column); 321 322 if (object instanceof Number ) 323 return ((Number ) object).floatValue(); 324 else if (object instanceof String ) 325 return Float.parseFloat((String ) object); 326 else if (object == null) 327 return 0; 328 else 329 return Float.parseFloat(String.valueOf(object)); 330 } 331 332 335 public String getString(int row, int column) 336 throws SQLException 337 { 338 Object object = getObject(row, column); 339 340 if (object instanceof String ) 341 return (String ) object; 342 else if (object == null) 343 return null; 344 else 345 return String.valueOf(object); 346 } 347 348 351 public final Object getObject(int row, int column) 352 { 353 return _results.get(row % CACHE_CHUNK_SIZE)[column]; 354 } 355 } 356 | Popular Tags |