anchor.diff:           handle_sync_event/4, terminate/3]).
   
   %% States
  --export([check_clock/2, get_txs_to_check/2, check_committing/2, commit_notification/2, 
  -commit_ts_notification/2, check_prepared/2, commit_notification2/2, return/2]).
  +-export([check_clock/2,
  +	 get_txs_to_check/2,
  +	 commit_notification/2, 
  +	 return/2]).
   
   %% Spawn
  --export([bcast_get_commit_ts/2]).
   
   -record(state, {type,
   		key,
   	 	tx,
   		client,
   		vnode,
  -        committing_txs,
  -		left_committing,
  -		prepared_txs,
  -		left_prepared,
  -		left_acks}).
  +		pending_txs}).
   
   %%%===================================================================
   %%% API
  
positive.code:     end,
      {next_state, get_txs_to_check, SD0, 0}.
  
  get_txs_to_check(timeout, SD0=#state{tx=Tx}) ->
      T_snapshot_time = Tx#tx.snapshot_time,
      %Concurrent txs
      Txprimes=[Tx, Tx, Tx],
      {Committing, Pendings} = pending_commits(T_snapshot_time, Txprimes),
      {next_state, check_committing, SD0=#state{committing_txs=Committing, prepared_txs=Pendings}, 0}.
  
  check_committing(timeout, SD0=#state{committing_txs=Committing, vnode=Vnode}) ->
      riak_core_vnode_master:sync_command(Vnode, {notify_commit, Committing}, ?CLOCKSIMASTER),
      {next_state, commit_notification, SD0=#state{left_committing=Committing}}.
      
  commit_notification({committed, TxId}, SD0=#state{left_committing=Left}) ->
      Left2=clean_left(TxId, Left),
      if Left2==[] ->
      	{next_state, check_prepared, SD0=#state{left_committing=Left2}, 0};
      true->
      	{next_state, commit_notification, SD0=#state{left_committing=Left2}}
      end.
  
  check_prepared(timeout, SD0=#state{tx=Tx, prepared_txs=Pendings}) ->
      spawn(clockSI_readitem_fsm, bcast_get_commit_ts, [Pendings, Tx#tx.snapshot_time]),
      {next_state, commit_ts_notification, SD0=#state{left_acks=Pendings}, 0}.
  
  %Either already committed or t'_ts_commit > t_snapshot_time
  commit_ts_notification({forget, TxId}, SD0=#state{left_acks=Left, vnode=Vnode, left_committing=Wait}) ->
      Left2=clean_left(TxId, Left),
      if Left2==[] ->
      	riak_core_vnode_master:sync_command(Vnode, {notify_commit, Wait}, ?CLOCKSIMASTER),
      	{next_state, commit_notification2, SD0=#state{left_committing=Wait, left_acks=Left2}};
      true->
      	{next_state, commit_ts_notification, SD0=#state{left_acks=Left2}}
      end;
  
  %Tx not commited and t'_ts_commit < T_snapshot_time
  commit_ts_notification({wait, TxId}, SD0=#state{left_acks=Left, vnode=Vnode, left_committing=Wait}) ->
      Left2=clean_left(TxId, Left),
      Wait2=lists:append(Wait, [TxId]),
      if Left2==[] ->
      	riak_core_vnode_master:sync_command(Vnode, {notify_commit, Wait2}, ?CLOCKSIMASTER),
      	{next_state, commit_notification2, SD0=#state{left_committing=Wait2, left_acks=Left2}};
      true->
      	{next_state, commit_ts_notification, SD0=#state{left_acks=Left2, left_committing=Wait2}}
      end.
  
  commit_notification2({committed, TxId}, SD0=#state{left_committing=Left}) ->
      Left2=clean_left(TxId, Left),
      if Left2==[] ->
      	{next_state, return, SD0=#state{left_committing=Left2}, 0};
      true->
      	{next_state, commit_notification, SD0=#state{left_committing=Left2}}
      end.
  
  return(timeout, SD0=#state{client=Client, tx= Tx, key=Key, type=Type}) ->
  
negative.code: %%%===================================================================
  %%% Internal Functions
  %%%===================================================================
  clean_left(TxId, Left) ->
      internal_clean_left(TxId, Left, []).
  
  