Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

JoinTest.java

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 2000-2003
00005  *      Sleepycat Software.  All rights reserved.
00006  *
00007  * $Id: JoinTest.java,v 12.2 2005/08/01 20:25:30 mark Exp $
00008  */
00009 package com.sleepycat.collections.test;
00010 
00011 import java.util.Map;
00012 
00013 import junit.framework.Test;
00014 import junit.framework.TestCase;
00015 
00016 import com.sleepycat.bind.serial.StoredClassCatalog;
00017 import com.sleepycat.bind.serial.test.MarshalledObject;
00018 import com.sleepycat.collections.StoredCollection;
00019 import com.sleepycat.collections.StoredContainer;
00020 import com.sleepycat.collections.StoredIterator;
00021 import com.sleepycat.collections.StoredMap;
00022 import com.sleepycat.collections.TransactionRunner;
00023 import com.sleepycat.collections.TransactionWorker;
00024 import com.sleepycat.collections.TupleSerialFactory;
00025 import com.sleepycat.compat.DbCompat;
00026 import com.sleepycat.db.Database;
00027 import com.sleepycat.db.DatabaseConfig;
00028 import com.sleepycat.db.Environment;
00029 import com.sleepycat.db.SecondaryConfig;
00030 import com.sleepycat.db.SecondaryDatabase;
00031 
00035 public class JoinTest extends TestCase
00036     implements TransactionWorker {
00037 
00038     private static final String MATCH_DATA = "d4"; // matches both keys = "yes"
00039     private static final String MATCH_KEY  = "k4"; // matches both keys = "yes"
00040     private static final String[] VALUES = {"yes", "yes"};
00041 
00042     public static void main(String[] args)
00043         throws Exception {
00044 
00045         junit.framework.TestResult tr =
00046             junit.textui.TestRunner.run(suite());
00047         if (tr.errorCount() > 0 ||
00048             tr.failureCount() > 0) {
00049             System.exit(1);
00050         } else {
00051             System.exit(0);
00052         }
00053     }
00054 
00055     public static Test suite()
00056         throws Exception {
00057 
00058         return new JoinTest();
00059     }
00060 
00061     private Environment env;
00062     private TransactionRunner runner;
00063     private StoredClassCatalog catalog;
00064     private TupleSerialFactory factory;
00065     private Database store;
00066     private SecondaryDatabase index1;
00067     private SecondaryDatabase index2;
00068     private StoredMap storeMap;
00069     private StoredMap indexMap1;
00070     private StoredMap indexMap2;
00071 
00072     public JoinTest() {
00073 
00074         super("JoinTest");
00075     }
00076 
00077     public void setUp()
00078         throws Exception {
00079 
00080         DbTestUtil.printTestName(getName());
00081         env = TestEnv.TXN.open(getName());
00082         runner = new TransactionRunner(env);
00083         createDatabase();
00084     }
00085 
00086     public void tearDown() {
00087 
00088         try {
00089             if (index1 != null) {
00090                 index1.close();
00091             }
00092             if (index2 != null) {
00093                 index2.close();
00094             }
00095             if (store != null) {
00096                 store.close();
00097             }
00098             if (catalog != null) {
00099                 catalog.close();
00100             }
00101             if (env != null) {
00102                 env.close();
00103             }
00104         } catch (Exception e) {
00105             System.out.println("Ignored exception during tearDown: " + e);
00106         } finally {
00107             /* Ensure that GC can cleanup. */
00108             index1 = null;
00109             index2 = null;
00110             store = null;
00111             catalog = null;
00112             env = null;
00113             runner = null;
00114             factory = null;
00115             storeMap = null;
00116             indexMap1 = null;
00117             indexMap2 = null;
00118         }
00119     }
00120 
00121     public void runTest()
00122         throws Exception {
00123 
00124         runner.run(this);
00125     }
00126 
00127     public void doWork()
00128         throws Exception {
00129 
00130         createViews();
00131         writeAndRead();
00132     }
00133 
00134     private void createDatabase()
00135         throws Exception {
00136 
00137         catalog = new StoredClassCatalog(openDb("catalog.db"));
00138         factory = new TupleSerialFactory(catalog);
00139         assertSame(catalog, factory.getCatalog());
00140 
00141         store = openDb("store.db");
00142         index1 = openSecondaryDb(store, "index1.db", "1");
00143         index2 = openSecondaryDb(store, "index2.db", "2");
00144     }
00145 
00146     private Database openDb(String file)
00147         throws Exception {
00148 
00149         DatabaseConfig config = new DatabaseConfig();
00150         DbCompat.setTypeBtree(config);
00151         config.setTransactional(true);
00152         config.setAllowCreate(true);
00153 
00154         return DbCompat.openDatabase(env, null, file, null, config);
00155     }
00156 
00157     private SecondaryDatabase openSecondaryDb(Database primary,
00158                                               String file,
00159                                               String keyName)
00160         throws Exception {
00161 
00162         SecondaryConfig secConfig = new SecondaryConfig();
00163         DbCompat.setTypeBtree(secConfig);
00164         secConfig.setTransactional(true);
00165         secConfig.setAllowCreate(true);
00166         DbCompat.setSortedDuplicates(secConfig, true);
00167         secConfig.setKeyCreator(factory.getKeyCreator(MarshalledObject.class,
00168                                                       keyName));
00169 
00170         return DbCompat.openSecondaryDatabase(env, null,
00171                                               file, null,
00172                                               primary, secConfig);
00173     }
00174 
00175     private void createViews()
00176         throws Exception {
00177 
00178         storeMap = factory.newMap(store, String.class,
00179                                          MarshalledObject.class, true);
00180         indexMap1 = factory.newMap(index1, String.class,
00181                                            MarshalledObject.class, true);
00182         indexMap2 = factory.newMap(index2, String.class,
00183                                            MarshalledObject.class, true);
00184     }
00185 
00186     private void writeAndRead()
00187         throws Exception {
00188 
00189         // write records: Data, PrimaryKey, IndexKey1, IndexKey2
00190         assertNull(storeMap.put(null,
00191             new MarshalledObject("d1", "k1", "no",  "yes")));
00192         assertNull(storeMap.put(null,
00193             new MarshalledObject("d2", "k2", "no",  "no")));
00194         assertNull(storeMap.put(null,
00195             new MarshalledObject("d3", "k3", "no",  "yes")));
00196         assertNull(storeMap.put(null,
00197             new MarshalledObject("d4", "k4", "yes", "yes")));
00198         assertNull(storeMap.put(null,
00199             new MarshalledObject("d5", "k5", "yes", "no")));
00200 
00201         Object o;
00202         Map.Entry e;
00203 
00204         // join values with index maps
00205         o = doJoin((StoredCollection) storeMap.values());
00206         assertEquals(MATCH_DATA, ((MarshalledObject) o).getData());
00207 
00208         // join keySet with index maps
00209         o = doJoin((StoredCollection) storeMap.keySet());
00210         assertEquals(MATCH_KEY, o);
00211 
00212         // join entrySet with index maps
00213         o = doJoin((StoredCollection) storeMap.entrySet());
00214         e = (Map.Entry) o;
00215         assertEquals(MATCH_KEY, e.getKey());
00216         assertEquals(MATCH_DATA, ((MarshalledObject) e.getValue()).getData());
00217     }
00218 
00219     private Object doJoin(StoredCollection coll) {
00220 
00221         StoredContainer[] indices = { indexMap1, indexMap2 };
00222         StoredIterator i = coll.join(indices, VALUES, null);
00223         try {
00224             assertTrue(i.hasNext());
00225             Object result = i.next();
00226             assertNotNull(result);
00227             assertFalse(i.hasNext());
00228             return result;
00229         } finally { i.close(); }
00230     }
00231 }
00232 

Generated on Sun Dec 25 12:14:55 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2