You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by rn...@apache.org on 2018/08/06 15:37:42 UTC

[couchdb] 01/02: Enforce partition:id format in doc ids

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

rnewson pushed a commit to branch user-partitioned-dbs-4
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit f5b2e39d3e6f36d0420c5e3f1c7dd37a07a617e3
Author: Robert Newson <rn...@apache.org>
AuthorDate: Thu Aug 2 14:41:03 2018 +0100

    Enforce partition:id format in doc ids
---
 src/couch/src/couch_doc.erl        |  24 ++++++++++++++++++++----
 src/couch/test/fixtures/test.couch | Bin 16482 -> 0 bytes
 src/mem3/src/mem3_util.erl         |   8 ++++++--
 3 files changed, 26 insertions(+), 6 deletions(-)

diff --git a/src/couch/src/couch_doc.erl b/src/couch/src/couch_doc.erl
index f960ec5..4b618bc 100644
--- a/src/couch/src/couch_doc.erl
+++ b/src/couch/src/couch_doc.erl
@@ -133,6 +133,12 @@ from_json_obj_validate(EJson) ->
 from_json_obj_validate(EJson, DbName) ->
     MaxSize = config:get_integer("couchdb", "max_document_size", 4294967296),
     Doc = from_json_obj(EJson, DbName),
+    case is_binary(DbName) andalso mem3:is_partitioned(DbName) of
+        true ->
+            couch_doc:validate_docid(Doc#doc.id, DbName);
+        false ->
+            ok
+    end,
     case couch_ejson_size:encoded_size(Doc#doc.body) =< MaxSize of
         true ->
              validate_attachment_sizes(Doc#doc.atts),
@@ -199,11 +205,21 @@ parse_revs(_) ->
 
 
 validate_docid(DocId, DbName) ->
-    case DbName =:= ?l2b(config:get("mem3", "shards_db", "_dbs")) andalso
-        lists:member(DocId, ?SYSTEM_DATABASES) of
-        true ->
+    SystemId = DbName =:= ?l2b(config:get("mem3", "shards_db", "_dbs")) andalso
+        lists:member(DocId, ?SYSTEM_DATABASES),
+    Partitioned = is_binary(DbName) andalso mem3:is_partitioned(DbName),
+    case {SystemId, Partitioned} of
+        {true, _} ->
             ok;
-        false ->
+        {false, true} ->
+            case binary:split(DocId, <<":">>) of
+                [Partition, Rest] ->
+                    ok = validate_docid(Partition),
+                    validate_docid(Rest);
+                _ ->
+                    throw({illegal_docid, <<"doc id must be of form partition:id">>})
+            end;
+        {false, false} ->
             validate_docid(DocId)
     end.
 
diff --git a/src/couch/test/fixtures/test.couch b/src/couch/test/fixtures/test.couch
deleted file mode 100644
index 32c79af..0000000
Binary files a/src/couch/test/fixtures/test.couch and /dev/null differ
diff --git a/src/mem3/src/mem3_util.erl b/src/mem3/src/mem3_util.erl
index cd9b76a..6832454 100644
--- a/src/mem3/src/mem3_util.erl
+++ b/src/mem3/src/mem3_util.erl
@@ -45,8 +45,12 @@ docid_hash(<<"_design/", _/binary>> = DocId, _Options) ->
 docid_hash(DocId, Options) when is_binary(DocId), is_list(Options) ->
     Data = case lists:member(partitioned, Options) of
         true ->
-            [Partition, _Rest] = binary:split(DocId, <<":">>),
-            Partition;
+            case binary:split(DocId, <<":">>) of
+                [Partition, _Rest] ->
+                    Partition;
+                _ ->
+                    throw({illegal_docid, <<"doc id must be of form partition:id">>})
+            end;
         false ->
             DocId
     end,