KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: MapLN.java,v 1.68 2006/11/17 23:47:28 mark 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.dbi.DatabaseImpl;
15 import com.sleepycat.je.dbi.MemoryBudget;
16 import com.sleepycat.je.log.LogEntryType;
17 import com.sleepycat.je.log.LogException;
18 import com.sleepycat.je.log.LogUtils;
19
20 /**
21  * A MapLN represents a Leaf Node in the JE DatabaseImpl Naming Tree.
22  */

23 public final class MapLN extends LN {
24
25     private static final String JavaDoc BEGIN_TAG = "<mapLN>";
26     private static final String JavaDoc END_TAG = "</mapLN>";
27
28     private DatabaseImpl databaseImpl;
29     private boolean deleted;
30
31     /**
32      * Create a new MapLn to hold a new databaseImpl. In the ideal world, we'd
33      * have a base LN class so that this MapLN doesn't have a superfluous data
34      * field, but we want to optimize the LN class for size and speed right
35      * now.
36      */

37     public MapLN(DatabaseImpl db) {
38         super(new byte[0]);
39         databaseImpl = db;
40         deleted = false;
41     }
42
43     /**
44      * Create an empty MapLN, to be filled in from the log.
45      */

46     public MapLN()
47         throws DatabaseException {
48
49         super();
50         databaseImpl = new DatabaseImpl();
51     }
52
53     public boolean isDeleted() {
54         return deleted;
55     }
56
57     void makeDeleted() {
58         deleted = true;
59
60         /* Release all references to nodes held by this database. */
61         databaseImpl.getTree().setRoot(null, true);
62     }
63
64     public DatabaseImpl getDatabase() {
65         return databaseImpl;
66     }
67
68     /**
69      * Initialize a node that has been faulted in from the log.
70      */

71     public void postFetchInit(DatabaseImpl db, long sourceLsn)
72         throws DatabaseException {
73
74         databaseImpl.setEnvironmentImpl(db.getDbEnvironment());
75     }
76
77     /**
78      * Compute the approximate size of this node in memory for evictor
79      * invocation purposes.
80      */

81     public long getMemorySizeIncludedByParent() {
82         return MemoryBudget.MAPLN_OVERHEAD +
83                databaseImpl.getAdditionalMemorySize();
84     }
85
86     /*
87      * Dumping
88      */

89
90     public String JavaDoc toString() {
91         return dumpString(0, true);
92     }
93     
94     public String JavaDoc beginTag() {
95         return BEGIN_TAG;
96     }
97
98     public String JavaDoc endTag() {
99         return END_TAG;
100     }
101
102     public String JavaDoc dumpString(int nSpaces, boolean dumpTags) {
103         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
104         sb.append(super.dumpString(nSpaces, dumpTags));
105         sb.append('\n');
106         sb.append(TreeUtils.indent(nSpaces));
107         sb.append("<deleted val=\"").append(Boolean.toString(deleted));
108         sb.append("\">");
109         sb.append('\n');
110         sb.append(databaseImpl.dumpString(nSpaces));
111         return sb.toString();
112     }
113
114     /*
115      * Logging
116      */

117
118     /**
119      * Log type for transactional entries.
120      */

121     protected LogEntryType getTransactionalLogType() {
122         return LogEntryType.LOG_MAPLN_TRANSACTIONAL;
123     }
124
125     /**
126      * @see LN#getLogType
127      */

128     public LogEntryType getLogType() {
129         return LogEntryType.LOG_MAPLN;
130     }
131
132     /**
133      * Overrides this method in order to get the true last logged size of the
134      * DatabaseImpl, which is specially computed by calling
135      * Tree.getLastLoggedSize().
136      *
137      * @see LN#getLastLoggedSize
138      */

139     public int getLastLoggedSize() {
140         return getLogSizeInternal(true);
141     }
142
143     /**
144      * @see LN#getLogSize
145      */

146     public int getLogSize() {
147         return getLogSizeInternal(false);
148     }
149
150     private int getLogSizeInternal(boolean lastLogged) {
151         return super.getLogSize() +
152             (lastLogged ? databaseImpl.getLastLoggedSize()
153                         : databaseImpl.getLogSize()) +
154             LogUtils.getBooleanLogSize();
155     }
156
157     /**
158      * @see LN#writeToLog
159      */

160     public void writeToLog(ByteBuffer JavaDoc logBuffer) {
161         /* Ask ancestors to write to log. */
162         super.writeToLog(logBuffer);
163         databaseImpl.writeToLog(logBuffer);
164         LogUtils.writeBoolean(logBuffer, deleted);
165     }
166
167     /**
168      * @see LN#readFromLog
169      */

170     public void readFromLog(ByteBuffer JavaDoc itemBuffer, byte entryTypeVersion)
171         throws LogException {
172
173         super.readFromLog(itemBuffer, entryTypeVersion);
174         databaseImpl.readFromLog(itemBuffer, entryTypeVersion);
175         deleted = LogUtils.readBoolean(itemBuffer);
176     }
177
178     /**
179      * Dump additional fields. Done this way so the additional info can be
180      * within the XML tags defining the dumped log entry.
181      */

182     protected void dumpLogAdditional(StringBuffer JavaDoc sb, boolean verbose) {
183         databaseImpl.dumpLog(sb, true);
184     }
185 }
186
Popular Tags