KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > collections > test > JoinTest


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2000,2006 Oracle. All rights reserved.
5  *
6  * $Id: JoinTest.java,v 1.30 2006/10/30 21:14:39 bostic Exp $
7  */

8
9 package com.sleepycat.collections.test;
10
11 import java.util.Map JavaDoc;
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 /**
33  * @author Mark Hayes
34  */

35 public class JoinTest extends TestCase
36     implements TransactionWorker {
37
38     private static final String JavaDoc MATCH_DATA = "d4"; // matches both keys = "yes"
39
private static final String JavaDoc MATCH_KEY = "k4"; // matches both keys = "yes"
40
private static final String JavaDoc[] VALUES = {"yes", "yes"};
41
42     public static void main(String JavaDoc[] args)
43         throws Exception JavaDoc {
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 JavaDoc {
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 JavaDoc {
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 JavaDoc e) {
105             System.out.println("Ignored exception during tearDown: " + e);
106         } finally {
107             /* Ensure that GC can cleanup. */
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 JavaDoc {
123
124         runner.run(this);
125     }
126
127     public void doWork()
128         throws Exception JavaDoc {
129
130         createViews();
131         writeAndRead();
132     }
133
134     private void createDatabase()
135         throws Exception JavaDoc {
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 JavaDoc file)
147         throws Exception JavaDoc {
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 JavaDoc file,
159                                               String JavaDoc keyName)
160         throws Exception JavaDoc {
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 JavaDoc {
177
178         storeMap = factory.newMap(store, String JavaDoc.class,
179                                          MarshalledObject.class, true);
180         indexMap1 = factory.newMap(index1, String JavaDoc.class,
181                                            MarshalledObject.class, true);
182         indexMap2 = factory.newMap(index2, String JavaDoc.class,
183                                            MarshalledObject.class, true);
184     }
185
186     private void writeAndRead()
187         throws Exception JavaDoc {
188
189         // write records: Data, PrimaryKey, IndexKey1, IndexKey2
190
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 JavaDoc o;
202         Map.Entry JavaDoc e;
203
204         // join values with index maps
205
o = doJoin((StoredCollection) storeMap.values());
206         assertEquals(MATCH_DATA, ((MarshalledObject) o).getData());
207
208         // join keySet with index maps
209
o = doJoin((StoredCollection) storeMap.keySet());
210         assertEquals(MATCH_KEY, o);
211
212         // join entrySet with index maps
213
o = doJoin((StoredCollection) storeMap.entrySet());
214         e = (Map.Entry JavaDoc) o;
215         assertEquals(MATCH_KEY, e.getKey());
216         assertEquals(MATCH_DATA, ((MarshalledObject) e.getValue()).getData());
217     }
218
219     private Object JavaDoc 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 JavaDoc result = i.next();
226             assertNotNull(result);
227             assertFalse(i.hasNext());
228             return result;
229         } finally { i.close(); }
230     }
231 }
232
233
Popular Tags