KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > dbi > SR12641


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

8
9 package com.sleepycat.je.dbi;
10
11 import java.io.File JavaDoc;
12 import java.io.FileOutputStream JavaDoc;
13 import java.io.IOException JavaDoc;
14 import java.io.PrintStream JavaDoc;
15
16 import junit.framework.TestCase;
17
18 import com.sleepycat.bind.tuple.IntegerBinding;
19 import com.sleepycat.je.Cursor;
20 import com.sleepycat.je.CursorConfig;
21 import com.sleepycat.je.Database;
22 import com.sleepycat.je.DatabaseConfig;
23 import com.sleepycat.je.DatabaseEntry;
24 import com.sleepycat.je.Environment;
25 import com.sleepycat.je.EnvironmentConfig;
26 import com.sleepycat.je.OperationStatus;
27 import com.sleepycat.je.config.EnvironmentParams;
28 import com.sleepycat.je.junit.JUnitThread;
29 import com.sleepycat.je.log.FileManager;
30 import com.sleepycat.je.util.TestUtils;
31
32 /**
33  * This reproduces the bug described SR [#12641], also related to SR [#9543].
34  *
35  * Note that allthough this is a JUnit test case, it is not run as part of the
36  * JUnit test suite. It takes a long time, and when it fails it hangs.
37  * Therefore, it was only used for debugging and is not intended to be a
38  * regression test.
39  *
40  * For some reason the bug was not reproducible with a simple main program,
41  * which is why a JUnit test was used.
42  */

43 public class SR12641 extends TestCase {
44
45     /* Use small NODE_MAX to cause lots of splits. */
46     private static final int NODE_MAX = 6;
47
48     private File JavaDoc envHome;
49     private Environment env;
50     private Database db;
51     private boolean dups;
52     private boolean writerStopped;
53
54     public SR12641()
55         throws Exception JavaDoc {
56
57         envHome = new File JavaDoc(System.getProperty(TestUtils.DEST_DIR));
58     }
59
60     public void setUp()
61         throws Exception JavaDoc {
62
63         TestUtils.removeFiles("Setup", envHome, FileManager.JE_SUFFIX);
64     }
65
66     public void tearDown()
67     throws Exception JavaDoc {
68
69         if (env != null) {
70             try {
71                 env.close();
72             } catch (Exception JavaDoc e) {
73                 System.err.println("TearDown: " + e);
74             }
75         }
76         env = null;
77         db = null;
78         TestUtils.removeFiles("TearDown", envHome, FileManager.JE_SUFFIX);
79     }
80
81     public void testSplitsWithScansDups()
82         throws Throwable JavaDoc {
83
84         dups = true;
85         testSplitsWithScans();
86     }
87
88     public void testSplitsWithScans()
89         throws Throwable JavaDoc {
90
91         open();
92
93         /* Cause splits in the last BIN. */
94         JUnitThread writer = new JUnitThread("writer") {
95             public void testBody() {
96                 try {
97                     DatabaseEntry key = new DatabaseEntry(new byte[1]);
98                     DatabaseEntry data = new DatabaseEntry(new byte[1]);
99                     OperationStatus status;
100
101                     Cursor cursor = db.openCursor(null, null);
102
103                     for (int i = 0; i < 100000; i += 1) {
104                         IntegerBinding.intToEntry(i, dups ? data : key);
105                         if (dups) {
106                             status = cursor.putNoDupData(key, data);
107                         } else {
108                             status = cursor.putNoOverwrite(key, data);
109                         }
110                         assertEquals(OperationStatus.SUCCESS, status);
111
112                         if (i % 5000 == 0) {
113                             System.out.println("Iteration: " + i);
114                         }
115                     }
116
117                     cursor.close();
118                     writerStopped = true;
119
120                 } catch (Exception JavaDoc e) {
121                     try {
122                         FileOutputStream JavaDoc os =
123                             new FileOutputStream JavaDoc(new File JavaDoc("./err.txt"));
124                         e.printStackTrace(new PrintStream JavaDoc(os));
125                         os.close();
126                     } catch (IOException JavaDoc ignored) {}
127                     System.exit(1);
128                 }
129             }
130         };
131
132         /* Move repeatedly from the last BIN to the prior BIN. */
133         JUnitThread reader = new JUnitThread("reader") {
134             public void testBody() {
135                 try {
136                     DatabaseEntry key = new DatabaseEntry();
137                     DatabaseEntry data = new DatabaseEntry();
138
139                     CursorConfig cursorConfig = new CursorConfig();
140                     cursorConfig.setReadUncommitted(true);
141                     Cursor cursor = db.openCursor(null, cursorConfig);
142
143                     while (!writerStopped) {
144                         cursor.getLast(key, data, null);
145                         for (int i = 0; i <= NODE_MAX; i += 1) {
146                             cursor.getPrev(key, data, null);
147                         }
148                     }
149
150                     cursor.close();
151
152                 } catch (Exception JavaDoc e) {
153                     try {
154                         FileOutputStream JavaDoc os =
155                             new FileOutputStream JavaDoc(new File JavaDoc("./err.txt"));
156                         e.printStackTrace(new PrintStream JavaDoc(os));
157                         os.close();
158                     } catch (IOException JavaDoc ignored) {}
159                     System.exit(1);
160                 }
161             }
162         };
163
164         writer.start();
165         reader.start();
166         writer.finishTest();
167         reader.finishTest();
168
169         close();
170         System.out.println("SUCCESS");
171     }
172
173     private void open()
174         throws Exception JavaDoc {
175
176         EnvironmentConfig envConfig = TestUtils.initEnvConfig();
177         envConfig.setConfigParam
178             (EnvironmentParams.NODE_MAX.getName(), String.valueOf(NODE_MAX));
179         envConfig.setAllowCreate(true);
180         env = new Environment(envHome, envConfig);
181
182         DatabaseConfig dbConfig = new DatabaseConfig();
183         dbConfig.setAllowCreate(true);
184         dbConfig.setExclusiveCreate(true);
185         dbConfig.setSortedDuplicates(dups);
186         db = env.openDatabase(null, "testDb", dbConfig);
187     }
188
189     private void close()
190         throws Exception JavaDoc {
191
192         db.close();
193         db = null;
194         env.close();
195         env = null;
196     }
197 }
198
Popular Tags