1 11 package org.eclipse.core.internal.indexing; 12 13 17 18 class IndexAnchor extends IndexedStoreObject { 19 20 public static final int SIZE = 32; 21 public static final int TYPE = 1; 22 23 protected static final int RootNodeAddressOffset = 2; 24 protected static final int RootNodeAddressLength = 4; 25 26 protected static final int NumberOfEntriesOffset = 14; 27 protected static final int NumberOfEntriesLength = 4; 28 29 protected Field numberOfEntriesField; 30 protected int numberOfEntries; 31 32 protected Field rootNodeAddressField; 33 protected ObjectAddress rootNodeAddress; 34 35 38 public IndexAnchor() { 39 super(); 40 numberOfEntries = 0; 41 rootNodeAddress = ObjectAddress.Null; 42 } 43 44 47 public IndexAnchor(Field f, ObjectStore store, ObjectAddress address) throws ObjectStoreException { 48 super(f, store, address); 49 } 50 51 54 private void setFields(Field f) { 55 rootNodeAddressField = f.subfield(RootNodeAddressOffset, RootNodeAddressLength); 56 numberOfEntriesField = f.subfield(NumberOfEntriesOffset, NumberOfEntriesLength); 57 } 58 59 63 protected void insertValues(Field f) { 64 super.insertValues(f); 65 setFields(f); 66 numberOfEntriesField.put(numberOfEntries); 67 rootNodeAddressField.put(rootNodeAddress); 68 } 69 70 74 protected void extractValues(Field f) throws ObjectStoreException { 75 super.extractValues(f); 76 setFields(f); 77 numberOfEntries = numberOfEntriesField.getInt(); 78 rootNodeAddress = new ObjectAddress(rootNodeAddressField.get()); 79 } 80 81 85 protected int getMinimumSize() { 86 return SIZE; 87 } 88 89 93 protected int getRequiredType() { 94 return TYPE; 95 } 96 97 100 public String toString() { 101 StringBuffer b = new StringBuffer (); 102 b.append("Anchor("); b.append(numberOfEntries); 104 b.append(","); b.append(rootNodeAddress); 106 b.append(")"); return b.toString(); 108 } 109 110 113 void entryInserted(IndexNode node) { 114 if (node.isLeaf()) { 115 numberOfEntries++; 116 setChanged(); 117 } 118 } 119 120 123 void entryRemoved(IndexNode node) { 124 if (node.isLeaf()) { 125 numberOfEntries--; 126 setChanged(); 127 } 128 } 129 130 133 void setRootNodeAddress(ObjectAddress rootNodeAddress) { 134 this.rootNodeAddress = rootNodeAddress; 135 setChanged(); 136 } 137 138 143 void find(byte key[], IndexCursor cursor) throws IndexedStoreException { 144 if (rootNodeAddress.isNull()) { 145 cursor.reset(); 146 } else { 147 IndexNode rootNode = acquireNode(rootNodeAddress); 148 rootNode.find(key, cursor); 149 rootNode.release(); 150 } 151 } 152 153 156 void findFirstEntry(IndexCursor cursor) throws IndexedStoreException { 157 if (rootNodeAddress.isNull()) { 158 cursor.reset(); 159 } else { 160 IndexNode rootNode = acquireNode(rootNodeAddress); 161 rootNode.findFirstEntry(cursor); 162 rootNode.release(); 163 } 164 } 165 166 169 void insert(byte[] key, byte[] value) throws IndexedStoreException { 170 if (rootNodeAddress.isNull()) { 171 IndexNode rootNode = new IndexNode(this.address); 172 try { 173 store.insertObject(rootNode); 174 } catch (ObjectStoreException e) { 175 throw new IndexedStoreException(IndexedStoreException.IndexNodeNotCreated, e); 176 } 177 rootNodeAddress = rootNode.getAddress(); 178 } 179 IndexNode rootNode = acquireNode(rootNodeAddress); 180 rootNode.insertEntry(key, value); 181 rootNode.release(); 182 } 183 } 184 | Popular Tags |