1 8 9 package com.sleepycat.je.dbi; 10 11 import java.util.Enumeration ; 12 import java.util.Hashtable ; 13 import java.util.Iterator ; 14 import java.util.Map ; 15 16 import com.sleepycat.je.DatabaseEntry; 17 import com.sleepycat.je.DatabaseException; 18 import com.sleepycat.je.LockMode; 19 import com.sleepycat.je.OperationStatus; 20 import com.sleepycat.je.util.StringDbt; 21 22 25 public class DbCursorSearchTest extends DbCursorTestBase { 26 27 public DbCursorSearchTest() 28 throws DatabaseException { 29 30 super(); 31 } 32 33 37 public void testSimpleSearchKey() 38 throws DatabaseException { 39 initEnv(false); 40 doSimpleCursorPuts(); 41 verify(simpleDataMap, false); 42 } 43 44 49 public void testSimpleDeleteAndSearchKey() 50 throws DatabaseException { 51 52 initEnv(false); 53 doSimpleCursorPuts(); 54 verify(simpleDataMap, true); 55 } 56 57 61 public void testLargeSearchKey() 62 throws DatabaseException { 63 64 initEnv(false); 65 Hashtable expectedData = new Hashtable (); 66 doLargePut(expectedData, N_KEYS); 67 verify(expectedData, false); 68 } 69 70 74 public void testLargeDeleteAndSearchKey() 75 throws DatabaseException { 76 77 initEnv(false); 78 Hashtable expectedData = new Hashtable (); 79 doLargePut(expectedData, N_KEYS); 80 verify(expectedData, true); 81 } 82 83 public void testLargeSearchKeyDuplicates() 84 throws DatabaseException { 85 86 initEnv(true); 87 Hashtable expectedData = new Hashtable (); 88 createRandomDuplicateData(expectedData, false); 89 90 verifyDuplicates(expectedData); 91 } 92 93 98 public void testSimpleSearchBothWithPartialDbt() 99 throws DatabaseException { 100 101 initEnv(false); 102 doSimpleCursorPuts(); 103 DatabaseEntry key = new DatabaseEntry("bar".getBytes()); 104 DatabaseEntry data = new DatabaseEntry(new byte[100]); 105 data.setSize(3); 106 System.arraycopy("two".getBytes(), 0, data.getData(), 0, 3); 107 OperationStatus status = 108 cursor2.getSearchBoth(key, data, LockMode.DEFAULT); 109 assertEquals(OperationStatus.SUCCESS, status); 110 } 111 112 public void testGetSearchBothNoDuplicatesAllowedSR9522() 113 throws DatabaseException { 114 115 initEnv(false); 116 doSimpleCursorPuts(); 117 DatabaseEntry key = new DatabaseEntry("bar".getBytes()); 118 DatabaseEntry data = new DatabaseEntry("two".getBytes()); 119 OperationStatus status = 120 cursor2.getSearchBoth(key, data, LockMode.DEFAULT); 121 assertEquals(OperationStatus.SUCCESS, status); 122 } 123 124 127 private void verify(Hashtable expectedData, boolean doDelete) 128 throws DatabaseException { 129 130 Iterator iter = expectedData.entrySet().iterator(); 131 StringDbt testKey = new StringDbt(); 132 StringDbt testData = new StringDbt(); 133 134 while (iter.hasNext()) { 136 Map.Entry entry = (Map.Entry ) iter.next(); 137 testKey.setString((String ) entry.getKey()); 138 139 OperationStatus status = cursor2.getSearchKey(testKey, testData, 141 LockMode.DEFAULT); 142 assertEquals(OperationStatus.SUCCESS, status); 143 assertEquals((String ) entry.getValue(), testData.getString()); 144 assertEquals((String ) entry.getKey(), testKey.getString()); 145 146 status = cursor2.getCurrent(testKey, testData, LockMode.DEFAULT); 148 assertEquals(OperationStatus.SUCCESS, status); 149 assertEquals((String ) entry.getValue(), testData.getString()); 150 assertEquals((String ) entry.getKey(), testKey.getString()); 151 152 if (doDelete) { 153 status = cursor2.delete(); 156 assertEquals(OperationStatus.SUCCESS, status); 157 158 status = 160 cursor2.getSearchKey(testKey, testData, LockMode.DEFAULT); 161 assertEquals(OperationStatus.NOTFOUND, status); 162 163 status = 165 cursor2.getSearchBoth(testKey, testData, LockMode.DEFAULT); 166 assertEquals(OperationStatus.NOTFOUND, status); 167 168 status = cursor2.getSearchKeyRange 175 (testKey, testData, LockMode.DEFAULT); 176 assertTrue(status == OperationStatus.SUCCESS || 177 status == OperationStatus.NOTFOUND); 178 } else { 179 status = 181 cursor2.getSearchBoth(testKey, testData, LockMode.DEFAULT); 182 assertEquals(OperationStatus.SUCCESS, status); 183 assertEquals((String ) entry.getValue(), testData.getString()); 184 assertEquals((String ) entry.getKey(), testKey.getString()); 185 186 status = 188 cursor2.getCurrent(testKey, testData, LockMode.DEFAULT); 189 assertEquals(OperationStatus.SUCCESS, status); 190 assertEquals((String ) entry.getValue(), testData.getString()); 191 assertEquals((String ) entry.getKey(), testKey.getString()); 192 193 status = cursor2.getSearchKeyRange 195 (testKey, testData, LockMode.DEFAULT); 196 assertEquals(OperationStatus.SUCCESS, status); 197 assertEquals((String ) entry.getValue(), testData.getString()); 198 assertEquals((String ) entry.getKey(), testKey.getString()); 199 200 byte[] keyBytes = testKey.getData(); 202 keyBytes[keyBytes.length - 1]--; 203 status = cursor2.getSearchKeyRange 204 (testKey, testData, LockMode.DEFAULT); 205 assertEquals(OperationStatus.SUCCESS, status); 206 assertEquals((String ) entry.getValue(), testData.getString()); 207 assertEquals((String ) entry.getKey(), testKey.getString()); 208 209 status = 211 cursor2.getCurrent(testKey, testData, LockMode.DEFAULT); 212 assertEquals(OperationStatus.SUCCESS, status); 213 assertEquals((String ) entry.getValue(), testData.getString()); 214 assertEquals((String ) entry.getKey(), testKey.getString()); 215 } 216 } 217 } 218 219 private void verifyDuplicates(Hashtable expectedData) 220 throws DatabaseException { 221 222 Enumeration iter = expectedData.keys(); 223 StringDbt testKey = new StringDbt(); 224 StringDbt testData = new StringDbt(); 225 226 while (iter.hasMoreElements()) { 228 String key = (String ) iter.nextElement(); 229 testKey.setString(key); 230 231 OperationStatus status = cursor2.getSearchKey(testKey, testData, 233 LockMode.DEFAULT); 234 assertEquals(OperationStatus.SUCCESS, status); 235 assertEquals(key, testKey.getString()); 236 String dataString = testData.getString(); 237 238 status = cursor2.getCurrent(testKey, testData, LockMode.DEFAULT); 240 assertEquals(OperationStatus.SUCCESS, status); 241 assertEquals(dataString, testData.getString()); 242 assertEquals(key, testKey.getString()); 243 244 byte[] keyBytes = testKey.getData(); 246 keyBytes[keyBytes.length - 1]--; 247 status = 248 cursor2.getSearchKeyRange(testKey, testData, LockMode.DEFAULT); 249 assertEquals(OperationStatus.SUCCESS, status); 250 assertEquals(dataString, testData.getString()); 251 assertEquals(key, testKey.getString()); 252 253 status = cursor2.getCurrent(testKey, testData, LockMode.DEFAULT); 255 assertEquals(OperationStatus.SUCCESS, status); 256 assertEquals(dataString, testData.getString()); 257 assertEquals(key, testKey.getString()); 258 259 Hashtable ht = (Hashtable ) expectedData.get(key); 260 261 Enumeration iter2 = ht.keys(); 262 while (iter2.hasMoreElements()) { 263 String expectedDataString = (String ) iter2.nextElement(); 264 testData.setString(expectedDataString); 265 266 status = 268 cursor2.getSearchBoth(testKey, testData, LockMode.DEFAULT); 269 assertEquals(OperationStatus.SUCCESS, status); 270 assertEquals(expectedDataString, testData.getString()); 271 assertEquals(key, testKey.getString()); 272 273 status = 275 cursor2.getCurrent(testKey, testData, LockMode.DEFAULT); 276 assertEquals(OperationStatus.SUCCESS, status); 277 assertEquals(expectedDataString, testData.getString()); 278 assertEquals(key, testKey.getString()); 279 280 byte[] dataBytes = testData.getData(); 282 dataBytes[dataBytes.length - 1]--; 283 status = cursor2.getSearchBothRange(testKey, testData, 284 LockMode.DEFAULT); 285 assertEquals(OperationStatus.SUCCESS, status); 286 assertEquals(key, testKey.getString()); 287 assertEquals(expectedDataString, testData.getString()); 288 289 status = cursor2.getCurrent(testKey, testData, 291 LockMode.DEFAULT); 292 assertEquals(OperationStatus.SUCCESS, status); 293 assertEquals(expectedDataString, testData.getString()); 294 assertEquals(key, testKey.getString()); 295 } 296 } 297 } 298 } 299 | Popular Tags |