anchor.diff:  -export([handle/2]).
   
   -record(state, {labels_queue :: queue(),
  -                ops_dict :: dict(),
  +                ops,
  +                queue_len,
                   myid}).
                  
   reg_name(MyId) ->  list_to_atom(integer_to_list(MyId) ++ atom_to_list(?MODULE)). 
  
positive.code:                 false ->
                      noop
              end,
              flush_queue(Rest, S0);
          update ->
              case dict:find(Label, Ops0) of
                  {ok, Value} ->
                      ok = execute_operation(Label, Value),
                      Ops1 = dict:erase(Label, Ops0),
                      flush_queue(Rest, S0#state{ops_dict=Ops1});
                  _ ->
                      lager:info("Operation not received for label: ~p", [Label]),
                      S0#state{labels_queue=queue:from_list(Labels)}
              end
      end.
  
  
negative.code:     gen_server:call({global, reg_name(MyId)}, Message, infinity).
  
  init([MyId]) ->
      Ops = ets:new(operations_converger, [set, named_table]),
      {ok, #state{labels_queue=queue:new(),
                  ops=Ops,
                  queue_len=0,
                  myid=MyId}}.
  
  handle_call({new_stream, Stream, _SenderId}, _From, S0=#state{labels_queue=Labels0, queue_len=QL0, ops=Ops, myid=MyId}) ->
      %lager:info("New stream received. Label: ~p", Stream),
      case QL0 of
          0 ->
              {Labels1, QL1} = flush_list(Stream, Ops, MyId),
              {reply, ok, S0#state{labels_queue=Labels1, queue_len=QL1}}; 
          _ ->
              {Labels1, Total} = lists:foldl(fun(Elem, Sum) ->
                                              {queue:in(Elem, Labels0), Sum + 1}
                                             end, 0, Stream),
              {reply, ok, S0#state{labels_queue=Labels1, queue_len=QL0+Total}}
      end;
  
  handle_call({new_operation, Label, Value}, _From, S0=#state{labels_queue=Labels0, ops=Ops, queue_len=QL0, myid=MyId}) ->
      %lager:info("New operation received. Label: ~p", [Label]),
      case queue:peek(Labels0) of
          {value, Label} ->
              ok = execute_operation(Label, Value),
              Labels1 = queue:drop(Labels0),
              {Labels2, QL1} = flush_queue(Labels1, QL0-1, Ops, MyId),
              {reply, ok, S0#state{labels_queue=Labels2, queue_len=QL1}};
          _ ->
              true = ets:insert(Ops, {Label, Value}),
              {reply, ok, S0}
      end.
  
  handle_cast(_Info, State) ->
      {noreply, State}.
  