1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
/*-
* See the file LICENSE for redistribution information.
*
* Copyright (c) 2002,2010 Oracle. All rights reserved.
*
* $Id: KeyTest.java,v 1.18.2.2 2010/01/04 15:30:48 cwl Exp $
*/
package com.sleepycat.je.tree;
import java.io.File;
import junit.framework.TestCase;
import com.sleepycat.je.DatabaseConfig;
import com.sleepycat.je.DbInternal;
import com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;
import com.sleepycat.je.dbi.DatabaseId;
import com.sleepycat.je.dbi.DatabaseImpl;
import com.sleepycat.je.dbi.EnvironmentImpl;
import com.sleepycat.je.util.TestUtils;
public class KeyTest extends TestCase {
private File envHome;
private Environment env;
public void setUp() {
envHome = new File(System.getProperty(TestUtils.DEST_DIR));
}
public void tearDown() {
}
public void testKeyPrefixer() {
assertEquals("aaa", makePrefix("aaaa", "aaab"));
assertEquals("a", makePrefix("abaa", "aaab"));
assertNull(makePrefix("baaa", "aaab"));
assertEquals("aaa", makePrefix("aaa", "aaa"));
assertEquals("aaa", makePrefix("aaa", "aaab"));
}
private String makePrefix(String k1, String k2) {
byte[] ret = Key.createKeyPrefix(k1.getBytes(), k2.getBytes());
if (ret == null) {
return null;
} else {
return new String(ret);
}
}
public void testKeyPrefixSubsetting() {
keyPrefixSubsetTest("aaa", "aaa", true);
keyPrefixSubsetTest("aa", "aaa", true);
keyPrefixSubsetTest("aaa", "aa", false);
keyPrefixSubsetTest("", "aa", false);
keyPrefixSubsetTest(null, "aa", false);
keyPrefixSubsetTest("baa", "aa", false);
}
private void keyPrefixSubsetTest(String keyPrefix,
String newKey,
boolean expect) {
try {
EnvironmentConfig envConfig = TestUtils.initEnvConfig();
envConfig.setAllowCreate(true);
env = new Environment(envHome, envConfig);
byte[] keyPrefixBytes =
(keyPrefix == null ? null : keyPrefix.getBytes());
byte[] newKeyBytes = newKey.getBytes();
DatabaseConfig dbConf = new DatabaseConfig();
dbConf.setKeyPrefixing(true);
EnvironmentImpl envImpl = DbInternal.envGetEnvironmentImpl(env);
DatabaseImpl databaseImpl =
new DatabaseImpl("dummy", new DatabaseId(10), envImpl, dbConf);
IN in = new IN(databaseImpl, null, 10, 10);
in.setKeyPrefix(keyPrefixBytes);
boolean result = in.compareToKeyPrefix(newKeyBytes);
assertTrue(result == expect);
} catch (Exception E) {
E.printStackTrace();
fail("caught " + E);
}
}
public void testKeyComparisonPerformance() {
byte[] key1 = "abcdefghijabcdefghij".getBytes();
byte[] key2 = "abcdefghijabcdefghij".getBytes();
for (int i = 0; i < 1000000; i++) {
assertTrue(Key.compareKeys(key1, key2, null) == 0);
}
}
public void testKeyComparison() {
byte[] key1 = "aaa".getBytes();
byte[] key2 = "aab".getBytes();
assertTrue(Key.compareKeys(key1, key2, null) < 0);
assertTrue(Key.compareKeys(key2, key1, null) > 0);
assertTrue(Key.compareKeys(key1, key1, null) == 0);
key1 = "aa".getBytes();
key2 = "aab".getBytes();
assertTrue(Key.compareKeys(key1, key2, null) < 0);
assertTrue(Key.compareKeys(key2, key1, null) > 0);
key1 = "".getBytes();
key2 = "aab".getBytes();
assertTrue(Key.compareKeys(key1, key2, null) < 0);
assertTrue(Key.compareKeys(key2, key1, null) > 0);
assertTrue(Key.compareKeys(key1, key1, null) == 0);
key1 = "".getBytes();
key2 = "".getBytes();
assertTrue(Key.compareKeys(key1, key2, null) == 0);
byte[] ba1 = { -1, -1, -1 };
byte[] ba2 = { 0x7f, 0x7f, 0x7f };
assertTrue(Key.compareKeys(ba1, ba2, null) > 0);
try {
Key.compareKeys(key1, null, null);
fail("NullPointerException not caught");
} catch (NullPointerException NPE) {
}
}
}
|