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 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
|
/*-
* See the file LICENSE for redistribution information.
*
* Copyright (c) 2002,2010 Oracle. All rights reserved.
*
* $Id: SplitTest.java,v 1.29.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.Database;
import com.sleepycat.je.DatabaseConfig;
import com.sleepycat.je.DatabaseEntry;
import com.sleepycat.je.DatabaseException;
import com.sleepycat.je.DbInternal;
import com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;
import com.sleepycat.je.LockMode;
import com.sleepycat.je.OperationStatus;
import com.sleepycat.je.Transaction;
import com.sleepycat.je.config.EnvironmentParams;
import com.sleepycat.je.tree.Key.DumpType;
import com.sleepycat.je.util.TestUtils;
public class SplitTest extends TestCase {
private static final boolean DEBUG = false;
private File envHome;
private Environment env;
private Database db;
public SplitTest() {
envHome = new File(System.getProperty(TestUtils.DEST_DIR));
}
public void setUp()
throws Exception {
TestUtils.removeLogFiles("Setup", envHome, false);
initEnv();
}
public void tearDown()
throws Exception {
try {
db.close();
env.close();
} catch (DatabaseException E) {
}
TestUtils.removeLogFiles("TearDown", envHome, true);
}
private void initEnv()
throws DatabaseException {
EnvironmentConfig envConfig = TestUtils.initEnvConfig();
envConfig.setConfigParam(EnvironmentParams.NODE_MAX.getName(), "4");
envConfig.setTransactional(true);
envConfig.setAllowCreate(true);
env = new Environment(envHome, envConfig);
String databaseName = "testDb";
Transaction txn = env.beginTransaction(null, null);
DatabaseConfig dbConfig = new DatabaseConfig();
dbConfig.setTransactional(true);
dbConfig.setAllowCreate(true);
db = env.openDatabase(txn, databaseName, dbConfig);
txn.commit();
}
/**
* Test splits on a case where the 0th entry gets promoted.
*/
public void test0Split()
throws Exception {
Key.DUMP_TYPE = DumpType.BINARY;
try {
/* Build up a tree. */
for (int i = 160; i > 0; i-= 10) {
assertEquals(OperationStatus.SUCCESS,
db.put(null, new DatabaseEntry
(new byte[] { (byte) i }),
new DatabaseEntry(new byte[] {1})));
}
if (DEBUG) {
System.out.println("<dump>");
DbInternal.dbGetDatabaseImpl(db).getTree().dump();
}
assertEquals(OperationStatus.SUCCESS,
db.put(null, new DatabaseEntry(new byte[]{(byte)151}),
new DatabaseEntry(new byte[] {1})));
assertEquals(OperationStatus.SUCCESS,
db.put(null, new DatabaseEntry(new byte[]{(byte)152}),
new DatabaseEntry(new byte[] {1})));
assertEquals(OperationStatus.SUCCESS,
db.put(null, new DatabaseEntry(new byte[]{(byte)153}),
new DatabaseEntry(new byte[] {1})));
if (DEBUG) {
DbInternal.dbGetDatabaseImpl(db).getTree().dump();
System.out.println("</dump>");
}
/*
* These inserts make a tree where the right most mid-level IN
* has an idkey greater than its parent entry.
*
* +---------------+
* | id = 90 |
* | 50 | 90 | 130 |
* +---------------+
* | | |
* |
* +-----------------+
* | id = 160 |
* | 130 | 150 | 152 |
* +-----------------+
* | | |
* | | +-----------+
* | | |
* +-----------+ +-----------+ +-----------------+
* | BIN | | BIN | | BIN |
* | id = 130 | | id = 150 | | id=160 |
* | 130 | 140 | | 150 | 151 | | 152 | 153 | 160 |
* +-----------+ +-----------+ +-----------------+
*
* Now delete records 130 and 140 to empty out the subtree with BIN
* with id=130.
*/
assertEquals(OperationStatus.SUCCESS,
db.delete(null,
new DatabaseEntry(new byte[]{(byte) 130})));
assertEquals(OperationStatus.SUCCESS,
db.delete(null,
new DatabaseEntry(new byte[]{(byte) 140})));
env.compress();
/*
* These deletes make the mid level IN's 0th entry > its parent
* reference.
*
* +---------------+
* | id = 90 |
* | 50 | 90 | 130 |
* +---------------+
* | | |
* |
* +-----------+
* | id = 160 |
* | 150 | 152 |
* +-----------+
* | |
* | |
* | |
* +-----------+ +-----------------+
* | BIN | | BIN |
* | id = 150 | | id=160 |
* | 150 | 151 | | 152 | 153 | 160 |
* +-----------+ +-----------------+
*
* Now insert 140 into BIN (id = 160) so that its first entry is
* less than the mid level IN.
*/
assertEquals(OperationStatus.SUCCESS,
db.put(null, new DatabaseEntry(new byte[]{(byte)140}),
new DatabaseEntry(new byte[] {1})));
/*
* Now note that the mid level tree's 0th entry is greater than its
* reference in the root.
*
* +---------------+
* | id = 90 |
* | 50 | 90 | 130 |
* +---------------+
* | | |
* |
* +-----------+
* | id = 160 |
* | 150 | 152 |
* +-----------+
* | |
* | |
* | |
* +----------------+ +-----------------+
* | BIN | | BIN |
* | id = 150 | | id=160 |
* | 140 |150 | 151 | | 152 | 153 | 160 |
* +----------------+ +-----------------+
*
* Now split the mid level node, putting the new child on the left.
*/
for (int i = 154; i < 159; i++) {
assertEquals(OperationStatus.SUCCESS,
db.put(null,
new DatabaseEntry(new byte[]{(byte)i}),
new DatabaseEntry(new byte[] {1})));
}
/*
* This used to result in the following broken tree, which would
* cause us to not be able to retrieve record 140. With the new
* split code, entry "150" in the root should stay 130.
*
* +---------------------+
* | id = 90 |
* | 50 | 90 | 150 | 154 | NOTE: we'v lost record 140
* +---------------------+
* | | | \
* | \
* +-----------+ +----------+
* | id = 150 | |id=160 |
* | 150 | 152 | |154 | 156 |
* +-----------+ +----------+
* | |
* | |
* | |
* +------------+ +-------+
* | BIN | | BIN |
* | id = 150 | | id=152|
* | 140|150|151| |152|153|
* +------------+ +-------+
*/
DatabaseEntry data = new DatabaseEntry();
assertEquals(OperationStatus.SUCCESS,
db.get(null, new DatabaseEntry(new byte[]
{ (byte) 140 }),
data, LockMode.DEFAULT));
} catch (Throwable t) {
t.printStackTrace();
throw new Exception(t);
}
}
}
|