1/*- 2 * See the file LICENSE for redistribution information. 3 * 4 * Copyright (c) 2002-2009 Oracle. All rights reserved. 5 * 6 */ 7 8 9package com.sleepycat.db.test; 10 11import org.junit.After; 12import org.junit.AfterClass; 13import org.junit.Before; 14import org.junit.BeforeClass; 15import org.junit.Test; 16import static org.junit.Assert.assertEquals; 17import static org.junit.Assert.fail; 18 19import com.sleepycat.db.*; 20 21import java.io.File; 22import java.io.FileNotFoundException; 23import java.io.IOException; 24 25import com.sleepycat.db.test.TestUtils; 26 27public class MultipleCursorTest { 28 public static final String MULTIPLECURSORTEST_DBNAME = "multiplecursortest.db"; 29 30 /* The data used by this test. */ 31 private static final String[] Key_Strings = { 32 "abc", 33 "def", 34 "ghi", 35 "jkl", 36 "mno", 37 "pqr", 38 "stu", 39 "vwx", 40 "yza", 41 "bcd", 42 "efg", 43 "hij", 44 "klm", 45 "nop", 46 "qrs", 47 "tuv", 48 "wxy", 49 }; 50 private static boolean verbose = false; 51 @BeforeClass public static void ClassInit() { 52 TestUtils.loadConfig(null); 53 TestUtils.check_file_removed(TestUtils.getDBFileName(MULTIPLECURSORTEST_DBNAME), true, true); 54 TestUtils.removeall(true, true, TestUtils.BASETEST_DBDIR, TestUtils.getDBFileName(MULTIPLECURSORTEST_DBNAME)); 55 } 56 57 @AfterClass public static void ClassShutdown() { 58 TestUtils.check_file_removed(TestUtils.getDBFileName(MULTIPLECURSORTEST_DBNAME), true, true); 59 TestUtils.removeall(true, true, TestUtils.BASETEST_DBDIR, TestUtils.getDBFileName(MULTIPLECURSORTEST_DBNAME)); 60 } 61 62 @Before public void PerTestInit() 63 throws Exception { 64 } 65 66 @After public void PerTestShutdown() 67 throws Exception { 68 } 69 public static void main(String []argv) { 70 verbose = true; 71 if (argv.length > 0 && argv[0].equals("-s")) { 72 try { 73 java.lang.Thread.sleep(15*1000); 74 } catch (InterruptedException e) { 75 } 76 } 77 try { 78 MultipleCursorTest mpt = new MultipleCursorTest(); 79 mpt.testMultiplePut(); 80 mpt.testMultipleDelete(); 81 } catch (DatabaseException dbe) { 82 System.out.println("MultipleCursorTest threw DatabaseException"); 83 } catch (FileNotFoundException fnfe) { 84 System.out.println("MultipleCursorTest threw FileNotFound"); 85 } 86 } 87 /* 88 * Test case implementations. 89 * To disable a test mark it with @Ignore 90 * To set a timeout(ms) notate like: @Test(timeout=1000) 91 * To indicate an expected exception notate like: (expected=Exception) 92 */ 93 94 @Test public void testMultiplePut() 95 throws DatabaseException, FileNotFoundException 96 { 97 Database db = createDatabase(); 98 byte [] buffer = new byte[1024]; 99 byte [] buffer2 = new byte[1024]; 100 int i; 101 102 /* Build up a bulk key/data pair. */ 103 MultipleKeyDataEntry kd = new MultipleKeyDataEntry(buffer); 104 DatabaseEntry key = new DatabaseEntry(); 105 DatabaseEntry data = new DatabaseEntry(); 106 /* Put 3 in the first round. */ 107 for (i = 0; i < 3; i++) { 108 key.setData(Key_Strings[i].getBytes()); 109 data.setData(Key_Strings[i].getBytes()); 110 kd.append(key, data); 111 } 112 if (verbose) 113 System.out.println("Built up a multi-key/data buffer."); 114 db.putMultipleKey(null, kd, false); 115 if (verbose) 116 System.out.println("Put a multi-key/data buffer."); 117 118 /* Build up separate bulk key/data DatabaseEntries */ 119 MultipleDataEntry keys = new MultipleDataEntry(buffer); 120 MultipleDataEntry datas = new MultipleDataEntry(buffer2); 121 /* Put 3 in the second round. */ 122 for (; i < 6; i++) { 123 key.setData(Key_Strings[i].getBytes()); 124 data.setData(Key_Strings[i].getBytes()); 125 keys.append(key); 126 datas.append(data); 127 } 128 if (verbose) 129 System.out.println("Built up multi-key and data buffers."); 130 db.putMultiple(null, keys, datas, false); 131 if (verbose) 132 System.out.println("Put multi-key and data buffers."); 133 134 // Bulk cursor, adding single items. 135 Cursor dbc = db.openCursor(null, CursorConfig.BULK_CURSOR); 136 for (; i < 12; i++) { 137 key.setData(Key_Strings[i].getBytes()); 138 data.setData(Key_Strings[i].getBytes()); 139 dbc.put(key, data); 140 } 141 dbc.close(); 142 143 if (verbose) 144 dumpDatabase(db); 145 } 146 @Test public void testMultipleDelete() 147 throws DatabaseException, FileNotFoundException 148 { 149 byte [] buffer = new byte[1024]; 150 int i; 151 Database db = createDatabase(); 152 populateDatabase(db, 0); 153 154 /* Build up separate bulk key/data DatabaseEntries */ 155 MultipleDataEntry keys = new MultipleDataEntry(buffer); 156 DatabaseEntry key = new DatabaseEntry(); 157 /* Put 3 in the second round. */ 158 for (i = 0; i < 6; i++) { 159 key.setData(Key_Strings[i].getBytes()); 160 keys.append(key); 161 } 162 db.deleteMultiple(null, keys); 163 // Bulk cursor, adding single items. 164 DatabaseEntry data = new DatabaseEntry(); 165 Cursor dbc = db.openCursor(null, CursorConfig.BULK_CURSOR); 166 for (; i < 12; i++) { 167 key.setData(Key_Strings[i].getBytes()); 168 dbc.getSearchKey(key, data, LockMode.DEFAULT); 169 dbc.delete(); 170 } 171 dbc.close(); 172 173 // Should have about 3 entries left. 174 if (verbose) 175 dumpDatabase(db); 176 } 177 178 /* Not implemented yet. 179 @Test public void testMultipleGet() 180 throws DatabaseException, FileNotFoundException 181 { 182 Database db = createDatabase(); 183 populateDatabase(db, 0); 184 } 185 */ 186 187 private Database createDatabase() 188 throws DatabaseException, FileNotFoundException 189 { 190 /* Create database. */ 191 Database db; 192 DatabaseConfig db_config = new DatabaseConfig(); 193 String name = TestUtils.getDBFileName(MULTIPLECURSORTEST_DBNAME); 194 195 db_config.setAllowCreate(true); 196 db_config.setType(DatabaseType.BTREE); 197 db_config.setSortedDuplicates(true); 198 199 db = new Database(name, null, db_config); 200 return db; 201 } 202 203 private void populateDatabase(Database db, int duplicates) 204 throws DatabaseException, FileNotFoundException 205 { 206 DatabaseEntry key = new DatabaseEntry(); 207 DatabaseEntry data = new DatabaseEntry(); 208 for (int i = 0; i < Key_Strings.length; i++) { 209 String datastr = new Integer(i).toString() + Key_Strings[i] + Key_Strings[i]; 210 key.setData(Key_Strings[i].getBytes()); 211 data.setData(datastr.getBytes()); 212 db.put(null, key, data); 213 for (int j = 0; j < duplicates; j++) { 214 datastr = new Integer(j).toString() + datastr + Key_Strings[i]; 215 data.setData(datastr.getBytes()); 216 db.put(null, key, data); 217 218 } 219 } 220 } 221 222 private void dumpDatabase(Database db) { 223 try { 224 Cursor dbc = db.openCursor(null, CursorConfig.DEFAULT); 225 DatabaseEntry key = new DatabaseEntry(); 226 DatabaseEntry data = new DatabaseEntry(); 227 228 System.out.println("Dumping database contents:"); 229 while (dbc.getNext(key, data, LockMode.DEFAULT) != OperationStatus.NOTFOUND) { 230 System.out.println("\tGot key : " + new String(key.getData())); 231 System.out.println("\t data: " + new String(data.getData())); 232 } 233 System.out.println("Finished dumping database contents."); 234 } catch (DatabaseException dbe) { 235 System.err.println("dumpDatabase caught an exception."); 236 } 237 } 238 239} 240