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 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
|
/*-
* See the file LICENSE for redistribution information.
*
* Copyright (c) 2002,2010 Oracle. All rights reserved.
*
* $Id: KeyRangeTest.java,v 1.45.2.2 2010/01/04 15:30:41 cwl Exp $
*/
package com.sleepycat.collections;
import java.io.File;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Comparator;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import com.sleepycat.bind.ByteArrayBinding;
import com.sleepycat.compat.DbCompat;
import com.sleepycat.je.Database;
import com.sleepycat.je.DatabaseConfig;
import com.sleepycat.je.DatabaseEntry;
import com.sleepycat.je.DatabaseException;
import com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;
import com.sleepycat.je.OperationStatus;
import com.sleepycat.util.keyrange.KeyRange;
import com.sleepycat.util.keyrange.KeyRangeException;
import com.sleepycat.util.test.SharedTestUtils;
/**
* @author Mark Hayes
*/
public class KeyRangeTest extends TestCase {
private static boolean VERBOSE = false;
private static final byte FF = (byte) 0xFF;
private static final byte[][] KEYS = {
/* 0 */ {1},
/* 1 */ {FF},
/* 2 */ {FF, 0},
/* 3 */ {FF, 0x7F},
/* 4 */ {FF, FF},
/* 5 */ {FF, FF, 0},
/* 6 */ {FF, FF, 0x7F},
/* 7 */ {FF, FF, FF},
};
private static byte[][] EXTREME_KEY_BYTES = {
/* 0 */ {0},
/* 1 */ {FF, FF, FF, FF},
};
private Environment env;
private Database store;
private DataView view;
private DataCursor cursor;
public static void main(String[] args)
throws Exception {
junit.framework.TestResult tr =
junit.textui.TestRunner.run(suite());
if (tr.errorCount() > 0 ||
tr.failureCount() > 0) {
System.exit(1);
} else {
System.exit(0);
}
}
public static Test suite() {
return new TestSuite(KeyRangeTest.class);
}
public KeyRangeTest(String name) {
super(name);
}
public void setUp()
throws Exception {
SharedTestUtils.printTestName(SharedTestUtils.qualifiedTestName(this));
}
private void openDb(Comparator<byte []> comparator)
throws Exception {
File dir = SharedTestUtils.getNewDir();
ByteArrayBinding dataBinding = new ByteArrayBinding();
EnvironmentConfig envConfig = new EnvironmentConfig();
envConfig.setAllowCreate(true);
DbCompat.setInitializeCache(envConfig, true);
env = new Environment(dir, envConfig);
DatabaseConfig dbConfig = new DatabaseConfig();
DbCompat.setTypeBtree(dbConfig);
dbConfig.setAllowCreate(true);
if (comparator != null) {
DbCompat.setBtreeComparator(dbConfig, comparator);
}
store = DbCompat.testOpenDatabase
(env, null, "test.db", null, dbConfig);
view = new DataView(store, dataBinding, dataBinding, null, true, null);
}
private void closeDb()
throws Exception {
store.close();
store = null;
env.close();
env = null;
}
public void tearDown()
throws Exception {
try {
if (store != null) {
store.close();
}
} catch (Exception e) {
System.out.println("Exception ignored during close: " + e);
}
try {
if (env != null) {
env.close();
}
} catch (Exception e) {
System.out.println("Exception ignored during close: " + e);
}
/* Ensure that GC can cleanup. */
env = null;
store = null;
view = null;
cursor = null;
}
public void testScan() throws Exception {
openDb(null);
doScan(false);
closeDb();
}
public void testScanComparator() throws Exception {
openDb(new ReverseComparator());
doScan(true);
closeDb();
}
private void doScan(boolean reversed) throws Exception {
byte[][] keys = new byte[KEYS.length][];
final int end = KEYS.length - 1;
cursor = new DataCursor(view, true);
for (int i = 0; i <= end; i++) {
keys[i] = KEYS[i];
cursor.put(keys[i], KEYS[i], null, false);
}
cursor.close();
byte[][] extremeKeys = new byte[EXTREME_KEY_BYTES.length][];
for (int i = 0; i < extremeKeys.length; i++) {
extremeKeys[i] = EXTREME_KEY_BYTES[i];
}
// with empty range
cursor = new DataCursor(view, false);
expectRange(KEYS, 0, end, reversed);
cursor.close();
// begin key only, inclusive
for (int i = 0; i <= end; i++) {
cursor = newCursor(view, keys[i], true, null, false, reversed);
expectRange(KEYS, i, end, reversed);
cursor.close();
}
// begin key only, exclusive
for (int i = 0; i <= end; i++) {
cursor = newCursor(view, keys[i], false, null, false, reversed);
expectRange(KEYS, i + 1, end, reversed);
cursor.close();
}
// end key only, inclusive
for (int i = 0; i <= end; i++) {
cursor = newCursor(view, null, false, keys[i], true, reversed);
expectRange(KEYS, 0, i, reversed);
cursor.close();
}
// end key only, exclusive
for (int i = 0; i <= end; i++) {
cursor = newCursor(view, null, false, keys[i], false, reversed);
expectRange(KEYS, 0, i - 1, reversed);
cursor.close();
}
// begin and end keys, inclusive and exclusive
for (int i = 0; i <= end; i++) {
for (int j = i; j <= end; j++) {
// begin inclusive, end inclusive
cursor = newCursor(view, keys[i], true, keys[j],
true, reversed);
expectRange(KEYS, i, j, reversed);
cursor.close();
// begin inclusive, end exclusive
cursor = newCursor(view, keys[i], true, keys[j],
false, reversed);
expectRange(KEYS, i, j - 1, reversed);
cursor.close();
// begin exclusive, end inclusive
cursor = newCursor(view, keys[i], false, keys[j],
true, reversed);
expectRange(KEYS, i + 1, j, reversed);
cursor.close();
// begin exclusive, end exclusive
cursor = newCursor(view, keys[i], false, keys[j],
false, reversed);
expectRange(KEYS, i + 1, j - 1, reversed);
cursor.close();
}
}
// single key range
for (int i = 0; i <= end; i++) {
cursor = new DataCursor(view, false, keys[i]);
expectRange(KEYS, i, i, reversed);
cursor.close();
}
// start with lower extreme (before any existing key)
cursor = newCursor(view, extremeKeys[0], true, null, false, reversed);
expectRange(KEYS, 0, end, reversed);
cursor.close();
// start with higher extreme (after any existing key)
cursor = newCursor(view, null, false, extremeKeys[1], true, reversed);
expectRange(KEYS, 0, end, reversed);
cursor.close();
}
private DataCursor newCursor(DataView view,
Object beginKey, boolean beginInclusive,
Object endKey, boolean endInclusive,
boolean reversed)
throws Exception {
if (reversed) {
return new DataCursor(view, false,
endKey, endInclusive,
beginKey, beginInclusive);
} else {
return new DataCursor(view, false,
beginKey, beginInclusive,
endKey, endInclusive);
}
}
private void expectRange(byte[][] bytes, int first, int last,
boolean reversed)
throws DatabaseException {
int i;
boolean init;
for (init = true, i = first;; i++, init = false) {
if (checkRange(bytes, first, last, i <= last,
reversed, !reversed, init, i)) {
break;
}
}
for (init = true, i = last;; i--, init = false) {
if (checkRange(bytes, first, last, i >= first,
reversed, reversed, init, i)) {
break;
}
}
}
private boolean checkRange(byte[][] bytes, int first, int last,
boolean inRange, boolean reversed,
boolean forward, boolean init,
int i)
throws DatabaseException {
OperationStatus s;
if (forward) {
if (init) {
s = cursor.getFirst(false);
} else {
s = cursor.getNext(false);
}
} else {
if (init) {
s = cursor.getLast(false);
} else {
s = cursor.getPrev(false);
}
}
String msg = " " + (forward ? "next" : "prev") + " i=" + i +
" first=" + first + " last=" + last +
(reversed ? " reversed" : " not reversed");
// check that moving past ends doesn't move the cursor
if (s == OperationStatus.SUCCESS && i == first) {
OperationStatus s2 = reversed ? cursor.getNext(false)
: cursor.getPrev(false);
assertEquals(msg, OperationStatus.NOTFOUND, s2);
}
if (s == OperationStatus.SUCCESS && i == last) {
OperationStatus s2 = reversed ? cursor.getPrev(false)
: cursor.getNext(false);
assertEquals(msg, OperationStatus.NOTFOUND, s2);
}
byte[] val = (s == OperationStatus.SUCCESS)
? ((byte[]) cursor.getCurrentValue())
: null;
if (inRange) {
assertNotNull("RangeNotFound" + msg, val);
if (!Arrays.equals(val, bytes[i])){
printBytes(val);
printBytes(bytes[i]);
fail("RangeKeyNotEqual" + msg);
}
if (VERBOSE) {
System.out.println("GotRange" + msg);
}
return false;
} else {
assertEquals("RangeExceeded" + msg, OperationStatus.NOTFOUND, s);
return true;
}
}
private void printBytes(byte[] bytes) {
for (int i = 0; i < bytes.length; i += 1) {
System.out.print(Integer.toHexString(bytes[i] & 0xFF));
System.out.print(' ');
}
System.out.println();
}
public void testSubRanges() {
DatabaseEntry begin = new DatabaseEntry();
DatabaseEntry begin2 = new DatabaseEntry();
DatabaseEntry end = new DatabaseEntry();
DatabaseEntry end2 = new DatabaseEntry();
KeyRange range = new KeyRange(null);
KeyRange range2;
/* Base range [1, 2] */
begin.setData(new byte[] { 1 });
end.setData(new byte[] { 2 });
range = range.subRange(begin, true, end, true);
/* Subrange (0, 1] is invalid **. */
begin2.setData(new byte[] { 0 });
end2.setData(new byte[] { 1 });
try {
range2 = range.subRange(begin2, false, end2, true);
fail();
} catch (KeyRangeException expected) {}
/* Subrange [1, 3) is invalid. */
begin2.setData(new byte[] { 1 });
end2.setData(new byte[] { 3 });
try {
range2 = range.subRange(begin2, true, end2, false);
fail();
} catch (KeyRangeException expected) {}
/* Subrange [2, 2] is valid. */
begin2.setData(new byte[] { 2 });
end2.setData(new byte[] { 2 });
range2 = range.subRange(begin2, true, end2, true);
/* Subrange [0, 1] is invalid. */
begin2.setData(new byte[] { 0 });
end2.setData(new byte[] { 1 });
try {
range2 = range.subRange(begin2, true, end2, true);
fail();
} catch (KeyRangeException expected) {}
/* Subrange (0, 3] is invalid. */
begin2.setData(new byte[] { 0 });
end2.setData(new byte[] { 3 });
try {
range2 = range.subRange(begin2, false, end2, true);
fail();
} catch (KeyRangeException expected) {}
/* Subrange [3, 3) is invalid. */
begin2.setData(new byte[] { 3 });
end2.setData(new byte[] { 3 });
try {
range2 = range.subRange(begin2, true, end2, false);
fail();
} catch (KeyRangeException expected) {}
}
@SuppressWarnings("serial")
public static class ReverseComparator implements Comparator<byte[]>,
Serializable {
public int compare(byte[] d1, byte[] d2) {
int cmp = KeyRange.compareBytes(d1, 0, d1.length,
d2, 0, d2.length);
if (cmp < 0) {
return 1;
} else if (cmp > 0) {
return -1;
} else {
return 0;
}
}
}
}
|