2011-12-16 00:04:31 +05:30
|
|
|
// Copyright (C) 2011 Red Hat, Inc. All rights reserved.
|
2011-12-06 19:23:05 +05:30
|
|
|
//
|
2011-12-06 19:13:56 +05:30
|
|
|
// This file is part of the thin-provisioning-tools source.
|
|
|
|
//
|
|
|
|
// thin-provisioning-tools is free software: you can redistribute it
|
|
|
|
// and/or modify it under the terms of the GNU General Public License
|
|
|
|
// as published by the Free Software Foundation, either version 3 of
|
|
|
|
// the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// thin-provisioning-tools is distributed in the hope that it will be
|
|
|
|
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty
|
|
|
|
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License along
|
|
|
|
// with thin-provisioning-tools. If not, see
|
|
|
|
// <http://www.gnu.org/licenses/>.
|
|
|
|
|
2013-03-22 20:27:46 +05:30
|
|
|
#include "gmock/gmock.h"
|
2013-01-10 01:54:11 +05:30
|
|
|
#include "persistent-data/transaction_manager.h"
|
2013-01-11 03:06:38 +05:30
|
|
|
#include "persistent-data/space-maps/core.h"
|
2013-01-12 00:56:51 +05:30
|
|
|
#include "persistent-data/data-structures/btree.h"
|
2014-01-30 03:07:25 +05:30
|
|
|
#include "persistent-data/data-structures/simple_traits.h"
|
2011-07-15 19:51:28 +05:30
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace persistent_data;
|
2013-03-22 20:27:46 +05:30
|
|
|
using namespace testing;
|
2011-07-15 19:51:28 +05:30
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
block_address const NR_BLOCKS = 102400;
|
|
|
|
|
2014-08-26 15:44:49 +05:30
|
|
|
class BtreeTests : public Test {
|
|
|
|
public:
|
|
|
|
BtreeTests()
|
2020-04-30 19:00:01 +05:30
|
|
|
: bm_(new block_manager("./test.data", NR_BLOCKS, 4, block_manager::READ_WRITE)),
|
2014-08-26 15:44:49 +05:30
|
|
|
sm_(new core_map(NR_BLOCKS)),
|
|
|
|
tm_(bm_, sm_) {
|
|
|
|
}
|
2011-07-15 19:51:28 +05:30
|
|
|
|
2014-08-26 15:44:49 +05:30
|
|
|
btree<1, uint64_traits>::ptr
|
|
|
|
create_btree() {
|
|
|
|
uint64_traits::ref_counter rc;
|
|
|
|
|
|
|
|
return btree<1, uint64_traits>::ptr(
|
|
|
|
new btree<1, uint64_traits>(tm_, rc));
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-04-30 19:00:01 +05:30
|
|
|
block_manager::ptr bm_;
|
2014-08-26 15:44:49 +05:30
|
|
|
space_map::ptr sm_;
|
|
|
|
transaction_manager tm_;
|
|
|
|
};
|
2011-07-22 20:39:56 +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-31 17:18:41 +05:30
|
|
|
class constraint_visitor : public btree<1, uint64_traits>::visitor {
|
2011-08-22 18:44:10 +05:30
|
|
|
public:
|
2013-05-01 20:46:23 +05:30
|
|
|
typedef btree_detail::node_location node_location;
|
2013-06-20 18:57:40 +05:30
|
|
|
typedef btree_detail::node_ref<block_traits> internal_node;
|
2011-11-01 17:01:03 +05:30
|
|
|
typedef btree_detail::node_ref<uint64_traits> leaf_node;
|
|
|
|
|
2013-05-01 20:46:23 +05:30
|
|
|
bool visit_internal(node_location const &loc,
|
2011-11-01 17:01:03 +05:30
|
|
|
internal_node const &n) {
|
2011-08-22 18:44:10 +05:30
|
|
|
check_duplicate_block(n.get_location());
|
2011-08-30 17:17:42 +05:30
|
|
|
return true;
|
2011-08-22 16:25:55 +05:30
|
|
|
}
|
|
|
|
|
2013-05-01 20:46:23 +05:30
|
|
|
bool visit_internal_leaf(node_location const &loc,
|
|
|
|
internal_node const &n) {
|
2011-08-22 18:44:10 +05:30
|
|
|
check_duplicate_block(n.get_location());
|
2011-08-30 17:17:42 +05:30
|
|
|
return true;
|
2011-08-22 16:25:55 +05:30
|
|
|
}
|
|
|
|
|
2013-05-01 20:46:23 +05:30
|
|
|
bool visit_leaf(node_location const &loc,
|
|
|
|
leaf_node const &n) {
|
2011-08-22 18:44:10 +05:30
|
|
|
check_duplicate_block(n.get_location());
|
2011-08-30 17:17:42 +05:30
|
|
|
return true;
|
2011-08-22 18:44:10 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
void check_constraints(btree<1, uint64_traits>::ptr tree) {
|
|
|
|
typedef btree<1, uint64_traits> tree_type;
|
2011-08-22 16:25:55 +05:30
|
|
|
|
2013-05-02 17:01:33 +05:30
|
|
|
constraint_visitor v;
|
2013-05-01 20:46:23 +05:30
|
|
|
tree->visit_depth_first(v);
|
2011-08-22 16:25:55 +05:30
|
|
|
}
|
2011-07-15 19:51:28 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
2014-08-26 15:44:49 +05:30
|
|
|
TEST_F(BtreeTests, empty_btree_contains_nothing)
|
2011-07-15 19:51:28 +05:30
|
|
|
{
|
2011-11-01 17:01:03 +05:30
|
|
|
btree<1, uint64_traits>::ptr 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};
|
2013-03-22 20:27:46 +05:30
|
|
|
ASSERT_FALSE(tree->lookup(key));
|
2011-07-15 19:51:28 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-26 15:44:49 +05:30
|
|
|
TEST_F(BtreeTests, insert_works)
|
2011-07-15 19:51:28 +05:30
|
|
|
{
|
2011-08-22 18:44:10 +05:30
|
|
|
unsigned const COUNT = 100000;
|
2011-07-15 19:51:28 +05:30
|
|
|
|
2011-11-01 17:01:03 +05:30
|
|
|
btree<1, uint64_traits>::ptr tree = create_btree();
|
2011-07-15 19:51:28 +05:30
|
|
|
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);
|
|
|
|
|
2011-11-01 17:01:03 +05:30
|
|
|
btree<1, uint64_traits>::maybe_value l = tree->lookup(key);
|
2016-02-04 14:32:42 +05:30
|
|
|
ASSERT_TRUE(!!l);
|
2013-03-22 20:27:46 +05:30
|
|
|
ASSERT_THAT(*l, Eq(i));
|
2011-07-15 19:51:28 +05:30
|
|
|
}
|
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
|
|
|
|
2014-08-26 15:44:49 +05:30
|
|
|
TEST_F(BtreeTests, insert_does_not_insert_imaginary_values)
|
2011-11-01 17:01:03 +05:30
|
|
|
{
|
|
|
|
btree<1, uint64_traits>::ptr tree = create_btree();
|
|
|
|
uint64_t key[1] = {0};
|
|
|
|
uint64_t value = 100;
|
|
|
|
|
|
|
|
btree<1, uint64_traits>::maybe_value l = tree->lookup(key);
|
2013-03-22 20:27:46 +05:30
|
|
|
ASSERT_FALSE(l);
|
2011-11-01 17:01:03 +05:30
|
|
|
|
|
|
|
key[0] = 1;
|
|
|
|
l = tree->lookup(key);
|
2013-03-22 20:27:46 +05:30
|
|
|
ASSERT_FALSE(l);
|
2011-11-01 17:01:03 +05:30
|
|
|
|
|
|
|
key[0] = 0;
|
|
|
|
tree->insert(key, value);
|
|
|
|
|
|
|
|
l = tree->lookup(key);
|
2016-02-04 14:32:42 +05:30
|
|
|
ASSERT_TRUE(!!l);
|
2013-03-22 20:27:46 +05:30
|
|
|
ASSERT_THAT(*l, Eq(100u));
|
2011-11-01 17:01:03 +05:30
|
|
|
|
|
|
|
key[0] = 1;
|
|
|
|
l = tree->lookup(key);
|
2013-03-22 20:27:46 +05:30
|
|
|
ASSERT_FALSE(l);
|
2011-11-01 17:01:03 +05:30
|
|
|
|
|
|
|
check_constraints(tree);
|
|
|
|
}
|
|
|
|
|
2014-08-26 15:44:49 +05:30
|
|
|
TEST_F(BtreeTests, clone)
|
2013-06-19 21:47:45 +05:30
|
|
|
{
|
|
|
|
typedef btree<1, uint64_traits> tree64;
|
|
|
|
|
|
|
|
unsigned const COUNT = 1000;
|
|
|
|
|
|
|
|
tree64::maybe_value l;
|
|
|
|
tree64::ptr tree = create_btree();
|
|
|
|
for (uint64_t i = 0; i < COUNT; i++) {
|
|
|
|
uint64_t key[1] = {i};
|
|
|
|
uint64_t value = i * 7;
|
|
|
|
|
|
|
|
tree->insert(key, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint64_t i = 0; i < COUNT; i++) {
|
|
|
|
uint64_t key[1] = {i};
|
|
|
|
uint64_t value = i * 7;
|
|
|
|
|
|
|
|
l = tree->lookup(key);
|
2016-02-04 14:32:42 +05:30
|
|
|
ASSERT_TRUE(!!l);
|
2013-06-19 21:47:45 +05:30
|
|
|
ASSERT_THAT(*l, Eq(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
tree64::ptr copy = tree->clone();
|
|
|
|
for (uint64_t i = 0; i < COUNT; i++) {
|
|
|
|
uint64_t key[1] = {i + COUNT};
|
|
|
|
uint64_t value = (i + COUNT) * 7;
|
|
|
|
|
|
|
|
copy->insert(key, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint64_t i = 0; i < COUNT; i++) {
|
|
|
|
uint64_t key[1] = {i};
|
|
|
|
uint64_t value = i * 7;
|
|
|
|
|
|
|
|
l = tree->lookup(key);
|
2016-02-04 14:32:42 +05:30
|
|
|
ASSERT_TRUE(!!l);
|
2013-06-19 21:47:45 +05:30
|
|
|
ASSERT_THAT(*l, Eq(value));
|
|
|
|
|
|
|
|
l = copy->lookup(key);
|
2016-02-04 14:32:42 +05:30
|
|
|
ASSERT_TRUE(!!l);
|
2013-06-19 21:47:45 +05:30
|
|
|
ASSERT_THAT(*l, Eq(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint64_t i = 0; i < COUNT; i++) {
|
|
|
|
uint64_t key[1] = {i + COUNT};
|
|
|
|
uint64_t value = (i + COUNT) * 7;
|
|
|
|
|
|
|
|
l = tree->lookup(key);
|
|
|
|
ASSERT_FALSE(l);
|
|
|
|
|
|
|
|
l = copy->lookup(key);
|
2016-02-04 14:32:42 +05:30
|
|
|
ASSERT_TRUE(!!l);
|
2013-06-19 21:47:45 +05:30
|
|
|
ASSERT_THAT(*l, Eq(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
check_constraints(tree);
|
|
|
|
}
|
|
|
|
|
2011-07-22 21:04:24 +05:30
|
|
|
//----------------------------------------------------------------
|