001: /*
002: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tc.objectserver.impl;
006:
007: import com.sleepycat.je.CursorConfig;
008: import com.tc.logging.TCLogger;
009: import com.tc.logging.TCLogging;
010: import com.tc.object.ObjectID;
011: import com.tc.objectserver.core.api.ManagedObject;
012: import com.tc.objectserver.core.impl.TestManagedObject;
013: import com.tc.objectserver.persistence.api.PersistenceTransaction;
014: import com.tc.objectserver.persistence.api.PersistenceTransactionProvider;
015: import com.tc.objectserver.persistence.impl.TestMutableSequence;
016: import com.tc.objectserver.persistence.impl.TestPersistenceTransactionProvider;
017: import com.tc.objectserver.persistence.sleepycat.DBEnvironment;
018: import com.tc.objectserver.persistence.sleepycat.ManagedObjectPersistorImpl;
019: import com.tc.objectserver.persistence.sleepycat.OidBitsArrayMapManagerImpl;
020: import com.tc.objectserver.persistence.sleepycat.SleepycatCollectionFactory;
021: import com.tc.objectserver.persistence.sleepycat.SleepycatCollectionsPersistor;
022: import com.tc.objectserver.persistence.sleepycat.SleepycatSerializationAdapterFactory;
023: import com.tc.properties.TCPropertiesImpl;
024: import com.tc.test.TCTestCase;
025: import com.tc.util.SyncObjectIdSet;
026:
027: import java.io.File;
028: import java.util.Collection;
029: import java.util.HashMap;
030: import java.util.HashSet;
031: import java.util.Iterator;
032: import java.util.Map;
033: import java.util.Random;
034:
035: public class ManagedObjectPersistorImplTest extends TCTestCase {
036: private static final TCLogger logger = TCLogging
037: .getTestingLogger(ManagedObjectPersistorImplTest.class);
038: private ManagedObjectPersistorImpl managedObjectPersistor;
039: private Map map;
040: private TestManagedObjectPersistor persistor;
041: private PersistentManagedObjectStore objectStore;
042: PersistenceTransactionProvider persistenceTransactionProvider;
043: DBEnvironment env;
044: private OidBitsArrayMapManagerImpl oidManager;
045: private final String OID_FAST_LOAD = "l2.objectmanager.loadObjectID.fastLoad";
046:
047: public ManagedObjectPersistorImplTest() {
048: //
049: }
050:
051: protected void setUp() throws Exception {
052: super .setUp();
053: // test only with Oid fastLoad enabled
054: TCPropertiesImpl.setProperty(OID_FAST_LOAD, "true");
055: assertTrue(TCPropertiesImpl.getProperties().getBoolean(
056: OID_FAST_LOAD));
057: boolean paranoid = true;
058: env = newDBEnvironment(paranoid);
059: env.open();
060: CursorConfig dbCursorConfig = new CursorConfig();
061: persistenceTransactionProvider = new TestPersistenceTransactionProvider();
062: CursorConfig rootDBCursorConfig = new CursorConfig();
063: SleepycatCollectionFactory sleepycatCollectionFactory = new SleepycatCollectionFactory();
064: SleepycatCollectionsPersistor sleepycatCollectionsPersistor = new SleepycatCollectionsPersistor(
065: logger, env.getMapsDatabase(),
066: sleepycatCollectionFactory);
067: managedObjectPersistor = new ManagedObjectPersistorImpl(logger,
068: env.getClassCatalogWrapper().getClassCatalog(),
069: new SleepycatSerializationAdapterFactory(), env
070: .getObjectDatabase(), env.getOidDatabase(),
071: dbCursorConfig, new TestMutableSequence(), env
072: .getRootDatabase(), rootDBCursorConfig,
073: persistenceTransactionProvider,
074: sleepycatCollectionsPersistor, env.isParanoidMode());
075: map = new HashMap();
076: persistor = new TestManagedObjectPersistor(map);
077: objectStore = new PersistentManagedObjectStore(
078: managedObjectPersistor);
079: oidManager = managedObjectPersistor.getOidManager();
080: }
081:
082: protected void tearDown() throws Exception {
083: env.close();
084: super .tearDown();
085: }
086:
087: private DBEnvironment newDBEnvironment(boolean paranoid)
088: throws Exception {
089: File dbHome;
090: int count = 0;
091: do {
092: dbHome = new File(this .getTempDirectory(), getClass()
093: .getName()
094: + "db" + (++count));
095: } while (dbHome.exists());
096: dbHome.mkdir();
097: assertTrue(dbHome.exists());
098: assertTrue(dbHome.isDirectory());
099: System.out.println("DB Home: " + dbHome);
100: DBEnvironment env = new DBEnvironment(paranoid, dbHome);
101: return env;
102: }
103:
104: private Collection createRandomObjects(int num) {
105: Random r = new Random();
106: HashSet objects = new HashSet(num);
107: HashSet ids = new HashSet(num);
108: for (int i = 0; i < num; i++) {
109: long id = (long) r.nextInt(num * 10) + 1;
110: if (ids.add(new Long(id))) {
111: ManagedObject mo = new TestManagedObject(new ObjectID(
112: id), new ObjectID[] {});
113: objects.add(mo);
114: }
115: }
116: logger.info("Test with " + objects.size() + " objects");
117: return (objects);
118: }
119:
120: private void verify(Collection objects) {
121: // verify a in-memory bit crosspond to an object ID
122: HashSet originalIds = new HashSet();
123: for (Iterator i = objects.iterator(); i.hasNext();) {
124: ManagedObject mo = (ManagedObject) i.next();
125: originalIds.add(mo.getID());
126: }
127: Collection inMemoryIds = oidManager.bitsArrayMapToObjectID();
128: assertTrue("Wrong bits in memory were set", originalIds
129: .containsAll(inMemoryIds));
130:
131: // verify on disk object IDs
132: // clear in memory arrays then read in from persistor
133: oidManager.resetBitsArrayMap();
134: SyncObjectIdSet idSet = managedObjectPersistor
135: .getAllObjectIDs();
136: idSet.snapshot(); // blocked while reading from disk
137: Collection diskIds = oidManager.bitsArrayMapToObjectID();
138: assertTrue("Wrong object IDs on disk", diskIds
139: .equals(inMemoryIds));
140:
141: }
142:
143: public void testOidBitsArraySave() throws Exception {
144: // wait for background retrieving persistent data
145: objectStore.getAllObjectIDs();
146: oidManager.resetBitsArrayMap();
147:
148: // publish data
149: Collection objects = createRandomObjects(15050);
150: PersistenceTransaction ptx = persistenceTransactionProvider
151: .newTransaction();
152: managedObjectPersistor.saveAllObjects(ptx, objects);
153: ptx.commit();
154:
155: // verify object IDs is in memory
156: for (Iterator i = objects.iterator(); i.hasNext();) {
157: ManagedObject mo = (ManagedObject) i.next();
158: assertTrue("Object:" + mo.getID() + " missed in memory! ",
159: oidManager.inMemoryContains(mo.getID()));
160: }
161:
162: verify(objects);
163: }
164:
165: public void testOidBitsArrayDeleteHalf() throws Exception {
166: // wait for background retrieving persistent data
167: objectStore.getAllObjectIDs();
168: oidManager.resetBitsArrayMap();
169:
170: // publish data
171: Collection objects = createRandomObjects(15050);
172: PersistenceTransaction ptx = persistenceTransactionProvider
173: .newTransaction();
174: managedObjectPersistor.saveAllObjects(ptx, objects);
175: ptx.commit();
176:
177: int total = objects.size();
178: HashSet toDelete = new HashSet();
179: int count = 0;
180: for (Iterator i = objects.iterator(); (count < total / 2)
181: && i.hasNext();) {
182: ManagedObject mo = (ManagedObject) i.next();
183: toDelete.add(mo.getID());
184: i.remove();
185: }
186: ptx = persistenceTransactionProvider.newTransaction();
187: managedObjectPersistor.deleteAllObjectsByID(ptx, toDelete);
188: ptx.commit();
189:
190: verify(objects);
191: }
192:
193: public void testOidBitsArrayDeleteAll() throws Exception {
194: // wait for background retrieving persistent data
195: objectStore.getAllObjectIDs();
196: oidManager.resetBitsArrayMap();
197:
198: // publish data
199: Collection objects = createRandomObjects(15050);
200: PersistenceTransaction ptx = persistenceTransactionProvider
201: .newTransaction();
202: managedObjectPersistor.saveAllObjects(ptx, objects);
203: ptx.commit();
204:
205: HashSet objectIds = new HashSet();
206: for (Iterator i = objects.iterator(); i.hasNext();) {
207: ManagedObject mo = (ManagedObject) i.next();
208: objectIds.add(mo.getID());
209: }
210: ptx = persistenceTransactionProvider.newTransaction();
211: managedObjectPersistor.deleteAllObjectsByID(ptx, objectIds);
212: ptx.commit();
213:
214: objects.clear();
215: verify(objects);
216: }
217:
218: }
|