2011-07-22 20:39:56 +05:30
|
|
|
#include "space_map_disk.h"
|
2011-11-03 20:14:00 +05:30
|
|
|
#include "space_map_core.h"
|
2011-07-22 20:39:56 +05:30
|
|
|
|
|
|
|
#define BOOST_TEST_MODULE SpaceMapDiskTests
|
|
|
|
#include <boost/test/included/unit_test.hpp>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace boost;
|
|
|
|
using namespace persistent_data;
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
|
|
|
namespace {
|
2011-11-03 20:14:00 +05:30
|
|
|
block_address const NR_BLOCKS = 1000; // FIXME: bump up
|
2011-07-22 20:39:56 +05:30
|
|
|
block_address const SUPERBLOCK = 0;
|
2011-11-03 20:14:00 +05:30
|
|
|
block_address const MAX_LOCKS = 8;
|
2011-07-22 20:39:56 +05:30
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
transaction_manager::ptr
|
2011-07-22 20:39:56 +05:30
|
|
|
create_tm() {
|
2011-08-31 17:18:41 +05:30
|
|
|
block_manager<>::ptr bm(
|
2011-11-03 20:14:00 +05:30
|
|
|
new block_manager<>("./test.data", NR_BLOCKS, MAX_LOCKS, true));
|
2011-07-22 20:39:56 +05:30
|
|
|
space_map::ptr sm(new core_map(1024));
|
2011-08-31 17:18:41 +05:30
|
|
|
transaction_manager::ptr tm(
|
|
|
|
new transaction_manager(bm, sm));
|
2011-07-22 20:39:56 +05:30
|
|
|
return tm;
|
|
|
|
}
|
|
|
|
|
|
|
|
persistent_space_map::ptr
|
|
|
|
create_sm_disk() {
|
2011-11-03 20:14:00 +05:30
|
|
|
transaction_manager::ptr tm = create_tm();
|
2011-08-31 17:18:41 +05:30
|
|
|
return persistent_data::create_disk_sm(tm, NR_BLOCKS);
|
2011-07-22 20:39:56 +05:30
|
|
|
}
|
2011-11-03 20:14:00 +05:30
|
|
|
|
|
|
|
persistent_space_map::ptr
|
|
|
|
create_sm_metadata() {
|
|
|
|
transaction_manager::ptr tm = create_tm();
|
|
|
|
return persistent_data::create_metadata_sm(tm, NR_BLOCKS);
|
|
|
|
}
|
2011-07-22 20:39:56 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
2011-11-03 20:14:00 +05:30
|
|
|
void _test_get_nr_blocks(space_map::ptr sm)
|
2011-07-22 20:39:56 +05:30
|
|
|
{
|
|
|
|
BOOST_CHECK_EQUAL(sm->get_nr_blocks(), NR_BLOCKS);
|
|
|
|
}
|
|
|
|
|
2011-11-03 20:14:00 +05:30
|
|
|
void _test_get_nr_free(space_map::ptr sm)
|
2011-07-22 20:39:56 +05:30
|
|
|
{
|
|
|
|
BOOST_CHECK_EQUAL(sm->get_nr_free(), NR_BLOCKS);
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < NR_BLOCKS; i++) {
|
|
|
|
sm->new_block();
|
|
|
|
BOOST_CHECK_EQUAL(sm->get_nr_free(), NR_BLOCKS - i - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < NR_BLOCKS; i++) {
|
|
|
|
sm->dec(i);
|
|
|
|
BOOST_CHECK_EQUAL(sm->get_nr_free(), i + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-03 20:14:00 +05:30
|
|
|
void _test_throws_no_space(space_map::ptr sm)
|
2011-07-22 20:39:56 +05:30
|
|
|
{
|
|
|
|
for (unsigned i = 0; i < NR_BLOCKS; i++)
|
|
|
|
sm->new_block();
|
|
|
|
|
|
|
|
BOOST_CHECK_THROW(sm->new_block(), std::runtime_error);
|
|
|
|
}
|
|
|
|
|
2011-11-03 20:14:00 +05:30
|
|
|
void _test_inc_and_dec(space_map::ptr sm)
|
2011-07-22 20:39:56 +05:30
|
|
|
{
|
|
|
|
block_address b = 63;
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < 50; i++) {
|
|
|
|
BOOST_CHECK_EQUAL(sm->get_count(b), i);
|
|
|
|
sm->inc(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned i = 50; i > 0; i--) {
|
|
|
|
BOOST_CHECK_EQUAL(sm->get_count(b), i);
|
|
|
|
sm->dec(b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-03 20:14:00 +05:30
|
|
|
void _test_not_allocated_twice(space_map::ptr sm)
|
2011-07-22 20:39:56 +05:30
|
|
|
{
|
|
|
|
block_address b = sm->new_block();
|
|
|
|
|
|
|
|
try {
|
|
|
|
for (;;)
|
|
|
|
BOOST_CHECK(sm->new_block() != b);
|
|
|
|
} catch (...) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-03 20:14:00 +05:30
|
|
|
void _test_set_count(space_map::ptr sm)
|
2011-07-22 20:39:56 +05:30
|
|
|
{
|
|
|
|
sm->set_count(43, 5);
|
|
|
|
BOOST_CHECK_EQUAL(sm->get_count(43), 5);
|
|
|
|
}
|
|
|
|
|
2011-11-03 20:14:00 +05:30
|
|
|
void _test_set_affects_nr_allocated(space_map::ptr sm)
|
2011-07-22 21:02:47 +05:30
|
|
|
{
|
|
|
|
for (unsigned i = 0; i < NR_BLOCKS; i++) {
|
|
|
|
sm->set_count(i, 1);
|
|
|
|
BOOST_CHECK_EQUAL(sm->get_nr_free(), NR_BLOCKS - i - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < NR_BLOCKS; i++) {
|
|
|
|
sm->set_count(i, 0);
|
|
|
|
BOOST_CHECK_EQUAL(sm->get_nr_free(), i + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-09 15:51:25 +05:30
|
|
|
// Ref counts below 3 gets stored as bitmaps, above 3 they go into a btree
|
|
|
|
// with uint32_t values. Worth checking this thoroughly, especially for
|
|
|
|
// the metadata format which may have complications due to recursion.
|
|
|
|
void _test_high_ref_counts(space_map::ptr sm)
|
|
|
|
{
|
|
|
|
srand(1234);
|
|
|
|
for (unsigned i = 0; i < NR_BLOCKS; i++)
|
|
|
|
sm->set_count(i, rand() % 6789);
|
|
|
|
sm->commit();
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < NR_BLOCKS; i++) {
|
|
|
|
sm->inc(i);
|
|
|
|
sm->inc(i);
|
|
|
|
if (i % 1000)
|
|
|
|
sm->commit();
|
|
|
|
}
|
|
|
|
sm->commit();
|
|
|
|
|
|
|
|
srand(1234);
|
|
|
|
for (unsigned i = 0; i < NR_BLOCKS; i++)
|
|
|
|
BOOST_CHECK_EQUAL(sm->get_count(i), (rand() % 6789) + 2);
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < NR_BLOCKS; i++)
|
|
|
|
sm->dec(i);
|
|
|
|
|
|
|
|
srand(1234);
|
|
|
|
for (unsigned i = 0; i < NR_BLOCKS; i++)
|
|
|
|
BOOST_CHECK_EQUAL(sm->get_count(i), (rand() % 6789) + 1);
|
|
|
|
}
|
|
|
|
|
2011-11-03 20:14:00 +05:30
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
BOOST_AUTO_TEST_CASE(reopen_an_sm)
|
|
|
|
{
|
|
|
|
space_map::ptr sm = create_sm_disk();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(test_disk_get_nr_blocks)
|
|
|
|
{
|
|
|
|
space_map::ptr sm = create_sm_disk();
|
|
|
|
_test_get_nr_blocks(sm);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(test_disk_get_nr_free)
|
|
|
|
{
|
|
|
|
space_map::ptr sm = create_sm_disk();
|
|
|
|
_test_get_nr_free(sm);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(test_disk_throws_no_space)
|
|
|
|
{
|
|
|
|
space_map::ptr sm = create_sm_disk();
|
|
|
|
_test_throws_no_space(sm);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(test_disk_inc_and_dec)
|
|
|
|
{
|
|
|
|
space_map::ptr sm = create_sm_disk();
|
|
|
|
_test_inc_and_dec(sm);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(test_disk_not_allocated_twice)
|
|
|
|
{
|
|
|
|
space_map::ptr sm = create_sm_disk();
|
|
|
|
_test_not_allocated_twice(sm);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(test_disk_set_count)
|
|
|
|
{
|
|
|
|
space_map::ptr sm = create_sm_disk();
|
|
|
|
_test_set_count(sm);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(test_disk_set_affects_nr_allocated)
|
|
|
|
{
|
|
|
|
space_map::ptr sm = create_sm_disk();
|
|
|
|
_test_set_affects_nr_allocated(sm);
|
|
|
|
}
|
|
|
|
|
2011-11-09 15:51:25 +05:30
|
|
|
BOOST_AUTO_TEST_CASE(test_disk_high_ref_counts)
|
|
|
|
{
|
|
|
|
space_map::ptr sm = create_sm_disk();
|
|
|
|
_test_high_ref_counts(sm);
|
|
|
|
}
|
|
|
|
|
2011-11-03 20:14:00 +05:30
|
|
|
BOOST_AUTO_TEST_CASE(test_disk_reopen)
|
2011-07-22 21:13:15 +05:30
|
|
|
{
|
|
|
|
unsigned char buffer[128];
|
|
|
|
|
|
|
|
{
|
2011-11-03 20:14:00 +05:30
|
|
|
persistent_space_map::ptr sm = create_sm_disk();
|
2011-07-22 21:13:15 +05:30
|
|
|
for (unsigned i = 0, step = 1; i < NR_BLOCKS; i += step, step++) {
|
|
|
|
sm->inc(i);
|
|
|
|
}
|
2011-11-03 20:14:00 +05:30
|
|
|
sm->commit();
|
2011-07-22 21:13:15 +05:30
|
|
|
|
|
|
|
BOOST_CHECK(sm->root_size() <= sizeof(buffer));
|
2011-11-03 20:14:00 +05:30
|
|
|
sm->copy_root(buffer, sizeof(buffer));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
transaction_manager::ptr tm = create_tm();
|
|
|
|
persistent_space_map::ptr sm = persistent_data::open_disk_sm(tm, buffer);
|
2011-07-22 21:13:15 +05:30
|
|
|
|
2011-11-03 20:14:00 +05:30
|
|
|
for (unsigned i = 0, step = 1; i < NR_BLOCKS; i += step, step++)
|
|
|
|
BOOST_CHECK_EQUAL(sm->get_count(i), 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(test_metadata_get_nr_blocks)
|
|
|
|
{
|
|
|
|
space_map::ptr sm = create_sm_metadata();
|
|
|
|
_test_get_nr_blocks(sm);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(test_metadata_get_nr_free)
|
|
|
|
{
|
|
|
|
space_map::ptr sm = create_sm_metadata();
|
|
|
|
_test_get_nr_free(sm);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(test_metadata_throws_no_space)
|
|
|
|
{
|
|
|
|
space_map::ptr sm = create_sm_metadata();
|
|
|
|
_test_throws_no_space(sm);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(test_metadata_inc_and_dec)
|
|
|
|
{
|
|
|
|
space_map::ptr sm = create_sm_metadata();
|
|
|
|
_test_inc_and_dec(sm);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(test_metadata_not_allocated_twice)
|
|
|
|
{
|
|
|
|
space_map::ptr sm = create_sm_metadata();
|
|
|
|
_test_not_allocated_twice(sm);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(test_metadata_set_count)
|
|
|
|
{
|
|
|
|
space_map::ptr sm = create_sm_metadata();
|
|
|
|
_test_set_count(sm);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(test_metadata_set_affects_nr_allocated)
|
|
|
|
{
|
|
|
|
space_map::ptr sm = create_sm_metadata();
|
|
|
|
_test_set_affects_nr_allocated(sm);
|
|
|
|
}
|
|
|
|
|
2011-11-09 15:51:25 +05:30
|
|
|
BOOST_AUTO_TEST_CASE(test_metadata_high_ref_counts)
|
|
|
|
{
|
|
|
|
space_map::ptr sm = create_sm_metadata();
|
|
|
|
_test_high_ref_counts(sm);
|
|
|
|
}
|
|
|
|
|
2011-11-03 20:14:00 +05:30
|
|
|
BOOST_AUTO_TEST_CASE(test_metadata_reopen)
|
|
|
|
{
|
|
|
|
unsigned char buffer[128];
|
|
|
|
|
|
|
|
{
|
|
|
|
persistent_space_map::ptr sm = create_sm_metadata();
|
|
|
|
for (unsigned i = 0, step = 1; i < NR_BLOCKS; i += step, step++) {
|
|
|
|
sm->inc(i);
|
|
|
|
}
|
|
|
|
sm->commit();
|
|
|
|
|
|
|
|
BOOST_CHECK(sm->root_size() <= sizeof(buffer));
|
2011-07-22 21:13:15 +05:30
|
|
|
sm->copy_root(buffer, sizeof(buffer));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2011-11-03 20:14:00 +05:30
|
|
|
transaction_manager::ptr tm = create_tm();
|
|
|
|
persistent_space_map::ptr sm = persistent_data::open_metadata_sm(tm, buffer);
|
2011-07-22 21:13:15 +05:30
|
|
|
|
|
|
|
for (unsigned i = 0, step = 1; i < NR_BLOCKS; i += step, step++)
|
|
|
|
BOOST_CHECK_EQUAL(sm->get_count(i), 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-22 20:39:56 +05:30
|
|
|
//----------------------------------------------------------------
|