2011-06-23 19:17:08 +05:30
|
|
|
#include "transaction_manager.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
using namespace boost;
|
|
|
|
using namespace persistent_data;
|
2011-07-14 20:01:00 +05:30
|
|
|
using namespace std;
|
2011-06-23 19:17:08 +05:30
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
2011-09-01 15:12:57 +05:30
|
|
|
transaction_manager::transaction_manager(block_manager<>::ptr bm,
|
2011-08-31 17:18:41 +05:30
|
|
|
space_map::ptr sm)
|
2011-06-23 19:17:08 +05:30
|
|
|
: bm_(bm),
|
|
|
|
sm_(sm)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
transaction_manager::~transaction_manager()
|
2011-06-23 19:17:08 +05:30
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
transaction_manager::write_ref
|
|
|
|
transaction_manager::begin(block_address superblock)
|
2011-06-23 19:17:08 +05:30
|
|
|
{
|
2011-08-24 18:57:45 +05:30
|
|
|
write_ref wr = bm_->superblock(superblock);
|
2011-06-23 19:17:08 +05:30
|
|
|
wipe_shadow_table();
|
2011-07-14 20:01:00 +05:30
|
|
|
return wr;
|
2011-06-23 19:17:08 +05:30
|
|
|
}
|
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
transaction_manager::write_ref
|
|
|
|
transaction_manager::begin(block_address superblock, validator v)
|
2011-06-23 19:17:08 +05:30
|
|
|
{
|
2011-08-24 18:57:45 +05:30
|
|
|
write_ref wr = bm_->superblock(superblock, v);
|
2011-07-14 20:01:00 +05:30
|
|
|
wipe_shadow_table();
|
|
|
|
return wr;
|
2011-06-23 19:17:08 +05:30
|
|
|
}
|
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
transaction_manager::write_ref
|
|
|
|
transaction_manager::new_block(validator v)
|
2011-06-23 19:17:08 +05:30
|
|
|
{
|
|
|
|
block_address b = sm_->new_block();
|
2011-07-14 20:01:00 +05:30
|
|
|
try {
|
|
|
|
add_shadow(b);
|
|
|
|
try {
|
|
|
|
return bm_->write_lock_zero(b, v);
|
|
|
|
} catch (...) {
|
|
|
|
remove_shadow(b);
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
} catch (...) {
|
|
|
|
sm_->dec(b);
|
|
|
|
throw;
|
|
|
|
}
|
2011-06-23 19:17:08 +05:30
|
|
|
}
|
|
|
|
|
2011-07-14 20:01:00 +05:30
|
|
|
// FIXME: make exception safe
|
2011-08-31 17:18:41 +05:30
|
|
|
pair<transaction_manager::write_ref, bool>
|
|
|
|
transaction_manager::shadow(block_address orig)
|
2011-06-23 19:17:08 +05:30
|
|
|
{
|
|
|
|
if (is_shadow(orig) &&
|
2011-07-14 20:01:00 +05:30
|
|
|
!sm_->count_possibly_greater_than_one(orig))
|
|
|
|
return make_pair(bm_->write_lock(orig), false);
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2011-08-24 18:57:45 +05:30
|
|
|
read_ref src = bm_->read_lock(orig);
|
|
|
|
write_ref dest = bm_->write_lock_zero(sm_->new_block());
|
2011-08-31 17:18:41 +05:30
|
|
|
::memcpy(dest.data(), src.data(), MD_BLOCK_SIZE);
|
2011-06-23 19:17:08 +05:30
|
|
|
|
|
|
|
ref_t count = sm_->get_count(orig);
|
2011-07-14 20:01:00 +05:30
|
|
|
if (count == 0)
|
|
|
|
throw runtime_error("shadowing free block");
|
|
|
|
sm_->dec(orig);
|
|
|
|
add_shadow(dest.get_location());
|
|
|
|
return make_pair(dest, count > 1);
|
2011-06-23 19:17:08 +05:30
|
|
|
}
|
|
|
|
|
2011-07-14 20:01:00 +05:30
|
|
|
// FIXME: duplicate code
|
2011-08-31 17:18:41 +05:30
|
|
|
pair<transaction_manager::write_ref, bool>
|
|
|
|
transaction_manager::shadow(block_address orig, validator v)
|
2011-06-23 19:17:08 +05:30
|
|
|
{
|
|
|
|
if (is_shadow(orig) &&
|
2011-11-03 20:14:00 +05:30
|
|
|
!sm_->count_possibly_greater_than_one(orig))
|
|
|
|
return make_pair(bm_->write_lock(orig, v), false);
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2011-08-24 18:57:45 +05:30
|
|
|
read_ref src = bm_->read_lock(orig, v);
|
|
|
|
write_ref dest = bm_->write_lock_zero(sm_->new_block(), v);
|
2011-08-31 17:18:41 +05:30
|
|
|
::memcpy(dest.data(), src.data(), MD_BLOCK_SIZE);
|
2011-06-23 19:17:08 +05:30
|
|
|
|
|
|
|
ref_t count = sm_->get_count(orig);
|
2011-07-14 20:01:00 +05:30
|
|
|
if (count == 0)
|
|
|
|
throw runtime_error("shadowing free block");
|
|
|
|
sm_->dec(orig);
|
2011-08-31 17:18:41 +05:30
|
|
|
add_shadow(dest.get_location());
|
2011-07-14 20:01:00 +05:30
|
|
|
return make_pair(dest, count > 1);
|
2011-06-23 19:17:08 +05:30
|
|
|
}
|
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
transaction_manager::read_ref
|
|
|
|
transaction_manager::read_lock(block_address b)
|
2011-06-23 19:17:08 +05:30
|
|
|
{
|
|
|
|
return bm_->read_lock(b);
|
|
|
|
}
|
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
transaction_manager::read_ref
|
|
|
|
transaction_manager::read_lock(block_address b, validator v)
|
2011-06-23 19:17:08 +05:30
|
|
|
{
|
|
|
|
return bm_->read_lock(b, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-08-31 17:18:41 +05:30
|
|
|
transaction_manager::add_shadow(block_address b)
|
2011-06-23 19:17:08 +05:30
|
|
|
{
|
2011-07-14 20:01:00 +05:30
|
|
|
shadows_.insert(b);
|
2011-06-23 19:17:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-08-31 17:18:41 +05:30
|
|
|
transaction_manager::remove_shadow(block_address b)
|
2011-06-23 19:17:08 +05:30
|
|
|
{
|
2011-07-14 20:01:00 +05:30
|
|
|
shadows_.erase(b);
|
2011-06-23 19:17:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2011-08-31 17:18:41 +05:30
|
|
|
transaction_manager::is_shadow(block_address b) const
|
2011-06-23 19:17:08 +05:30
|
|
|
{
|
|
|
|
return shadows_.count(b) > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-08-31 17:18:41 +05:30
|
|
|
transaction_manager::wipe_shadow_table()
|
2011-06-23 19:17:08 +05:30
|
|
|
{
|
|
|
|
shadows_.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|