couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From iilyak <...@git.apache.org>
Subject [GitHub] couchdb-couch-epi pull request: 2796 improve performance of provid...
Date Wed, 02 Sep 2015 17:26:26 GMT
Github user iilyak commented on a diff in the pull request:

    https://github.com/apache/couchdb-couch-epi/pull/8#discussion_r38559864
  
    --- Diff: src/couch_epi_data_gen.erl ---
    @@ -174,27 +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 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) ->
    --- End diff --
    
    `if_exists` suppose to call a function with passed arguments. If we want to rename `if_exists`
into `maybe_call` we would need to use `apply(M, F, A)`. Since we have only two kinds of functions
(with arity 0 and arity 1). We could have maybe_call with different arity . Something like:
    
    ```erlang
    maybe_call(Handle, Function, Default) ->
            case erlang:function_exported(Handle, Func, 0) of
                true -> Handle:Function();
                false -> Default
            end.
    maybe_call(Handle, Function, Arg, Default) ->
            case erlang:function_exported(Handle, Func, 1) of
                true -> Handle:Function(Arg);
                false -> Default
            end.
    ``` 
    This approach wouldn't work for this https://github.com/cloudant/couchdb-couch-epi/blob/36b2be55a89f766df44b05d988d040e6a3125529/src/couch_epi_data_gen.erl#L178.
Since we need to use try/catch to detect other errors.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message