1 4 package com.tc.objectserver.persistence.sleepycat; 5 6 import com.sleepycat.je.DatabaseException; 7 import com.tc.logging.TCLogger; 8 import com.tc.logging.TCLogging; 9 import com.tc.object.ObjectID; 10 import com.tc.objectserver.persistence.api.PersistenceTransaction; 11 import com.tc.objectserver.persistence.api.PersistenceTransactionProvider; 12 import com.tc.objectserver.persistence.api.PersistentCollectionFactory; 13 import com.tc.test.TCTestCase; 14 import com.tc.util.Assert; 15 16 import java.io.File ; 17 import java.io.IOException ; 18 import java.util.Arrays ; 19 import java.util.Collection ; 20 import java.util.HashMap ; 21 import java.util.Map ; 22 import java.util.Set ; 23 24 public class SleepycatCollectionsTest extends TCTestCase { 25 26 private SleepycatPersistor persistor; 27 private PersistenceTransactionProvider ptp; 28 private DBEnvironment env; 29 private PersistentCollectionFactory collectionsFactory; 30 private SleepycatCollectionsPersistor collectionsPersistor; 31 private static int dbHomeCounter = 0; 32 private static File tempDirectory; 33 34 public void setUp() throws Exception { 35 36 if (env != null) env.close(); 37 File dbHome = newDBHome(); 38 TCLogger logger = TCLogging.getLogger(getClass()); 39 CustomSerializationAdapterFactory saf = new CustomSerializationAdapterFactory(); 40 env = new DBEnvironment(true, dbHome); 41 persistor = new SleepycatPersistor(logger, env, saf); 42 ptp = persistor.getPersistenceTransactionProvider(); 43 collectionsFactory = persistor.getPersistentCollectionFactory(); 44 collectionsPersistor = persistor.getCollectionsPersistor(); 45 } 46 47 private File newDBHome() throws IOException { 49 File file; 50 if (tempDirectory == null) tempDirectory = getTempDirectory(); 51 ++dbHomeCounter; 52 for (file = new File (tempDirectory, "db" + dbHomeCounter); file.exists(); ++dbHomeCounter) { 53 } 55 assertFalse(file.exists()); 56 System.err.println("DB Home = " + file); 57 return file; 58 } 59 60 public void tearDown() throws Exception { 61 persistor = null; 62 ptp = null; 63 env = null; 64 } 65 66 public void testSleepycatPersistableMap() throws Exception { 67 ObjectID id = new ObjectID(7); 68 SleepycatPersistableMap sMap = (SleepycatPersistableMap) collectionsFactory.createPersistentMap(id); 69 addToMap(sMap); 70 Map localMap = new HashMap (); 71 addToMap(localMap); 72 equals(localMap, sMap); 73 74 PersistenceTransaction tx = ptp.newTransaction(); 75 collectionsPersistor.saveMap(tx, sMap); 76 tx.commit(); 77 equals(localMap, sMap); 78 79 tx = ptp.newTransaction(); 80 SleepycatPersistableMap sMap2 = collectionsPersistor.loadMap(tx, new ObjectID(1000)); 81 tx.commit(); 82 equals(new HashMap (), sMap2); 83 84 tx = ptp.newTransaction(); 85 sMap2 = collectionsPersistor.loadMap(tx, id); 86 tx.commit(); 87 equals(localMap, sMap2); 88 89 System.err.println(" Adding more maps ...."); 90 addMoreMaps(); 91 92 System.err.println(" Loading map again ...."); 93 tx = ptp.newTransaction(); 94 sMap2 = collectionsPersistor.loadMap(tx, id); 95 tx.commit(); 96 equals(localMap, sMap2); 97 98 System.err.println(" Loading different map ...."); 99 tx = ptp.newTransaction(); 100 SleepycatPersistableMap sMap3 = collectionsPersistor.loadMap(tx, new ObjectID(25)); 101 tx.commit(); 102 equals(localMap, sMap3); 103 104 addToMap(sMap, 2); 105 addToMap(localMap, 2); 106 equals(localMap, sMap); 107 108 tx = ptp.newTransaction(); 109 collectionsPersistor.saveMap(tx, sMap); 110 tx.commit(); 111 equals(localMap, sMap); 112 113 tx = ptp.newTransaction(); 114 sMap2 = collectionsPersistor.loadMap(tx, id); 115 tx.commit(); 116 equals(localMap, sMap2); 117 118 addAndRemoveFromMap(sMap); 119 addAndRemoveFromMap(localMap); 120 Assert.assertEquals(localMap, sMap); 121 122 tx = ptp.newTransaction(); 123 collectionsPersistor.saveMap(tx, sMap); 124 tx.commit(); 125 equals(localMap, sMap); 126 127 tx = ptp.newTransaction(); 128 sMap2 = collectionsPersistor.loadMap(tx, id); 129 tx.commit(); 130 equals(localMap, sMap2); 131 132 addRemoveClearFromMap(sMap); 133 addRemoveClearFromMap(localMap); 134 equals(localMap, sMap); 135 136 tx = ptp.newTransaction(); 137 collectionsPersistor.saveMap(tx, sMap); 138 tx.commit(); 139 equals(localMap, sMap); 140 141 tx = ptp.newTransaction(); 142 sMap2 = collectionsPersistor.loadMap(tx, id); 143 tx.commit(); 144 equals(localMap, sMap2); 145 146 tx = ptp.newTransaction(); 147 Assert.assertTrue(collectionsPersistor.deleteCollection(tx, id)); 148 tx.commit(); 149 150 tx = ptp.newTransaction(); 151 sMap2 = collectionsPersistor.loadMap(tx, id); 152 tx.commit(); 153 equals(new HashMap (), sMap2); 154 155 tx = ptp.newTransaction(); 156 Assert.assertFalse(collectionsPersistor.deleteCollection(tx, id)); 157 tx.commit(); 158 159 } 160 161 private void equals(Map m1, Map m2) { 162 Assert.assertEquals(m1.size(), m2.size()); 163 Assert.assertEquals(m1, m2); 164 equals(m1.keySet(), m2.keySet()); 165 equals(m1.values(), m2.values()); 166 Assert.assertEquals(m1.entrySet(), m2.entrySet()); 167 } 168 169 private void equals(Collection c1, Collection c2) { 171 Assert.assertEquals(c1.size(), c2.size()); 172 Assert.assertTrue(c1.containsAll(c2)); 173 Assert.assertTrue(c2.containsAll(c1)); 174 } 175 176 private void equals(Set s1, Set s2) { 177 Assert.assertEquals(s1, s2); 178 equals(Arrays.asList(s1.toArray()), Arrays.asList(s2.toArray())); 179 } 180 181 private void addMoreMaps() throws IOException , DatabaseException { 182 for (int j = 20; j < 40; j++) { 183 ObjectID id = new ObjectID(j); 184 SleepycatPersistableMap sMap = (SleepycatPersistableMap) collectionsFactory.createPersistentMap(id); 185 addToMap(sMap); 186 PersistenceTransaction tx = ptp.newTransaction(); 187 collectionsPersistor.saveMap(tx, sMap); 188 tx.commit(); 189 } 190 } 191 192 private void addToMap(Map map) { 193 addToMap(map, 1); 194 } 195 196 private void addToMap(Map map, int increCount) { 197 int j = 0; 198 for (int i = 0; i < 50; i++, j += increCount) { 199 map.put(new ObjectID(j), new ObjectID(100 + j)); 200 map.put(new Integer (j), new Long (j)); 201 map.put(new String ("" + j), new String ("" + j)); 202 map.put(new Double (j + 0.005), new Float (j - 0.004)); 203 } 204 } 205 206 private void addAndRemoveFromMap(Map map) { 207 int j = 50; 208 for (int i = 0; i < 50; i++, j ++) { 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 map.remove(new ObjectID(j-25)); 214 map.remove(new Integer (j-25)); 215 map.remove(new String (""+(j-25))); 216 map.remove(new Double ((j-25) + 0.005)); 217 } 218 } 219 220 private void addRemoveClearFromMap(Map map) { 221 int j = 100; 222 for (int i = 0; i < 50; i++, j ++) { 223 map.put(new ObjectID(j), new ObjectID(100 + j)); 224 map.put(new Integer (j), new Long (j)); 225 map.put(new String ("" + j), new String ("" + j)); 226 map.put(new Double (j + 0.005), new Float (j - 0.004)); 227 map.remove(new ObjectID(j-25)); 228 map.remove(new Integer (j-25)); 229 map.remove(new String (""+(j-25))); 230 map.remove(new Double ((j-25) + 0.005)); 231 if(i % 20 == 19) { 232 map.clear(); 233 } 234 } 235 } 236 237 } 238 | Popular Tags |