001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2000,2008 Oracle. All rights reserved.
005: *
006: * $Id: JoinTest.java,v 1.30.2.2 2008/01/07 15:14:24 cwl Exp $
007: */
008:
009: package com.sleepycat.collections.test;
010:
011: import java.util.Map;
012:
013: import junit.framework.Test;
014: import junit.framework.TestCase;
015:
016: import com.sleepycat.bind.serial.StoredClassCatalog;
017: import com.sleepycat.bind.serial.test.MarshalledObject;
018: import com.sleepycat.collections.StoredCollection;
019: import com.sleepycat.collections.StoredContainer;
020: import com.sleepycat.collections.StoredIterator;
021: import com.sleepycat.collections.StoredMap;
022: import com.sleepycat.collections.TransactionRunner;
023: import com.sleepycat.collections.TransactionWorker;
024: import com.sleepycat.collections.TupleSerialFactory;
025: import com.sleepycat.compat.DbCompat;
026: import com.sleepycat.je.Database;
027: import com.sleepycat.je.DatabaseConfig;
028: import com.sleepycat.je.Environment;
029: import com.sleepycat.je.SecondaryConfig;
030: import com.sleepycat.je.SecondaryDatabase;
031:
032: /**
033: * @author Mark Hayes
034: */
035: public class JoinTest extends TestCase implements TransactionWorker {
036:
037: private static final String MATCH_DATA = "d4"; // matches both keys = "yes"
038: private static final String MATCH_KEY = "k4"; // matches both keys = "yes"
039: private static final String[] VALUES = { "yes", "yes" };
040:
041: public static void main(String[] args) throws Exception {
042:
043: junit.framework.TestResult tr = junit.textui.TestRunner
044: .run(suite());
045: if (tr.errorCount() > 0 || tr.failureCount() > 0) {
046: System.exit(1);
047: } else {
048: System.exit(0);
049: }
050: }
051:
052: public static Test suite() throws Exception {
053:
054: return new JoinTest();
055: }
056:
057: private Environment env;
058: private TransactionRunner runner;
059: private StoredClassCatalog catalog;
060: private TupleSerialFactory factory;
061: private Database store;
062: private SecondaryDatabase index1;
063: private SecondaryDatabase index2;
064: private StoredMap storeMap;
065: private StoredMap indexMap1;
066: private StoredMap indexMap2;
067:
068: public JoinTest() {
069:
070: super ("JoinTest");
071: }
072:
073: public void setUp() throws Exception {
074:
075: DbTestUtil.printTestName(getName());
076: env = TestEnv.TXN.open(getName());
077: runner = new TransactionRunner(env);
078: createDatabase();
079: }
080:
081: public void tearDown() {
082:
083: try {
084: if (index1 != null) {
085: index1.close();
086: }
087: if (index2 != null) {
088: index2.close();
089: }
090: if (store != null) {
091: store.close();
092: }
093: if (catalog != null) {
094: catalog.close();
095: }
096: if (env != null) {
097: env.close();
098: }
099: } catch (Exception e) {
100: System.out.println("Ignored exception during tearDown: "
101: + e);
102: } finally {
103: /* Ensure that GC can cleanup. */
104: index1 = null;
105: index2 = null;
106: store = null;
107: catalog = null;
108: env = null;
109: runner = null;
110: factory = null;
111: storeMap = null;
112: indexMap1 = null;
113: indexMap2 = null;
114: }
115: }
116:
117: public void runTest() throws Exception {
118:
119: runner.run(this );
120: }
121:
122: public void doWork() throws Exception {
123:
124: createViews();
125: writeAndRead();
126: }
127:
128: private void createDatabase() throws Exception {
129:
130: catalog = new StoredClassCatalog(openDb("catalog.db"));
131: factory = new TupleSerialFactory(catalog);
132: assertSame(catalog, factory.getCatalog());
133:
134: store = openDb("store.db");
135: index1 = openSecondaryDb(store, "index1.db", "1");
136: index2 = openSecondaryDb(store, "index2.db", "2");
137: }
138:
139: private Database openDb(String file) throws Exception {
140:
141: DatabaseConfig config = new DatabaseConfig();
142: DbCompat.setTypeBtree(config);
143: config.setTransactional(true);
144: config.setAllowCreate(true);
145:
146: return DbCompat.openDatabase(env, null, file, null, config);
147: }
148:
149: private SecondaryDatabase openSecondaryDb(Database primary,
150: String file, String keyName) throws Exception {
151:
152: SecondaryConfig secConfig = new SecondaryConfig();
153: DbCompat.setTypeBtree(secConfig);
154: secConfig.setTransactional(true);
155: secConfig.setAllowCreate(true);
156: DbCompat.setSortedDuplicates(secConfig, true);
157: secConfig.setKeyCreator(factory.getKeyCreator(
158: MarshalledObject.class, keyName));
159:
160: return DbCompat.openSecondaryDatabase(env, null, file, null,
161: primary, secConfig);
162: }
163:
164: private void createViews() throws Exception {
165:
166: storeMap = factory.newMap(store, String.class,
167: MarshalledObject.class, true);
168: indexMap1 = factory.newMap(index1, String.class,
169: MarshalledObject.class, true);
170: indexMap2 = factory.newMap(index2, String.class,
171: MarshalledObject.class, true);
172: }
173:
174: private void writeAndRead() throws Exception {
175:
176: // write records: Data, PrimaryKey, IndexKey1, IndexKey2
177: assertNull(storeMap.put(null, new MarshalledObject("d1", "k1",
178: "no", "yes")));
179: assertNull(storeMap.put(null, new MarshalledObject("d2", "k2",
180: "no", "no")));
181: assertNull(storeMap.put(null, new MarshalledObject("d3", "k3",
182: "no", "yes")));
183: assertNull(storeMap.put(null, new MarshalledObject("d4", "k4",
184: "yes", "yes")));
185: assertNull(storeMap.put(null, new MarshalledObject("d5", "k5",
186: "yes", "no")));
187:
188: Object o;
189: Map.Entry e;
190:
191: // join values with index maps
192: o = doJoin((StoredCollection) storeMap.values());
193: assertEquals(MATCH_DATA, ((MarshalledObject) o).getData());
194:
195: // join keySet with index maps
196: o = doJoin((StoredCollection) storeMap.keySet());
197: assertEquals(MATCH_KEY, o);
198:
199: // join entrySet with index maps
200: o = doJoin((StoredCollection) storeMap.entrySet());
201: e = (Map.Entry) o;
202: assertEquals(MATCH_KEY, e.getKey());
203: assertEquals(MATCH_DATA, ((MarshalledObject) e.getValue())
204: .getData());
205: }
206:
207: private Object doJoin(StoredCollection coll) {
208:
209: StoredContainer[] indices = { indexMap1, indexMap2 };
210: StoredIterator i = coll.join(indices, VALUES, null);
211: try {
212: assertTrue(i.hasNext());
213: Object result = i.next();
214: assertNotNull(result);
215: assertFalse(i.hasNext());
216: return result;
217: } finally {
218: i.close();
219: }
220: }
221: }
|