couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rnew...@apache.org
Subject [4/5] couchdb-couch-epi git commit: Don't use try/catch to handle missing plugins
Date Wed, 02 Sep 2015 18:31:03 GMT
Don't use try/catch to handle missing plugins

COUCHDB-2796


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

Branch: refs/heads/master
Commit: 36b2be55a89f766df44b05d988d040e6a3125529
Parents: 7f32e4f
Author: ILYA Khlopotov <iilyak@ca.ibm.com>
Authored: Wed Sep 2 09:37:46 2015 -0700
Committer: ILYA Khlopotov <iilyak@ca.ibm.com>
Committed: Wed Sep 2 09:53:39 2015 -0700

----------------------------------------------------------------------
 src/couch_epi_data.erl          | 18 ++-------------
 src/couch_epi_data_gen.erl      | 44 +++++++++++++++++++++++-------------
 src/couch_epi_data_source.erl   | 12 +---------
 src/couch_epi_functions_gen.erl | 37 ++++++++++++++++--------------
 4 files changed, 51 insertions(+), 60 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-couch-epi/blob/36b2be55/src/couch_epi_data.erl
----------------------------------------------------------------------
diff --git a/src/couch_epi_data.erl b/src/couch_epi_data.erl
index 680e5f5..b685dae 100644
--- a/src/couch_epi_data.erl
+++ b/src/couch_epi_data.erl
@@ -126,7 +126,7 @@ safe_set(Hash, #state{} = State) ->
         key = Key} = State,
     try
         Data = get_from_module(Module),
