KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > tree > Node


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: Node.java,v 1.93 2006/10/30 21:14:26 bostic Exp $
7  */

8
9 package com.sleepycat.je.tree;
10
11 import java.nio.ByteBuffer JavaDoc;
12
13 import com.sleepycat.je.DatabaseException;
14 import com.sleepycat.je.cleaner.UtilizationTracker;
15 import com.sleepycat.je.dbi.DatabaseImpl;
16 import com.sleepycat.je.dbi.INList;
17 import com.sleepycat.je.latch.LatchNotHeldException;
18 import com.sleepycat.je.log.LogEntryType;
19 import com.sleepycat.je.log.LogException;
20 import com.sleepycat.je.log.LogReadable;
21 import com.sleepycat.je.log.LogUtils;
22 import com.sleepycat.je.log.LogWritable;
23 import com.sleepycat.je.log.LoggableObject;
24
25 /**
26  * A Node contains all the common base information for any JE B-Tree node.
27  */

28 public abstract class Node
29     implements LoggableObject, LogReadable, LogWritable {
30
31     /*
32      * The last allocated id. Note that nodeids will be shared
33      * across db environments. lastAllocatedId must be initialized at
34      * startup by Recovery.
35      */

36     public synchronized static void setLastNodeId(long id) {
37         if (lastAllocatedId < id) {
38             lastAllocatedId = id;
39         }
40     }
41
42     private static long lastAllocatedId = 0;
43
44     private static final String JavaDoc BEGIN_TAG = "<node>";
45     private static final String JavaDoc END_TAG = "</node>";
46
47     // The unique id of this node
48
private long nodeId;
49
50     /**
51      * Disallow use
52      */

53     private Node() {
54     }
55
56     /**
57      * Create a new node, assigning it the next available node id.
58      */

59     protected Node(boolean init) {
60         if (init) {
61             nodeId = getNextNodeId();
62         }
63     }
64
65     /**
66      * Increment and return the next usable id. Must be synchronized.
67      */

68     public static synchronized long getNextNodeId() {
69         return ++lastAllocatedId;
70     }
71
72     /**
73      * Get the latest id, for checkpointing.
74      */

75     public static synchronized long getLastId() {
76         return lastAllocatedId;
77     }
78
79     /**
80      * Initialize a node that has been faulted in from the log
81      */

82     public void postFetchInit(DatabaseImpl db, long sourceLsn)
83         throws DatabaseException {
84
85         /* Nothing to do. */
86     }
87
88     public long getNodeId() {
89     return nodeId;
90     }
91
92     /* For unit tests only. */
93     void setNodeId(long nid) {
94     nodeId = nid;
95     }
96
97     public void latchShared()
98     throws DatabaseException {
99
100     }
101
102     public void releaseLatch()
103     throws LatchNotHeldException {
104
105     }
106
107     public void verify(byte[] maxKey)
108     throws DatabaseException {
109     }
110
111     /**
112      * @return true if this node is a duplicate-bearing node type, false
113      * if otherwise.
114      */

115     public boolean containsDuplicates() {
116     return false;
117     }
118
119     /**
120      * Cover for LN's and just return 0 since they'll always be at the bottom
121      * of the tree.
122      */

123     int getLevel() {
124     return 0;
125     }
126
127     /*
128      * Depth first search through a duplicate tree looking for an LN that
129      * has nodeId. When we find it, set location.bin and index and return
130      * true. If we don't find it, return false.
131      *
132      * No latching is performed.
133      */

134     boolean matchLNByNodeId(TreeLocation location, long nodeId)
135     throws DatabaseException {
136
137     throw new DatabaseException("matchLNByNodeId called on non DIN/DBIN");
138     }
139
140     /**
141      * Add yourself to the in memory list if you're a type of node that
142      * should belong.
143      */

144     abstract void rebuildINList(INList inList)
145         throws DatabaseException;
146
147     /**
148      * Remove yourself from the in memory list if you're a type of node that
149      * is put there.
150      */

151     abstract void accountForSubtreeRemoval(INList inList,
152                                            UtilizationTracker tracker)
153         throws DatabaseException;
154
155     /**
156      * @return true if you're part of a deletable subtree.
157      */

158     abstract boolean isValidForDelete()
159         throws DatabaseException;
160
161     /**
162      * @return true if you're an IN in the search path
163      */

164     abstract protected boolean isSoughtNode(long nid, boolean updateGeneration)
165         throws DatabaseException;
166
167     /**
168      * @return true if you can be the ancestor of the target IN.
169      * Currently the determining factor is whether the target IN contains
170      * duplicates.
171      */

172     abstract protected boolean canBeAncestor(boolean targetContainsDuplicates);
173
174     /**
175      * Return the approximate size of this node in memory, if this
176      * size should be included in it's parents memory accounting.
177      * For example, all INs return 0, because they are accounted for
178      * individually. LNs must return a count, they're not counted on
179      * the INList.
180      */

181     protected long getMemorySizeIncludedByParent() {
182         return 0;
183     }
184
185     /*
186      * Dumping
187      */

188
189     /**
190      * Default toString method at the root of the tree.
191      */

192     public String JavaDoc toString() {
193         return this.dumpString(0, true);
194     }
195
196     private String JavaDoc beginTag() {
197     return BEGIN_TAG;
198     }
199
200     private String JavaDoc endTag() {
201     return END_TAG;
202     }
203
204     public void dump(int nSpaces) {
205     System.out.print(dumpString(nSpaces, true));
206     }
207     
208     String JavaDoc dumpString(int nSpaces, boolean dumpTags) {
209         StringBuffer JavaDoc self = new StringBuffer JavaDoc();
210         self.append(TreeUtils.indent(nSpaces));
211     if (dumpTags) {
212         self.append(beginTag());
213     }
214         self.append(nodeId);
215     if (dumpTags) {
216         self.append(endTag());
217     }
218         return self.toString();
219     }
220
221     public String JavaDoc shortDescription() {
222     return "<" + getType() + "/" + getNodeId();
223     }
224
225     public String JavaDoc getType() {
226     return getClass().getName();
227     }
228
229     /*
230      * Logging support
231      */

232
233     /**
234      * @see LoggableObject#getLogType
235      */

236     public abstract LogEntryType getLogType();
237
238     /**
239      * @see LoggableObject#marshallOutsideWriteLatch
240      * By default, nodes can be marshalled outside the log write latch.
241      */

242     public boolean marshallOutsideWriteLatch() {
243         return true;
244     }
245
246     /**
247      * @see LoggableObject#countAsObsoleteWhenLogged
248      */

249     public boolean countAsObsoleteWhenLogged() {
250         return false;
251     }
252
253     /**
254      * @see LoggableObject#postLogWork
255      */

256     public void postLogWork(long justLoggedLsn)
257         throws DatabaseException {
258
259     }
260
261     /**
262      * @see LoggableObject#getLogSize
263      */

264     public int getLogSize() {
265         return LogUtils.LONG_BYTES;
266     }
267
268     /**
269      * @see LogWritable#writeToLog
270      */

271     public void writeToLog(ByteBuffer JavaDoc logBuffer) {
272         LogUtils.writeLong(logBuffer, nodeId);
273     }
274
275     /**
276      * @see LogReadable#readFromLog
277      */

278     public void readFromLog(ByteBuffer JavaDoc itemBuffer, byte entryTypeVersion)
279     throws LogException {
280
281         nodeId = LogUtils.readLong(itemBuffer);
282     }
283
284     /**
285      * @see LogReadable#dumpLog
286      */

287     public void dumpLog(StringBuffer JavaDoc sb, boolean verbose) {
288         sb.append(BEGIN_TAG);
289         sb.append(nodeId);
290         sb.append(END_TAG);
291     }
292 }
293
Popular Tags