KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > cleaner > FileSummary


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: FileSummary.java,v 1.17 2006/11/03 03:07:48 mark Exp $
7  */

8
9 package com.sleepycat.je.cleaner;
10
11 import java.nio.ByteBuffer JavaDoc;
12
13 import com.sleepycat.je.DatabaseException;
14 import com.sleepycat.je.log.LogReadable;
15 import com.sleepycat.je.log.LogUtils;
16 import com.sleepycat.je.log.LogWritable;
17
18 public class FileSummary implements LogWritable, LogReadable {
19
20     /* Persistent fields. */
21     public int totalCount; // Total # of log entries
22
public int totalSize; // Total bytes in log file
23
public int totalINCount; // Number of IN log entries
24
public int totalINSize; // Byte size of IN log entries
25
public int totalLNCount; // Number of LN log entries
26
public int totalLNSize; // Byte size of LN log entries
27
public int obsoleteINCount; // Number of obsolete IN log entries
28
public int obsoleteLNCount; // Number of obsolete LN log entries
29
public int obsoleteLNSize; // Byte size of obsolete LN log entries
30
public int obsoleteLNSizeCounted; // Number obsolete LNs with size counted
31

32     /**
33      * Creates an empty summary.
34      */

35     public FileSummary() {
36     }
37
38     /**
39      * Returns whether this summary contains any non-zero totals.
40      */

41     public boolean isEmpty() {
42
43         return totalCount == 0 &&
44                totalSize == 0 &&
45                obsoleteINCount == 0 &&
46                obsoleteLNCount == 0;
47     }
48
49     /**
50      * Returns the approximate byte size of all obsolete LN entries. In
51      * FileSummaryLN version 3 and greater the exact tracked size is used.
52      */

53     public int getObsoleteLNSize() {
54
55         if (totalLNCount == 0) {
56             return 0;
57         }
58
59         /*
60          * Use the tracked obsolete size for all entries for which the size was
61          * counted, plus the average size for all obsolete entries whose size
62          * was not counted.
63          */

64         int obsolete = obsoleteLNSize;
65         int notCounted = obsoleteLNCount - obsoleteLNSizeCounted;
66         if (notCounted > 0) {
67             /* Use long arithmetic. */
68             long total = totalLNSize;
69             /* Scale by 255 to reduce integer truncation error. */
70             total <<= 8;
71             long avgSizePerLN = total / totalLNCount;
72             obsolete += (int) ((notCounted * avgSizePerLN) >> 8);
73         }
74         return obsolete;
75     }
76
77     /**
78      * Returns the approximate byte size of all obsolete IN entries.
79      */

80     public int getObsoleteINSize() {
81
82         if (totalINCount == 0) {
83             return 0;
84         }
85         /* Use long arithmetic. */
86         long size = totalINSize;
87         /* Scale by 255 to reduce integer truncation error. */
88         size <<= 8;
89         long avgSizePerIN = size / totalINCount;
90         return (int) ((obsoleteINCount * avgSizePerIN) >> 8);
91     }
92
93     /**
94      * Returns an estimate of the total bytes that are obsolete.
95      */

96     public int getObsoleteSize()
97         throws DatabaseException {
98
99         if (totalSize > 0) {
100             /* Leftover (non-IN non-LN) space is considered obsolete. */
101             int leftoverSize = totalSize - (totalINSize + totalLNSize);
102             int obsoleteSize = getObsoleteLNSize() +
103                                getObsoleteINSize() +
104                                leftoverSize;
105
106             /*
107              * Don't report more obsolete bytes than the total. We may
108              * calculate more than the total because of (intentional)
109              * double-counting during recovery.
110              */

111             if (obsoleteSize > totalSize) {
112                 obsoleteSize = totalSize;
113             }
114             return obsoleteSize;
115         } else {
116             return 0;
117         }
118     }
119
120     /**
121      * Returns the total number of entries counted. This value is guaranted
122      * to increase whenever the tracking information about a file changes. It
123      * is used a key discriminator for FileSummaryLN records.
124      */

125     public int getEntriesCounted() {
126         return totalCount + obsoleteLNCount + obsoleteINCount;
127     }
128
129     /**
130      * Returns the number of non-obsolete LN and IN entries.
131      */

132     public int getNonObsoleteCount() {
133         return totalLNCount +
134                totalINCount -
135                obsoleteLNCount -
136                obsoleteINCount;
137     }
138
139     /**
140      * Reset all totals to zero.
141      */

142     public void reset() {
143
144         totalCount = 0;
145         totalSize = 0;
146         totalINCount = 0;
147         totalINSize = 0;
148         totalLNCount = 0;
149         totalLNSize = 0;
150         obsoleteINCount = 0;
151         obsoleteLNCount = 0;
152         obsoleteLNSize = 0;
153         obsoleteLNSizeCounted = 0;
154     }
155
156     /**
157      * Add the totals of the given summary object to the totals of this object.
158      */

159     public void add(FileSummary o) {
160
161         totalCount += o.totalCount;
162         totalSize += o.totalSize;
163         totalINCount += o.totalINCount;
164         totalINSize += o.totalINSize;
165         totalLNCount += o.totalLNCount;
166         totalLNSize += o.totalLNSize;
167         obsoleteINCount += o.obsoleteINCount;
168         obsoleteLNCount += o.obsoleteLNCount;
169         obsoleteLNSize += o.obsoleteLNSize;
170         obsoleteLNSizeCounted += o.obsoleteLNSizeCounted;
171     }
172
173     /**
174      * @see LogWritable#getLogSize
175      */

176     public int getLogSize() {
177
178         return 10 * LogUtils.getIntLogSize();
179     }
180
181     /**
182      * @see LogWritable#writeToLog
183      */

184     public void writeToLog(ByteBuffer JavaDoc buf) {
185
186         LogUtils.writeInt(buf, totalCount);
187         LogUtils.writeInt(buf, totalSize);
188         LogUtils.writeInt(buf, totalINCount);
189         LogUtils.writeInt(buf, totalINSize);
190         LogUtils.writeInt(buf, totalLNCount);
191         LogUtils.writeInt(buf, totalLNSize);
192         LogUtils.writeInt(buf, obsoleteINCount);
193         LogUtils.writeInt(buf, obsoleteLNCount);
194         LogUtils.writeInt(buf, obsoleteLNSize);
195         LogUtils.writeInt(buf, obsoleteLNSizeCounted);
196     }
197
198     /**
199      * @see LogReadable#readFromLog
200      */

201     public void readFromLog(ByteBuffer JavaDoc buf, byte entryTypeVersion) {
202
203         totalCount = LogUtils.readInt(buf);
204         totalSize = LogUtils.readInt(buf);
205         totalINCount = LogUtils.readInt(buf);
206         totalINSize = LogUtils.readInt(buf);
207         totalLNCount = LogUtils.readInt(buf);
208         totalLNSize = LogUtils.readInt(buf);
209         obsoleteINCount = LogUtils.readInt(buf);
210         if (obsoleteINCount == -1) {
211
212             /*
213              * If INs were not counted in an older log file written by 1.5.3 or
214              * earlier, consider all INs to be obsolete. This causes the file
215              * to be cleaned, and then IN counting will be accurate.
216              */

217             obsoleteINCount = totalINCount;
218         }
219         obsoleteLNCount = LogUtils.readInt(buf);
220
221         /*
222          * obsoleteLNSize and obsoleteLNSizeCounted were added in FileSummaryLN
223          * version 3.
224          */

225         if (entryTypeVersion >= 3) {
226             obsoleteLNSize = LogUtils.readInt(buf);
227             obsoleteLNSizeCounted = LogUtils.readInt(buf);
228         } else {
229             obsoleteLNSize = 0;
230             obsoleteLNSizeCounted = 0;
231         }
232     }
233
234     /**
235      * @see LogReadable#dumpLog
236      */

237     public void dumpLog(StringBuffer JavaDoc buf, boolean verbose) {
238
239         buf.append("<summary totalCount=\"");
240         buf.append(totalCount);
241         buf.append("\" totalSize=\"");
242         buf.append(totalSize);
243         buf.append("\" totalINCount=\"");
244         buf.append(totalINCount);
245         buf.append("\" totalINSize=\"");
246         buf.append(totalINSize);
247         buf.append("\" totalLNCount=\"");
248         buf.append(totalLNCount);
249         buf.append("\" totalLNSize=\"");
250         buf.append(totalLNSize);
251         buf.append("\" obsoleteINCount=\"");
252         buf.append(obsoleteINCount);
253         buf.append("\" obsoleteLNCount=\"");
254         buf.append(obsoleteLNCount);
255         buf.append("\" obsoleteLNSize=\"");
256         buf.append(obsoleteLNSize);
257         buf.append("\" obsoleteLNSizeCounted=\"");
258         buf.append(obsoleteLNSizeCounted);
259         buf.append("\"/>");
260     }
261
262     /**
263      * Never called.
264      * @see LogReadable#getTransactionId
265      */

266     public long getTransactionId() {
267     return -1;
268     }
269
270     /**
271      * Never called.
272      * @see LogReadable#logEntryIsTransactional
273      */

274     public boolean logEntryIsTransactional() {
275     return false;
276     }
277
278     public String JavaDoc toString() {
279         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
280         dumpLog(buf, true);
281         return buf.toString();
282     }
283 }
284
Popular Tags