Merge branch 'graphene/undo-patch' into bitshares
This commit is contained in:
commit
844df7a7d4
1 changed files with 92 additions and 6 deletions
|
|
@ -127,25 +127,111 @@ void undo_database::merge()
|
||||||
FC_ASSERT( _stack.size() >=2 );
|
FC_ASSERT( _stack.size() >=2 );
|
||||||
auto& state = _stack.back();
|
auto& state = _stack.back();
|
||||||
auto& prev_state = _stack[_stack.size()-2];
|
auto& prev_state = _stack[_stack.size()-2];
|
||||||
|
|
||||||
|
// An object's relationship to a state can be:
|
||||||
|
// in new_ids : new
|
||||||
|
// in old_values (was=X) : upd(was=X)
|
||||||
|
// in removed (was=X) : del(was=X)
|
||||||
|
// not in any of above : nop
|
||||||
|
//
|
||||||
|
// When merging A=prev_state and B=state we have a 4x4 matrix of all possibilities:
|
||||||
|
//
|
||||||
|
// |--------------------- B ----------------------|
|
||||||
|
//
|
||||||
|
// +------------+------------+------------+------------+
|
||||||
|
// | new | upd(was=Y) | del(was=Y) | nop |
|
||||||
|
// +------------+------------+------------+------------+------------+
|
||||||
|
// / | new | N/A | new A| nop C| new A|
|
||||||
|
// | +------------+------------+------------+------------+------------+
|
||||||
|
// | | upd(was=X) | N/A | upd(was=X)A| del(was=X)C| upd(was=X)A|
|
||||||
|
// A +------------+------------+------------+------------+------------+
|
||||||
|
// | | del(was=X) | N/A | N/A | N/A | del(was=X)A|
|
||||||
|
// | +------------+------------+------------+------------+------------+
|
||||||
|
// \ | nop | new B| upd(was=Y)B| del(was=Y)B| nop AB|
|
||||||
|
// +------------+------------+------------+------------+------------+
|
||||||
|
//
|
||||||
|
// Each entry was composed by labelling what should occur in the given case.
|
||||||
|
//
|
||||||
|
// Type A means the composition of states contains the same entry as the first of the two merged states for that object.
|
||||||
|
// Type B means the composition of states contains the same entry as the second of the two merged states for that object.
|
||||||
|
// Type C means the composition of states contains an entry different from either of the merged states for that object.
|
||||||
|
// Type N/A means the composition of states violates causal timing.
|
||||||
|
// Type AB means both type A and type B simultaneously.
|
||||||
|
//
|
||||||
|
// The merge() operation is defined as modifying prev_state in-place to be the state object which represents the composition of
|
||||||
|
// state A and B.
|
||||||
|
//
|
||||||
|
// Type A (and AB) can be implemented as a no-op; prev_state already contains the correct value for the merged state.
|
||||||
|
// Type B (and AB) can be implemented by copying from state to prev_state.
|
||||||
|
// Type C needs special case-by-case logic.
|
||||||
|
// Type N/A can be ignored or assert(false) as it can only occur if prev_state and state have illegal values
|
||||||
|
// (a serious logic error which should never happen).
|
||||||
|
//
|
||||||
|
|
||||||
|
// We can only be outside type A/AB (the nop path) if B is not nop, so it suffices to iterate through B's three containers.
|
||||||
|
|
||||||
|
// *+upd
|
||||||
for( auto& obj : state.old_values )
|
for( auto& obj : state.old_values )
|
||||||
{
|
{
|
||||||
if( prev_state.new_ids.find(obj.second->id) != prev_state.new_ids.end() )
|
if( prev_state.new_ids.find(obj.second->id) != prev_state.new_ids.end() )
|
||||||
|
{
|
||||||
|
// new+upd -> new, type A
|
||||||
continue;
|
continue;
|
||||||
if( prev_state.old_values.find(obj.second->id) == prev_state.old_values.end() )
|
}
|
||||||
prev_state.old_values[obj.second->id] = std::move(obj.second);
|
if( prev_state.old_values.find(obj.second->id) != prev_state.old_values.end() )
|
||||||
|
{
|
||||||
|
// upd(was=X) + upd(was=Y) -> upd(was=X), type A
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
// del+upd -> N/A
|
||||||
|
assert( prev_state.removed.find(obj.second->id) == prev_state.removed.end() );
|
||||||
|
// nop+upd(was=Y) -> upd(was=Y), type B
|
||||||
|
prev_state.old_values[obj.second->id] = std::move(obj.second);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// *+new, but we assume the N/A cases don't happen, leaving type B nop+new -> new
|
||||||
for( auto id : state.new_ids )
|
for( auto id : state.new_ids )
|
||||||
prev_state.new_ids.insert(id);
|
prev_state.new_ids.insert(id);
|
||||||
|
|
||||||
|
// old_index_next_ids can only be updated, iterate over *+upd cases
|
||||||
for( auto& item : state.old_index_next_ids )
|
for( auto& item : state.old_index_next_ids )
|
||||||
{
|
{
|
||||||
if( prev_state.old_index_next_ids.find( item.first ) == prev_state.old_index_next_ids.end() )
|
if( prev_state.old_index_next_ids.find( item.first ) == prev_state.old_index_next_ids.end() )
|
||||||
|
{
|
||||||
|
// nop+upd(was=Y) -> upd(was=Y), type B
|
||||||
prev_state.old_index_next_ids[item.first] = item.second;
|
prev_state.old_index_next_ids[item.first] = item.second;
|
||||||
}
|
continue;
|
||||||
for( auto& obj : state.removed )
|
}
|
||||||
if( prev_state.new_ids.find(obj.second->id) == prev_state.new_ids.end() )
|
|
||||||
prev_state.removed[obj.second->id] = std::move(obj.second);
|
|
||||||
else
|
else
|
||||||
|
{
|
||||||
|
// upd(was=X)+upd(was=Y) -> upd(was=X), type A
|
||||||
|
// type A implementation is a no-op, as discussed above, so there is no code here
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// *+del
|
||||||
|
for( auto& obj : state.removed )
|
||||||
|
{
|
||||||
|
if( prev_state.new_ids.find(obj.second->id) != prev_state.new_ids.end() )
|
||||||
|
{
|
||||||
|
// new + del -> nop (type C)
|
||||||
prev_state.new_ids.erase(obj.second->id);
|
prev_state.new_ids.erase(obj.second->id);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
auto it = prev_state.old_values.find(obj.second->id);
|
||||||
|
if( it != prev_state.old_values.end() )
|
||||||
|
{
|
||||||
|
// upd(was=X) + del(was=Y) -> del(was=X)
|
||||||
|
prev_state.removed[obj.second->id] = std::move(it->second);
|
||||||
|
prev_state.old_values.erase(obj.second->id);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
// del + del -> N/A
|
||||||
|
assert( prev_state.removed.find( obj.second->id ) == prev_state.removed.end() );
|
||||||
|
// nop + del(was=Y) -> del(was=Y)
|
||||||
|
prev_state.removed[obj.second->id] = std::move(obj.second);
|
||||||
|
}
|
||||||
_stack.pop_back();
|
_stack.pop_back();
|
||||||
--_active_sessions;
|
--_active_sessions;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue