anchor.diff:              lists:member(N, Live)]
       catch error:database_does_not_exist ->
           ok
  -    end,
  -    {ok, St};
  -handle_event(<<"shards/", _:18/binary, _/binary>> = ShardName, deleted, St) ->
  -    mem3_sync:remove_shard(ShardName),
  -    {ok, St};
  -handle_event(_DbName, _Event, St) ->
  -    {ok, St}.
  -
  -handle_cast(Msg, St) ->
  -    couch_log:notice("unexpected cast to mem3_sync_event_listener: ~p", [Msg]),
  -    {ok, St}.
  -
  -handle_info(Msg, St) ->
  -    couch_log:notice("unexpected info to mem3_sync_event_listener: ~p", [Msg]),
  -    {ok, St}.
  +    end.
  
positive.code: 
  -module(mem3_sync_event_listener).
  -behavior(couch_event_listener).
  
  -export([
      start_link/0
  
negative.code:     handle_info/2
  ]).
  
  -export([
      handle_config_change/5,
      handle_config_terminate/3
  ]).
  
  -include_lib("mem3/include/mem3.hrl").
  
  -record(state, {
      nodes,
      shards,
      users,
      delay,
      frequency,
      last_push,
      buckets
  }).
  
  %% Calling mem3_sync:push/2 on every update has a measurable performance cost,
  %% so we'd like to coalesce multiple update messages from couch_event in to a
  %% single push call. Doing this while ensuring both correctness (i.e., no lost
  %% updates) and an even load profile is somewhat subtle. This implementation
  %% groups updated shards in a list of "buckets" (see bucket_shard/2) and
  %% guarantees that each shard is in no more than one bucket at a time - i.e.,
  %% any update messages received before the shard's current bucket has been
  %% pushed will be ignored - thereby reducing the frequency with which a single
  %% shard will be pushed. mem3_sync:push/2 is called on all shards in the
  %% *oldest* bucket roughly every mem3.sync_frequency milliseconds (see
  %% maybe_push_shards/1) to even out the load on mem3_sync.
  
  start_link() ->
      couch_event_listener:start_link(?MODULE, [], [all_dbs]).
  
  init(_) ->
      config:listen_for_changes(?MODULE, undefined),
      Delay = config:get_integer("mem3", "sync_delay", 5000),
      Frequency = config:get_integer("mem3", "sync_frequency", 500),
      Buckets = lists:duplicate(Delay div Frequency + 1, sets:new()),
      St = #state{
          nodes = mem3_sync:nodes_db(),
          shards = mem3_sync:shards_db(),
          users = mem3_sync:users_db(),
          delay = Delay,
          frequency = Frequency,
          buckets = Buckets
      },
      {ok, St}.
  
  terminate(_Reason, _State) ->
      ok.
  