1 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 ; 40 import java.sql.SQLException ; 41 42 43 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 (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 93 public Table getTable() 94 { 95 return _table; 96 } 97 98 101 public final long getBlockId() 102 { 103 return _blockId; 104 } 105 106 109 public final void setBlockId(long blockId) 110 { 111 _blockId = blockId; 112 } 113 114 117 public final long getRowAddress() 118 { 119 return _table.blockIdToAddress(_blockId) + _rowOffset; 120 } 121 122 125 public final int getRowOffset() 126 { 127 return _rowOffset; 128 } 129 130 133 public final void setRowOffset(int rowOffset) 134 { 135 _rowOffset = rowOffset; 136 } 137 138 141 public final byte []getBuffer() 142 { 143 return _buffer; 144 } 145 146 149 public Transaction getTransaction() 150 { 151 return _transaction; 152 } 153 154 157 public QueryContext getQueryContext() 158 { 159 return _queryContext; 160 } 161 162 public void init(QueryContext queryContext) 163 throws SQLException 164 { 165 init(queryContext.getTransaction()); 166 167 _queryContext = queryContext; 168 } 169 170 public void init(Transaction xa) 171 throws SQLException 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 190 } 191 192 public void initRow() 193 throws IOException 194 { 195 _rowOffset = -_rowLength; 196 } 197 198 public void prevRow() 199 { 200 _rowOffset -= _rowLength; 201 } 202 203 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 225 public boolean nextRow() 226 throws IOException 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 249 public boolean next() 250 throws IOException 251 { 252 do { 253 if (nextRow()) 254 return true; 255 } while (nextBlock()); 256 257 return false; 258 } 259 260 263 public boolean nextBlock() 264 throws IOException 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 294 public void setRow(long rowAddr) 295 throws IOException 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 319 public void initNullRow() 320 throws IOException 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 336 public boolean isNullRow() 337 { 338 return _buffer == _nullBuffer; 339 } 340 341 344 public boolean isNull(Column column) 345 throws SQLException 346 { 347 return column.isNull(_buffer, _rowOffset); 348 } 349 350 353 public String getString(Column column) 354 throws SQLException 355 { 356 return column.getString(_buffer, _rowOffset); 357 } 358 359 366 public int getInteger(Column column) 367 throws SQLException 368 { 369 return column.getInteger(_buffer, _rowOffset); 370 } 371 372 379 public long getLong(Column column) 380 throws SQLException 381 { 382 return column.getLong(_buffer, _rowOffset); 383 } 384 385 392 public double getDouble(Column column) 393 throws SQLException 394 { 395 return column.getDouble(_buffer, _rowOffset); 396 } 397 398 public boolean isEqual(Column column, byte []matchBuffer) 399 throws SQLException 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 407 { 408 return column.isEqual(_buffer, _rowOffset, 409 matchBuffer, 0, matchLength); 410 } 411 412 public boolean isEqual(Column column, String string) 413 throws SQLException 414 { 415 return column.isEqual(_buffer, _rowOffset, string); 416 } 417 418 421 public void evalToResult(Column column, SelectResult result) 422 throws SQLException 423 { 424 column.evalToResult(_buffer, _rowOffset, result); 425 } 426 427 public void delete() 428 throws SQLException 429 { 430 setDirty(); 431 _table.delete(_transaction, _block, _buffer, _rowOffset); 432 } 433 434 public void setDirty() 435 throws SQLException 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 |