You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by ga...@apache.org on 2016/04/11 16:27:31 UTC

[4/4] couch commit: updated refs/heads/local-docs-2978 to 2531c3b

Create md5 etag for _local docs

This makes a unique ETAG for _local docs, so that they are cached
correctly, and fetched again when the document changes.

fixes COUCHDB-2978


Project: http://git-wip-us.apache.org/repos/asf/couchdb-couch/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-couch/commit/2531c3b8
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-couch/tree/2531c3b8
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-couch/diff/2531c3b8

Branch: refs/heads/local-docs-2978
Commit: 2531c3b81aa5430d50b32ae44f2a034681ddce69
Parents: 79c98d8
Author: Garren Smith <ga...@gmail.com>
Authored: Thu Apr 7 16:06:31 2016 +0200
Committer: Garren Smith <ga...@gmail.com>
Committed: Mon Apr 11 16:27:12 2016 +0200

----------------------------------------------------------------------
 src/couch_httpd.erl       | 15 ++++++++++++---
 test/couch_etag_tests.erl | 28 ++++++++++++++++++++++++++++
 2 files changed, 40 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-couch/blob/2531c3b8/src/couch_httpd.erl
----------------------------------------------------------------------
diff --git a/src/couch_httpd.erl b/src/couch_httpd.erl
index 7370594..c4f5a9c 100644
--- a/src/couch_httpd.erl
+++ b/src/couch_httpd.erl
@@ -21,7 +21,7 @@
 -export([make_fun_spec_strs/1]).
 -export([make_arity_1_fun/1, make_arity_2_fun/1, make_arity_3_fun/1]).
 -export([parse_form/1,json_body/1,json_body_obj/1,body/1]).
--export([doc_etag/1, make_etag/1, etag_match/2, etag_respond/3, etag_maybe/2]).
+-export([doc_etag/1, doc_etag/3, make_etag/1, etag_match/2, etag_respond/3, etag_maybe/2]).
 -export([primary_header_value/2,partition/1,serve_file/3,serve_file/4, server_header/0]).
 -export([start_chunked_response/3,send_chunk/2,log_request/2]).
 -export([start_response_length/4, start_response/3, send/2]).
@@ -594,8 +594,17 @@ maybe_decompress(Httpd, Body) ->
         throw({bad_ctype, [Else, " is not a supported content encoding."]})
     end.
 
-doc_etag(#doc{revs={Start, [DiskRev|_]}}) ->
-    "\"" ++ ?b2l(couch_doc:rev_to_str({Start, DiskRev})) ++ "\"".
+doc_etag(#doc{id=Id, body=Body, revs={Start, [DiskRev|_]}}) ->
+    doc_etag(Id, Body, {Start, DiskRev}).
+
+doc_etag(<<"_local/", _/binary>>, Body, {Start, DiskRev}) ->
+    make_etag({Start, DiskRev, Body});
+doc_etag(_Id, _Body, {Start, DiskRev}) ->
+    rev_etag({Start, DiskRev}).
+
+rev_etag({Start, DiskRev}) ->
+    Rev = couch_doc:rev_to_str({Start, DiskRev}),
+     <<$", Rev/binary, $">>.
 
 make_etag(Term) ->
     <<SigInt:128/integer>> = couch_crypto:hash(md5, term_to_binary(Term)),

http://git-wip-us.apache.org/repos/asf/couchdb-couch/blob/2531c3b8/test/couch_etag_tests.erl
----------------------------------------------------------------------
diff --git a/test/couch_etag_tests.erl b/test/couch_etag_tests.erl
new file mode 100644
index 0000000..0e3125b
--- /dev/null
+++ b/test/couch_etag_tests.erl
@@ -0,0 +1,28 @@
+% Licensed under the Apache License, Version 2.0 (the "License"); you may not
+% use this file except in compliance with the License. You may obtain a copy of
+% the License at
+%
+%   http://www.apache.org/licenses/LICENSE-2.0
+%
+% Unless required by applicable law or agreed to in writing, software
+% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+% License for the specific language governing permissions and limitations under
+% the License.
+
+-module(couch_etag_tests).
+
+-include_lib("eunit/include/eunit.hrl").
+
+local_with_empty_body_test() ->
+    Etag = couch_httpd:doc_etag(<<"_local/local-and-empty">>, {[]}, {0, <<"1">>}),
+    ?assertEqual(Etag, <<"\"5ZVXQYO7VLEOU0TL9VXDNP5PV\"">>).
+
+
+local_with_body_test() ->
+    Etag = couch_httpd:doc_etag(<<"_local/local-with-body">>, {[{<<"hello">>,<<"world">>},{<<"relax">>,true}]}, {0, <<"1">>}),
+    ?assertEqual(Etag, <<"\"CEFXP6WH8OKYIWO1GLGBHKCCA\"">>).
+
+normal_doc_uses_rev() ->
+    Etag = couch_httpd:doc_etag(<<"nomal-doc">>, {[{<<"hello">>,<<"again">>},{<<"relax">>,true}]}, {1, <<"efda11e34e88ebe31a2f83e84a0435b6">>}),
+    ?assertEqual(Etag, <<"\"2-efda11e34e88ebe31a2f83e84a0435b6\"">>).