view changes: handle case where the doc is deleted
With this change we also emit a changes when the doc is removed from the
view.
diff --git a/src/couch_mrview_updater.erl b/src/couch_mrview_updater.erl
index d6f2713..4d51f78 100644
--- a/src/couch_mrview_updater.erl
+++ b/src/couch_mrview_updater.erl
@@ -254,10 +254,13 @@
log_removed([], _, Log) ->
Log;
log_removed([{ok, {DocId, VIdKeys}} | Rest], Seq, Log) ->
- Log2 = lists:foldl(fun({Id, Keys}, Log1) ->
+ Log2 = lists:foldl(fun
+ ({Id, Keys}, Log1) when is_list(Keys) ->
lists:foldl(fun(Key, Log3) ->
dict:append(DocId, {Id, {Key, Seq, del}}, Log3)
- end, Log1, Keys)
+ end, Log1, Keys);
+ ({Id, Key}, Log1) ->
+ dict:append(DocId, {Id, {Key, Seq, del}}, Log1)
end, Log, VIdKeys),
log_removed(Rest, Seq, Log2);
log_removed([_ | Rest], Seq, Log) ->
@@ -366,7 +369,7 @@
RemValue = {[{<<"_removed">>, true}]},
{Log1, AddAcc, DelAcc} = walk_log(Btree, fun({DocId, VIdKeys}, {Log2, AddAcc2, DelAcc2}) ->
- {Log3, AddAcc3, DelAcc3} = lists:foldl(fun({ViewId,{Key, Seq, Op}},
+ {Log3, AddAcc3, DelAcc3} = lists:foldl(fun({ViewId,{Key, Seq,_Op}},
{Log4, AddAcc4, DelAcc4}) ->
IsUpdated = lists:member({DocId, ViewId, Key},
@@ -393,7 +396,7 @@
AddAcc4)
end,
{Log4, AddAcc5, DelAcc5};
- false when Op /= del ->
+ false ->
%% an update operation has been
%% logged for this key. We must now
%% record it as deleted in the
@@ -411,16 +414,7 @@
{{UpdatedSeq, Key},
{DocId, RemValue}},
AddAcc4),
- {Log5, AddAcc5, DelAcc5};
- false ->
- %% the key has already been
- %% registered in the view as
- %% deleted, make sure to add it
- %% to the new log.
- Log5 = dict:append(DocId,
- {ViewId,
- {Key, Seq, del}}, Log4),
- {Log5, AddAcc4, DelAcc4}
+ {Log5, AddAcc5, DelAcc5}
end
end, {Log2, AddAcc2, DelAcc2}, VIdKeys),
{ok, {Log3, AddAcc3, DelAcc3}}