diff options
author | Nick Vatamaniuc <vatamane@gmail.com> | 2021-04-14 00:27:07 -0400 |
---|---|---|
committer | Nick Vatamaniuc <nickva@users.noreply.github.com> | 2021-04-16 17:44:43 -0400 |
commit | e31ae8d99173bc95801a06903764980b8c6ae7a0 (patch) | |
tree | e689f20a46a80bb2366e43cd894f89cf9c432093 | |
parent | d3176243a2adfef16b450d59ff99e5cd5a3d141d (diff) | |
download | couchdb-e31ae8d99173bc95801a06903764980b8c6ae7a0.tar.gz |
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.
-rw-r--r-- | src/chttpd/src/chttpd_show.erl | 2 | ||||
-rw-r--r-- | src/couch/src/couch_doc.erl | 59 | ||||
-rw-r--r-- | 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 295d753a0..b17309a0b 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 7224921d4..4d0a13d14 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 f2efcaa5e..cb78323c3 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. |