1 19 package org.netbeans.mdr.persistence.btreeimpl.btreeindex; 20 21 import org.netbeans.mdr.persistence.*; 22 import java.util.*; 23 import java.io.*; 24 25 32 public class BtreeIterator extends Object implements Iterator { 33 34 protected Btree btree; 35 protected SearchResult current; 36 protected BtreePageSource pageSource; 37 protected int modCount; 38 39 BtreeIterator(Btree btree) { 40 try { 41 this.btree = btree; 42 pageSource = btree.pageSource; 43 btree.beginRead(); 44 modCount = btree.modCount; 45 current = btree.getFirst(); 46 if (current.entryNum >= 0) { 47 current.entryNum--; 48 } 49 } catch (StorageException e) { 50 throw new RuntimeStorageException(e); 51 } finally { 52 btree.endRead(); 53 } 54 } 55 56 61 public boolean hasNext() { 62 63 try { 64 btree.beginRead(); 65 checkModCount(); 66 if (!BtreePage.hasNext(null, current)) { 67 pageSource.unpinPage(current.page); 68 current.page = null; 69 return false; 70 } else { 71 return true; 72 } 73 } catch (StorageException e) { 74 throw new RuntimeStorageException(e); 75 } finally { 76 btree.endRead(); 77 } 78 } 79 80 90 public Object next() throws NoSuchElementException { 91 92 BtreePage oldPage; 93 94 if (current.page == null) { 95 throw new NoSuchElementException(); 96 } 97 try { 98 btree.beginRead(); 99 checkModCount(); 100 oldPage = current.page; 101 BtreePage.getNext(null, current); 102 if (current.page != oldPage) { 103 pageSource.unpinPage(oldPage); 104 } 105 if (current.matched) { 106 return getCurrentItem(); 107 } else { 108 throw new NoSuchElementException(); 109 } 110 } catch (StorageException e) { 111 throw new RuntimeStorageException(e); 112 } finally { 113 btree.endRead(); 114 } 115 } 116 117 120 Object getCurrentItem() throws StorageException { 121 return btree.dataInfo.fromBuffer( 122 current.page.getData(current.entryNum)); 123 } 124 125 void checkModCount() { 126 if (btree.modCount > modCount) { 127 throw new ConcurrentModificationException("Index " + btree.getName() 128 + " has been modified since iterator was created."); 129 } 130 } 131 132 135 protected void finalize() { 136 if (current.page != null) { 137 pageSource.unpinPage(current.page); 138 current.page = null; 139 } 140 } 141 142 147 public void remove() throws UnsupportedOperationException { 148 throw new UnsupportedOperationException (); 149 } 150 } 151 | Popular Tags |