KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > db > table > TableIterator


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  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.db.table;
31
32 import com.caucho.db.sql.QueryContext;
33 import com.caucho.db.sql.SelectResult;
34 import com.caucho.db.store.Block;
35 import com.caucho.db.store.Store;
36 import com.caucho.db.store.Transaction;
37 import com.caucho.util.L10N;
38
39 import java.io.IOException JavaDoc;
40 import java.sql.SQLException JavaDoc;
41
42
43 /**
44  * Iterates over a table's rows.
45  */

46 public class TableIterator {
47   private static final L10N L = new L10N(TableIterator.class);
48   
49   private final static byte []_nullBuffer = new byte[256];
50   
51   private Table _table;
52   private Column []_columns;
53   
54   private Transaction _transaction;
55   private QueryContext _queryContext;
56
57   private long _blockId;
58   private int _rowLength;
59   private int _rowEnd;
60   private int _rowOffset;
61
62   private Block _block;
63   private byte []_buffer;
64
65   public TableIterator()
66   {
67   }
68
69   TableIterator(Table table)
70   {
71     init(table);
72   }
73
74   public void init(Table table)
75   {
76     _table = table;
77     
78     if (table.getId() == 0) {
79       throw new IllegalStateException JavaDoc(L.l("iterating with closed table."));
80     }
81       
82     _columns = table.getColumns();
83
84     _rowLength = table.getRowLength();
85     _rowEnd = table.getRowEnd();
86     _rowOffset = _rowEnd;
87     _blockId = 0;
88   }
89
90   /**
91    * Returns the table of the iterator.
92    */

93   public Table getTable()
94   {
95     return _table;
96   }
97
98   /**
99    * Returns the current block id of the iterator.
100    */

101   public final long getBlockId()
102   {
103     return _blockId;
104   }
105
106   /**
107    * Sets the current block id of the iterator.
108    */

109   public final void setBlockId(long blockId)
110   {
111     _blockId = blockId;
112   }
113
114   /**
115    * Returns the current address.
116    */

117   public final long getRowAddress()
118   {
119     return _table.blockIdToAddress(_blockId) + _rowOffset;
120   }
121
122   /**
123    * Returns the current row offset of the iterator.
124    */

125   public final int getRowOffset()
126   {
127     return _rowOffset;
128   }
129
130   /**
131    * Sets the current row offset of the iterator.
132    */

133   public final void setRowOffset(int rowOffset)
134   {
135     _rowOffset = rowOffset;
136   }
137
138   /**
139    * Gets the current block.
140    */

141   public final byte []getBuffer()
142   {
143     return _buffer;
144   }
145
146   /**
147    * Returns the transaction for the iterator.
148    */

149   public Transaction getTransaction()
150   {
151     return _transaction;
152   }
153
154   /**
155    * Returns the query context for the iterator.
156    */

157   public QueryContext getQueryContext()
158   {
159     return _queryContext;
160   }
161
162   public void init(QueryContext queryContext)
163     throws SQLException JavaDoc
164   {
165     init(queryContext.getTransaction());
166
167     _queryContext = queryContext;
168   }
169
170   public void init(Transaction xa)
171     throws SQLException JavaDoc
172   {
173     Block block = _block;
174     _block = null;
175     _buffer = null;
176
177     if (block != null)
178       block.free();
179     
180     _blockId = 0;
181     _rowOffset = Integer.MAX_VALUE / 2;
182     _queryContext = null;
183     _transaction = xa;
184
185     // XXX:
186
/*
187     if (! _transaction.isAutoCommit())
188       _transaction.lockRead(_table.getLock());
189     */

190   }
191
192   public void initRow()
193     throws IOException JavaDoc
194   {
195     _rowOffset = -_rowLength;
196   }
197        
198   public void prevRow()
199   {
200     _rowOffset -= _rowLength;
201   }
202
203   /**
204    * Sets the row.
205    */

206   void setRow(Block block, int rowOffset)
207   {
208     _block = block;
209     _buffer = block.getBuffer();
210     _blockId = block.getBlockId();
211     _rowOffset = rowOffset;
212   }
213
214   public Block getBlock()
215   {
216     return _block;
217   }
218
219   /**
220    * Returns the next tuple in the current row.
221    *
222    * @return true if a tuple is found,
223    * or false if the block has no more tuples
224    */

225   public boolean nextRow()
226     throws IOException JavaDoc
227   {
228     int rowOffset = _rowOffset;
229     int rowLength = _rowLength;
230     int rowEnd = _rowEnd;
231     byte []buffer = _buffer;
232
233     rowOffset += rowLength;
234     for (; rowOffset < rowEnd; rowOffset += rowLength) {
235       if ((buffer[rowOffset] & Table.ROW_VALID) != 0) {
236     _rowOffset = rowOffset;
237     return true;
238       }
239     }
240
241     _rowOffset = rowOffset;
242
243     return false;
244   }
245
246   /**
247    * Returns the next row.
248    */

249   public boolean next()
250     throws IOException JavaDoc
251   {
252     do {
253       if (nextRow())
254     return true;
255     } while (nextBlock());
256
257     return false;
258   }
259
260   /**
261    * Returns the following block.
262    */

263   public boolean nextBlock()
264     throws IOException JavaDoc
265   {
266     byte []buffer = _buffer;
267
268     Block block = _block;
269     _block = null;
270     _buffer = null;
271
272     if (block != null)
273       block.free();
274
275     _blockId = _table.firstRow(_blockId + Table.BLOCK_SIZE);
276
277     if (_blockId < 0) {
278       return false;
279     }
280
281     block = _transaction.readBlock(_table, _blockId);
282
283     buffer = block.getBuffer();
284     _block = block;
285     _buffer = buffer;
286     _rowOffset = 0;
287
288     return true;
289   }
290
291   /**
292    * Sets the next row.
293    */

294   public void setRow(long rowAddr)
295     throws IOException JavaDoc
296   {
297     long blockId = _table.addressToBlockId(rowAddr);
298
299     if (blockId != _blockId) {
300       _blockId = blockId;
301     
302       Block block = _block;
303       _block = null;
304       _buffer = null;
305
306       if (block != null)
307     block.free();
308
309       _block = _transaction.readBlock(_table, _blockId);
310       _buffer = _block.getBuffer();
311     }
312     
313     _rowOffset = (int) (rowAddr & Store.BLOCK_OFFSET_MASK);
314   }
315
316   /**
317    * Sets the next row.
318    */

319   public void initNullRow()
320     throws IOException JavaDoc
321   {
322     Block block = _block;
323     _block = null;
324     _buffer = null;
325
326     if (block != null)
327       block.free();
328
329     _rowOffset = 0;
330     _buffer = _nullBuffer;
331   }
332
333   /**
334    * Returns true for the null for (for OUTER JOINs)
335    */

336   public boolean isNullRow()
337   {
338     return _buffer == _nullBuffer;
339   }
340
341   /**
342    * Returns true if the column is null.
343    */

344   public boolean isNull(Column column)
345     throws SQLException JavaDoc
346   {
347     return column.isNull(_buffer, _rowOffset);
348   }
349
350   /**
351    * Returns the string for the column at the given index.
352    */

353   public String JavaDoc getString(Column column)
354     throws SQLException JavaDoc
355   {
356     return column.getString(_buffer, _rowOffset);
357   }
358
359   /**
360    * Returns the column's value as an integer
361    *
362    * @param index column index in the row
363    *
364    * @return the integer value
365    */

366   public int getInteger(Column column)
367     throws SQLException JavaDoc
368   {
369     return column.getInteger(_buffer, _rowOffset);
370   }
371
372   /**
373    * Returns the column's long value.
374    *
375    * @param index column index in the row
376    *
377    * @return the long value
378    */

379   public long getLong(Column column)
380     throws SQLException JavaDoc
381   {
382     return column.getLong(_buffer, _rowOffset);
383   }
384
385   /**
386    * Returns the column's double value.
387    *
388    * @param index column index in the row
389    *
390    * @return the double value
391    */

392   public double getDouble(Column column)
393     throws SQLException JavaDoc
394   {
395     return column.getDouble(_buffer, _rowOffset);
396   }
397
398   public boolean isEqual(Column column, byte []matchBuffer)
399     throws SQLException JavaDoc
400   {
401     return column.isEqual(_buffer, _rowOffset,
402               matchBuffer, 0, matchBuffer.length);
403   }
404
405   public boolean isEqual(Column column, byte []matchBuffer, int matchLength)
406     throws SQLException JavaDoc
407   {
408     return column.isEqual(_buffer, _rowOffset,
409               matchBuffer, 0, matchLength);
410   }
411
412   public boolean isEqual(Column column, String JavaDoc string)
413     throws SQLException JavaDoc
414   {
415     return column.isEqual(_buffer, _rowOffset, string);
416   }
417
418   /**
419    * Evaluates the row to the result.
420    */

421   public void evalToResult(Column column, SelectResult result)
422     throws SQLException JavaDoc
423   {
424     column.evalToResult(_buffer, _rowOffset, result);
425   }
426
427   public void delete()
428     throws SQLException JavaDoc
429   {
430     setDirty();
431     _table.delete(_transaction, _block, _buffer, _rowOffset);
432   }
433   
434   public void setDirty()
435     throws SQLException JavaDoc
436   {
437     _transaction.addUpdateBlock(_block);
438
439     _block.setDirty(getRowOffset(), getRowOffset() + _rowLength);
440   }
441
442   public void free()
443   {
444     Block block = _block;
445     _block = null;
446
447     if (block != null)
448       block.free();
449   }
450 }
451
Popular Tags