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 31 public class BtreeKeySet extends AbstractSet { 32 33 private Btree btree; 34 35 BtreeKeySet(Btree btree) { 36 this.btree = btree; 37 } 38 39 public int size() { 40 int count = 0; 41 BtreeKeyIterator i = new BtreeKeyIterator(); 42 while (i.hasNext()) { 43 i.nextKey(); 44 count++; 45 } 46 return count; 47 } 48 49 public boolean isEmpty() { 50 return !iterator().hasNext(); 51 } 52 53 public Iterator iterator() { 54 return new BtreeKeyIterator(); 55 } 56 57 60 public class BtreeKeyIterator extends Object implements Iterator { 61 62 protected SearchResult current; 63 protected BtreePageSource pageSource; 64 protected int modCount; 65 66 BtreeKeyIterator() { 67 try { 68 btree.beginRead(); 69 modCount = btree.modCount; 70 pageSource = btree.pageSource; 71 current = btree.getFirst(); 72 if (current.entryNum >= 0) { 73 current.entryNum--; 74 } 75 } catch (StorageException e) { 76 throw new RuntimeStorageException(e); 77 } finally { 78 btree.endRead(); 79 } 80 } 81 82 87 public boolean hasNext() { 88 89 boolean result; 90 91 try { 92 btree.beginRead(); 93 checkModCount(); 94 if (!BtreePage.hasNext(null, current)) { 95 pageSource.unpinPage(current.page); 96 current.page = null; 97 result = false; 98 } else { 99 result = true; 100 } 101 } catch (StorageException se) { 102 throw new RuntimeStorageException(se); 103 } finally { 104 btree.endRead(); 105 } 106 return result; 107 } 108 109 118 public Object next() throws NoSuchElementException { 119 120 byte[] key = nextKey(); 121 return btree.keyInfo.fromBuffer(key); 122 } 123 124 private byte[] nextKey() throws NoSuchElementException { 125 126 BtreePage oldPage; 127 byte[] key; 128 129 if (current.page == null) { 130 throw new NoSuchElementException(); 131 } 132 try { 133 btree.beginRead(); 134 checkModCount(); 135 oldPage = current.page; 136 BtreePage.getNext(null, current); 137 if (current.page != oldPage) { 138 pageSource.unpinPage(oldPage); 139 } 140 if (current.matched) { 141 key = current.page.getKey(current.entryNum); 142 } else { 143 throw new NoSuchElementException(); 144 } 145 146 while (BtreePage.hasNext(key, current)) { 147 oldPage = current.page; 148 BtreePage.getNext(null, current); 149 if (current.page != oldPage) { 150 pageSource.unpinPage(oldPage); 151 } 152 } 153 } catch (StorageException e) { 154 throw new RuntimeStorageException(e); 155 } finally { 156 btree.endRead(); 157 } 158 return key; 159 } 160 161 private void checkModCount() { 162 if (btree.modCount > modCount) { 163 throw new ConcurrentModificationException("Index " 164 + btree.getName() 165 + " has been modified since iterator was created."); 166 } 167 } 168 169 172 protected void finalize() { 173 if (current.page != null) { 174 pageSource.unpinPage(current.page); 175 current.page = null; 176 } 177 } 178 179 184 public void remove() throws UnsupportedOperationException { 185 throw new UnsupportedOperationException (); 186 } 187 } 188 } 189 | Popular Tags |