anchor.diff:      end.
   
   delete_db_doc(Db, DocId) ->
  -    case couch_db:open_doc(Db, DocId, []) of
  -    {not_found, _} ->
  -        ok;
  -    {ok, OldDoc} ->
  -        {ok, _} = couch_db:update_doc(Db, OldDoc#doc{deleted=true}, [])
  -    end.
  +    {ok, Revs} = couch_db:open_doc_revs(Db, DocId, all, []),
  +    Docs = [Doc#doc{deleted=true} || {ok, #doc{deleted=false}=Doc} <- Revs],
  +    couch_db:update_docs(Db, Docs, []),
  +    ok.
   
   build_shards(DbName, DocProps) ->
       {ByNode} = couch_util:get_value(<<"by_node">>, DocProps, {[]}),
  
positive.code: update_db_doc(Db, #doc{id=Id, body=Body} = Doc) ->
      case couch_db:open_doc(Db, Id, []) of
      {not_found, _} ->
          {ok, _} = couch_db:update_doc(Db, Doc, []);
      {ok, #doc{body=Body}} ->
          ok;
      {ok, OldDoc} ->
          {ok, _} = couch_db:update_doc(Db, OldDoc#doc{body=Body}, [])
      end.
  
  delete_db_doc(DocId) ->
  
negative.code:     try
          update_db_doc(Db, Doc)
      catch conflict ->
          % one more check to detect a real conflict
          #doc{id=Id, body=Body} = Doc,
          case couch_db:open_doc(Db, Id, []) of
          {ok, #doc{body=Body}} ->
              % assume this is a race with a replication
              ok;
          {ok, _} ->
              conflict
          end
      after
          couch_db:close(Db)
      end.
  