1 11 package org.eclipse.core.internal.indexing; 12 13 public class IndexCursor { 14 15 private IndexedStore store; 16 private ObjectAddress anchorAddress; 17 private int entryNumber; 18 private IndexNode leafNode; 19 private boolean entryRemoved; 20 21 25 IndexCursor(IndexedStore store, ObjectAddress anchorAddress) { 26 this.anchorAddress = anchorAddress; 27 this.store = store; 28 this.leafNode = null; 29 this.entryNumber = -1; 30 } 31 32 37 private void adjust() throws IndexedStoreException { 38 if (leafNode == null) 39 return; 40 if (entryNumber >= leafNode.getNumberOfEntries()) { 41 ObjectAddress next = leafNode.getNextAddress(); 42 int n = entryNumber - leafNode.getNumberOfEntries(); 43 set(next, n); 44 } else if (entryNumber < 0) { 45 ObjectAddress previous = leafNode.getPreviousAddress(); 46 int n = entryNumber; 47 set(previous, n); 48 } else { 49 } 50 } 51 52 56 public void close() throws IndexedStoreException { 57 reset(); 58 } 59 60 64 void entryInserted(int i) throws IndexedStoreException { 65 if (entryNumber >= i) 66 entryNumber++; 67 adjust(); 68 } 69 70 73 void entryRemoved(int i) throws IndexedStoreException { 74 entryRemoved = (entryNumber == i); 75 if (entryNumber > i) 76 entryNumber--; 77 adjust(); 78 } 79 80 85 public synchronized IndexCursor find(byte[] b) throws IndexedStoreException { 86 IndexAnchor anchor = store.acquireAnchor(anchorAddress); 87 anchor.find(b, this); 88 anchor.release(); 89 entryRemoved = false; 90 return this; 91 } 92 93 98 public synchronized IndexCursor find(String s) throws IndexedStoreException { 99 return find(Convert.toUTF8(s)); 100 } 101 102 105 public synchronized IndexCursor findFirstEntry() throws IndexedStoreException { 106 IndexAnchor anchor = store.acquireAnchor(anchorAddress); 107 anchor.findFirstEntry(this); 108 anchor.release(); 109 entryRemoved = false; 110 return this; 111 } 112 113 120 public synchronized byte[] getKey() throws IndexedStoreException { 121 if (entryRemoved) 122 throw new IndexedStoreException(IndexedStoreException.EntryRemoved); 123 if (leafNode == null) 124 return null; 125 byte[] key = leafNode.getKey(entryNumber); 126 return key; 127 } 128 129 136 public synchronized byte[] getValue() throws IndexedStoreException { 137 if (entryRemoved) 138 throw new IndexedStoreException(IndexedStoreException.EntryRemoved); 139 if (leafNode == null) 140 return null; 141 byte[] value = leafNode.getValue(entryNumber); 142 return value; 143 } 144 145 149 ObjectAddress getValueAsObjectAddress() throws IndexedStoreException { 150 byte[] value = getValue(); 151 if (value == null) 152 return null; 153 return new ObjectAddress(value); 154 } 155 156 160 public synchronized ObjectID getValueAsObjectID() throws IndexedStoreException { 161 byte[] value = getValue(); 162 if (value == null) 163 return null; 164 return new ObjectID(value); 165 } 166 167 170 public synchronized boolean isAtBeginning() throws IndexedStoreException { 171 if (entryRemoved) 172 throw new IndexedStoreException(IndexedStoreException.EntryRemoved); 173 return (leafNode == null); 174 } 175 176 180 public synchronized boolean isSet() throws IndexedStoreException { 181 if (entryRemoved) 182 throw new IndexedStoreException(IndexedStoreException.EntryRemoved); 183 return !(leafNode == null); 184 } 185 186 193 public synchronized boolean keyEquals(byte[] b) throws IndexedStoreException { 194 if (entryRemoved) 195 throw new IndexedStoreException(IndexedStoreException.EntryRemoved); 196 if (leafNode == null) 197 return false; 198 byte[] key = leafNode.getKey(entryNumber); 199 if (b.length != key.length) { 200 return false; 201 } 202 for (int i = 0; i < b.length; i++) { 203 if (key[i] != b[i]) { 204 return false; 205 } 206 } 207 return true; 208 } 209 210 218 public synchronized boolean keyMatches(byte[] b) throws IndexedStoreException { 219 if (entryRemoved) 220 throw new IndexedStoreException(IndexedStoreException.EntryRemoved); 221 if (leafNode == null) 222 return false; 223 byte[] key = leafNode.getKey(entryNumber); 224 if (key.length < b.length) { 225 return false; 226 } 227 for (int i = 0; i < b.length; i++) { 228 if (key[i] != b[i]) { 229 return false; 230 } 231 } 232 return true; 233 } 234 235 240 public synchronized boolean keyMatches(String s) throws IndexedStoreException { 241 return keyMatches(Convert.toUTF8(s)); 242 } 243 244 253 public synchronized IndexCursor next() throws IndexedStoreException { 254 if (isAtBeginning()) { 255 findFirstEntry(); 256 } else { 257 entryNumber++; 258 adjust(); 259 } 260 return this; 261 } 262 263 267 void nodeSplit() throws IndexedStoreException { 268 adjust(); 269 } 270 271 281 public synchronized void remove() throws IndexedStoreException { 282 removeEntry(); 283 } 284 285 295 void removeEntry() throws IndexedStoreException { 296 if (entryRemoved) 297 throw new IndexedStoreException(IndexedStoreException.EntryRemoved); 298 if (leafNode == null) 299 return; 300 ObjectAddress address = leafNode.getAddress(); 301 leafNode.removeEntry(entryNumber); 302 entryRemoved = false; 304 305 while (!address.isNull()) { 306 IndexNode node = store.acquireNode(address); 307 if (node.getNumberOfEntries() > 0) { 308 node.release(); 309 break; 310 } 311 ObjectAddress parentAddress = node.getParentAddress(); 312 node.unlink(); 313 node.release(); 314 store.removeObject(address); 315 address = parentAddress; 316 } 317 } 318 319 322 public synchronized void reset() throws IndexedStoreException { 323 unset(); 324 entryRemoved = false; 325 } 326 327 330 void set(ObjectAddress leafNodeAddress, int entryNumber) throws IndexedStoreException { 331 unset(); 332 if (leafNodeAddress.isNull()) 333 return; 334 leafNode = store.acquireNode(leafNodeAddress); 335 leafNode.addCursor(this); 336 if (entryNumber >= 0) 337 this.entryNumber = entryNumber; 338 else 339 this.entryNumber = leafNode.getNumberOfEntries() + entryNumber; 340 adjust(); 341 } 342 343 346 private void unset() throws IndexedStoreException { 347 if (leafNode != null) { 348 leafNode.removeCursor(this); 349 leafNode.release(); 350 } 351 entryNumber = -1; 352 leafNode = null; 353 entryRemoved = false; 354 } 355 356 361 void updateEntry(byte[] b) throws IndexedStoreException { 362 if (entryRemoved) 363 throw new IndexedStoreException(IndexedStoreException.EntryRemoved); 364 if (b.length > 2048) 365 throw new IndexedStoreException(IndexedStoreException.EntryValueLengthError); 366 if (leafNode == null) 367 return; 368 leafNode.updateValueAt(entryNumber, b); 369 } 370 371 379 public synchronized void updateValue(byte[] b) throws IndexedStoreException { 380 updateEntry(b); 381 } 382 383 387 public synchronized void updateValue(Insertable anObject) throws IndexedStoreException { 388 updateValue(anObject.toByteArray()); 389 } 390 } 391 | Popular Tags |