001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.objectserver.persistence.sleepycat;
005:
006: import com.sleepycat.je.DatabaseException;
007: import com.tc.logging.TCLogger;
008: import com.tc.logging.TCLogging;
009: import com.tc.object.ObjectID;
010: import com.tc.objectserver.persistence.api.PersistenceTransaction;
011: import com.tc.objectserver.persistence.api.PersistenceTransactionProvider;
012: import com.tc.objectserver.persistence.api.PersistentCollectionFactory;
013: import com.tc.test.TCTestCase;
014: import com.tc.util.Assert;
015:
016: import java.io.File;
017: import java.io.IOException;
018: import java.util.Arrays;
019: import java.util.Collection;
020: import java.util.HashMap;
021: import java.util.Map;
022: import java.util.Set;
023:
024: public class SleepycatCollectionsTest extends TCTestCase {
025:
026: private SleepycatPersistor persistor;
027: private PersistenceTransactionProvider ptp;
028: private DBEnvironment env;
029: private PersistentCollectionFactory collectionsFactory;
030: private SleepycatCollectionsPersistor collectionsPersistor;
031: private static int dbHomeCounter = 0;
032: private static File tempDirectory;
033:
034: public void setUp() throws Exception {
035:
036: if (env != null)
037: env.close();
038: File dbHome = newDBHome();
039: TCLogger logger = TCLogging.getLogger(getClass());
040: CustomSerializationAdapterFactory saf = new CustomSerializationAdapterFactory();
041: env = new DBEnvironment(true, dbHome);
042: persistor = new SleepycatPersistor(logger, env, saf);
043: ptp = persistor.getPersistenceTransactionProvider();
044: collectionsFactory = persistor.getPersistentCollectionFactory();
045: collectionsPersistor = persistor.getCollectionsPersistor();
046: }
047:
048: // XXX:: Check SleepycatSerializationTest if you want know why its done like this or ask Orion.
049: private File newDBHome() throws IOException {
050: File file;
051: if (tempDirectory == null)
052: tempDirectory = getTempDirectory();
053: ++dbHomeCounter;
054: for (file = new File(tempDirectory, "db" + dbHomeCounter); file
055: .exists(); ++dbHomeCounter) {
056: //
057: }
058: assertFalse(file.exists());
059: System.err.println("DB Home = " + file);
060: return file;
061: }
062:
063: public void tearDown() throws Exception {
064: persistor = null;
065: ptp = null;
066: env = null;
067: }
068:
069: public void testSleepycatPersistableMap() throws Exception {
070: ObjectID id = new ObjectID(7);
071: SleepycatPersistableMap sMap = (SleepycatPersistableMap) collectionsFactory
072: .createPersistentMap(id);
073: addToMap(sMap);
074: Map localMap = new HashMap();
075: addToMap(localMap);
076: equals(localMap, sMap);
077:
078: PersistenceTransaction tx = ptp.newTransaction();
079: collectionsPersistor.saveMap(tx, sMap);
080: tx.commit();
081: equals(localMap, sMap);
082:
083: tx = ptp.newTransaction();
084: SleepycatPersistableMap sMap2 = collectionsPersistor.loadMap(
085: tx, new ObjectID(1000));
086: tx.commit();
087: equals(new HashMap(), sMap2);
088:
089: tx = ptp.newTransaction();
090: sMap2 = collectionsPersistor.loadMap(tx, id);
091: tx.commit();
092: equals(localMap, sMap2);
093:
094: System.err.println(" Adding more maps ....");
095: addMoreMaps();
096:
097: System.err.println(" Loading map again ....");
098: tx = ptp.newTransaction();
099: sMap2 = collectionsPersistor.loadMap(tx, id);
100: tx.commit();
101: equals(localMap, sMap2);
102:
103: System.err.println(" Loading different map ....");
104: tx = ptp.newTransaction();
105: SleepycatPersistableMap sMap3 = collectionsPersistor.loadMap(
106: tx, new ObjectID(25));
107: tx.commit();
108: equals(localMap, sMap3);
109:
110: addToMap(sMap, 2);
111: addToMap(localMap, 2);
112: equals(localMap, sMap);
113:
114: tx = ptp.newTransaction();
115: collectionsPersistor.saveMap(tx, sMap);
116: tx.commit();
117: equals(localMap, sMap);
118:
119: tx = ptp.newTransaction();
120: sMap2 = collectionsPersistor.loadMap(tx, id);
121: tx.commit();
122: equals(localMap, sMap2);
123:
124: addAndRemoveFromMap(sMap);
125: addAndRemoveFromMap(localMap);
126: Assert.assertEquals(localMap, sMap);
127:
128: tx = ptp.newTransaction();
129: collectionsPersistor.saveMap(tx, sMap);
130: tx.commit();
131: equals(localMap, sMap);
132:
133: tx = ptp.newTransaction();
134: sMap2 = collectionsPersistor.loadMap(tx, id);
135: tx.commit();
136: equals(localMap, sMap2);
137:
138: addRemoveClearFromMap(sMap);
139: addRemoveClearFromMap(localMap);
140: equals(localMap, sMap);
141:
142: tx = ptp.newTransaction();
143: collectionsPersistor.saveMap(tx, sMap);
144: tx.commit();
145: equals(localMap, sMap);
146:
147: tx = ptp.newTransaction();
148: sMap2 = collectionsPersistor.loadMap(tx, id);
149: tx.commit();
150: equals(localMap, sMap2);
151:
152: tx = ptp.newTransaction();
153: Assert
154: .assertTrue(collectionsPersistor.deleteCollection(tx,
155: id));
156: tx.commit();
157:
158: tx = ptp.newTransaction();
159: sMap2 = collectionsPersistor.loadMap(tx, id);
160: tx.commit();
161: equals(new HashMap(), sMap2);
162:
163: tx = ptp.newTransaction();
164: Assert.assertFalse(collectionsPersistor
165: .deleteCollection(tx, id));
166: tx.commit();
167:
168: }
169:
170: private void equals(Map m1, Map m2) {
171: Assert.assertEquals(m1.size(), m2.size());
172: Assert.assertEquals(m1, m2);
173: equals(m1.keySet(), m2.keySet());
174: equals(m1.values(), m2.values());
175: Assert.assertEquals(m1.entrySet(), m2.entrySet());
176: }
177:
178: // This implementation does not care about the order
179: private void equals(Collection c1, Collection c2) {
180: Assert.assertEquals(c1.size(), c2.size());
181: Assert.assertTrue(c1.containsAll(c2));
182: Assert.assertTrue(c2.containsAll(c1));
183: }
184:
185: private void equals(Set s1, Set s2) {
186: Assert.assertEquals(s1, s2);
187: equals(Arrays.asList(s1.toArray()), Arrays.asList(s2.toArray()));
188: }
189:
190: private void addMoreMaps() throws IOException, DatabaseException {
191: for (int j = 20; j < 40; j++) {
192: ObjectID id = new ObjectID(j);
193: SleepycatPersistableMap sMap = (SleepycatPersistableMap) collectionsFactory
194: .createPersistentMap(id);
195: addToMap(sMap);
196: PersistenceTransaction tx = ptp.newTransaction();
197: collectionsPersistor.saveMap(tx, sMap);
198: tx.commit();
199: }
200: }
201:
202: private void addToMap(Map map) {
203: addToMap(map, 1);
204: }
205:
206: private void addToMap(Map map, int increCount) {
207: int j = 0;
208: for (int i = 0; i < 50; i++, j += increCount) {
209: map.put(new ObjectID(j), new ObjectID(100 + j));
210: map.put(new Integer(j), new Long(j));
211: map.put(new String("" + j), new String("" + j));
212: map.put(new Double(j + 0.005), new Float(j - 0.004));
213: }
214: }
215:
216: private void addAndRemoveFromMap(Map map) {
217: int j = 50;
218: for (int i = 0; i < 50; i++, j++) {
219: map.put(new ObjectID(j), new ObjectID(100 + j));
220: map.put(new Integer(j), new Long(j));
221: map.put(new String("" + j), new String("" + j));
222: map.put(new Double(j + 0.005), new Float(j - 0.004));
223: map.remove(new ObjectID(j - 25));
224: map.remove(new Integer(j - 25));
225: map.remove(new String("" + (j - 25)));
226: map.remove(new Double((j - 25) + 0.005));
227: }
228: }
229:
230: private void addRemoveClearFromMap(Map map) {
231: int j = 100;
232: for (int i = 0; i < 50; i++, j++) {
233: map.put(new ObjectID(j), new ObjectID(100 + j));
234: map.put(new Integer(j), new Long(j));
235: map.put(new String("" + j), new String("" + j));
236: map.put(new Double(j + 0.005), new Float(j - 0.004));
237: map.remove(new ObjectID(j - 25));
238: map.remove(new Integer(j - 25));
239: map.remove(new String("" + (j - 25)));
240: map.remove(new Double((j - 25) + 0.005));
241: if (i % 20 == 19) {
242: map.clear();
243: }
244: }
245: }
246:
247: }
|