anchor.diff:               {passive, Passive}]
       end.
   
  -maybe_define_group(Bucket, BucketConfig, DDocId, Map) ->
  +define_group(Bucket, BucketConfig, DDocId, Map) ->
       SetName = list_to_binary(Bucket),
       NumVBuckets = proplists:get_value(num_vbuckets, BucketConfig),
   
       Active = proplists:get_value(active, Map),
       Passive = proplists:get_value(passive, Map),
   
  -    case couch_set_view:is_view_defined(SetName, DDocId) of
  -        true ->
  +    Params = #set_view_params{max_partitions=NumVBuckets,
  +                              active_partitions=Active,
  +                              passive_partitions=Passive},
  +
  +    try
  +        ok = couch_set_view:define_group(SetName, DDocId, Params)
  +    catch
  +        throw:{not_found, deleted} ->
  +            %% The document has been deleted but we still think it's
  +            %% alive. Eventually we will get a notification from master db
  +            %% watcher and delete it from a list of design documents.
  +            ok
  +    end.
  +
  +apply_ddoc_map(Bucket, DDocId, Active, Passive, ToRemove) ->
  +    SetName = list_to_binary(Bucket),
  +
  +    try
  +        ok = couch_set_view:set_partition_states(SetName, DDocId,
  +                                                 Active, Passive, ToRemove)
  +    catch
  +        throw:{not_found, deleted} ->
  +            %% The document has been deleted but we still think it's
  +            %% alive. Eventually we will get a notification from master db
  +            %% watcher and delete it from a list of design documents.
               ok;
  -        false ->
  -            Params = #set_view_params{max_partitions=NumVBuckets,
  -                                      active_partitions=Active,
  -                                      passive_partitions=Passive},
  -            ok = couch_set_view:define_group(SetName, DDocId, Params)
  -    end.
  -
  -apply_ddoc_map(Bucket, DDocId, NumVBuckets, Active, Passive, ToRemove) ->
  -    SetName = list_to_binary(Bucket),
  -
  -    case couch_set_view:is_view_defined(SetName, DDocId) of
  -        true ->
  -            ok = couch_set_view:set_partition_states(SetName, DDocId,
  -                                                     Active, Passive, ToRemove);
  -        false ->
  -            Params = #set_view_params{max_partitions=NumVBuckets,
  -                                      active_partitions=Active,
  -                                      passive_partitions=Passive},
  -            ok = couch_set_view:define_group(SetName, DDocId, Params)
  -    end.
  -
  -apply_map(Bucket, BucketConfig, OldMap, NewMap) ->
  -    DDocDbName = master_db(Bucket),
  -    NumVBuckets = proplists:get_value(num_vbuckets, BucketConfig),
  -
  -    {ok, DDocDb} = couch_db:open_int(DDocDbName, []),
  -
  -    {ok, DDocs} =
  -        try
  -            couch_db:get_design_docs(DDocDb)
  -        after
  -            couch_db:close(DDocDb)
  -        end,
  -
  +        throw:view_undefined ->
  +            %% Design document has been updated but we have not reacted on it
  +            %% yet. As previously eventually we will get a notification about
  +            %% this change and define a group again.
  +            ok
  +    end.
  +
  +apply_map(Bucket, DDocs, OldMap, NewMap) ->
       ?log_debug("Applying map ~p to bucket ~s", [NewMap, Bucket]),
   
       Active = proplists:get_value(active, NewMap),
  
positive.code: 
      ?log_debug("Partitions to remove: ~p", [ToRemove]),
  
      lists:foreach(
        fun (#doc{id = DDocId} = _DDoc) ->
                apply_ddoc_map(Bucket, DDocId, NumVBuckets,
                               Active, Passive, ToRemove)
        end, DDocs).
  
  get_vbucket_state(Bucket, VBucket) ->
      do_get_vbucket_state(list_to_binary(Bucket), VBucket).
  
negative.code:                 vbucket_states,
                  master_db_watcher,
                  map,
                  ddocs,
                  pending_vbucket_states}).
  
  start_link(Bucket) ->
  