You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by va...@apache.org on 2021/04/16 21:45:14 UTC

[couchdb] 09/24: Clean up couch_doc

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

vatamane pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit e31ae8d99173bc95801a06903764980b8c6ae7a0
Author: Nick Vatamaniuc <va...@gmail.com>
AuthorDate: Wed Apr 14 00:27:07 2021 -0400

    Clean up couch_doc
    
    The main change is to remove `validate_docid/1,2` and use
    `fabric2_db:validate_docid/1` instead.
    
    `with_ejson_body` is also not needed as request bodies are parsed
    to ejson and fabric2_fdb also deserializes to ejson.
---
 src/chttpd/src/chttpd_show.erl    |  2 +-
 src/couch/src/couch_doc.erl       | 59 ++-------------------------------------
 src/couch/src/couch_partition.erl |  2 +-
 3 files changed, 5 insertions(+), 58 deletions(-)

diff --git a/src/chttpd/src/chttpd_show.erl b/src/chttpd/src/chttpd_show.erl
index 295d753..b17309a 100644
--- a/src/chttpd/src/chttpd_show.erl
+++ b/src/chttpd/src/chttpd_show.erl
@@ -65,7 +65,7 @@ send_doc_update_response(Req, Db, DDoc, UpdateName, Doc, DocId) ->
                 Options = [{user_ctx, Req#httpd.user_ctx}]
             end,
             NewDoc = couch_db:doc_from_json_obj_validate(Db, {NewJsonDoc}),
-            couch_doc:validate_docid(NewDoc#doc.id),
+            fabric2_db:validate_docid(NewDoc#doc.id),
             {UpdateResult, NewRev} = fabric:update_doc(Db, NewDoc, Options),
             chttpd_stats:incr_writes(),
             NewRevStr = couch_doc:rev_to_str(NewRev),
diff --git a/src/couch/src/couch_doc.erl b/src/couch/src/couch_doc.erl
index 7224921..4d0a13d 100644
--- a/src/couch/src/couch_doc.erl
+++ b/src/couch/src/couch_doc.erl
@@ -16,14 +16,13 @@
 -export([from_json_obj/1, from_json_obj_validate/1]).
 -export([from_json_obj/2, from_json_obj_validate/2]).
 -export([to_json_obj/2, has_stubs/1, merge_stubs/2]).
--export([validate_docid/1, validate_docid/2, get_validate_doc_fun/1]).
+-export([get_validate_doc_fun/1]).
 -export([doc_from_multi_part_stream/2, doc_from_multi_part_stream/3]).
 -export([doc_from_multi_part_stream/4]).
 -export([doc_to_multi_part_stream/5, len_doc_to_multi_part_stream/4]).
 -export([restart_open_doc_revs/3]).
 -export([to_path/1]).
 
--export([with_ejson_body/1]).
 -export([is_deleted/1]).
 
 
@@ -115,7 +114,7 @@ to_json_attachments(Atts, OutputData, Follows, ShowEnc) ->
     [{<<"_attachments">>, {Props}}].
 
 to_json_obj(Doc, Options) ->
-    doc_to_json_obj(with_ejson_body(Doc), Options).
+    doc_to_json_obj(Doc, Options).
 
 doc_to_json_obj(#doc{id=Id,deleted=Del,body=Body,revs={Start, RevIds},
             meta=Meta}=Doc,Options)->
@@ -198,58 +197,12 @@ parse_revs(_) ->
     throw({bad_request, "Invalid list of revisions"}).
 
 
-validate_docid(DocId, DbName) ->
-    case DbName =:= ?l2b(config:get("mem3", "shards_db", "_dbs")) andalso
-        couch_db:is_system_db_name(DocId) of
-        true ->
-            ok;
-        false ->
-            validate_docid(DocId)
-    end.
-
-validate_docid(<<"">>) ->
-    throw({illegal_docid, <<"Document id must not be empty">>});
-validate_docid(<<"_design/">>) ->
-    throw({illegal_docid, <<"Illegal document id `_design/`">>});
-validate_docid(<<"_local/">>) ->
-    throw({illegal_docid, <<"Illegal document id `_local/`">>});
-validate_docid(Id) when is_binary(Id) ->
-    MaxLen = case config:get("couchdb", "max_document_id_length", "infinity") of
-        "infinity" -> infinity;
-        IntegerVal -> list_to_integer(IntegerVal)
-    end,
-    case MaxLen > 0 andalso byte_size(Id) > MaxLen of
-        true -> throw({illegal_docid, <<"Document id is too long">>});
-        false -> ok
-    end,
-    case couch_util:validate_utf8(Id) of
-        false -> throw({illegal_docid, <<"Document id must be valid UTF-8">>});
-        true -> ok
-    end,
-    case Id of
-    <<"_design/", _/binary>> -> ok;
-    <<"_local/", _/binary>> -> ok;
-    <<"_", _/binary>> ->
-        case couch_db_plugin:validate_docid(Id) of
-            true ->
-                ok;
-            false ->
-                throw(
-                  {illegal_docid,
-                   <<"Only reserved document ids may start with underscore.">>})
-        end;
-    _Else -> ok
-    end;
-validate_docid(Id) ->
-    couch_log:debug("Document id is not a string: ~p", [Id]),
-    throw({illegal_docid, <<"Document id must be a string">>}).
-
 transfer_fields([], #doc{body=Fields}=Doc, _) ->
     % convert fields back to json object
     Doc#doc{body={lists:reverse(Fields)}};
 
 transfer_fields([{<<"_id">>, Id} | Rest], Doc, DbName) ->
-    validate_docid(Id, DbName),
+    fabric2_db:validate_docid(Id),
     transfer_fields(Rest, Doc#doc{id=Id}, DbName);
 
 transfer_fields([{<<"_rev">>, Rev} | Rest], #doc{revs={0, []}}=Doc, DbName) ->
@@ -518,9 +471,3 @@ flush_parser_messages(Ref) ->
     after 0 ->
         ok
     end.
-
-
-with_ejson_body(#doc{body = Body} = Doc) when is_binary(Body) ->
-    Doc#doc{body = couch_compress:decompress(Body)};
-with_ejson_body(#doc{body = {_}} = Doc) ->
-    Doc.
diff --git a/src/couch/src/couch_partition.erl b/src/couch/src/couch_partition.erl
index f2efcaa..cb78323 100644
--- a/src/couch/src/couch_partition.erl
+++ b/src/couch/src/couch_partition.erl
@@ -122,7 +122,7 @@ validate_docid(DocId) when is_binary(DocId) ->
             throw({illegal_docid, <<"Doc id must be of form partition:id">>});
         {Partition, PartitionedDocId} ->
             validate_partition(Partition),
-            couch_doc:validate_docid(PartitionedDocId)
+            fabric2_db:validate_docid(PartitionedDocId)
     end.