use of com.jd.blockchain.ledger.merkletree.BytesConverter in project jdchain-core by blockchain-jd-com.
the class MerkleSortTreeTest method testMultiDataCountIterator.
/**
* 测试包含数据策略中计数大于 1 的数据迭代;
*/
@Test
public void testMultiDataCountIterator() {
TreeOptions options = createTreeOptions();
MemoryKVStorage storage = new MemoryKVStorage();
DataPolicy<byte[]> bytesDataPolicy = new DefaultDataPolicy<byte[]>() {
@Override
public byte[] updateData(long id, byte[] origData, byte[] newData) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
if (origData == null) {
BytesUtils.writeInt(1, out);
} else {
int count = BytesUtils.toInt(origData) + 1;
BytesUtils.writeInt(count, out);
out.write(origData, 4, origData.length - 4);
}
BytesEncoding.writeInNormal(newData, out);
return out.toByteArray();
}
@Override
public long count(long id, byte[] data) {
return BytesUtils.toInt(data);
}
@Override
public SkippingIterator<MerkleValue<byte[]>> iterator(long id, byte[] bytesData, long count, BytesConverter<byte[]> converter) {
byte[][] values = new byte[(int) count][];
ByteArrayInputStream in = new ByteArrayInputStream(bytesData, 4, bytesData.length - 4);
for (int i = 0; i < values.length; i++) {
values[i] = BytesEncoding.readInNormal(in);
}
return new BytesEntriesIterator(id, values);
}
};
MerkleSortTree<byte[]> mst = MerkleSortTree.createBytesTree(options, DEFAULT_MKL_KEY_PREFIX, storage, bytesDataPolicy);
int count = 16;
byte[][] datas = generateRandomData(count);
long[] ids = new long[count];
int startIndex = 10;
for (int i = 0; i < startIndex; i++) {
ids[i] = i;
}
// 从 10 开始,连续3条不同的记录使用相同的 编码;
int testId = startIndex + 2;
ids[startIndex] = testId;
ids[startIndex + 1] = testId;
ids[startIndex + 2] = testId;
for (int i = 0; i < ids.length - startIndex - 3; i++) {
ids[startIndex + i + 3] = startIndex + i + 5;
}
addDatas(ids, datas, mst);
mst.commit();
// 验证所有的数据都能够正常检索;
SkippingIterator<MerkleValue<byte[]>> iter = mst.iterator();
assertEquals(count, iter.getTotalCount());
assertIteratorEquals(count, datas, ids, 0, iter);
// 验证略过中间数据也能够正常检索:跳跃到连续 id 的前一条;
iter = mst.iterator();
iter.skip(startIndex - 1);
int i = startIndex - 1;
assertIteratorEquals(count - (startIndex - 1), datas, ids, startIndex - 1, iter);
// 验证略过中间数据也能够正常检索:跳跃到连续 id 的第1条;
iter = mst.iterator();
iter.skip(startIndex);
i = startIndex;
{
MerkleValue<byte[]> v = iter.next();
assertNotNull(v);
assertEquals(testId, v.getId());
assertArrayEquals(datas[i], v.getValue());
v = iter.next();
assertNotNull(v);
assertEquals(testId, v.getId());
assertArrayEquals(datas[i + 1], v.getValue());
v = iter.next();
assertNotNull(v);
assertEquals(testId, v.getId());
assertArrayEquals(datas[i + 2], v.getValue());
}
assertIteratorEquals(count - (i + 3), datas, ids, i + 3, iter);
// 验证略过中间数据也能够正常检索:跳跃到连续 id 的第2条;
iter = mst.iterator();
iter.skip(startIndex + 1);
i = startIndex;
{
MerkleValue<byte[]> v = iter.next();
assertNotNull(v);
assertEquals(testId, v.getId());
assertArrayEquals(datas[i + 1], v.getValue());
v = iter.next();
assertNotNull(v);
assertEquals(testId, v.getId());
assertArrayEquals(datas[i + 2], v.getValue());
}
assertIteratorEquals(count - (i + 3), datas, ids, i + 3, iter);
// 验证略过中间数据也能够正常检索:跳跃到连续 id 的第3条;
iter = mst.iterator();
iter.skip(startIndex + 2);
i = startIndex;
{
MerkleValue<byte[]> v = iter.next();
assertNotNull(v);
assertEquals(testId, v.getId());
assertArrayEquals(datas[i + 2], v.getValue());
}
assertIteratorEquals(count - (i + 3), datas, ids, i + 3, iter);
// 验证略过中间数据也能够正常检索:跳跃到连续 id 第3条;
iter = mst.iterator();
iter.skip(startIndex + 3);
assertIteratorEquals(count - (startIndex + 3), datas, ids, startIndex + 3, iter);
}
Aggregations