1 8 9 package com.sleepycat.collections.test; 10 11 import java.util.Map ; 12 13 import junit.framework.Test; 14 import junit.framework.TestCase; 15 16 import com.sleepycat.bind.serial.StoredClassCatalog; 17 import com.sleepycat.bind.serial.test.MarshalledObject; 18 import com.sleepycat.collections.StoredCollection; 19 import com.sleepycat.collections.StoredContainer; 20 import com.sleepycat.collections.StoredIterator; 21 import com.sleepycat.collections.StoredMap; 22 import com.sleepycat.collections.TransactionRunner; 23 import com.sleepycat.collections.TransactionWorker; 24 import com.sleepycat.collections.TupleSerialFactory; 25 import com.sleepycat.compat.DbCompat; 26 import com.sleepycat.je.Database; 27 import com.sleepycat.je.DatabaseConfig; 28 import com.sleepycat.je.Environment; 29 import com.sleepycat.je.SecondaryConfig; 30 import com.sleepycat.je.SecondaryDatabase; 31 32 35 public class JoinTest extends TestCase 36 implements TransactionWorker { 37 38 private static final String MATCH_DATA = "d4"; private static final String MATCH_KEY = "k4"; private static final String [] VALUES = {"yes", "yes"}; 41 42 public static void main(String [] args) 43 throws Exception { 44 45 junit.framework.TestResult tr = 46 junit.textui.TestRunner.run(suite()); 47 if (tr.errorCount() > 0 || 48 tr.failureCount() > 0) { 49 System.exit(1); 50 } else { 51 System.exit(0); 52 } 53 } 54 55 public static Test suite() 56 throws Exception { 57 58 return new JoinTest(); 59 } 60 61 private Environment env; 62 private TransactionRunner runner; 63 private StoredClassCatalog catalog; 64 private TupleSerialFactory factory; 65 private Database store; 66 private SecondaryDatabase index1; 67 private SecondaryDatabase index2; 68 private StoredMap storeMap; 69 private StoredMap indexMap1; 70 private StoredMap indexMap2; 71 72 public JoinTest() { 73 74 super("JoinTest"); 75 } 76 77 public void setUp() 78 throws Exception { 79 80 DbTestUtil.printTestName(getName()); 81 env = TestEnv.TXN.open(getName()); 82 runner = new TransactionRunner(env); 83 createDatabase(); 84 } 85 86 public void tearDown() { 87 88 try { 89 if (index1 != null) { 90 index1.close(); 91 } 92 if (index2 != null) { 93 index2.close(); 94 } 95 if (store != null) { 96 store.close(); 97 } 98 if (catalog != null) { 99 catalog.close(); 100 } 101 if (env != null) { 102 env.close(); 103 } 104 } catch (Exception e) { 105 System.out.println("Ignored exception during tearDown: " + e); 106 } finally { 107 108 index1 = null; 109 index2 = null; 110 store = null; 111 catalog = null; 112 env = null; 113 runner = null; 114 factory = null; 115 storeMap = null; 116 indexMap1 = null; 117 indexMap2 = null; 118 } 119 } 120 121 public void runTest() 122 throws Exception { 123 124 runner.run(this); 125 } 126 127 public void doWork() 128 throws Exception { 129 130 createViews(); 131 writeAndRead(); 132 } 133 134 private void createDatabase() 135 throws Exception { 136 137 catalog = new StoredClassCatalog(openDb("catalog.db")); 138 factory = new TupleSerialFactory(catalog); 139 assertSame(catalog, factory.getCatalog()); 140 141 store = openDb("store.db"); 142 index1 = openSecondaryDb(store, "index1.db", "1"); 143 index2 = openSecondaryDb(store, "index2.db", "2"); 144 } 145 146 private Database openDb(String file) 147 throws Exception { 148 149 DatabaseConfig config = new DatabaseConfig(); 150 DbCompat.setTypeBtree(config); 151 config.setTransactional(true); 152 config.setAllowCreate(true); 153 154 return DbCompat.openDatabase(env, null, file, null, config); 155 } 156 157 private SecondaryDatabase openSecondaryDb(Database primary, 158 String file, 159 String keyName) 160 throws Exception { 161 162 SecondaryConfig secConfig = new SecondaryConfig(); 163 DbCompat.setTypeBtree(secConfig); 164 secConfig.setTransactional(true); 165 secConfig.setAllowCreate(true); 166 DbCompat.setSortedDuplicates(secConfig, true); 167 secConfig.setKeyCreator(factory.getKeyCreator(MarshalledObject.class, 168 keyName)); 169 170 return DbCompat.openSecondaryDatabase(env, null, 171 file, null, 172 primary, secConfig); 173 } 174 175 private void createViews() 176 throws Exception { 177 178 storeMap = factory.newMap(store, String .class, 179 MarshalledObject.class, true); 180 indexMap1 = factory.newMap(index1, String .class, 181 MarshalledObject.class, true); 182 indexMap2 = factory.newMap(index2, String .class, 183 MarshalledObject.class, true); 184 } 185 186 private void writeAndRead() 187 throws Exception { 188 189 assertNull(storeMap.put(null, 191 new MarshalledObject("d1", "k1", "no", "yes"))); 192 assertNull(storeMap.put(null, 193 new MarshalledObject("d2", "k2", "no", "no"))); 194 assertNull(storeMap.put(null, 195 new MarshalledObject("d3", "k3", "no", "yes"))); 196 assertNull(storeMap.put(null, 197 new MarshalledObject("d4", "k4", "yes", "yes"))); 198 assertNull(storeMap.put(null, 199 new MarshalledObject("d5", "k5", "yes", "no"))); 200 201 Object o; 202 Map.Entry e; 203 204 o = doJoin((StoredCollection) storeMap.values()); 206 assertEquals(MATCH_DATA, ((MarshalledObject) o).getData()); 207 208 o = doJoin((StoredCollection) storeMap.keySet()); 210 assertEquals(MATCH_KEY, o); 211 212 o = doJoin((StoredCollection) storeMap.entrySet()); 214 e = (Map.Entry ) o; 215 assertEquals(MATCH_KEY, e.getKey()); 216 assertEquals(MATCH_DATA, ((MarshalledObject) e.getValue()).getData()); 217 } 218 219 private Object doJoin(StoredCollection coll) { 220 221 StoredContainer[] indices = { indexMap1, indexMap2 }; 222 StoredIterator i = coll.join(indices, VALUES, null); 223 try { 224 assertTrue(i.hasNext()); 225 Object result = i.next(); 226 assertNotNull(result); 227 assertFalse(i.hasNext()); 228 return result; 229 } finally { i.close(); } 230 } 231 } 232 233 | Popular Tags |