KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > objectserver > persistence > sleepycat > SleepycatTCDBDiff


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.objectserver.persistence.sleepycat;
5
6 import com.sleepycat.je.DatabaseException;
7 import com.tc.logging.TCLogging;
8 import com.tc.object.ObjectID;
9 import com.tc.objectserver.core.api.ManagedObject;
10 import com.tc.objectserver.core.api.ManagedObjectState;
11 import com.tc.objectserver.managedobject.ManagedObjectChangeListener;
12 import com.tc.objectserver.managedobject.ManagedObjectChangeListenerProviderImpl;
13 import com.tc.objectserver.managedobject.ManagedObjectStateFactory;
14 import com.tc.objectserver.persistence.api.ClassPersistor;
15 import com.tc.objectserver.persistence.api.ClientStatePersistor;
16 import com.tc.objectserver.persistence.api.ManagedObjectPersistor;
17 import com.tc.objectserver.persistence.api.TransactionPersistor;
18 import com.tc.objectserver.persistence.impl.StringIndexImpl;
19 import com.tc.util.Assert;
20 import com.tc.util.SyncObjectIdSet;
21
22 import gnu.trove.TObjectLongHashMap;
23
24 import java.io.File JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.Set JavaDoc;
31
32 public class SleepycatTCDBDiff {
33   
34   private static final String JavaDoc VERSION_STRING = "SleepycatTCDBDiff [ Ver 0.2]";
35
36   private final SleepycatPersistor sdb1;
37   private final SleepycatPersistor sdb2;
38   private final File JavaDoc d1;
39   private final File JavaDoc d2;
40   private final boolean moDiff;
41   private final ManagedObjectStateFactory mosf1;
42   private final ManagedObjectStateFactory mosf2;
43
44   public SleepycatTCDBDiff(File JavaDoc d1, File JavaDoc d2, boolean moDiff) throws DatabaseException, IOException JavaDoc {
45     this.d1 = d1;
46     this.d2 = d2;
47     this.moDiff = moDiff;
48     this.sdb1 = new SleepycatPersistor(TCLogging.getLogger(SleepycatTCDBDiff.class), new DBEnvironment(true, d1),
49                                        new CustomSerializationAdapterFactory());
50     this.sdb2 = new SleepycatPersistor(TCLogging.getLogger(SleepycatTCDBDiff.class), new DBEnvironment(true, d2),
51                                        new CustomSerializationAdapterFactory());
52
53     // Since we dont create any new MOState Objects in this program, we use 1 of the 2 persistor.
54
ManagedObjectChangeListenerProviderImpl moclp = new ManagedObjectChangeListenerProviderImpl();
55     moclp.setListener(new ManagedObjectChangeListener() {
56
57       public void changed(ObjectID changedObject, ObjectID oldReference, ObjectID newReference) {
58         // NOP
59
}
60
61     });
62     ManagedObjectStateFactory.disableSingleton(true);
63     mosf1 = ManagedObjectStateFactory.createInstance(moclp, sdb1);
64     mosf2 = ManagedObjectStateFactory.createInstance(moclp, sdb2);
65
66   }
67
68   public void diff() {
69     log("Diffing [1] " + d1 + " and [2] " + d2);
70     diffStringIndexer((StringIndexImpl) sdb1.getStringIndex(), (StringIndexImpl) sdb2.getStringIndex());
71     diffManagedObjects(sdb1.getManagedObjectPersistor(), sdb2.getManagedObjectPersistor());
72     diffClientStates(sdb1.getClientStatePersistor(), sdb2.getClientStatePersistor());
73     diffTransactions(sdb1.getTransactionPersistor(), sdb2.getTransactionPersistor());
74     diffGeneratedClasses(sdb1.getClassPersistor(), sdb2.getClassPersistor());
75   }
76
77   private void diffGeneratedClasses(ClassPersistor cp1, ClassPersistor cp2) {
78     log("Diffing Generated Classes...(Partial implementation)");
79     Map m1 = cp1.retrieveAllClasses();
80     Map m2 = cp2.retrieveAllClasses();
81     if (!m1.keySet().equals(m2.keySet())) {
82       log("*** [1] containing " + m1.size() + " generated classes differs from [2] containing " + m2.size());
83     }
84   }
85
86   private void diffTransactions(TransactionPersistor tp1, TransactionPersistor tp2) {
87     log("Diffing Transactions...");
88     Collection JavaDoc txns1 = tp1.loadAllGlobalTransactionDescriptors();
89     Collection JavaDoc txns2 = tp2.loadAllGlobalTransactionDescriptors();
90     if (!txns1.equals(txns2)) {
91       log("*** [1] containing " + txns1.size() + " Transactions differs from [2] containing " + txns2.size()
92           + " Transactions");
93     }
94   }
95
96   private void diffClientStates(ClientStatePersistor cp1, ClientStatePersistor cp2) {
97     log("Diffing ClientStates...");
98     Set cids1 = cp1.loadClientIDs();
99     Set cids2 = cp2.loadClientIDs();
100     if (!cids1.equals(cids2)) {
101       log("*** [1] containing " + cids1.size() + " ClientIDs differs from [2] containing " + cids2.size()
102           + " ClientIDs");
103     }
104   }
105
106   private void diffManagedObjects(ManagedObjectPersistor mp1, ManagedObjectPersistor mp2) {
107     log("Diffing ManagedObjects...(Partial implementation)");
108     SyncObjectIdSet oids1 = mp1.getAllObjectIDs();
109     SyncObjectIdSet oids2 = mp2.getAllObjectIDs();
110     if (!oids1.equals(oids2)) {
111       log("*** [1] containing " + oids1.size() + " ObjectIDs differs from [2] containing " + oids2.size()
112           + " ObjectIDs");
113     }
114
115     if (!moDiff) return;
116
117     // The diff only makes sense if the sequence in which the objects created are same.
118
// First diff the common set
119
Set common = new HashSet JavaDoc(oids1);
120     common.retainAll(oids2);
121     for (Iterator JavaDoc i = common.iterator(); i.hasNext();) {
122       ObjectID oid = (ObjectID) i.next();
123       ManagedObjectStateFactory.setInstance(mosf1);
124       ManagedObject mo1 = mp1.loadObjectByID(oid);
125       ManagedObjectStateFactory.setInstance(mosf2);
126       ManagedObject mo2 = mp2.loadObjectByID(oid);
127       Assert.assertEquals(mo1.getID(), mo2.getID());
128       ManagedObjectState ms1 = mo1.getManagedObjectState();
129       ManagedObjectState ms2 = mo2.getManagedObjectState();
130       if (!ms1.equals(ms2)) {
131         log("****** [1] " + ms1 + " differs from [2] " + ms2);
132       }
133     }
134
135     // Unique
136
oids1.removeAll(common);
137     if (!oids1.isEmpty()) {
138       log("****** [1] contains " + oids1 + " not in [2]");
139     }
140     oids2.removeAll(common);
141     if (!oids2.isEmpty()) {
142       log("****** [2] contains " + oids2 + " not in [1]");
143     }
144   }
145
146   private static void log(String JavaDoc message) {
147     System.out.println(message);
148   }
149
150   private void diffStringIndexer(StringIndexImpl stringIndex1, StringIndexImpl stringIndex2) {
151     log("Diffing StringIndexes...");
152     TObjectLongHashMap map1 = stringIndex1.getString2LongMappings();
153     TObjectLongHashMap map2 = stringIndex2.getString2LongMappings();
154     if (!map1.equals(map2)) {
155       log("*** [1] containing " + map1.size() + " mapping differs from [2] containing " + map2.size() + " mapping");
156     }
157   }
158
159   public static void main(String JavaDoc[] args) {
160     boolean moDiff = false;
161     
162     log(VERSION_STRING);
163     if (args == null || args.length < 2) {
164       usage();
165       System.exit(-1);
166     } else if (args.length > 2) {
167       Assert.assertEquals("-mo", args[2]);
168       moDiff = true;
169     }
170     try {
171       File JavaDoc d1 = new File JavaDoc(args[0]);
172       validateDir(d1);
173       File JavaDoc d2 = new File JavaDoc(args[1]);
174       validateDir(d2);
175
176       SleepycatTCDBDiff sdiff = new SleepycatTCDBDiff(d1, d2, moDiff);
177       sdiff.diff();
178     } catch (Exception JavaDoc ex) {
179       ex.printStackTrace();
180       System.exit(-2);
181     }
182   }
183
184   private static void validateDir(File JavaDoc dir) {
185     if (!dir.exists() || !dir.isDirectory()) { throw new RuntimeException JavaDoc("Not a valid directory : " + dir); }
186   }
187
188   private static void usage() {
189     log("Usage: SleepycatTCDBDiff <environment home directory1> <environment home directory2> [-mo]");
190   }
191
192 }
193
Popular Tags