2013-05-08 16:30:24 +05:30
|
|
|
#include "gmock/gmock.h"
|
|
|
|
|
|
|
|
#include "test_utils.h"
|
|
|
|
|
|
|
|
#include "persistent-data/data-structures/btree_damage_visitor.h"
|
2013-05-08 21:08:38 +05:30
|
|
|
#include "persistent-data/endian_utils.h"
|
|
|
|
#include "persistent-data/space-maps/core.h"
|
|
|
|
#include "persistent-data/transaction_manager.h"
|
2013-05-08 16:30:24 +05:30
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace persistent_data;
|
|
|
|
using namespace test;
|
|
|
|
using namespace testing;
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
block_address const BLOCK_SIZE = 4096;
|
|
|
|
block_address const NR_BLOCKS = 102400;
|
2013-05-09 18:01:04 +05:30
|
|
|
block_address const SUPERBLOCK = 0;
|
2013-05-08 16:30:24 +05:30
|
|
|
|
2013-05-08 21:08:38 +05:30
|
|
|
struct thing {
|
2013-05-09 18:01:04 +05:30
|
|
|
thing(/* uint32_t x_ = 0, */ uint64_t y_ = 0)
|
|
|
|
: /* x(x_), */
|
|
|
|
y(y_) {
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator ==(thing const &rhs) const {
|
|
|
|
return /* (x == rhs.x) && */ (y == rhs.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
//uint32_t x;
|
2013-05-08 21:08:38 +05:30
|
|
|
uint64_t y;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct thing_disk {
|
2013-05-09 18:01:04 +05:30
|
|
|
//le32 x;
|
2013-05-08 21:08:38 +05:30
|
|
|
le64 y;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct thing_traits {
|
|
|
|
typedef thing_disk disk_type;
|
|
|
|
typedef thing value_type;
|
|
|
|
typedef persistent_data::no_op_ref_counter<value_type> ref_counter;
|
|
|
|
|
|
|
|
static void unpack(thing_disk const &disk, thing &value) {
|
2013-05-09 18:01:04 +05:30
|
|
|
// value.x = to_cpu<uint32_t>(disk.x);
|
2013-05-08 21:08:38 +05:30
|
|
|
value.y = to_cpu<uint64_t>(disk.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void pack(thing const &value, thing_disk &disk) {
|
2013-05-09 18:01:04 +05:30
|
|
|
// disk.x = to_disk<le32>(value.x);
|
2013-05-08 21:08:38 +05:30
|
|
|
disk.y = to_disk<le64>(value.y);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class value_visitor_mock {
|
|
|
|
public:
|
|
|
|
MOCK_METHOD1(visit, void(thing const &));
|
|
|
|
};
|
|
|
|
|
|
|
|
class damage_visitor_mock {
|
|
|
|
public:
|
|
|
|
MOCK_METHOD1(visit, void(btree_detail::damage const &));
|
|
|
|
};
|
|
|
|
|
2013-05-08 16:30:24 +05:30
|
|
|
class BTreeDamageVisitorTests : public Test {
|
|
|
|
public:
|
|
|
|
BTreeDamageVisitorTests()
|
2013-05-08 21:08:38 +05:30
|
|
|
: bm_(create_bm<BLOCK_SIZE>(NR_BLOCKS)),
|
2013-05-09 18:01:04 +05:30
|
|
|
sm_(setup_core_map()),
|
2013-05-08 21:08:38 +05:30
|
|
|
tm_(new transaction_manager(bm_, sm_)),
|
2013-05-09 18:01:04 +05:30
|
|
|
tree_(new btree<1, thing_traits>(tm_, rc_)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
space_map::ptr setup_core_map() {
|
|
|
|
space_map::ptr sm(new core_map(NR_BLOCKS));
|
|
|
|
sm->inc(SUPERBLOCK);
|
|
|
|
return sm;
|
|
|
|
}
|
|
|
|
|
|
|
|
void commit() {
|
|
|
|
block_manager<>::write_ref superblock(bm_->superblock(SUPERBLOCK));
|
2013-05-08 21:08:38 +05:30
|
|
|
}
|
|
|
|
|
2013-05-09 18:01:04 +05:30
|
|
|
void trash_block(block_address b) {
|
2013-05-08 21:08:38 +05:30
|
|
|
::test::zero_block(bm_, b);
|
2013-05-08 16:30:24 +05:30
|
|
|
}
|
|
|
|
|
2013-05-09 18:01:04 +05:30
|
|
|
void insert_values(unsigned nr) {
|
|
|
|
for (unsigned i = 0; i < nr; i++) {
|
|
|
|
uint64_t key[1] = {i};
|
|
|
|
thing value(i /*, i + 1234 */);
|
|
|
|
|
|
|
|
tree_->insert(key, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void expect_no_values() {
|
|
|
|
EXPECT_CALL(value_visitor_, visit(_)).Times(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void expect_nr_values(unsigned nr) {
|
|
|
|
for (unsigned i = 0; i < nr; i++)
|
|
|
|
EXPECT_CALL(value_visitor_, visit(Eq(thing(i /*, i + 1234 */)))).Times(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void expect_value(unsigned n) {
|
|
|
|
EXPECT_CALL(value_visitor_, visit(Eq(thing(n /*, n + 1234 */)))).Times(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void expect_no_damage() {
|
|
|
|
EXPECT_CALL(damage_visitor_, visit(_)).Times(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void expect_damage(unsigned level, range<uint64_t> keys) {
|
|
|
|
EXPECT_CALL(damage_visitor_, visit(Eq(damage(level, keys, "foo")))).Times(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void run() {
|
|
|
|
// We must commit before we do the test to ensure
|
|
|
|
// all the block numbers and checksums are written
|
|
|
|
// to the btree nodes.
|
|
|
|
commit();
|
|
|
|
|
|
|
|
block_counter counter;
|
|
|
|
btree_damage_visitor<value_visitor_mock, damage_visitor_mock, 1, thing_traits>
|
|
|
|
visitor(counter, value_visitor_, damage_visitor_);
|
|
|
|
tree_->visit_depth_first(visitor);
|
|
|
|
}
|
|
|
|
|
2013-05-08 16:30:24 +05:30
|
|
|
with_temp_directory dir_;
|
|
|
|
block_manager<>::ptr bm_;
|
2013-05-08 21:08:38 +05:30
|
|
|
space_map::ptr sm_;
|
|
|
|
transaction_manager::ptr tm_;
|
|
|
|
|
|
|
|
thing_traits::ref_counter rc_;
|
2013-05-09 18:01:04 +05:30
|
|
|
btree<1, thing_traits>::ptr tree_;
|
2013-05-08 21:08:38 +05:30
|
|
|
|
|
|
|
value_visitor_mock value_visitor_;
|
|
|
|
damage_visitor_mock damage_visitor_;
|
2013-05-08 16:30:24 +05:30
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
2013-05-09 18:01:04 +05:30
|
|
|
TEST_F(BTreeDamageVisitorTests, visiting_an_empty_tree)
|
2013-05-08 16:30:24 +05:30
|
|
|
{
|
2013-05-09 18:01:04 +05:30
|
|
|
expect_no_values();
|
|
|
|
expect_no_damage();
|
2013-05-08 21:08:38 +05:30
|
|
|
|
2013-05-09 18:01:04 +05:30
|
|
|
run();
|
2013-05-08 21:08:38 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BTreeDamageVisitorTests, visiting_an_tree_with_a_trashed_root)
|
|
|
|
{
|
2013-05-09 18:01:04 +05:30
|
|
|
trash_block(tree_->get_root());
|
2013-05-08 21:08:38 +05:30
|
|
|
|
2013-05-09 18:01:04 +05:30
|
|
|
expect_no_values();
|
|
|
|
expect_damage(0, range<uint64_t>());
|
2013-05-08 21:08:38 +05:30
|
|
|
|
2013-05-09 18:01:04 +05:30
|
|
|
run();
|
2013-05-08 16:30:24 +05:30
|
|
|
}
|
|
|
|
|
2013-05-09 18:01:04 +05:30
|
|
|
TEST_F(BTreeDamageVisitorTests, visiting_a_populated_tree_with_no_damage)
|
|
|
|
{
|
|
|
|
insert_values(1000);
|
|
|
|
|
|
|
|
expect_nr_values(1000);
|
|
|
|
expect_no_damage();
|
|
|
|
|
|
|
|
run();
|
|
|
|
}
|
2013-05-08 21:08:38 +05:30
|
|
|
|
2013-05-08 16:30:24 +05:30
|
|
|
//----------------------------------------------------------------
|