2011-07-15 19:51:28 +05:30
|
|
|
#include "transaction_manager.h"
|
|
|
|
#include "core_map.h"
|
|
|
|
#include "btree.h"
|
|
|
|
|
|
|
|
#define BOOST_TEST_MODULE BTreeTests
|
|
|
|
#include <boost/test/included/unit_test.hpp>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace boost;
|
|
|
|
using namespace persistent_data;
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
block_address const NR_BLOCKS = 102400;
|
|
|
|
|
|
|
|
transaction_manager<4096>::ptr
|
|
|
|
create_tm() {
|
|
|
|
block_manager<4096>::ptr bm(new block_manager<4096>("./test.data", NR_BLOCKS));
|
|
|
|
space_map::ptr sm(new core_map(NR_BLOCKS));
|
|
|
|
transaction_manager<4096>::ptr tm(new transaction_manager<4096>(bm, sm));
|
|
|
|
return tm;
|
|
|
|
}
|
|
|
|
|
|
|
|
btree<1, uint64_traits, 4096>::ptr
|
|
|
|
create_btree() {
|
2011-07-22 20:39:56 +05:30
|
|
|
typename uint64_traits::ref_counter rc;
|
|
|
|
|
2011-07-15 19:51:28 +05:30
|
|
|
return btree<1, uint64_traits, 4096>::ptr(
|
|
|
|
new btree<1, uint64_traits, 4096>(
|
2011-07-22 20:39:56 +05:30
|
|
|
create_tm(), rc));
|
2011-07-15 19:51:28 +05:30
|
|
|
}
|
2011-08-22 16:25:55 +05:30
|
|
|
|
2011-08-22 18:44:10 +05:30
|
|
|
// Checks that a btree is well formed.
|
|
|
|
//
|
|
|
|
// i) No block should be in the tree more than once.
|
|
|
|
//
|
2011-08-22 16:25:55 +05:30
|
|
|
class constraint_visitor : public btree<1, uint64_traits, 4096>::visitor {
|
2011-08-22 18:44:10 +05:30
|
|
|
public:
|
2011-08-22 16:25:55 +05:30
|
|
|
void visit_internal(unsigned level, btree_detail::node_ref<uint64_traits, 4096> const &n) {
|
2011-08-22 18:44:10 +05:30
|
|
|
check_duplicate_block(n.get_location());
|
2011-08-22 16:25:55 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void visit_internal_leaf(unsigned level, btree_detail::node_ref<uint64_traits, 4096> const &n) {
|
2011-08-22 18:44:10 +05:30
|
|
|
check_duplicate_block(n.get_location());
|
2011-08-22 16:25:55 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void visit_leaf(unsigned level, btree_detail::node_ref<uint64_traits, 4096> const &n) {
|
2011-08-22 18:44:10 +05:30
|
|
|
check_duplicate_block(n.get_location());
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void check_duplicate_block(block_address b) {
|
|
|
|
if (seen_.count(b)) {
|
|
|
|
ostringstream out;
|
|
|
|
out << "duplicate block in btree: " << b;
|
|
|
|
throw runtime_error(out.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
seen_.insert(b);
|
2011-08-22 16:25:55 +05:30
|
|
|
}
|
2011-08-22 18:44:10 +05:30
|
|
|
|
|
|
|
set<block_address> seen_;
|
2011-08-22 16:25:55 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
void check_constraints(btree<1, uint64_traits, 4096>::ptr tree) {
|
|
|
|
typedef btree<1, uint64_traits, 4096> tree_type;
|
|
|
|
|
|
|
|
typename tree_type::visitor::ptr v(new constraint_visitor);
|
|
|
|
tree->visit(v);
|
|
|
|
}
|
2011-07-15 19:51:28 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(empty_btree_contains_nothing)
|
|
|
|
{
|
|
|
|
auto tree = create_btree();
|
2011-08-22 18:44:10 +05:30
|
|
|
check_constraints(tree);
|
2011-07-15 19:51:28 +05:30
|
|
|
|
|
|
|
for (uint64_t i = 0; i < 1000; i++) {
|
|
|
|
uint64_t key[1] = {i};
|
|
|
|
BOOST_CHECK(!tree->lookup(key));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(insert_works)
|
|
|
|
{
|
2011-08-22 18:44:10 +05:30
|
|
|
unsigned const COUNT = 100000;
|
2011-07-15 19:51:28 +05:30
|
|
|
|
|
|
|
auto tree = create_btree();
|
|
|
|
for (uint64_t i = 0; i < COUNT; i++) {
|
|
|
|
uint64_t key[1] = {i * 7};
|
|
|
|
uint64_t value = i;
|
2011-08-22 18:44:10 +05:30
|
|
|
|
2011-07-15 19:51:28 +05:30
|
|
|
tree->insert(key, value);
|
|
|
|
|
|
|
|
auto l = tree->lookup(key);
|
|
|
|
BOOST_CHECK(l);
|
|
|
|
BOOST_CHECK_EQUAL(*l, i);
|
|
|
|
}
|
2011-08-22 18:44:10 +05:30
|
|
|
|
|
|
|
check_constraints(tree);
|
2011-07-15 19:51:28 +05:30
|
|
|
}
|
2011-07-22 21:04:24 +05:30
|
|
|
|
|
|
|
//----------------------------------------------------------------
|