You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by ja...@apache.org on 2022/08/06 14:10:49 UTC

[couchdb] 03/21: feat(access): handle new records in couch_doc

This is an automated email from the ASF dual-hosted git repository.

jan pushed a commit to branch feat/access-2022
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit 4a98ed03b40be475801a61957e39a08cc74987df
Author: Jan Lehnardt <ja...@apache.org>
AuthorDate: Fri Jun 24 17:01:04 2022 +0200

    feat(access): handle new records in couch_doc
---
 src/couch/src/couch_doc.erl | 44 +++++++++++++++++++++++++++++++++++++-------
 1 file changed, 37 insertions(+), 7 deletions(-)

diff --git a/src/couch/src/couch_doc.erl b/src/couch/src/couch_doc.erl
index 95b1c8b41..61ea4cbe8 100644
--- a/src/couch/src/couch_doc.erl
+++ b/src/couch/src/couch_doc.erl
@@ -26,6 +26,8 @@
 -export([with_ejson_body/1]).
 -export([is_deleted/1]).
 
+-export([has_access/1, has_no_access/1]).
+
 -include_lib("couch/include/couch_db.hrl").
 
 -spec to_path(#doc{}) -> path().
@@ -40,15 +42,28 @@ to_branch(Doc, [RevId | Rest]) ->
     [{RevId, ?REV_MISSING, to_branch(Doc, Rest)}].
 
 % helpers used by to_json_obj
+reduce_access({Access}) -> Access;
+reduce_access(Access) -> Access.
+
 to_json_rev(0, []) ->
     [];
 to_json_rev(Start, [FirstRevId | _]) ->
     [{<<"_rev">>, ?l2b([integer_to_list(Start), "-", revid_to_str(FirstRevId)])}].
 
-to_json_body(true, {Body}) ->
+% TODO: remove if we can
+% to_json_body(Del, Body) ->
+%     to_json_body(Del, Body, []).
+
+to_json_body(true, {Body}, []) ->
     Body ++ [{<<"_deleted">>, true}];
-to_json_body(false, {Body}) ->
-    Body.
+to_json_body(false, {Body}, []) ->
+    Body;
+to_json_body(true, {Body}, Access0) ->
+    Access = reduce_access(Access0),
+    Body ++ [{<<"_deleted">>, true}] ++ [{<<"_access">>, {Access}}];
+to_json_body(false, {Body}, Access0) ->
+    Access = reduce_access(Access0),
+    Body ++ [{<<"_access">>, Access}].
 
 to_json_revisions(Options, Start, RevIds0) ->
     RevIds =
@@ -138,14 +153,15 @@ doc_to_json_obj(
         deleted = Del,
         body = Body,
         revs = {Start, RevIds},
-        meta = Meta
+        meta = Meta,
+        access = Access
     } = Doc,
     Options
 ) ->
     {
         [{<<"_id">>, Id}] ++
             to_json_rev(Start, RevIds) ++
-            to_json_body(Del, Body) ++
+            to_json_body(Del, Body, Access) ++
             to_json_revisions(Options, Start, RevIds) ++
             to_json_meta(Meta) ++
             to_json_attachments(Doc#doc.atts, Options)
@@ -401,7 +417,7 @@ max_seq(Tree, UpdateSeq) ->
     end,
     couch_key_tree:fold(FoldFun, UpdateSeq, Tree).
 
-to_doc_info_path(#full_doc_info{id = Id, rev_tree = Tree, update_seq = FDISeq}) ->
+to_doc_info_path(#full_doc_info{id = Id, rev_tree = Tree, update_seq = FDISeq, access = Access}) ->
     RevInfosAndPath = [
         {rev_info(Node), Path}
      || {_Leaf, Path} = Node <-
@@ -419,7 +435,7 @@ to_doc_info_path(#full_doc_info{id = Id, rev_tree = Tree, update_seq = FDISeq})
     ),
     [{_RevInfo, WinPath} | _] = SortedRevInfosAndPath,
     RevInfos = [RevInfo || {RevInfo, _Path} <- SortedRevInfosAndPath],
-    {#doc_info{id = Id, high_seq = max_seq(Tree, FDISeq), revs = RevInfos}, WinPath}.
+    {#doc_info{id = Id, high_seq = max_seq(Tree, FDISeq), revs = RevInfos, access = Access}, WinPath}.
 
 rev_info({#leaf{} = Leaf, {Pos, [RevId | _]}}) ->
     #rev_info{
@@ -459,6 +475,20 @@ is_deleted(Tree) ->
             false
     end.
 
+get_access({Props}) ->
+    get_access(couch_doc:from_json_obj({Props}));
+get_access(#doc{access=Access}) ->
+    Access.
+
+has_access(Doc) ->
+    has_access1(get_access(Doc)).
+
+has_no_access(Doc) ->
+    not has_access1(get_access(Doc)).
+
+has_access1([]) -> false;
+has_access1(_) -> true.
+
 get_validate_doc_fun({Props}) ->
     get_validate_doc_fun(couch_doc:from_json_obj({Props}));
 get_validate_doc_fun(#doc{body = {Props}} = DDoc) ->