1 8 9 package com.sleepycat.je.dbi; 10 11 import java.io.File ; 12 import java.io.FileOutputStream ; 13 import java.io.IOException ; 14 import java.io.PrintStream ; 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 43 public class SR12641 extends TestCase { 44 45 46 private static final int NODE_MAX = 6; 47 48 private File envHome; 49 private Environment env; 50 private Database db; 51 private boolean dups; 52 private boolean writerStopped; 53 54 public SR12641() 55 throws Exception { 56 57 envHome = new File (System.getProperty(TestUtils.DEST_DIR)); 58 } 59 60 public void setUp() 61 throws Exception { 62 63 TestUtils.removeFiles("Setup", envHome, FileManager.JE_SUFFIX); 64 } 65 66 public void tearDown() 67 throws Exception { 68 69 if (env != null) { 70 try { 71 env.close(); 72 } catch (Exception 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 { 83 84 dups = true; 85 testSplitsWithScans(); 86 } 87 88 public void testSplitsWithScans() 89 throws Throwable { 90 91 open(); 92 93 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 e) { 121 try { 122 FileOutputStream os = 123 new FileOutputStream (new File ("./err.txt")); 124 e.printStackTrace(new PrintStream (os)); 125 os.close(); 126 } catch (IOException ignored) {} 127 System.exit(1); 128 } 129 } 130 }; 131 132 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 e) { 153 try { 154 FileOutputStream os = 155 new FileOutputStream (new File ("./err.txt")); 156 e.printStackTrace(new PrintStream (os)); 157 os.close(); 158 } catch (IOException 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 { 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 { 191 192 db.close(); 193 db = null; 194 env.close(); 195 env = null; 196 } 197 } 198 | Popular Tags |