This is an automated email from the ASF dual-hosted git repository. jan pushed a commit to branch rebase/access-2023 in repository https://gitbox.apache.org/repos/asf/couchdb.git
commit d83ec7c6193004e6577de627d19a631d6013510e Author: Jan Lehnardt <[email protected]> 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 7b867f08d..72035e354 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(Db, {Props}) -> get_validate_doc_fun(Db, couch_doc:from_json_obj({Props})); get_validate_doc_fun(Db, #doc{body = {Props}} = DDoc) ->