-        OldData = current(Handle, Subscriber),
+        OldData = couch_epi_data_gen:current_data(Handle, Subscriber),
         ok = couch_epi_data_gen:set(Handle, Subscriber, Data),
         couch_epi_server:notify(Subscriber, Key, {data, OldData}, {data, Data}),
         {ok, State#state{hash = Hash}}
@@ -135,21 +135,7 @@ safe_set(Hash, #state{} = State) ->
     end.
 
 get_from_module(Module) ->
-    try
-        Module:data()
-    catch
-        error:undef -> []
-    end.
-
-current(Handle, Subscriber) ->
-    try
-        case couch_epi_data_gen:by_source(Handle, Subscriber) of
-            undefined -> [];
-            Data -> Data
-        end
-    catch error:undef ->
-        []
-    end.
+    Module:data().
 
 maybe_start_keeper(Key) ->
     Handle = couch_epi_data_gen:get_handle(Key),

http://git-wip-us.apache.org/repos/asf/couchdb-couch-epi/blob/36b2be55/src/couch_epi_data_gen.erl
----------------------------------------------------------------------
diff --git a/src/couch_epi_data_gen.erl b/src/couch_epi_data_gen.erl
index ad84b80..d7e0c65 100644
--- a/src/couch_epi_data_gen.erl
+++ b/src/couch_epi_data_gen.erl
@@ -24,6 +24,7 @@
 -export([keys/1, subscribers/1]).
 
 -export([save/3]).
+-export([current_data/2]).
 
 set(Handle, Source, Data) ->
     case is_updated(Handle, Source, Data) of
@@ -174,32 +175,43 @@ module_name({Service, Key}) when is_list(Service) andalso is_list(Key)
->
 
 is_updated(Handle, Source, Data) ->
     Sig = couch_epi_util:hash(Data),
-    try Handle:version(Source) of
-        {error, {unknown, Source}} -> true;
-        {error, Reason} -> throw(Reason);
-        Sig -> false;
-        _ -> true
-    catch
-        error:undef -> true;
-        Class:Reason ->
-            throw({Class, {Source, Reason}})
-    end.
+    if_exists(Handle, version, 1, true, fun() ->
+        try Handle:version(Source) of
+            {error, {unknown, Source}} -> true;
+            {error, Reason} -> throw(Reason);
+            Sig -> false;
+            _ -> true
+        catch
+            Class:Reason ->
+                throw({Class, {Source, Reason}})
+        end
+    end).
 
 save(Handle, undefined, []) ->
-    case get_current_data(Handle) of
+    case current_data(Handle) of
         [] -> generate(Handle, []);
         _Else -> ok
     end;
 save(Handle, Source, Data) ->
-    CurrentData = get_current_data(Handle),
+    CurrentData = current_data(Handle),
     NewDefs = lists:keystore(Source, 1, CurrentData, {Source, Data}),
     generate(Handle, NewDefs).
 
-get_current_data(Handle) ->
-    try Handle:by_source()
-    catch error:undef -> []
-    end.
+current_data(Handle, Subscriber) ->
+    if_exists(Handle, by_source, 1, [], fun() ->
+        Handle:by_source(Subscriber)
+    end).
 
+current_data(Handle) ->
+    if_exists(Handle, by_source, 0, [], fun() ->
+        Handle:by_source()
+    end).
+
+if_exists(Handle, Func, Arity, Default, Fun) ->
+    case erlang:function_exported(Handle, Func, Arity) of
+        true -> Fun();
+        false -> Default
+    end.
 
 defined_keys(Defs) ->
     Keys = fold_defs(Defs, [], fun({_Source, Key, _Data}, Acc) ->

http://git-wip-us.apache.org/repos/asf/couchdb-couch-epi/blob/36b2be55/src/couch_epi_data_source.erl
----------------------------------------------------------------------
diff --git a/src/couch_epi_data_source.erl b/src/couch_epi_data_source.erl
index f44430d..f905e8f 100644
--- a/src/couch_epi_data_source.erl
+++ b/src/couch_epi_data_source.erl
@@ -161,7 +161,7 @@ safe_set(Hash, Data, #state{} = State) ->
         key = Key} = State,
 
     try
-        OldData = current(Handle, Subscriber),
+        OldData = couch_epi_data_gen:current_data(Handle, Subscriber),
         ok = couch_epi_data_gen:set(Handle, Subscriber, Data),
         couch_epi_server:notify(Subscriber, Key, {data, OldData}, {data, Data}),
         {ok, State#state{hash = Hash}}
@@ -181,16 +181,6 @@ hash_of_file(FilePath) ->
     {ok, Data} = file:read_file(FilePath),
     couch_epi_util:md5(Data).
 
-current(Handle, Subscriber) ->
-    try
-        case couch_epi_data_gen:by_source(Handle, Subscriber) of
-            undefined -> [];
-            Data -> Data
-        end
-    catch error:undef ->
-        []
-    end.
-
 maybe_start_keeper(Key) ->
     Handle = couch_epi_data_gen:get_handle(Key),
     couch_epi_module_keeper:maybe_start_keeper(couch_epi_data_gen, Handle).

http://git-wip-us.apache.org/repos/asf/couchdb-couch-epi/blob/36b2be55/src/couch_epi_functions_gen.erl
----------------------------------------------------------------------
diff --git a/src/couch_epi_functions_gen.erl b/src/couch_epi_functions_gen.erl
index 2fba66f..8c63b9c 100644
--- a/src/couch_epi_functions_gen.erl
+++ b/src/couch_epi_functions_gen.erl
@@ -190,16 +190,17 @@ module_name(ServiceId) when is_list(ServiceId) ->
 
 is_updated(Handle, Source, Modules) ->
     Sig = hash(Modules),
-    try Handle:version(Source) of
-        {error, {unknown, Source}} -> true;
-        {error, Reason} -> throw(Reason);
-        Sig -> false;
-        _ -> true
-    catch
-        error:undef -> true;
-        Class:Reason ->
-            throw({Class, {Source, Reason}})
-    end.
+    if_exists(Handle, version, 1, true, fun() ->
+        try Handle:version(Source) of
+            {error, {unknown, Source}} -> true;
+            {error, Reason} -> throw(Reason);
+            Sig -> false;
+            _ -> true
+        catch
+            Class:Reason ->
+                throw({Class, {Source, Reason}})
+        end
+    end).
 
 save(Handle, undefined, []) ->
     case get_current_definitions(Handle) of
@@ -218,8 +219,14 @@ definitions(Source, Modules) ->
     {Source, SrcDefs}.
 
 get_current_definitions(Handle) ->
-    try Handle:definitions()
-    catch error:undef -> []
+    if_exists(Handle, definitions, 0, [], fun() ->
+        Handle:definitions()
+    end).
+
+if_exists(Handle, Func, Arity, Default, Fun) ->
+    case erlang:function_exported(Handle, Func, Arity) of
+        true -> Fun();
+        false -> Default
     end.
 
 defined_providers(Defs) ->
@@ -300,11 +307,7 @@ parse_opts([], Acc) ->
     Acc.
 
 providers(Handle, Function, Arity, #opts{ignore_providers = true}) ->
-    try
-        Handle:providers(Function, Arity)
-    catch
-        error:undef -> []
-    end;
+    Handle:providers(Function, Arity);
 providers(Handle, Function, Arity, #opts{}) ->
     Handle:providers(Function, Arity).
 


Mime
View raw message