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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
|
/*-
* See the file LICENSE for redistribution information.
*
* Copyright (c) 2002,2010 Oracle. All rights reserved.
*
* $Id: TreeTestBase.java,v 1.59.2.2 2010/01/04 15:30:48 cwl Exp $
*/
package com.sleepycat.je.tree;
import java.io.File;
import java.io.IOException;
import junit.framework.TestCase;
import com.sleepycat.je.CacheMode;
import com.sleepycat.je.Database;
import com.sleepycat.je.DatabaseConfig;
import com.sleepycat.je.DatabaseException;
import com.sleepycat.je.DbInternal;
import com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;
import com.sleepycat.je.config.EnvironmentParams;
import com.sleepycat.je.dbi.NullCursor;
import com.sleepycat.je.log.FileManager;
import com.sleepycat.je.log.ReplicationContext;
import com.sleepycat.je.txn.LockResult;
import com.sleepycat.je.util.TestUtils;
public class TreeTestBase extends TestCase {
static protected final boolean DEBUG = true;
static protected int N_KEY_BYTES = 10;
static protected int N_ITERS = 1;
static protected int N_KEYS = 10000;
static protected int MAX_ENTRIES_PER_NODE = 6;
protected Tree tree = null;
protected byte[] minKey = null;
protected byte[] maxKey = null;
protected Database db = null;
protected Environment env = null;
protected File envHome = null;
public TreeTestBase() {
envHome = new File(System.getProperty(TestUtils.DEST_DIR));
}
public void setUp()
throws IOException {
TestUtils.removeFiles("Setup", envHome, FileManager.JE_SUFFIX);
}
void initEnv(boolean duplicatesAllowed)
throws DatabaseException {
EnvironmentConfig envConfig = TestUtils.initEnvConfig();
envConfig.setConfigParam(EnvironmentParams.ENV_RUN_EVICTOR.getName(),
"false");
envConfig.setConfigParam(EnvironmentParams.NODE_MAX.getName(),
Integer.toString(MAX_ENTRIES_PER_NODE));
envConfig.setAllowCreate(true);
envConfig.setTxnNoSync(Boolean.getBoolean(TestUtils.NO_SYNC));
env = new Environment(envHome, envConfig);
DatabaseConfig dbConfig = new DatabaseConfig();
dbConfig.setAllowCreate(true);
dbConfig.setSortedDuplicates(duplicatesAllowed);
db = env.openDatabase(null, "foo", dbConfig);
tree = DbInternal.dbGetDatabaseImpl(db).getTree();
minKey = null;
maxKey = null;
}
public void tearDown()
throws DatabaseException, IOException {
db.close();
if (env != null) {
env.close();
}
env = null;
db = null;
tree = null;
minKey = null;
maxKey = null;
TestUtils.removeFiles("TearDown", envHome, FileManager.JE_SUFFIX);
}
protected IN makeDupIN(IN old) {
IN ret = new IN(DbInternal.dbGetDatabaseImpl(db),
old.getIdentifierKey(),
MAX_ENTRIES_PER_NODE, 2);
ret.setNodeId(old.getNodeId());
ret.setIsRoot(old.isRoot());
for (int i = 0; i < old.getNEntries(); i++) {
ret.setEntry(i, old.getTarget(i), old.getKey(i),
old.getLsn(i), old.getState(i));
}
return ret;
}
/**
* Helper routine to insert a key and immediately read it back.
*/
protected void insertAndRetrieve(NullCursor cursor, byte[] key, LN ln)
throws DatabaseException {
if (minKey == null) {
minKey = key;
} else if (Key.compareKeys(key, minKey, null) < 0) {
minKey = key;
}
if (maxKey == null) {
maxKey = key;
} else if (Key.compareKeys(maxKey, key, null) < 0) {
maxKey = key;
}
TestUtils.checkLatchCount();
assertTrue(tree.insert(ln, key, false, cursor,
new LockResult(null, null),
ReplicationContext.NO_REPLICATE));
TestUtils.checkLatchCount();
assertTrue(retrieveLN(key) == ln);
}
/**
* Helper routine to read the LN referred to by key.
*/
protected LN retrieveLN(byte[] key)
throws DatabaseException {
TestUtils.checkLatchCount();
IN n = tree.search(key, Tree.SearchType.NORMAL, -1,
null, CacheMode.DEFAULT);
if (!(n instanceof BIN)) {
fail("search didn't return a BIN for key: " + key);
}
BIN bin = (BIN) n;
try {
int index = bin.findEntry(key, false, true);
if (index == -1) {
fail("Didn't read back key: " + key);
} else {
Node node = bin.getTarget(index);
if (node instanceof LN) {
return (LN) node;
} else {
fail("Didn't read back LN for: " + key);
}
}
/* We never get here, but the compiler doesn't know that. */
return null;
} finally {
bin.releaseLatch();
TestUtils.checkLatchCount();
}
}
/**
* Using getNextBin, count all the keys in the database. Ensure that
* they're returned in ascending order.
*/
protected int countAndValidateKeys(Tree tree)
throws DatabaseException {
TestUtils.checkLatchCount();
BIN nextBin = (BIN) tree.getFirstNode(CacheMode.DEFAULT);
byte[] prevKey = { 0x00 };
int cnt = 0;
while (nextBin != null) {
for (int i = 0; i < nextBin.getNEntries(); i++) {
byte[] curKey = nextBin.getKey(i);
if (Key.compareKeys(curKey, prevKey, null) <= 0) {
throw new InconsistentNodeException
("keys are out of order");
}
cnt++;
prevKey = curKey;
}
nextBin = tree.getNextBin(nextBin,
false /*traverseWithinDupTree*/,
CacheMode.DEFAULT);
}
TestUtils.checkLatchCount();
return cnt;
}
/**
* Using getPrevBin, count all the keys in the database. Ensure that
* they're returned in descending order.
*/
protected int countAndValidateKeysBackwards(Tree tree)
throws DatabaseException {
TestUtils.checkLatchCount();
BIN nextBin = (BIN) tree.getLastNode(CacheMode.DEFAULT);
byte[] prevKey = null;
int cnt = 0;
while (nextBin != null) {
for (int i = nextBin.getNEntries() - 1; i >= 0; i--) {
byte[] curKey = nextBin.getKey(i);
if (prevKey != null &&
Key.compareKeys(prevKey, curKey, null) <= 0) {
throw new InconsistentNodeException
("keys are out of order");
}
cnt++;
prevKey = curKey;
}
nextBin = tree.getPrevBin(nextBin,
false /*traverseWithinDupTree*/,
CacheMode.DEFAULT);
}
return cnt;
}
}
|