git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@1415870 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Timothy A. Bish 2012-11-30 22:02:51 +00:00
parent 321d9ef310
commit 81c167b205
3 changed files with 83 additions and 54 deletions

View File

@ -37,6 +37,7 @@ import org.apache.activemq.store.kahadb.disk.page.Transaction;
import org.apache.activemq.store.kahadb.disk.util.LongMarshaller;
import org.apache.activemq.store.kahadb.disk.util.StringMarshaller;
import org.apache.activemq.store.kahadb.disk.util.VariableMarshaller;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -51,24 +52,24 @@ public class BTreeIndexTest extends IndexTestSupport {
nf.setMinimumIntegerDigits(6);
nf.setGroupingUsed(false);
}
@Override
protected Index<String, Long> createIndex() throws Exception {
long id = tx.allocate().getPageId();
tx.commit();
BTreeIndex<String, Long> index = new BTreeIndex<String,Long>(pf, id);
index.setKeyMarshaller(StringMarshaller.INSTANCE);
index.setValueMarshaller(LongMarshaller.INSTANCE);
return index;
}
/**
* Yeah, the current implementation does NOT try to balance the tree. Here is
* a test case showing that it gets out of balance.
*
* Yeah, the current implementation does NOT try to balance the tree. Here is
* a test case showing that it gets out of balance.
*
* @throws Exception
*/
public void disabled_testTreeBalancing() throws Exception {
@ -77,9 +78,9 @@ public class BTreeIndexTest extends IndexTestSupport {
BTreeIndex index = ((BTreeIndex)this.index);
this.index.load(tx);
tx.commit();
doInsert(50);
int minLeafDepth = index.getMinLeafDepth(tx);
int maxLeafDepth = index.getMaxLeafDepth(tx);
assertTrue("Tree is balanced", maxLeafDepth-minLeafDepth <= 1);
@ -97,7 +98,8 @@ public class BTreeIndexTest extends IndexTestSupport {
this.index.unload(tx);
}
@Test(timeout=60000)
public void testPruning() throws Exception {
createPageFileAndIndex(100);
@ -105,14 +107,14 @@ public class BTreeIndexTest extends IndexTestSupport {
this.index.load(tx);
tx.commit();
int minLeafDepth = index.getMinLeafDepth(tx);
int maxLeafDepth = index.getMaxLeafDepth(tx);
assertEquals(1, minLeafDepth);
assertEquals(1, maxLeafDepth);
doInsert(1000);
minLeafDepth = index.getMinLeafDepth(tx);
maxLeafDepth = index.getMaxLeafDepth(tx);
assertTrue("Depth of tree grew", minLeafDepth > 1);
@ -130,15 +132,16 @@ public class BTreeIndexTest extends IndexTestSupport {
tx.commit();
}
@Test(timeout=60000)
public void testIteration() throws Exception {
createPageFileAndIndex(500);
BTreeIndex<String,Long> index = ((BTreeIndex<String,Long>)this.index);
this.index.load(tx);
tx.commit();
// Insert in reverse order..
doInsertReverse(1000);
this.index.unload(tx);
tx.commit();
this.index.load(tx);
@ -149,7 +152,7 @@ public class BTreeIndexTest extends IndexTestSupport {
// BTree should iterate it in sorted order.
int counter=0;
for (Iterator<Map.Entry<String,Long>> i = index.iterator(tx); i.hasNext();) {
Map.Entry<String,Long> entry = (Map.Entry<String,Long>)i.next();
Map.Entry<String,Long> entry = i.next();
assertEquals(key(counter),entry.getKey());
assertEquals(counter,(long)entry.getValue());
counter++;
@ -158,38 +161,40 @@ public class BTreeIndexTest extends IndexTestSupport {
this.index.unload(tx);
tx.commit();
}
@Test(timeout=60000)
public void testVisitor() throws Exception {
createPageFileAndIndex(100);
BTreeIndex<String,Long> index = ((BTreeIndex<String,Long>)this.index);
this.index.load(tx);
tx.commit();
// Insert in reverse order..
doInsert(1000);
this.index.unload(tx);
tx.commit();
this.index.load(tx);
tx.commit();
// BTree should iterate it in sorted order.
index.visit(tx, new BTreeVisitor<String, Long>(){
public boolean isInterestedInKeysBetween(String first, String second) {
@Override
public boolean isInterestedInKeysBetween(String first, String second) {
return true;
}
public void visit(List<String> keys, List<Long> values) {
@Override
public void visit(List<String> keys, List<Long> values) {
}
});
this.index.unload(tx);
tx.commit();
}
@Test(timeout=60000)
public void testRandomRemove() throws Exception {
createPageFileAndIndex(100);
@ -231,6 +236,7 @@ public class BTreeIndexTest extends IndexTestSupport {
}
}
@Test(timeout=300000)
public void testRandomAddRemove() throws Exception {
createPageFileAndIndex(1024);
@ -273,6 +279,7 @@ public class BTreeIndexTest extends IndexTestSupport {
}
}
@Test(timeout=60000)
public void testRemovePattern() throws Exception {
createPageFileAndIndex(100);
BTreeIndex<String,Long> index = ((BTreeIndex<String,Long>)this.index);
@ -319,9 +326,10 @@ public class BTreeIndexTest extends IndexTestSupport {
assertNull(index.getLast(tx));
}
@Test(timeout=60000)
public void testLargeValue() throws Exception {
//System.setProperty("maxKahaDBTxSize", "" + (1024*1024*1024));
pf = new PageFile(directory, getClass().getName());
pf = new PageFile(getDirectory(), getClass().getName());
pf.setPageSize(4*1024);
//pf.setEnablePageCaching(false);
pf.load();
@ -366,8 +374,9 @@ public class BTreeIndexTest extends IndexTestSupport {
tx.commit();
}
@Test(timeout=60000)
public void testLargeValueOverflow() throws Exception {
pf = new PageFile(directory, getClass().getName());
pf = new PageFile(getDirectory(), getClass().getName());
pf.setPageSize(4*1024);
pf.setWriteBatchSize(1);
pf.load();
@ -422,8 +431,9 @@ public class BTreeIndexTest extends IndexTestSupport {
tx.commit();
}
@Test(timeout=60000)
public void testIndexRepeatFillClearIncrementingPageReuse() throws Exception {
pf = new PageFile(directory, getClass().getName());
pf = new PageFile(getDirectory(), getClass().getName());
pf.setPageSize(4*1024);
pf.load();
@ -467,11 +477,12 @@ public class BTreeIndexTest extends IndexTestSupport {
}
}
@Test(timeout=60000)
public void testListIndexConsistancyOverTime() throws Exception {
final int NUM_ITERATIONS = 50;
pf = new PageFile(directory, getClass().getName());
pf = new PageFile(getDirectory(), getClass().getName());
pf.setPageSize(4*1024);
//pf.setEnablePageCaching(false);
pf.setWriteBatchSize(1);
@ -543,7 +554,7 @@ public class BTreeIndexTest extends IndexTestSupport {
}
/**
* Overriding so that this generates keys that are the worst case for the BTree. Keys that
* always insert to the end of the BTree.
* always insert to the end of the BTree.
*/
@Override
protected String key(int i) {
@ -553,7 +564,8 @@ public class BTreeIndexTest extends IndexTestSupport {
static class HashSetStringMarshaller extends VariableMarshaller<HashSet<String>> {
final static HashSetStringMarshaller INSTANCE = new HashSetStringMarshaller();
public void writePayload(HashSet<String> object, DataOutput dataOut) throws IOException {
@Override
public void writePayload(HashSet<String> object, DataOutput dataOut) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oout = new ObjectOutputStream(baos);
oout.writeObject(object);
@ -564,7 +576,8 @@ public class BTreeIndexTest extends IndexTestSupport {
dataOut.write(data);
}
public HashSet<String> readPayload(DataInput dataIn) throws IOException {
@Override
public HashSet<String> readPayload(DataInput dataIn) throws IOException {
int dataLen = dataIn.readInt();
byte[] data = new byte[dataLen];
dataIn.readFully(data);

View File

@ -24,6 +24,8 @@ import junit.framework.TestCase;
import org.apache.activemq.store.kahadb.disk.page.PageFile;
import org.apache.activemq.store.kahadb.disk.page.Transaction;
import org.apache.activemq.util.IOHelper;
import org.junit.After;
import org.junit.Test;
/**
* Test a HashIndex
@ -37,28 +39,26 @@ public abstract class IndexTestSupport extends TestCase {
protected PageFile pf;
protected Transaction tx;
/**
* @throws java.lang.Exception
* @see junit.framework.TestCase#setUp()
*/
protected void setUp() throws Exception {
super.setUp();
directory = new File(IOHelper.getDefaultDataDirectory() + System.currentTimeMillis());
IOHelper.delete(directory);
}
protected void tearDown() throws Exception {
@Override
@After
public void tearDown() throws Exception {
if( pf!=null ) {
pf.unload();
pf.delete();
}
}
public File getDirectory() {
if (directory != null) {
IOHelper.delete(directory);
}
directory = new File(IOHelper.getDefaultDataDirectory() + System.currentTimeMillis());
IOHelper.delete(directory);
return directory;
}
protected void createPageFileAndIndex(int pageSize) throws Exception {
pf = new PageFile(directory, getClass().getName());
pf = new PageFile(getDirectory(), getClass().getName());
pf.setPageSize(pageSize);
pf.load();
tx = pf.tx();
@ -67,6 +67,7 @@ public abstract class IndexTestSupport extends TestCase {
abstract protected Index<String, Long> createIndex() throws Exception;
@Test(timeout=60000)
public void testIndex() throws Exception {
createPageFileAndIndex(500);
this.index.load(tx);

View File

@ -36,6 +36,7 @@ import org.apache.activemq.store.kahadb.disk.util.Sequence;
import org.apache.activemq.store.kahadb.disk.util.SequenceSet;
import org.apache.activemq.store.kahadb.disk.util.StringMarshaller;
import org.apache.activemq.store.kahadb.disk.util.VariableMarshaller;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -64,6 +65,7 @@ public class ListIndexTest extends IndexTestSupport {
return index;
}
@Test(timeout=60000)
public void testSize() throws Exception {
createPageFileAndIndex(100);
@ -98,6 +100,7 @@ public class ListIndexTest extends IndexTestSupport {
tx.commit();
}
@Test(timeout=60000)
public void testPut() throws Exception {
createPageFileAndIndex(100);
@ -130,6 +133,7 @@ public class ListIndexTest extends IndexTestSupport {
tx.commit();
}
@Test(timeout=60000)
public void testAddFirst() throws Exception {
createPageFileAndIndex(100);
@ -155,7 +159,7 @@ public class ListIndexTest extends IndexTestSupport {
counter--;
int count = 0;
while (iterator.hasNext() && count < counter) {
Map.Entry<String, Long> entry = (Map.Entry<String, Long>) iterator.next();
Map.Entry<String, Long> entry = iterator.next();
assertEquals(key(count), entry.getKey());
assertEquals(count, (long) entry.getValue());
count++;
@ -163,6 +167,7 @@ public class ListIndexTest extends IndexTestSupport {
tx.commit();
}
@Test(timeout=60000)
public void testPruning() throws Exception {
createPageFileAndIndex(100);
@ -212,6 +217,7 @@ public class ListIndexTest extends IndexTestSupport {
tx.commit();
}
@Test(timeout=60000)
public void testIterationAddFirst() throws Exception {
createPageFileAndIndex(100);
ListIndex<String, Long> index = ((ListIndex<String, Long>) this.index);
@ -229,7 +235,7 @@ public class ListIndexTest extends IndexTestSupport {
int counter = 0;
for (Iterator<Map.Entry<String, Long>> i = index.iterator(tx); i.hasNext(); ) {
Map.Entry<String, Long> entry = (Map.Entry<String, Long>) i.next();
Map.Entry<String, Long> entry = i.next();
assertEquals(key(counter), entry.getKey());
assertEquals(counter, (long) entry.getValue());
counter++;
@ -245,7 +251,7 @@ public class ListIndexTest extends IndexTestSupport {
tx.commit();
}
@Test(timeout=60000)
public void testIteration() throws Exception {
createPageFileAndIndex(100);
ListIndex<String, Long> index = ((ListIndex<String, Long>) this.index);
@ -263,7 +269,7 @@ public class ListIndexTest extends IndexTestSupport {
int counter = 0;
for (Iterator<Map.Entry<String, Long>> i = index.iterator(tx); i.hasNext(); ) {
Map.Entry<String, Long> entry = (Map.Entry<String, Long>) i.next();
Map.Entry<String, Long> entry = i.next();
assertEquals(key(counter), entry.getKey());
assertEquals(counter, (long) entry.getValue());
counter++;
@ -274,6 +280,7 @@ public class ListIndexTest extends IndexTestSupport {
tx.commit();
}
@Test(timeout=60000)
public void testRandomRemove() throws Exception {
createPageFileAndIndex(4*1024);
@ -298,6 +305,7 @@ public class ListIndexTest extends IndexTestSupport {
}
}
@Test(timeout=60000)
public void testRemovePattern() throws Exception {
createPageFileAndIndex(100);
ListIndex<String, Long> index = ((ListIndex<String, Long>) this.index);
@ -311,6 +319,7 @@ public class ListIndexTest extends IndexTestSupport {
index.remove(tx, key(1566));
}
@Test(timeout=60000)
public void testLargeAppendRemoveTimed() throws Exception {
createPageFileAndIndex(1024*4);
ListIndex<String, Long> listIndex = ((ListIndex<String, Long>) this.index);
@ -345,8 +354,9 @@ public class ListIndexTest extends IndexTestSupport {
return min + (int)(Math.random() * ((max - min) + 1));
}
@Test(timeout=60000)
public void testLargeValueOverflow() throws Exception {
pf = new PageFile(directory, getClass().getName());
pf = new PageFile(getDirectory(), getClass().getName());
pf.setPageSize(4*1024);
pf.setEnablePageCaching(false);
pf.setWriteBatchSize(1);
@ -429,11 +439,12 @@ public class ListIndexTest extends IndexTestSupport {
return "key:" + nf.format(i);
}
@Test(timeout=60000)
public void testListIndexConsistencyOverTime() throws Exception {
final int NUM_ITERATIONS = 100;
pf = new PageFile(directory, getClass().getName());
pf = new PageFile(getDirectory(), getClass().getName());
pf.setPageSize(4*1024);
pf.setEnablePageCaching(false);
pf.setWriteBatchSize(1);
@ -496,11 +507,12 @@ public class ListIndexTest extends IndexTestSupport {
}
}
@Test(timeout=60000)
public void testListLargeDataAddWithReverseRemove() throws Exception {
final int NUM_ITERATIONS = 100;
pf = new PageFile(directory, getClass().getName());
pf = new PageFile(getDirectory(), getClass().getName());
pf.setPageSize(4*1024);
pf.setEnablePageCaching(false);
pf.setWriteBatchSize(1);
@ -568,7 +580,7 @@ public class ListIndexTest extends IndexTestSupport {
final int NUM_ITERATIONS = 200;
final int RANGE = 200000;
pf = new PageFile(directory, getClass().getName());
pf = new PageFile(getDirectory(), getClass().getName());
pf.setPageSize(4*1024);
pf.load();
tx = pf.tx();
@ -599,11 +611,12 @@ public class ListIndexTest extends IndexTestSupport {
LOG.info("duration: " + (System.currentTimeMillis() - start));
}
@Test(timeout=60000)
public void testListLargeDataAddAndNonSequentialRemove() throws Exception {
final int NUM_ITERATIONS = 100;
pf = new PageFile(directory, getClass().getName());
pf = new PageFile(getDirectory(), getClass().getName());
pf.setPageSize(4*1024);
pf.setEnablePageCaching(false);
pf.setWriteBatchSize(1);
@ -681,7 +694,8 @@ public class ListIndexTest extends IndexTestSupport {
static class HashSetStringMarshaller extends VariableMarshaller<HashSet<String>> {
final static HashSetStringMarshaller INSTANCE = new HashSetStringMarshaller();
public void writePayload(HashSet<String> object, DataOutput dataOut) throws IOException {
@Override
public void writePayload(HashSet<String> object, DataOutput dataOut) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oout = new ObjectOutputStream(baos);
oout.writeObject(object);
@ -692,7 +706,8 @@ public class ListIndexTest extends IndexTestSupport {
dataOut.write(data);
}
@SuppressWarnings("unchecked")
@Override
@SuppressWarnings("unchecked")
public HashSet<String> readPayload(DataInput dataIn) throws IOException {
int dataLen = dataIn.readInt();
byte[] data = new byte[dataLen];