anchor.diff:  %% So can add a stop function to ordered_filter
   %% Or can have the filter function return a tuple, one vale for stopping
   %% one for including
  -    Res = lists:filter(fun({_OpId,Op}) ->
  -			       OpCommitTime=Op#clocksi_payload.commit_time,
  -			       (belongs_to_snapshot_op(Threshold,OpCommitTime,Op#clocksi_payload.snapshot_time))
  -		       end, OpsDict),
  -    NewOps = case Res of
  -		 [] ->
  -		     [First|_Rest] = OpsDict,
  -		     [First];
  -		 _ ->
  -		     Res
  -	     end,
  -    {length(NewOps),NewOps}.
  +    Res = reverse_and_filter(fun({_OpId,Op}) ->
  +				     OpCommitTime=Op#clocksi_payload.commit_time,
  +				     (belongs_to_snapshot_op(Threshold,OpCommitTime,Op#clocksi_payload.snapshot_time))
  +			     end, OpsDict, ?FIRST_OP, []),
  +    case Res of
  +	{_,[]} ->
  +	    [First|_Rest] = OpsDict,
  +	    {1,[{?FIRST_OP,First}]};
  +	_ ->
  +	    Res
  +    end.
  +
  +%% This is an internal function used to convert the tuple stored in ets
  +%% to a tuple and list usable by the materializer
  +-spec tuple_to_key(tuple()) -> {any(),non_neg_integer(),non_neg_integer(),list()}.
  +tuple_to_key(Tuple) ->
  +    Key = element(1, Tuple),
  +    Length = element(2, Tuple),
  +    OpId = element(3, Tuple),
  +    Ops = tuple_to_key_int(?FIRST_OP,Length+?FIRST_OP,Tuple,[]),
  +    {Key,Length,OpId,Ops}.
  +tuple_to_key_int(Next,Next,_Tuple,Acc) ->
  +    Acc;
  +tuple_to_key_int(Next,Last,Tuple,Acc) ->
  +    tuple_to_key_int(Next+1,Last,Tuple,[element(Next,Tuple)|Acc]).
  +
  +%% This is an internal function used to filter ops and reverse the list
  +%% It returns a tuple where the first element is the lenght of the list returned
  +%% The elements in the list also include the location that they will be placed
  +%% in the tuple in the ets table, this way the list can be used
  +%% directly in the erlang:make_tuple function
  +-spec reverse_and_filter(fun(),list(),non_neg_integer(),list()) -> {non_neg_integer(),list()}.
  +reverse_and_filter(_Fun,[],Id,Acc) ->
  +    {Id-?FIRST_OP,Acc};
  +reverse_and_filter(Fun,[First|Rest],Id,Acc) ->
  +    case Fun(First) of
  +	true ->
  +	    reverse_and_filter(Fun,Rest,Id+1,[{Id,First}|Acc]);
  +	false ->
  +	    reverse_and_filter(Fun,Rest,Id,Acc)
  +    end.
   
   %% @doc Insert an operation and start garbage collection triggered by writes.
   %% the mechanism is very simple; when there are more than OPS_THRESHOLD
  
positive.code: op_insert_gc(Key, DownstreamOp, OpsCache, SnapshotCache)->
      case ets:member(OpsCache, Key) of
  	false ->
  	    ets:insert(OpsCache, erlang:make_tuple(4+?OPS_THRESHOLD,0,[{1,Key}]));
  	true ->
  	    ok
      end,
  
negative.code:             {_, _} = internal_read(Key, Type, SnapshotTime, ignore, OpsCache, SnapshotCache, true),
  	    %% Have to get the new ops dict because the interal_read can change it
  	    Length1 = ets:lookup_element(OpsCache, Key, 2),
  	    true = ets:update_element(OpsCache, Key, [{Length1+?FIRST_OP,{NewId,DownstreamOp}}, {2,Length1+1}]);
          false ->
  	    true = ets:update_element(OpsCache, Key, [{Length+?FIRST_OP,{NewId,DownstreamOp}}, {2,Length+1}])
      end.
  
  
  