couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rnew...@apache.org
Subject [08/13] couch commit: updated refs/heads/master to 311ba94
Date Wed, 12 Aug 2015 15:54:08 GMT
Add couch_db_plugin:before_doc_update/2


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

Branch: refs/heads/master
Commit: 1edf06c41c1477df0227ed19d90f3399e5fe96bc
Parents: 0cf35ce
Author: ILYA Khlopotov <iilyak@ca.ibm.com>
Authored: Fri Jun 19 10:41:32 2015 -0700
Committer: ILYA Khlopotov <iilyak@ca.ibm.com>
Committed: Tue Aug 11 12:24:59 2015 -0700

----------------------------------------------------------------------
 src/couch_db.erl               | 11 +++++------
 src/couch_db_plugin.erl        | 17 ++++++++++++++++-
 test/couch_db_plugin_tests.erl | 28 ++++++++++++++++++++++++++--
 3 files changed, 47 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-couch/blob/1edf06c4/src/couch_db.erl
----------------------------------------------------------------------
diff --git a/src/couch_db.erl b/src/couch_db.erl
index 81e2df8..a693fa2 100644
--- a/src/couch_db.erl
+++ b/src/couch_db.erl
@@ -1069,13 +1069,12 @@ prepare_doc_summaries(Db, BucketList) ->
         Bucket) || Bucket <- BucketList].
 
 
-before_docs_update(#db{before_doc_update = nil}, BucketList) ->
-    BucketList;
-before_docs_update(#db{before_doc_update = Fun} = Db, BucketList) ->
+before_docs_update(#db{} = Db, BucketList) ->
     [lists:map(
-        fun(Doc) ->
-            Fun(couch_doc:with_ejson_body(Doc), Db)
-        end,
+            fun(Doc) ->
+                DocWithBody = couch_doc:with_ejson_body(Doc),
+                couch_db_plugin:before_doc_update(Db, DocWithBody)
+            end,
         Bucket) || Bucket <- BucketList].
 
 

http://git-wip-us.apache.org/repos/asf/couchdb-couch/blob/1edf06c4/src/couch_db_plugin.erl
----------------------------------------------------------------------
diff --git a/src/couch_db_plugin.erl b/src/couch_db_plugin.erl
index c192abc..4e08aa2 100644
--- a/src/couch_db_plugin.erl
+++ b/src/couch_db_plugin.erl
@@ -12,7 +12,10 @@
 
 -module(couch_db_plugin).
 
--export([validate_dbname/2]).
+-export([
+    validate_dbname/2,
+    before_doc_update/2
+]).
 
 -define(SERVICE_ID, couch_db).
 
@@ -28,7 +31,19 @@ validate_dbname(DbName, Normalized) ->
     couch_epi:any(Handle, ?SERVICE_ID, validate_dbname, [DbName, Normalized],
         [ignore_providers]).
 
+before_doc_update(#db{before_doc_update = Fun} = Db, Doc0) ->
+    case with_pipe(before_doc_update, [Doc0, Db]) of
+        [Doc1, _Db] when is_function(Fun) -> Fun(Doc1, Db);
+        [Doc1, _Db] -> Doc1
+    end.
 
 %% ------------------------------------------------------------------
 %% Internal Function Definitions
 %% ------------------------------------------------------------------
+
+with_pipe(Func, Args) ->
+    do_apply(Func, Args, [ignore_providers, pipe]).
+
+do_apply(Func, Args, Opts) ->
+    Handle = couch_epi:get_handle(?SERVICE_ID),
+    couch_epi:apply(Handle, ?SERVICE_ID, Func, Args, Opts).

http://git-wip-us.apache.org/repos/asf/couchdb-couch/blob/1edf06c4/test/couch_db_plugin_tests.erl
----------------------------------------------------------------------
diff --git a/test/couch_db_plugin_tests.erl b/test/couch_db_plugin_tests.erl
index 980980d..c26ea4d 100644
--- a/test/couch_db_plugin_tests.erl
+++ b/test/couch_db_plugin_tests.erl
@@ -13,7 +13,8 @@
 -module(couch_db_plugin_tests).
 
 -export([
-    validate_dbname/2
+    validate_dbname/2,
+    before_doc_update/2
 ]).
 
 -include_lib("couch/include/couch_eunit.hrl").
@@ -40,6 +41,10 @@ validate_dbname({true, _Db}, _) -> true;
 validate_dbname({false, _Db}, _) -> false;
 validate_dbname({fail, _Db}, _) -> throw(validate_dbname).
 
+before_doc_update({fail, _Doc}, _Db) -> throw(before_doc_update);
+before_doc_update({true, Doc}, Db) -> [{true, [before_doc_update|Doc]}, Db];
+before_doc_update({false, Doc}, Db) -> [{false, Doc}, Db].
+
 callback_test_() ->
     {
         "callback tests",
@@ -48,7 +53,11 @@ callback_test_() ->
             [
                 fun validate_dbname_match/0,
                 fun validate_dbname_no_match/0,
-                fun validate_dbname_throw/0
+                fun validate_dbname_throw/0,
+
+                fun before_doc_update_match/0,
+                fun before_doc_update_no_match/0,
+                fun before_doc_update_throw/0
             ]
         }
     }.
@@ -68,3 +77,18 @@ validate_dbname_throw() ->
     ?_assertThrow(
         validate_dbname,
         couch_db_plugin:validate_dbname({fail, [db]}, db)).
+
+before_doc_update_match() ->
+    ?_assertMatch(
+        {true, [before_doc_update, doc]},
+        couch_db_plugin:before_doc_update(#db{}, {true, [doc]})).
+
+before_doc_update_no_match() ->
+    ?_assertMatch(
+        {false, [doc]},
+        couch_db_plugin:before_doc_update(#db{}, {false, [doc]})).
+
+before_doc_update_throw() ->
+    ?_assertThrow(
+        before_doc_update,
+        couch_db_plugin:before_doc_update(#db{}, {fail, [doc]})).


Mime
View raw message