Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

address chainbase uniqueness violation issues #44

Merged
merged 1 commit into from
May 6, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions include/chainbase/chainbase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,15 @@ namespace chainbase {
return *insert_result.first;
}

/**
* @pre modifier cannot change the object in such a way that causes a uniqueness violation for any unique indices
* @pre any modifications done within an undo session for a given generic_index must satisfy the condition that the compacted set of modifications in the session can be undone in any order without causing a uniqueness violation in any intermediate step
*/
template<typename Modifier>
void modify( const value_type& obj, Modifier&& m ) {
on_modify( obj );
auto ok = _indices.modify( _indices.iterator_to( obj ), m );
if( !ok ) BOOST_THROW_EXCEPTION( std::logic_error( "Could not modify object, most likely a uniqueness constraint was violated" ) );
if( !ok ) std::abort(); // uniqueness violation
}

void remove( const value_type& obj ) {
Expand Down Expand Up @@ -309,28 +313,31 @@ namespace chainbase {
/**
* Restores the state to how it was prior to the current session discarding all changes
* made between the last revision and the current revision.
*
* This function will not throw an exception but will abort if a uniqueness constraint violation
* is encountered while undoing, likely because of a prior violation of the preconditions of modify.
*/
void undo() {
if( !enabled() ) return;

const auto& head = _stack.back();

for( auto& item : head.old_values ) {
auto ok = _indices.modify( _indices.find( item.second.id ), [&]( value_type& v ) {
v = std::move( item.second );
});
if( !ok ) BOOST_THROW_EXCEPTION( std::logic_error( "Could not modify object, most likely a uniqueness constraint was violated" ) );
}

for( auto id : head.new_ids )
{
_indices.erase( _indices.find( id ) );
}
_next_id = head.old_next_id;

for( auto& item : head.old_values ) {
auto ok = _indices.modify( _indices.find( item.second.id ), [&]( value_type& v ) {
v = std::move( item.second );
});
if( !ok ) std::abort(); // uniqueness violation
}

for( auto& item : head.removed_values ) {
bool ok = _indices.emplace( std::move( item.second ) ).second;
if( !ok ) BOOST_THROW_EXCEPTION( std::logic_error( "Could not restore object, most likely a uniqueness constraint was violated" ) );
if( !ok ) std::abort(); // uniqueness violation
}

_stack.pop_back();
Expand Down