use of org.apache.cassandra.utils.MerkleTree.TreeRange in project eiger by wlloyd.
the class MerkleTreeTest method testSerialization.
@Test
public void testSerialization() throws Exception {
Range full = new Range(tok(-1), tok(-1));
ByteArrayOutputStream bout = new ByteArrayOutputStream();
ObjectOutputStream oout = new ObjectOutputStream(bout);
// populate and validate the tree
mt.maxsize(256);
mt.init();
for (TreeRange range : mt.invalids()) range.addAll(new HIterator(range.right));
byte[] initialhash = mt.hash(full);
oout.writeObject(mt);
oout.close();
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
ObjectInputStream oin = new ObjectInputStream(bin);
MerkleTree restored = (MerkleTree) oin.readObject();
// restore partitioner after serialization
restored.partitioner(partitioner);
restored.fullRange = fullRange();
assertHashEquals(initialhash, restored.hash(full));
}
use of org.apache.cassandra.utils.MerkleTree.TreeRange in project eiger by wlloyd.
the class MerkleTreeTest method testValidateTree.
/**
* Generate two trees with different splits, but containing the same keys, and
* check that they compare equally.
*
* The set of keys used in this test is: #{2,4,6,8,12,14,0}
*/
@Test
public void testValidateTree() {
// this test needs slightly more resolution
TOKEN_SCALE = new BigInteger("16");
Range full = new Range(tok(-1), tok(-1));
Iterator<TreeRange> ranges;
MerkleTree mt2 = new MerkleTree(partitioner, fullRange(), RECOMMENDED_DEPTH, Integer.MAX_VALUE);
mt.split(tok(8));
mt.split(tok(4));
mt.split(tok(12));
mt.split(tok(6));
mt.split(tok(10));
ranges = mt.invalids();
// (-1,4]: depth 2
ranges.next().addAll(new HIterator(2, 4));
// (4,6]
ranges.next().addAll(new HIterator(6));
// (6,8]
ranges.next().addAll(new HIterator(8));
// (8,10]
ranges.next().addAll(new HIterator(/*empty*/
new int[0]));
// (10,12]
ranges.next().addAll(new HIterator(12));
// (12,-1]: depth 2
ranges.next().addAll(new HIterator(14, -1));
mt2.split(tok(8));
mt2.split(tok(4));
mt2.split(tok(12));
mt2.split(tok(2));
mt2.split(tok(10));
mt2.split(tok(9));
mt2.split(tok(11));
ranges = mt2.invalids();
// (-1,2]
ranges.next().addAll(new HIterator(2));
// (2,4]
ranges.next().addAll(new HIterator(4));
// (4,8]: depth 2
ranges.next().addAll(new HIterator(6, 8));
// (8,9]
ranges.next().addAll(new HIterator(/*empty*/
new int[0]));
// (9,10]
ranges.next().addAll(new HIterator(/*empty*/
new int[0]));
// (10,11]: depth 4
ranges.next().addAll(new HIterator(/*empty*/
new int[0]));
// (11,12]: depth 4
ranges.next().addAll(new HIterator(12));
// (12,-1]: depth 2
ranges.next().addAll(new HIterator(14, -1));
byte[] mthash = mt.hash(full);
byte[] mt2hash = mt2.hash(full);
assertHashEquals("Tree hashes did not match: " + mt + " && " + mt2, mthash, mt2hash);
}
use of org.apache.cassandra.utils.MerkleTree.TreeRange in project eiger by wlloyd.
the class MerkleTreeTest method testInvalids.
@Test
public void testInvalids() {
Iterator<TreeRange> ranges;
// (zero, zero]
ranges = mt.invalids();
assertEquals(new Range(tok(-1), tok(-1)), ranges.next());
assertFalse(ranges.hasNext());
// all invalid
mt.split(tok(4));
mt.split(tok(2));
mt.split(tok(6));
mt.split(tok(3));
mt.split(tok(5));
ranges = mt.invalids();
assertEquals(new Range(tok(6), tok(-1)), ranges.next());
assertEquals(new Range(tok(-1), tok(2)), ranges.next());
assertEquals(new Range(tok(2), tok(3)), ranges.next());
assertEquals(new Range(tok(3), tok(4)), ranges.next());
assertEquals(new Range(tok(4), tok(5)), ranges.next());
assertEquals(new Range(tok(5), tok(6)), ranges.next());
assertEquals(new Range(tok(6), tok(-1)), ranges.next());
assertFalse(ranges.hasNext());
}
Aggregations