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
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
|
|
|
template <uint32_t BlockSize>
|
|
|
|
transaction_manager<BlockSize>::transaction_manager(typename block_manager<BlockSize>::ptr bm,
|
|
|
|
space_map::ptr sm)
|
|
|
|
: bm_(bm),
|
|
|
|
sm_(sm)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template <uint32_t BlockSize>
|
|
|
|
transaction_manager<BlockSize>::~transaction_manager()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template <uint32_t BlockSize>
|
2011-07-14 20:01:00 +05:30
|
|
|
typename transaction_manager<BlockSize>::write_ref
|
|
|
|
transaction_manager<BlockSize>::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
|
|
|
}
|
|
|
|
|
|
|
|
template <uint32_t BlockSize>
|
2011-07-14 20:01:00 +05:30
|
|
|
typename transaction_manager<BlockSize>::write_ref
|
|
|
|
transaction_manager<BlockSize>::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-07-14 20:01:00 +05:30
|
|
|
// FIXME: these explicit try/catches are gross
|
2011-06-23 19:17:08 +05:30
|
|
|
template <uint32_t BlockSize>
|
|
|
|
typename transaction_manager<BlockSize>::write_ref
|
|
|
|
transaction_manager<BlockSize>::new_block()
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
} catch (...) {
|
|
|
|
remove_shadow(b);
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (...) {
|
|
|
|
sm_->dec(b);
|
|
|
|
throw;
|
|
|
|
}
|
2011-06-23 19:17:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
template <uint32_t BlockSize>
|
|
|
|
typename transaction_manager<BlockSize>::write_ref
|
2011-07-14 20:01:00 +05:30
|
|
|
transaction_manager<BlockSize>::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-06-23 19:17:08 +05:30
|
|
|
template <uint32_t BlockSize>
|
2011-07-14 20:01:00 +05:30
|
|
|
pair<typename transaction_manager<BlockSize>::write_ref, bool>
|
|
|
|
transaction_manager<BlockSize>::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-07-14 20:01:00 +05:30
|
|
|
::memcpy(dest.data(), src.data(), BlockSize);
|
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-06-23 19:17:08 +05:30
|
|
|
template <uint32_t BlockSize>
|
2011-07-14 20:01:00 +05:30
|
|
|
pair<typename transaction_manager<BlockSize>::write_ref, bool>
|
|
|
|
transaction_manager<BlockSize>::shadow(block_address orig, validator v)
|
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, v);
|
|
|
|
write_ref dest = bm_->write_lock_zero(sm_->new_block(), v);
|
2011-06-23 19:17:08 +05:30
|
|
|
::memcpy(dest->data_, src->data_, BlockSize);
|
|
|
|
|
|
|
|
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-06-23 19:17:08 +05:30
|
|
|
add_shadow(dest->location_);
|
2011-07-14 20:01:00 +05:30
|
|
|
return make_pair(dest, count > 1);
|
2011-06-23 19:17:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
template <uint32_t BlockSize>
|
|
|
|
typename transaction_manager<BlockSize>::read_ref
|
|
|
|
transaction_manager<BlockSize>::read_lock(block_address b)
|
|
|
|
{
|
|
|
|
return bm_->read_lock(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <uint32_t BlockSize>
|
|
|
|
typename transaction_manager<BlockSize>::read_ref
|
2011-07-14 20:01:00 +05:30
|
|
|
transaction_manager<BlockSize>::read_lock(block_address b, validator v)
|
2011-06-23 19:17:08 +05:30
|
|
|
{
|
|
|
|
return bm_->read_lock(b, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <uint32_t BlockSize>
|
|
|
|
void
|
2011-07-14 20:01:00 +05:30
|
|
|
transaction_manager<BlockSize>::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
|
|
|
}
|
|
|
|
|
|
|
|
template <uint32_t BlockSize>
|
|
|
|
void
|
2011-07-14 20:01:00 +05:30
|
|
|
transaction_manager<BlockSize>::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
|
|
|
}
|
|
|
|
|
|
|
|
template <uint32_t BlockSize>
|
|
|
|
bool
|
2011-07-14 20:01:00 +05:30
|
|
|
transaction_manager<BlockSize>::is_shadow(block_address b) const
|
2011-06-23 19:17:08 +05:30
|
|
|
{
|
|
|
|
return shadows_.count(b) > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <uint32_t BlockSize>
|
|
|
|
void
|
|
|
|
transaction_manager<BlockSize>::wipe_shadow_table()
|
|
|
|
{
|
|
|
|
shadows_.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|