KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > indexing > IndexAnchor


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.core.internal.indexing;
12
13 /**
14  * An IndexAnchor provides a place to hang index-wide information in a fixed spot, especially
15  * since the root node may change due to a root node split.
16  */

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     /**
36      * Constructs a new index anchor from nothing.
37      */

38     public IndexAnchor() {
39         super();
40         numberOfEntries = 0;
41         rootNodeAddress = ObjectAddress.Null;
42     }
43
44     /**
45      * Constructs a new index anchor from a field read from the store. Used by the factory.
46      */

47     public IndexAnchor(Field f, ObjectStore store, ObjectAddress address) throws ObjectStoreException {
48         super(f, store, address);
49     }
50
51     /**
52      * Sets the fields definitions. Done after the contents are set.
53      */

54     private void setFields(Field f) {
55         rootNodeAddressField = f.subfield(RootNodeAddressOffset, RootNodeAddressLength);
56         numberOfEntriesField = f.subfield(NumberOfEntriesOffset, NumberOfEntriesLength);
57     }
58
59     /**
60      * Places the contents of the fields into the buffer.
61      * Subclasses should implement and call super.
62      */

63     protected void insertValues(Field f) {
64         super.insertValues(f);
65         setFields(f);
66         numberOfEntriesField.put(numberOfEntries);
67         rootNodeAddressField.put(rootNodeAddress);
68     }
69
70     /**
71      * Places the contents of the buffer into the fields.
72      * Subclasses should implement and call super.
73      */

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     /**
82      * Returns the minimum size of this object's instance -- including its type field.
83      * Subclasses should override.
84      */

85     protected int getMinimumSize() {
86         return SIZE;
87     }
88
89     /**
90      * Returns the required type of this class of object.
91      * Subclasses must override.
92      */

93     protected int getRequiredType() {
94         return TYPE;
95     }
96
97     /**
98      * Returns a printable representation of this object.
99      */

100     public String JavaDoc toString() {
101         StringBuffer JavaDoc b = new StringBuffer JavaDoc();
102         b.append("Anchor("); //$NON-NLS-1$
103
b.append(numberOfEntries);
104         b.append(","); //$NON-NLS-1$
105
b.append(rootNodeAddress);
106         b.append(")"); //$NON-NLS-1$
107
return b.toString();
108     }
109
110     /**
111      * Processes the notification that an entry was inserted.
112      */

113     void entryInserted(IndexNode node) {
114         if (node.isLeaf()) {
115             numberOfEntries++;
116             setChanged();
117         }
118     }
119
120     /**
121      * Processes the notification by a leaf node that an entry was removed.
122      */

123     void entryRemoved(IndexNode node) {
124         if (node.isLeaf()) {
125             numberOfEntries--;
126             setChanged();
127         }
128     }
129
130     /**
131      * Sets the root node address. Set when root node is initialized or split.
132      */

133     void setRootNodeAddress(ObjectAddress rootNodeAddress) {
134         this.rootNodeAddress = rootNodeAddress;
135         setChanged();
136     }
137
138     /**
139      * This method returns a cursor set to the first entry in the index whose key
140      * is greater than or equal to the key provided. To set a cursor to the beginning
141      * of the index use a key of zero length.
142      */

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     /**
154      * This method returns a cursor set to the first entry in the index.
155      */

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     /**
167      * Insert an entry into an index.
168      */

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