KickJava   Java API By Example, From Geeks To Geeks.

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


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

8
9 package com.sleepycat.je.cleaner;
10
11 import java.io.PrintStream JavaDoc;
12 import java.util.ArrayList JavaDoc;
13 import java.util.HashSet JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.Set JavaDoc;
17
18 import com.sleepycat.je.Database;
19 import com.sleepycat.je.DatabaseException;
20 import com.sleepycat.je.DbInternal;
21 import com.sleepycat.je.dbi.DatabaseImpl;
22 import com.sleepycat.je.dbi.SortedLSNTreeWalker;
23 import com.sleepycat.je.dbi.SortedLSNTreeWalker.TreeNodeProcessor;
24 import com.sleepycat.je.log.LogEntryType;
25 import com.sleepycat.je.tree.Node;
26 import com.sleepycat.je.utilint.DbLsn;
27
28 /**
29  * Verify cleaner data structures
30  */

31 public class VerifyUtils {
32
33     private static final boolean DEBUG = false;
34
35     /**
36      * Compare the LSNs referenced by a given Database to the lsns held
37      * in the utilization profile. Assumes that the database and
38      * environment is quiescent, and that there is no current cleaner
39      * activity.
40      */

41     public static void checkLsns(Database db)
42         throws DatabaseException {
43         checkLsns(DbInternal.dbGetDatabaseImpl(db), System.out);
44     }
45
46     /**
47      * Compare the lsns referenced by a given Database to the lsns held
48      * in the utilization profile. Assumes that the database and
49      * environment is quiescent, and that there is no current cleaner
50      * activity.
51      */

52     public static void checkLsns(DatabaseImpl dbImpl,
53                                  PrintStream JavaDoc out)
54         throws DatabaseException {
55     
56         /* Get all the LSNs in the database. */
57         GatherLSNs gatherLsns = new GatherLSNs();
58         long rootLsn = dbImpl.getTree().getRootLsn();
59         List JavaDoc savedExceptions = new ArrayList JavaDoc();
60
61         SortedLSNTreeWalker walker =
62             new SortedLSNTreeWalker(dbImpl,
63                                     false, // don't remove from INList
64
false, // don't set db state
65
rootLsn,
66                                     gatherLsns,
67                                     savedExceptions,
68                     null);
69         walker.walk();
70
71         /* Print out any exceptions seen during the walk. */
72         if (savedExceptions.size() > 0) {
73             out.println(savedExceptions.size() +
74                         " problems seen during tree walk for checkLsns");
75             Iterator JavaDoc iter = savedExceptions.iterator();
76             while (iter.hasNext()) {
77                 Exception JavaDoc e = (Exception JavaDoc) iter.next();
78                 out.println(" " + e);
79             }
80         }
81         
82         Set JavaDoc lsnsInTree = gatherLsns.getLsns();
83         lsnsInTree.add(new Long JavaDoc(rootLsn));
84     
85         /* Get all the files used by this database. */
86         Iterator JavaDoc iter = lsnsInTree.iterator();
87         Set JavaDoc fileNums = new HashSet JavaDoc();
88     
89         while (iter.hasNext()) {
90             long lsn = ((Long JavaDoc) iter.next()).longValue();
91             fileNums.add(new Long JavaDoc(DbLsn.getFileNumber(lsn)));
92         }
93     
94         /* Gather up the obsolete lsns in these file summary lns */
95         iter = fileNums.iterator();
96         Set JavaDoc obsoleteLsns = new HashSet JavaDoc();
97         UtilizationProfile profile =
98             dbImpl.getDbEnvironment().getUtilizationProfile();
99     
100         while (iter.hasNext()) {
101             Long JavaDoc fileNum = (Long JavaDoc) iter.next();
102     
103             PackedOffsets obsoleteOffsets = new PackedOffsets();
104             TrackedFileSummary tfs =
105                 profile.getObsoleteDetail(fileNum,
106                                           obsoleteOffsets,
107                                           false /* logUpdate */);
108             PackedOffsets.Iterator obsoleteIter = obsoleteOffsets.iterator();
109             while (obsoleteIter.hasNext()) {
110             long offset = obsoleteIter.next();
111             Long JavaDoc oneLsn = new Long JavaDoc(DbLsn.makeLsn(fileNum.longValue(),
112                                              offset));
113                 obsoleteLsns.add(oneLsn);
114                 if (DEBUG) {
115                     out.println("Adding 0x" +
116                                 Long.toHexString(oneLsn.longValue()));
117                 }
118             }
119         }
120     
121         /* Check than none the lsns in the tree is in the UP. */
122         boolean error = false;
123         iter = lsnsInTree.iterator();
124         while (iter.hasNext()) {
125             Long JavaDoc lsn = (Long JavaDoc) iter.next();
126             if (obsoleteLsns.contains(lsn)) {
127                 out.println("Obsolete LSN set contains valid LSN " +
128                             DbLsn.getNoFormatString(lsn.longValue()));
129                 error = true;
130             }
131         }
132     
133         /*
134          * Check that none of the lsns in the file summary ln is in the
135          * tree.
136          */

137         iter = obsoleteLsns.iterator();
138         while (iter.hasNext()) {
139             Long JavaDoc lsn = (Long JavaDoc) iter.next();
140             if (lsnsInTree.contains(lsn)) {
141                 out.println("Tree contains obsolete LSN " +
142                             DbLsn.getNoFormatString(lsn.longValue()));
143                 error = true;
144             }
145         }
146     
147         if (error) {
148             throw new DatabaseException("Lsn mismatch");
149         }
150
151         if (savedExceptions.size() > 0) {
152             throw new DatabaseException("Sorted LSN Walk problem");
153         }
154     }
155
156     private static class GatherLSNs implements TreeNodeProcessor {
157         private Set JavaDoc lsns = new HashSet JavaDoc();
158     
159         public void processLSN(long childLSN,
160                    LogEntryType childType,
161                    Node ignore,
162                    byte[] ignore2)
163         throws DatabaseException {
164
165             lsns.add(new Long JavaDoc(childLSN));
166         }
167     
168     /* ignore */
169     public void processDupCount(long ignore) {
170     }
171
172         public Set JavaDoc getLsns() {
173             return lsns;
174         }
175     }
176 }
177
Popular Tags