1 package com.daffodilwoods.daffodildb.server.datasystem.indexsystem; 2 3 import java.util.*; 4 import com.daffodilwoods.daffodildb.server.datasystem.interfaces.*; 5 import com.daffodilwoods.daffodildb.server.datasystem.utility.*; 6 import com.daffodilwoods.daffodildb.server.sql99.common.*; 7 import com.daffodilwoods.daffodildb.server.sql99.utils.*; 8 import com.daffodilwoods.database.resource.*; 9 import com.daffodilwoods.daffodildb.server.datasystem.persistentsystem.DatabaseConstants; 10 import com.daffodilwoods.daffodildb.server.datasystem.btree.*; 11 import com.daffodilwoods.daffodildb.utils.field.FieldBase; 12 import com.daffodilwoods.daffodildb.utils.BufferRange; 13 import com.daffodilwoods.daffodildb.utils.comparator.SuperComparator; 14 import com.daffodilwoods.daffodildb.server.datasystem.persistentsystem.TableKey; 15 16 24 25 public class IndexTableIterator implements _IndexIterator,_UserTableOperations,_TableOperations, _SingleIterator, _IndexIteratorInfo, _UniqueIterator{ 26 27 30 31 private _IndexTableList indexTable; 32 33 36 private int index; 37 38 41 42 private _TableIterator iterator; 43 44 45 48 49 private BTreeNavigator btreeNavigator; 50 51 52 53 58 59 private int status; 60 61 _Index btree; 62 63 64 private static Object monitor = new Object (); 65 66 private ReadWriteLock readWriteLock; 67 68 public IndexTableIterator(_IndexTableList indexTable0,int index0,_TableIterator iterator0) { 69 indexTable = indexTable0; 70 index = index0; 71 iterator = iterator0; 72 btree = indexTable.getIndex(index); 73 btreeNavigator = btree.getNavigator(); 74 status = -1; 75 readWriteLock = indexTable.getLock() ; 76 } 77 78 79 84 85 public boolean first() throws DException { 86 try{ 87 readWriteLock.lockRowForRead(monitor); 88 return btreeNavigator.first() ? setKeyAndStatus(0,true,"first") : setKeyAndStatus(-1,false,"first"); 89 }finally{ 90 readWriteLock.releaseRowForRead(monitor); 91 } 92 } 93 94 99 100 public boolean last() throws DException { 101 try{ 102 readWriteLock.lockRowForRead(monitor); 103 return btreeNavigator.last() ? setKeyAndStatus(0,true,"last") 104 : setKeyAndStatus(1,false,"last"); 105 }finally{ 106 readWriteLock.releaseRowForRead(monitor); 107 } 108 } 109 110 116 117 public boolean next() throws DException { 118 if(status != 0) return status == 1 ? false : first(); 119 try{ 120 readWriteLock.lockRowForRead(monitor); 121 return btreeNavigator.next() ? setKeyAndStatus(0,true,"next") : setKeyAndStatus(1,false,"next"); 122 }finally{ 123 readWriteLock.releaseRowForRead(monitor); 124 } 125 126 } 127 128 134 135 public boolean previous() throws DException { 136 if(status != 0) return status == -1 ? false : last(); 137 try{ 138 readWriteLock.lockRowForRead(monitor); 139 return btreeNavigator.previous() ? setKeyAndStatus(0,true,"previous") : setKeyAndStatus(-1,false,"previous"); 140 }finally{ 141 readWriteLock.releaseRowForRead(monitor); 142 } 143 144 } 145 146 152 153 public void insert(Object value) throws DException { 154 try{ 155 readWriteLock.lockTable(); 156 Object key = indexTable.insert(iterator,(Object [])value,index); 157 move(key); 158 }finally{ 159 readWriteLock.releaseTable(); 160 } 161 } 162 163 164 169 170 public void update(Object value) throws DException { 171 try{ 172 readWriteLock.lockTable(); 173 if(status != 0){ 174 throw new DException("DSE2019",new Object [] {new Integer (status)}); 175 } 176 iterator.move(btreeNavigator.getValue()); 177 indexTable.update(iterator,(Object [])value,index); 178 }finally{ 179 readWriteLock.releaseTable(); 180 } 181 182 } 183 184 190 191 public void update(int[] columns, Object [] value) throws DException { 192 if(columns.length != value.length) 193 throw DatabaseConstants.COLUMN_VALUES_NOTEQUAL_EXCEPTION; 194 if(status != 0){ 195 throw new DException("DSE2019",new Object [] {new Integer (status)}); 196 } 197 Object key = null; 198 try{ 199 key = btreeNavigator.getValue(); 200 readWriteLock.lockTable(); 201 iterator.move(key); 202 indexTable.update(iterator,columns,value,index); 203 }finally{ 204 readWriteLock.releaseTable(); 205 206 } 207 } 208 209 213 214 public void delete() throws DException { 215 try{ 216 readWriteLock.lockTable(); 217 if(status != 0){ 218 throw new DException("DSE2019",new Object [] {new Integer (status)}); 219 } 220 iterator.move(btreeNavigator.getValue()); 221 indexTable.delete(iterator,index); 222 }finally{ 223 readWriteLock.releaseTable(); 224 } 225 226 } 227 228 233 234 235 public Object getKey() throws DException { 236 return btreeNavigator.getCurrentKey(); 237 } 238 239 245 public Object getColumnValues(int[] columns) throws DException { 246 if(status != 0) 247 throw new DException("DSE2019",new Object [] {new Integer (status)}); 248 try{ 249 readWriteLock.lockRowForRead(btreeNavigator.getValue()); 250 moveIterator(); 251 FieldBase[] buffers = columns == null ? (FieldBase[])indexTable.getColumnObjects(iterator) : (FieldBase[])indexTable.getColumnObjects(iterator,columns); 252 return buffers; 253 }finally{ 254 readWriteLock.releaseRowForRead(btreeNavigator.getValue()); 255 } 256 257 } 258 259 public boolean seekFromTop(_IndexPredicate[] condition ) throws DException { 260 try{ 261 readWriteLock.lockRowForRead(monitor); 262 _IndexKey key = (_IndexKey)btree.seekFromTopRelative(condition); 263 if(key != null){ 264 move(key); 265 return true; 266 } 267 return false; 268 }finally{ 269 readWriteLock.releaseRowForRead(monitor); 270 } 271 272 } 273 274 public boolean seekFromBottom(_IndexPredicate[] condition ) throws DException { 275 try{ 276 readWriteLock.lockRowForRead(monitor); 277 _IndexKey key = (_IndexKey)btree.seekFromBottomRelative(condition); 278 if(key != null){ 279 move(key); 280 return true; 281 } 282 return false; 283 }finally{ 284 readWriteLock.releaseRowForRead(monitor); 285 } 286 287 } 288 289 public Object [] show(boolean flag) throws DException{ 290 btree.showBTree(); 291 ArrayList list = new ArrayList(); 292 if(first()){ 293 do{ 294 Object [] value = (Object [])getColumnValues(); 295 if(flag) 296 ; list.add(value); 298 } while(next()); 299 }else 300 ; return list.size() == 0 ? new Object [0] : list.toArray(); 302 } 303 304 311 312 public void insert(_DatabaseUser user, Object value) throws DException { 313 try{ 314 readWriteLock.lockTable(); 315 Object key = indexTable.insert(iterator,user,(Object [])value,index); 316 move(key); 317 }finally{ 318 readWriteLock.releaseTable(); 319 } 320 } 321 322 328 329 public void update(_DatabaseUser user, Object value) throws DException { 330 try{ 331 readWriteLock.lockTable(); 332 if(status != 0){ 333 throw new DException("DSE2019",new Object [] {new Integer (status)}); 334 } 335 Object obj = btreeNavigator.getValue(); 336 iterator.move(obj); 337 indexTable.update(iterator,user,(Object [])value,index); 338 }finally{ 339 readWriteLock.releaseTable(); 340 } 341 342 } 343 344 351 352 public void update(_DatabaseUser user, int[] columns, Object [] value) throws DException { 353 if(columns.length != value.length) 354 throw DatabaseConstants.COLUMN_VALUES_NOTEQUAL_EXCEPTION; 355 if(status != 0){ 356 throw new DException("DSE2019",new Object [] {new Integer (status)}); 357 } 358 Object key = null; 359 try{ 360 key = btreeNavigator.getValue(); 361 readWriteLock.lockTable(); 362 iterator.move(key); 363 indexTable.update(iterator,user,columns,value,index); 364 }finally{ 365 readWriteLock.releaseTable(); 366 } 367 } 368 369 374 375 public void delete(_DatabaseUser user) throws DException { 376 try{ 377 readWriteLock.lockTable(); 378 if(status != 0){ 379 throw new DException("DSE2019",new Object [] {new Integer (status)}); 380 } 381 iterator.move(btreeNavigator.getValue()); 382 indexTable.delete(iterator,user,index); 383 }finally{ 384 readWriteLock.releaseTable(); 385 } 386 } 387 388 public _Record getRecord() throws DException { 389 _Record record = new Record(indexTable.getTableCharacteristics(),null); 390 record.setObject((Object [])getColumnValues()); 391 return record; 392 } 393 394 public Object getColumnValues(_Reference[] references) throws DException { 395 if(status != 0){ 396 throw new DException("DSE2019",new Object [] {new Integer (status)}); 397 } 398 int[] columns = new int[references.length]; 399 _TableCharacteristics tc = indexTable.getTableCharacteristics(); 400 for (int i = 0; i < references.length; i++) 401 columns[i] = getColumn(references[i],tc); 402 return getColumnValues(columns); 403 421 } 422 423 public boolean seek(Object indexKey ) throws DException { 424 try{ 425 readWriteLock.lockRowForRead(monitor); 426 Object key = indexTable.seek(btree,indexKey); 427 if(key != null){ 428 btreeNavigator.move(key); 429 return setKeyAndStatus(0,true,"seek"); 430 } 431 return false; 432 }finally{ 433 readWriteLock.releaseRowForRead(monitor); 434 } 435 436 } 437 438 public _KeyColumnInformation[] getKeyColumnInformations() throws DException { 439 String [] columns = btree.getIndexInformation().getColumns(); 440 boolean[] orders = btree.getIndexInformation().getOrderOfColumns(); 441 int len = columns.length; 442 _KeyColumnInformation[] columnInformations = new _KeyColumnInformation[len]; 443 _TableCharacteristics tc = indexTable.getTableCharacteristics(); 444 445 int[] columnSize = tc.getColumnSizes(); 446 int[] columnTypes = tc.getColumnTypes(); 447 ColumnDetails cd; 448 for (int i = 0; i < len; i++) { 449 cd = new ColumnDetails(); 450 cd.setColumn(new String [] {columns[i]}); 451 cd.setType(TypeConstants.REFERENCE); 452 cd.setDatatype(columnTypes[(tc.getIndexForColumnName(columns[i]))]); 453 cd.setSize(columnSize[tc.getIndexForColumnName(columns[i])]); 454 columnInformations[i] = new TableKeyColumnInformation(cd,orders[i]); 455 } 456 return columnInformations; 457 } 458 459 460 public Object [] getUniqueColumnReference() throws DException{ 461 return btree.getUniqueColumnReference(); 462 } 463 464 public int getBtreeIndex() throws DException{ 465 return index; 466 } 467 468 public SuperComparator getComparator() { 469 return btree.getComparator(); 470 } 471 472 public boolean readjust(Object updatedKey,Object secondKey) throws DException { 473 SuperComparator comparator = getComparator(); 474 if( comparator.compare(((_Key)updatedKey).getKeyValue(),btreeNavigator.getKey()) < 0 && comparator.compare(((_Key)updatedKey).getKeyValue(), ((_Key)secondKey).getKeyValue()) > 0){ 475 move(updatedKey); 476 return true; 477 } 478 return false; 479 } 480 481 public boolean seekFromTopRelative(Object indexKey) throws com.daffodilwoods.database.resource.DException { 482 try{ 483 readWriteLock.lockRowForRead(monitor); 484 Object kee = btreeNavigator.getCurrentKey(); 485 _IndexKey key = (_IndexKey)indexTable.seekFromTopRelative(btree,kee,indexKey); 486 if(key != null){ 487 if(kee == null) 488 btreeNavigator.move(key); 489 return setKeyAndStatus(0,true,"seekFromTopRelative"); 490 } 491 else{ 492 if(kee != null) 493 btreeNavigator.setNodeAndPosition(null,0); 494 return setKeyAndStatus(-1,false,"seekFromTopRelative"); 495 } 496 }finally{ 497 readWriteLock.releaseRowForRead(monitor); 498 } 499 500 } 501 502 public boolean seekFromBottomRelative(Object indexKey) throws com.daffodilwoods.database.resource.DException { 503 try{ 504 readWriteLock.lockRowForRead(monitor); 505 Object kee = btreeNavigator.getCurrentKey(); 506 _IndexKey key = (_IndexKey)indexTable.seekFromBottomRelative(btree,kee,indexKey); 507 if(key != null){ 508 if(kee == null) 509 btreeNavigator.move(key); 510 return setKeyAndStatus(0,true,"seekFromBottomRelative"); 511 } 512 else{ 513 if(kee != null) 514 btreeNavigator.setNodeAndPosition(null,0); 515 return setKeyAndStatus(-1,false,"seekFromBottomRelative"); 516 } 517 }finally{ 518 readWriteLock.releaseRowForRead(monitor); 519 } 520 521 } 522 523 public _TableCharacteristics getTableCharacteristics() throws DException{ 524 return indexTable.getTableCharacteristics(); 525 } 526 527 public _IndexInformation[] getUniqueInformation() throws DException{ 528 return indexTable.getIndexInformations(); 529 } 530 531 protected boolean setKeyAndStatus(int num,boolean flag,String str) throws DException{ 532 status = num; 533 return flag; 534 } 535 536 public boolean locateKey(Object indexKey,boolean flag) throws com.daffodilwoods.database.resource.DException { 537 try{ 538 readWriteLock.lockRowForRead(monitor); 539 _IndexKey key = (_IndexKey)indexTable.locateKey(btree,indexKey,flag); 540 if(key != null) 541 btreeNavigator.move(key); 542 else 543 btreeNavigator.move(btree.keyInstance() ); 544 if(flag) 545 return key != null ? setKeyAndStatus(0,true,"locateKey") : setKeyAndStatus(1,false,"locateKey"); 546 else 547 return key != null ? setKeyAndStatus(0,true,"locateKey") : setKeyAndStatus(-1,false,"locateKey"); 548 }finally{ 549 readWriteLock.releaseRowForRead(monitor); 550 } 551 552 } 553 554 public Object getColumnValues(int column) throws DException { 555 if(status != 0) 556 throw new DException("DSE2019",new Object [] {new Integer (status)}); 557 try{ 558 readWriteLock.lockRowForRead(btreeNavigator.getValue()); 559 try { 560 BufferRange buff = (BufferRange)btreeNavigator.getColumnValues(column); 561 return indexTable.getTableCharacteristics().getObject(column,buff); 562 } 563 catch (DException ex) { 564 if(!ex.getDseCode().equals("DSE2045")) 565 throw ex; 566 } 567 moveIterator(); 568 FieldBase buffer = (FieldBase)indexTable.getColumnObjects(iterator,column); 569 return buffer; 570 }finally{ 571 readWriteLock.releaseRowForRead(btreeNavigator.getValue()); 572 } 573 } 574 575 public Object getColumnValues() throws DException { 576 if(status != 0) 577 throw new DException("DSE2019",new Object [] {new Integer (status)}); 578 try{ 579 readWriteLock.lockRowForRead(btreeNavigator.getValue()); 580 moveIterator(); 581 return indexTable.getColumnObjects(iterator) ; 582 }finally{ 583 readWriteLock.releaseRowForRead(btreeNavigator.getValue()); 584 } 585 } 586 587 588 589 public Object getColumnValues(_Reference references) throws com.daffodilwoods.database.resource.DException { 590 if(status != 0){ 591 throw new DException("DSE2019",new Object [] {new Integer (status)}); 592 } 593 _TableCharacteristics tc = indexTable.getTableCharacteristics(); 594 return getColumnValues(getColumn(references,tc)); 595 611 } 612 613 public SuperComparator getObjectComparator() throws DException{ 614 return getComparator(); 615 } 616 617 public boolean seekKeyAddress(Object parm1) throws com.daffodilwoods.database.resource.DException { 618 iterator.move(parm1); 619 Object seekKey = iterator.getColumnValues((int[])getUniqueColumnReference()[0]); 620 return seek(seekKey); 621 } 622 623 public Object getPhysicalAddress() throws com.daffodilwoods.database.resource.DException { 624 return btreeNavigator.getValue(); 625 } 626 627 public void ensureRecordInMemory() throws com.daffodilwoods.database.resource.DException { 628 629 throw new java.lang.UnsupportedOperationException ("Method ensureRecordInMemory() not yet implemented."); 630 } 631 632 public void moveOnActualKey(Object parm1) throws com.daffodilwoods.database.resource.DException { 633 634 throw new java.lang.UnsupportedOperationException ("Method moveOnActualKey() not yet implemented."); 635 } 636 637 public Object getActualKey() throws com.daffodilwoods.database.resource.DException { 638 639 throw new java.lang.UnsupportedOperationException ("Method getActualKey() not yet implemented."); 640 } 641 642 private void moveIterator()throws DException{ 643 if(status != 0) 644 throw new DException("DSE2019",new Object [] {new Integer (status)}); 645 iterator.move(btreeNavigator.getValue()); 646 } 647 648 private int getColumn(_Reference references,_TableCharacteristics tc )throws DException{ 649 int column = -1; 650 try{ 651 column = references.getIndex(); 652 } 653 catch(DException de) { 654 if(!de.getDseCode().equalsIgnoreCase("DSE565")) 655 throw de; 656 column = tc.getIndexForColumnName(references.getColumn()); 657 references.setIndex(column); 658 } 659 return column; 660 } 661 662 public void move(Object value)throws DException{ 663 btreeNavigator.move(value); 664 status = 0; 665 } 666 667 public FieldBase field(_Reference reference) throws com.daffodilwoods.database.resource.DException { 668 if(status != 0){ 669 throw new DException("DSE2019",new Object [] {new Integer (status)}); 670 } 671 return field(getColumn(reference,indexTable.getTableCharacteristics())); 672 673 689 } 690 691 public FieldBase[] fields(_Reference[] references) throws com.daffodilwoods.database.resource.DException { 692 int[] columns = new int[references.length]; 693 _TableCharacteristics tc = indexTable.getTableCharacteristics(); 694 for (int i = 0; i < references.length; i++) 695 columns[i] = getColumn(references[i],tc); 696 return fields(columns); 697 715 } 716 717 public FieldBase[] fields(int[] columns) throws com.daffodilwoods.database.resource.DException { 718 return (FieldBase[])getColumnValues(columns); 719 730 } 731 732 public FieldBase field(int column) throws DException { 733 return (FieldBase)getColumnValues(column); 734 745 } 746 747 public void setFieldsValue(int[] columns, FieldBase[] values) throws DException { 748 try{ 749 readWriteLock.lockRowForRead(btreeNavigator.getValue()); 750 moveIterator(); 751 Object [] buffers = (Object [])iterator.getColumnValues(columns); 752 for (int i = 0; i < buffers.length; i++) { 753 try { 754 values[i].setBufferRange((BufferRange)buffers[i]); 755 } 756 catch (ClassCastException ex) { 757 values[i] = (FieldBase)buffers[i]; } 759 } 760 }finally{ 761 readWriteLock.releaseRowForRead(btreeNavigator.getValue()); 762 } 763 764 } 765 766 public void setFieldValue(int column, FieldBase value) throws DException { 767 try{ 768 readWriteLock.lockRowForRead(btreeNavigator.getValue()); 769 try { 770 value.setBufferRange((BufferRange)btreeNavigator.getColumnValues(column)); 771 }catch (DException ex) { 772 if(!ex.getDseCode().equals("DSE2045")) 773 throw ex; 774 } 775 moveIterator(); 776 Object buffer = iterator.getColumnValues(column); 777 try { 778 value.setBufferRange((BufferRange)buffer); 779 } 780 catch (ClassCastException ex) { 781 value = (FieldBase)buffer; 782 } 783 }finally{ 784 readWriteLock.releaseRowForRead(btreeNavigator.getValue()); 785 } 786 } 787 788 public _Index getBtree(){ 789 return btree; 790 } 791 792 public byte[] getByteKey() throws DException{ 793 throw new java.lang.UnsupportedOperationException ( 794 "Method getByteKey() not yet implemented."); 795 } 796 public void moveByteKey(byte[] key) throws DException{ 797 throw new java.lang.UnsupportedOperationException ( 798 "Method moveByteKey() not yet implemented."); 799 } 800 801 public boolean hasAnyRecords() throws DException { 802 return btree.getSize() > 0; 803 804 } 805 public void deleteBlobClobRecord(_DatabaseUser user) throws DException { 806 try{ 807 readWriteLock.lockTable(); 808 if(status != 0){ 809 throw new DException("DSE2019",new Object [] {new Integer (status)}); 810 } 811 iterator.move(btreeNavigator.getValue()); 812 indexTable.deleteBlobClobRecord(iterator,user,index); 813 }finally{ 814 readWriteLock.releaseTable(); 815 } 816 817 } 818 public _TableIterator getUnderlyingIterator(){ 819 return iterator ; 820 } 821 822 public _IndexTableList getIndexTable(){ 823 return indexTable; 824 } 825 826 827 } 828 | Popular Tags |