2014-01-23 06:16:03 +05:30
|
|
|
#include "era/metadata.h"
|
|
|
|
#include "persistent-data/space-maps/core.h"
|
|
|
|
|
|
|
|
using namespace era;
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
// FIXME: duplication
|
|
|
|
transaction_manager::ptr
|
|
|
|
open_tm(block_manager<>::ptr bm) {
|
2017-09-21 14:52:38 +05:30
|
|
|
auto nr_blocks = bm->get_nr_blocks();
|
|
|
|
if (!nr_blocks)
|
|
|
|
throw runtime_error("Metadata is not large enough for superblock.");
|
|
|
|
|
|
|
|
space_map::ptr sm(new core_map(nr_blocks));
|
2014-01-23 06:16:03 +05:30
|
|
|
sm->inc(SUPERBLOCK_LOCATION);
|
|
|
|
transaction_manager::ptr tm(new transaction_manager(bm, sm));
|
|
|
|
return tm;
|
|
|
|
}
|
2014-08-21 15:50:43 +05:30
|
|
|
|
|
|
|
void
|
|
|
|
copy_space_maps(space_map::ptr lhs, space_map::ptr rhs) {
|
|
|
|
for (block_address b = 0; b < rhs->get_nr_blocks(); b++) {
|
|
|
|
uint32_t count = rhs->get_count(b);
|
|
|
|
if (count > 0)
|
|
|
|
lhs->set_count(b, rhs->get_count(b));
|
|
|
|
}
|
|
|
|
}
|
2014-01-23 06:16:03 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
metadata::metadata(block_manager<>::ptr bm, open_type ot)
|
|
|
|
{
|
|
|
|
switch (ot) {
|
|
|
|
case CREATE:
|
2014-08-21 15:50:43 +05:30
|
|
|
create_metadata(bm);
|
2014-01-23 06:16:03 +05:30
|
|
|
break;
|
|
|
|
|
|
|
|
case OPEN:
|
|
|
|
open_metadata(bm);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-31 22:01:27 +05:30
|
|
|
metadata::metadata(block_manager<>::ptr bm, block_address metadata_snap)
|
|
|
|
{
|
|
|
|
open_metadata(bm);
|
|
|
|
}
|
|
|
|
|
2014-01-23 06:16:03 +05:30
|
|
|
void
|
2014-08-21 15:50:43 +05:30
|
|
|
metadata::create_metadata(block_manager<>::ptr bm)
|
|
|
|
{
|
|
|
|
tm_ = open_tm(bm);
|
|
|
|
|
|
|
|
space_map::ptr core = tm_->get_sm();
|
2014-08-26 15:44:49 +05:30
|
|
|
metadata_sm_ = create_metadata_sm(*tm_, tm_->get_bm()->get_nr_blocks());
|
2014-08-21 15:50:43 +05:30
|
|
|
copy_space_maps(metadata_sm_, core);
|
|
|
|
tm_->set_sm(metadata_sm_);
|
|
|
|
|
2014-08-26 15:44:49 +05:30
|
|
|
writeset_tree_ = writeset_tree::ptr(new writeset_tree(*tm_, era_detail_traits::ref_counter(tm_)));
|
|
|
|
era_array_ = era_array::ptr(new era_array(*tm_,
|
2014-08-21 15:50:43 +05:30
|
|
|
uint32_traits::ref_counter()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-01-31 22:01:27 +05:30
|
|
|
metadata::open_metadata(block_manager<>::ptr bm, block_address loc)
|
2014-01-23 06:16:03 +05:30
|
|
|
{
|
|
|
|
tm_ = open_tm(bm);
|
2014-01-31 22:01:27 +05:30
|
|
|
sb_ = read_superblock(tm_->get_bm(), loc);
|
2014-01-23 06:16:03 +05:30
|
|
|
|
2014-08-26 15:44:49 +05:30
|
|
|
writeset_tree_ = writeset_tree::ptr(new writeset_tree(*tm_,
|
2014-01-30 03:07:25 +05:30
|
|
|
sb_.writeset_tree_root,
|
|
|
|
era_detail_traits::ref_counter(tm_)));
|
2014-01-23 06:16:03 +05:30
|
|
|
|
2014-08-26 15:44:49 +05:30
|
|
|
era_array_ = era_array::ptr(new era_array(*tm_,
|
2014-01-23 06:16:03 +05:30
|
|
|
uint32_traits::ref_counter(),
|
|
|
|
sb_.era_array_root,
|
|
|
|
sb_.nr_blocks));
|
|
|
|
}
|
|
|
|
|
2014-08-07 20:13:01 +05:30
|
|
|
void
|
|
|
|
metadata::commit()
|
|
|
|
{
|
|
|
|
commit_space_map();
|
|
|
|
commit_writesets();
|
|
|
|
commit_era_array();
|
|
|
|
commit_superblock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
metadata::commit_space_map()
|
|
|
|
{
|
|
|
|
metadata_sm_->commit();
|
|
|
|
metadata_sm_->copy_root(&sb_.metadata_space_map_root, sizeof(sb_.metadata_space_map_root));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
metadata::commit_writesets()
|
|
|
|
{
|
|
|
|
sb_.writeset_tree_root = writeset_tree_->get_root();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
metadata::commit_era_array()
|
|
|
|
{
|
|
|
|
sb_.era_array_root = era_array_->get_root();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
metadata::commit_superblock()
|
|
|
|
{
|
|
|
|
write_superblock(tm_->get_bm(), sb_);
|
|
|
|
}
|
|
|
|
|
2014-01-23 06:16:03 +05:30
|
|
|
//----------------------------------------------------------------
|