Skip to content

Commit

Permalink
add support for include_deleted: true option in a design document.
Browse files Browse the repository at this point in the history
This option allows a view to map deleted documents.

Remember that documents deleted with the DELETE HTTP verb will look like
{_id: id, _rev: rev, _deleted: true} to the indexer. If you want to
store extra data on the deleted document you can use _bulk_docs or
updating a document with the member '_deleted: true' using the HTTP verb
PUT.
  • Loading branch information
benoitc committed May 11, 2012
1 parent 282671e commit e4b75f6
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 12 deletions.
4 changes: 3 additions & 1 deletion apps/couch_index/src/couch_index_updater.erl
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,13 @@ update(Idx, Mod, IdxState) ->
UpdateOpts = Mod:get(update_options, IdxState),
CommittedOnly = lists:member(committed_only, UpdateOpts),
IncludeDesign = lists:member(include_design, UpdateOpts),
IncludeDeleted = lists:member(include_deleted, UpdateOpts),
DocOpts = case lists:member(local_seq, UpdateOpts) of
true -> [conflicts, deleted_conflicts, local_seq];
_ -> [conflicts, deleted_conflicts]
end,


couch_util:with_db(DbName, fun(Db) ->
DbUpdateSeq = couch_db:get_update_seq(Db),
DbCommittedSeq = couch_db:get_committed_update_seq(Db),
Expand All @@ -146,7 +148,7 @@ update(Idx, Mod, IdxState) ->
case {IncludeDesign, DocId} of
{false, <<"_design/", _/binary>>} ->
{nil, Seq};
_ when Deleted ->
_ when Deleted, IncludeDeleted /= true ->
{#doc{id=DocId, deleted=true}, Seq};
_ ->
{ok, Doc} = couch_db:open_doc_int(Db, DocInfo, DocOpts),
Expand Down
1 change: 1 addition & 0 deletions apps/couch_mrview/include/couch_mrview.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
language,
design_opts=[],
seq_indexed=false,
include_deleted=false,
lib,
views,
id_btree=nil,
Expand Down
1 change: 0 additions & 1 deletion apps/couch_mrview/src/couch_mrview.erl
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ view_changes_since(Db, DDoc, VName, StartSeq, Callback, Args, Acc) ->
VName, Args1),

#mrst{seq_indexed=SeqIndexed, seq_btree=SeqBtree} = State,

case SeqIndexed of
false ->
{error, seqs_not_indexed};
Expand Down
5 changes: 4 additions & 1 deletion apps/couch_mrview/src/couch_mrview_index.erl
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@ get(Property, State) ->
Opts = State#mrst.design_opts,
IncDesign = couch_util:get_value(<<"include_design">>, Opts, false),
LocalSeq = couch_util:get_value(<<"local_seq">>, Opts, false),
IncludeDeleted = couch_util:get_value(<<"include_deleted">>,
Opts, false),
if IncDesign -> [include_design]; true -> [] end
++ if LocalSeq -> [local_seq]; true -> [] end;
++ if LocalSeq -> [local_seq]; true -> [] end
++ if IncludeDeleted -> [include_deleted]; true -> [] end;
info ->
#mrst{
fd = Fd,
Expand Down
15 changes: 7 additions & 8 deletions apps/couch_mrview/src/couch_mrview_updater.erl
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ purge(_Db, PurgeSeq, PurgedIdRevs, State) ->
true ->
PurgeSeqs = [{ViewId, Seq} || {{ViewId, Seq}, _Id} <-
couch_mrview_util:to_seqkvs(Lookups, [])],

{ok, SeqBtree1} = couch_btree:add_remove(SeqBtree, [], PurgeSeqs),
SeqBtree1;
_ ->
Expand Down Expand Up @@ -112,7 +111,8 @@ process_doc(Doc, Seq, #mrst{doc_acc=Acc}=State) when length(Acc) > 100 ->
process_doc(Doc, Seq, State#mrst{doc_acc=[]});
process_doc(nil, Seq, #mrst{doc_acc=Acc}=State) ->
{ok, State#mrst{doc_acc=[{nil, Seq, nil} | Acc]}};
process_doc(#doc{id=Id, deleted=true}, Seq, #mrst{doc_acc=Acc}=State) ->
process_doc(#doc{id=Id, deleted=true}, Seq, #mrst{doc_acc=Acc,
include_deleted=false}=State) ->
{ok, State#mrst{doc_acc=[{Id, Seq, deleted} | Acc]}};
process_doc(#doc{id=Id}=Doc, Seq, #mrst{doc_acc=Acc}=State) ->
{ok, State#mrst{doc_acc=[{Id, Seq, Doc} | Acc]}}.
Expand Down Expand Up @@ -254,16 +254,15 @@ write_kvs(State, UpdateSeq, ViewKVs, DocIdKeys) ->
first_build=FirstBuild
} = State,

{ok, ToRemove, IdBtree2} = update_id_btree(IdBtree, DocIdKeys, FirstBuild),

SeqBtree2 = case SeqIndexed of
{ok, ToRemove, IdBtree2} = update_id_btree(IdBtree, DocIdKeys, FirstBuild),
{ok, SeqBtree2} = case SeqIndexed of
true ->
ToRemBySeq = [{ViewId, Seq} || {{ViewId, Seq}, _Id} <-
couch_mrview_util:to_seqkvs(ToRemove, [])],
{ok, SeqBtree1} = update_seq_btree(SeqBtree, DocIdKeys,
ToRemBySeq),
SeqBtree1;
_ -> nil
update_seq_btree(SeqBtree, DocIdKeys, ToRemBySeq);
_ ->
{ok, nil}
end,

ToRemByView = collapse_rem_keys(ToRemove, dict:new()),
Expand Down
7 changes: 6 additions & 1 deletion apps/couch_mrview/src/couch_mrview_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ ddoc_to_mrst(DbName, #doc{id=Id, body={Fields}}) ->
Lib = couch_util:get_value(<<"lib">>, RawViews, {[]}),
SeqIndexed = couch_util:get_value(<<"seq_indexed">>, DesignOpts,
false),
IncludeDeleted = couch_util:get_value(<<"include_deleted">>, DesignOpts,
false),

IdxState = #mrst{
db_name=DbName,
Expand All @@ -109,7 +111,8 @@ ddoc_to_mrst(DbName, #doc{id=Id, body={Fields}}) ->
views=Views,
language=Language,
design_opts=DesignOpts,
seq_indexed=SeqIndexed
seq_indexed=SeqIndexed,
include_deleted=IncludeDeleted
},
SigInfo = {?MRFMT, Views, Language, DesignOpts,
couch_index_util:sort_lib(Lib)},
Expand Down Expand Up @@ -741,6 +744,8 @@ to_seqkvs([], Acc) ->
lists:reverse(Acc);
to_seqkvs([{not_found, _} | Rest], Acc) ->
to_seqkvs(Rest, Acc);
to_seqkvs([{ok, DocIdKeys} | Rest], Acc) ->
to_seqkvs([DocIdKeys | Rest], Acc);
to_seqkvs([{_Id, {_Seq, []}} | Rest], Acc) ->
to_seqkvs(Rest, Acc);
to_seqkvs([{Id, {Seq, Keys}} | Rest], Acc0) ->
Expand Down

0 comments on commit e4b75f6

Please sign in to comment.