KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: DupCountLN.java,v 1.29 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.dbi.MemoryBudget;
14 import com.sleepycat.je.log.LogEntryType;
15 import com.sleepycat.je.log.LogException;
16 import com.sleepycat.je.log.LogUtils;
17
18 /**
19  * A DupCountLN represents the transactional part of the root of a
20  * duplicate tree, specifically the count of dupes in the tree.
21  */

22 public final class DupCountLN extends LN {
23
24     private static final String JavaDoc BEGIN_TAG = "<dupCountLN>";
25     private static final String JavaDoc END_TAG = "</dupCountLN>";
26
27     private int dupCount;
28
29     /**
30      * Create a new DupCountLn to hold a new DIN.
31      */

32     public DupCountLN(int count) {
33         super(new byte[0]);
34
35         /*
36          * This ctor is always called from Tree.createDuplicateEntry
37          * where there will be one existing LN and a new dup LN being
38          * inserted to create the new duplicate tree. So the minimum
39          * starting point for a duplicate tree is 2 entries.
40          */

41         this.dupCount = count;
42     }
43
44     /**
45      * Create an empty DupCountLN, to be filled in from the log.
46      */

47     public DupCountLN() {
48         super();
49         dupCount = 0;
50     }
51
52     public int getDupCount() {
53         return dupCount;
54     }
55
56     public int incDupCount() {
57         dupCount++;
58         setDirty();
59         assert dupCount >= 0;
60         return dupCount;
61     }
62
63     public int decDupCount() {
64         dupCount--;
65         setDirty();
66         assert dupCount >= 0;
67         return dupCount;
68     }
69
70     void setDupCount(int dupCount) {
71         this.dupCount = dupCount;
72         setDirty();
73     }
74
75     /**
76      * @return true if this node is a duplicate-bearing node type, false
77      * if otherwise.
78      */

79     public boolean containsDuplicates() {
80         return true;
81     }
82
83     public boolean isDeleted() {
84         return false;
85     }
86
87     /**
88      * Compute the approximate size of this node in memory for evictor
89      * invocation purposes.
90      */

91     public long getMemorySizeIncludedByParent() {
92         return MemoryBudget.DUPCOUNTLN_OVERHEAD;
93     }
94
95     /*
96      * DbStat support.
97      */

98     public void accumulateStats(TreeWalkerStatsAccumulator acc) {
99     acc.processDupCountLN(this, new Long JavaDoc(getNodeId()));
100     }
101
102     /*
103      * Dumping
104      */

105
106     public String JavaDoc toString() {
107         return dumpString(0, true);
108     }
109     
110     public String JavaDoc beginTag() {
111         return BEGIN_TAG;
112     }
113
114     public String JavaDoc endTag() {
115         return END_TAG;
116     }
117
118     public String JavaDoc dumpString(int nSpaces, boolean dumpTags) {
119         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
120         if (dumpTags) {
121             sb.append(TreeUtils.indent(nSpaces));
122             sb.append(beginTag());
123             sb.append('\n');
124         }
125         sb.append(TreeUtils.indent(nSpaces+2));
126         sb.append("<count v=\"").append(dupCount).append("\"/>").append('\n');
127         sb.append(super.dumpString(nSpaces, false));
128         if (dumpTags) {
129             sb.append(TreeUtils.indent(nSpaces));
130             sb.append(endTag());
131         }
132         return sb.toString();
133     }
134
135     /*
136      * Logging
137      */

138
139     /**
140      * Log type for transactional entries.
141      */

142     protected LogEntryType getTransactionalLogType() {
143         return LogEntryType.LOG_DUPCOUNTLN_TRANSACTIONAL;
144     }
145
146     /**
147      * @see LN#getLogType
148      */

149     public LogEntryType getLogType() {
150         return LogEntryType.LOG_DUPCOUNTLN;
151     }
152
153     /**
154      * @see LN#getLogSize
155      */

156     public int getLogSize() {
157         return super.getLogSize() +
158             LogUtils.INT_BYTES;
159     }
160
161     /**
162      * @see LN#writeToLog
163      */

164     public void writeToLog(ByteBuffer JavaDoc logBuffer) {
165         // Ask ancestors to write to log
166
super.writeToLog(logBuffer);
167         LogUtils.writeInt(logBuffer, dupCount);
168     }
169
170     /**
171      * @see LN#readFromLog
172      */

173     public void readFromLog(ByteBuffer JavaDoc itemBuffer, byte entryTypeVersion)
174         throws LogException {
175
176         super.readFromLog(itemBuffer, entryTypeVersion);
177         dupCount = LogUtils.readInt(itemBuffer);
178     }
179
180     /**
181      * Dump additional fields
182      */

183     protected void dumpLogAdditional(StringBuffer JavaDoc sb, boolean verbose) {
184         super.dumpLogAdditional(sb, verbose);
185         sb.append("<count v=\"").append(dupCount).append("\"/>");
186     }
187 }
188
Popular Tags