1 8 9 package com.sleepycat.je; 10 11 import java.io.File ; 12 import java.io.IOException ; 13 import java.util.Arrays ; 14 15 import junit.framework.TestCase; 16 17 import com.sleepycat.je.DbInternal; 18 import com.sleepycat.je.config.EnvironmentParams; 19 import com.sleepycat.je.util.TestUtils; 20 21 public class DatabaseEntryTest extends TestCase { 22 23 private File envHome; 24 private Environment env; 25 private Database db; 26 27 public DatabaseEntryTest() { 28 envHome = new File (System.getProperty(TestUtils.DEST_DIR)); 29 } 30 31 public void setUp() 32 throws IOException { 33 34 TestUtils.removeLogFiles("Setup", envHome, false); 35 } 36 37 public void tearDown() 38 throws IOException { 39 40 TestUtils.removeLogFiles("TearDown", envHome, false); 41 } 42 43 public void testBasic() 44 throws Exception { 45 46 47 int size = 10; 48 byte [] foo = new byte[size]; 49 byte val = 1; 50 Arrays.fill(foo, val); 51 52 DatabaseEntry dbtA = new DatabaseEntry(foo); 53 assertEquals(foo.length, dbtA.getSize()); 54 assertTrue(Arrays.equals(foo, dbtA.getData())); 55 56 57 dbtA.setData(null); 58 assertEquals(0, dbtA.getSize()); 59 assertFalse(Arrays.equals(foo, dbtA.getData())); 60 61 62 DatabaseEntry dbtLater = new DatabaseEntry(); 63 assertTrue(dbtLater.getData() == null); 64 assertEquals(0, dbtLater.getSize()); 65 dbtLater.setData(foo); 66 assertTrue(Arrays.equals(foo, dbtLater.getData())); 67 68 69 DatabaseEntry dbtOffset = new DatabaseEntry(foo, 1, 1); 70 assertEquals(1, dbtOffset.getOffset()); 71 assertEquals(1, dbtOffset.getSize()); 72 dbtOffset.setData(foo); 73 assertEquals(0, dbtOffset.getOffset()); 74 assertEquals(foo.length, dbtOffset.getSize()); 75 } 76 77 public void testOffset() 78 throws DatabaseException { 79 80 final int N_BYTES = 30; 81 82 openDb(false); 83 84 DatabaseEntry originalKey = new DatabaseEntry(new byte[N_BYTES]); 85 DatabaseEntry originalData = new DatabaseEntry(new byte[N_BYTES]); 86 for (int i = 0; i < N_BYTES; i++) { 87 originalKey.getData()[i] = (byte) i; 88 originalData.getData()[i] = (byte) i; 89 } 90 91 originalKey.setSize(10); 92 originalKey.setOffset(10); 93 originalData.setSize(10); 94 originalData.setOffset(10); 95 96 db.put(null, originalKey, originalData); 97 98 Cursor cursor = db.openCursor(null, CursorConfig.DEFAULT); 99 100 DatabaseEntry foundKey = new DatabaseEntry(); 101 DatabaseEntry foundData = new DatabaseEntry(); 102 103 assertEquals(OperationStatus.SUCCESS, 104 cursor.getFirst(foundKey, foundData, 105 LockMode.DEFAULT)); 106 107 assertEquals(0, foundKey.getOffset()); 108 assertEquals(0, foundData.getOffset()); 109 assertEquals(10, foundKey.getSize()); 110 assertEquals(10, foundData.getSize()); 111 for (int i = 0; i < 10; i++) { 112 assertEquals(i + 10, foundKey.getData()[i]); 113 assertEquals(i + 10, foundData.getData()[i]); 114 } 115 116 cursor.close(); 117 closeDb(); 118 } 119 120 public void testPartial() 121 throws DatabaseException { 122 123 openDb(false); 124 125 DatabaseEntry originalKey = new DatabaseEntry(new byte[20]); 126 DatabaseEntry originalData = new DatabaseEntry(new byte[20]); 127 for (int i = 0; i < 20; i++) { 128 originalKey.getData()[i] = (byte) i; 129 originalData.getData()[i] = (byte) i; 130 } 131 132 originalData.setPartial(true); 133 originalData.setPartialLength(10); 134 originalData.setPartialOffset(10); 135 136 db.put(null, originalKey, originalData); 137 138 Cursor cursor = db.openCursor(null, CursorConfig.DEFAULT); 139 140 DatabaseEntry foundKey = new DatabaseEntry(); 141 DatabaseEntry foundData = new DatabaseEntry(); 142 143 assertEquals(OperationStatus.SUCCESS, 144 cursor.getFirst(foundKey, foundData, 145 LockMode.DEFAULT)); 146 147 assertEquals(0, foundKey.getOffset()); 148 assertEquals(20, foundKey.getSize()); 149 for (int i = 0; i < 20; i++) { 150 assertEquals(i, foundKey.getData()[i]); 151 } 152 153 assertEquals(0, foundData.getOffset()); 154 assertEquals(30, foundData.getSize()); 155 for (int i = 0; i < 10; i++) { 156 assertEquals(0, foundData.getData()[i]); 157 } 158 for (int i = 0; i < 20; i++) { 159 assertEquals(i, foundData.getData()[i + 10]); 160 } 161 162 foundKey.setPartial(5, 10, true); 163 foundData.setPartial(5, 20, true); 164 165 assertEquals(OperationStatus.SUCCESS, 166 cursor.getFirst(foundKey, foundData, 167 LockMode.DEFAULT)); 168 assertEquals(0, foundKey.getOffset()); 169 assertEquals(10, foundKey.getSize()); 170 for (int i = 0; i < 10; i++) { 171 assertEquals(i + 5, foundKey.getData()[i]); 172 } 173 174 assertEquals(0, foundData.getOffset()); 175 assertEquals(20, foundData.getSize()); 176 for (int i = 0; i < 5; i++) { 177 assertEquals(0, foundData.getData()[i]); 178 } 179 for (int i = 0; i < 15; i++) { 180 assertEquals(i, foundData.getData()[i + 5]); 181 } 182 183 184 185 originalKey.setPartial(true); 186 originalKey.setPartialLength(10); 187 originalKey.setPartialOffset(10); 188 189 try { 190 db.put(null, originalKey, originalData); 191 fail(); 192 } catch (IllegalArgumentException expected) {} 193 try { 194 db.putNoOverwrite(null, originalKey, originalData); 195 fail(); 196 } catch (IllegalArgumentException expected) {} 197 try { 198 db.putNoDupData(null, originalKey, originalData); 199 fail(); 200 } catch (IllegalArgumentException expected) {} 201 202 try { 203 cursor.put(originalKey, originalData); 204 fail(); 205 } catch (IllegalArgumentException expected) {} 206 try { 207 cursor.putNoOverwrite(originalKey, originalData); 208 fail(); 209 } catch (IllegalArgumentException expected) {} 210 try { 211 cursor.putNoDupData(originalKey, originalData); 212 fail(); 213 } catch (IllegalArgumentException expected) {} 214 215 cursor.close(); 216 closeDb(); 217 } 218 219 public void testPartialCursorPuts() 220 throws DatabaseException { 221 222 openDb(false); 223 224 DatabaseEntry originalKey = new DatabaseEntry(new byte[20]); 225 DatabaseEntry originalData = new DatabaseEntry(new byte[20]); 226 for (int i = 0; i < 20; i++) { 227 originalKey.getData()[i] = (byte) i; 228 originalData.getData()[i] = (byte) i; 229 } 230 231 232 db.put(null, originalKey, originalData); 233 234 Cursor cursor = db.openCursor(null, CursorConfig.DEFAULT); 235 236 DatabaseEntry foundKey = new DatabaseEntry(); 237 DatabaseEntry foundData = new DatabaseEntry(); 238 239 assertEquals(OperationStatus.SUCCESS, 240 cursor.getFirst(foundKey, foundData, 241 LockMode.DEFAULT)); 242 243 assertEquals(0, foundKey.getOffset()); 244 assertEquals(20, foundKey.getSize()); 245 for (int i = 0; i < 20; i++) { 246 assertEquals(i, foundKey.getData()[i]); 247 } 248 249 assertEquals(0, foundData.getOffset()); 250 assertEquals(20, foundData.getSize()); 251 252 for (int i = 0; i < 20; i++) { 253 assertEquals(i, foundData.getData()[i]); 254 } 255 256 for (int i = 0; i < 10; i++) { 257 foundData.getData()[i] = (byte) (i + 50); 258 } 259 260 foundData.setPartial(true); 261 foundData.setPartialLength(10); 262 foundData.setPartialOffset(10); 263 264 cursor.putCurrent(foundData); 265 266 foundData = new DatabaseEntry(); 267 268 assertEquals(OperationStatus.SUCCESS, 269 cursor.getFirst(foundKey, foundData, 270 LockMode.DEFAULT)); 271 assertEquals(0, foundKey.getOffset()); 272 assertEquals(20, foundKey.getSize()); 273 assertEquals(0, foundData.getOffset()); 274 assertEquals(30, foundData.getSize()); 275 for (int i = 0; i < 10; i++) { 276 assertEquals(foundData.getData()[i], i); 277 assertEquals(foundData.getData()[i + 10], (i + 50)); 278 assertEquals(foundData.getData()[i + 20], (i + 10)); 279 } 280 281 cursor.close(); 282 closeDb(); 283 } 284 285 private void openDb(boolean dups) 286 throws DatabaseException { 287 288 EnvironmentConfig envConfig = TestUtils.initEnvConfig(); 289 DbInternal.disableParameterValidation(envConfig); 290 envConfig.setTransactional(true); 291 envConfig.setAllowCreate(true); 292 envConfig.setConfigParam(EnvironmentParams.LOG_FILE_MAX.getName(), 293 "1024"); 294 envConfig.setConfigParam(EnvironmentParams.ENV_CHECK_LEAKS.getName(), 295 "true"); 296 envConfig.setConfigParam(EnvironmentParams.NODE_MAX.getName(), 297 "6"); 298 env = new Environment(envHome, envConfig); 299 300 DatabaseConfig dbConfig = new DatabaseConfig(); 301 dbConfig.setAllowCreate(true); 302 dbConfig.setSortedDuplicates(dups); 303 db = env.openDatabase(null, "testDB", dbConfig); 304 } 305 306 private void closeDb() 307 throws DatabaseException { 308 309 if (db != null) { 310 db.close(); 311 db = null; 312 } 313 if (env != null) { 314 env.close(); 315 env = null; 316 } 317 } 318 } 319 | Popular Tags |