assertFalse(itExpected.hasNext());
}
public void testOverlayBufferTree() {
MultiOverlayBufferTree tree = new MultiOverlayBufferTree(new byte[0], DefaultByteRangeComparator
.getInstance());
// randomly insert 200 elements in a map
final int numElements = 200;
final SortedMap<String, byte[]> map1 = new TreeMap<String, byte[]>();
for (int i = 0; i < numElements; i++) {
String keyString = Integer.toHexString(i);
String valString = Integer.toHexString((int) (Math.random() * Integer.MAX_VALUE));
byte[] key = keyString.getBytes();
byte[] val = valString.getBytes();
map1.put(keyString, val);
tree.insert(key, val);
}
// look up each key
for (int i = 0; i < numElements; i++) {
String keyString = Integer.toHexString(i);
byte[] key = keyString.getBytes();
assertEquals(tree.lookup(key), map1.get(keyString));
}
// create a new overlay
int snap1 = tree.newOverlay();
final SortedMap<String, byte[]> map2 = new TreeMap<String, byte[]>(map1);
// remove every second element
for (int i = 0; i < numElements; i += 2) {
String keyString = Integer.toHexString(i);
byte[] key = keyString.getBytes();
tree.insert(key, null);
map2.remove(keyString);
}
// look up all elements
for (int i = 0; i < numElements; i++) {
String keyString = Integer.toHexString(i);
byte[] key = keyString.getBytes();
byte[] mapVal = map2.get(keyString);
byte[] val = tree.lookup(key);
if (mapVal != null)
assertEquals(mapVal, val);
else
assertTrue(val == null || val.length == 0);
}
// create a new overlay
int snap2 = tree.newOverlay();
final SortedMap<String, byte[]> map3 = new TreeMap<String, byte[]>(map2);
// overwrite every 5th element
for (int i = 0; i < numElements; i += 5) {
String keyString = Integer.toHexString(i);
byte[] key = keyString.getBytes();
byte[] val = Integer.toHexString((int) (Math.random() * Integer.MAX_VALUE)).getBytes();
tree.insert(key, val);
map3.put(keyString, val);
}
// look up all elements
for (int i = 0; i < numElements; i++) {
String keyString = Integer.toHexString(i);
byte[] key = keyString.getBytes();
byte[] mapVal = map3.get(keyString);
byte[] val = tree.lookup(key);
if (mapVal != null)
assertEquals(mapVal, val);
else
assertTrue(val == null || val.length == 0);
}
Iterator<Entry<byte[], byte[]>> it = tree.prefixLookup(null, false, true);
Iterator<byte[]> itExpected = map3.values().iterator();
while (it.hasNext())
assertEquals(itExpected.next(), it.next().getValue());
assertFalse(itExpected.hasNext());
it = tree.prefixLookup(null, snap1, false, true);
itExpected = map1.values().iterator();
while (it.hasNext())
assertEquals(itExpected.next(), it.next().getValue());
assertFalse(itExpected.hasNext());
it = tree.prefixLookup(null, snap2, false, true);
itExpected = map2.values().iterator();
while (it.hasNext())
assertEquals(itExpected.next(), it.next().getValue());
assertFalse(itExpected.hasNext());
it = tree.prefixLookup("3".getBytes(), false, true);
itExpected = map3.subMap("3", "4").values().iterator();
while (it.hasNext())
assertEquals(itExpected.next(), it.next().getValue());
assertFalse(itExpected.hasNext());
}