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: Simplify couch epi
Date Tue, 29 Sep 2015 16:06:07 GMT
Github user iilyak commented on a diff in the pull request:

    https://github.com/apache/couchdb-couch-epi/pull/12#discussion_r40691917
  
    --- Diff: src/couch_epi_data.erl ---
    @@ -12,131 +12,103 @@
     
     -module(couch_epi_data).
     
    --behaviour(gen_server).
    +-include("couch_epi.hrl").
     
     %% ------------------------------------------------------------------
     %% API Function Exports
     %% ------------------------------------------------------------------
     
    --export([childspec/4]).
    --export([start_link/4, reload/1]).
    --export([wait/1, stop/1]).
    -
    -%% ------------------------------------------------------------------
    -%% gen_server Function Exports
    -%% ------------------------------------------------------------------
    -
    --export([init/1, handle_call/3, handle_cast/2, handle_info/2,
    -         terminate/2, code_change/3]).
    -
    --record(state, {
    -    subscriber, module, key, hash, handle,
    -    initialized = false, pending = []}).
    +-export([interval/1, data/1]).
     
     %% ------------------------------------------------------------------
     %% API Function Definitions
     %% ------------------------------------------------------------------
     
    -childspec(Id, App, EpiKey, Module) ->
    -    {
    -        Id,
    -        {?MODULE, start_link, [
    -            App,
    -            EpiKey,
    -            Module,
    -            []
    -        ]},
    -        permanent,
    -        5000,
    -        worker,
    -        [Module]
    -    }.
    -
    -start_link(SubscriberApp, {epi_key, Key}, Module, Options) ->
    -    maybe_start_keeper(Key),
    -    gen_server:start_link(?MODULE, [SubscriberApp, Module, Key, Options], []).
    -
    -reload(Server) ->
    -    gen_server:call(Server, reload).
    -
    -wait(Server) ->
    -    gen_server:call(Server, wait).
    -
    -stop(Server) ->
    -    catch gen_server:call(Server, stop).
    +interval(Specs) ->
    +    extract_minimal_interval(Specs).
     
    -%% ------------------------------------------------------------------
    -%% gen_server Function Definitions
    -%% ------------------------------------------------------------------
    -
    -init([Subscriber, Module, Key, _Options]) ->
    -    gen_server:cast(self(), init),
    -    {ok, #state{
    -        subscriber = Subscriber,
    -        module = Module,
    -        key = Key,
    -        handle = couch_epi_data_gen:get_handle(Key)}}.
    -
    -handle_call(wait, _From, #state{initialized = true} = State) ->
    -    {reply, ok, State};
    -handle_call(wait, From, #state{pending = Pending} = State) ->
    -    {noreply, State#state{pending = [From | Pending]}};
    -handle_call(reload, _From, State) ->
    -    {Res, NewState} = reload_if_updated(State),
    -    {reply, Res, NewState};
    -handle_call(stop, _From, State) ->
    -    {stop, normal, State};
    -handle_call(_Request, _From, State) ->
    -    {reply, ok, State}.
    -
    -handle_cast(init, #state{pending = Pending} = State) ->
    -    {_, NewState} = reload_if_updated(State),
    -    [gen_server:reply(Client, ok) || Client <- Pending],
    -    {noreply, NewState#state{initialized = true, pending = []}};
    -handle_cast(_Msg, State) ->
    -    {noreply, State}.
    -
    -handle_info(_Info, State) ->
    -    {noreply, State}.
    -
    -terminate(_Reason, _State) ->
    -    ok.
    -
    -code_change(_OldVsn, State, _Extra) ->
    -    {_, NewState} = reload_if_updated(State),
    -    {ok, NewState}.
    +data(Specs) ->
    +    Locators = locate_sources(Specs),
    +    case lists:foldl(fun collect_data/2, {ok, [], []}, Locators) of
    +        {ok, Hashes, Data} ->
    +            {ok, couch_epi_util:hash(Hashes), Data};
    +        Error ->
    +            Error
    +    end.
     
     %% ------------------------------------------------------------------
     %% Internal Function Definitions
     %% ------------------------------------------------------------------
     
    -reload_if_updated(#state{hash = OldHash, module = Module} = State) ->
    -    case couch_epi_functions_gen:hash([Module]) of
    -        OldHash ->
    -            {ok, State};
    -        Hash ->
    -            safe_set(Hash, State)
    +collect_data({App, Locator}, {ok, HashAcc, DataAcc}) ->
    +    case definitions(Locator) of
    +        {ok, Hash, Data} ->
    +            {ok, [Hash | HashAcc], [{App, Data} | DataAcc]};
    +        Error ->
    +            Error
    +    end;
    +collect_data({_App, _Locator}, Error) ->
    +    Error.
    +
    +extract_minimal_interval(Specs) ->
    +    lists:foldl(fun minimal_interval/2, undefined, Specs).
    +
    +minimal_interval({_App, #couch_epi_spec{options = Options}}, Min) ->
    +    case lists:keyfind(interval, 1, Options) of
    +        {interval, Interval} -> min(Interval, Min);
    +        false -> Min
         end.
     
    -safe_set(Hash, #state{} = State) ->
    -    #state{
    -        handle = Handle,
    -        subscriber = Subscriber,
    -        module = Module,
    -        key = Key} = State,
    -    try
    -        Data = get_from_module(Module),
    -        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}}
    -    catch Class:Reason ->
    -        {{Class, Reason}, State}
    +locate_sources(Specs) ->
    +    lists:map(fun({ProviderApp, #couch_epi_spec{value = Src}}) ->
    +        {ok, Locator} = locate(ProviderApp, Src),
    +        {ProviderApp, Locator}
    +    end, Specs).
    +
    +locate(App, {priv_file, FileName}) ->
    +    case priv_path(App, FileName) of
    +        {ok, FilePath} ->
    +            ok = ensure_exists(FilePath),
    +            {ok, {file, FilePath}};
    +        Else ->
    +            Else
    +    end;
    +locate(_App, {file, FilePath}) ->
    +    ok = ensure_exists(FilePath),
    +    {ok, {file, FilePath}};
    +locate(_App, Locator) ->
    +    {ok, Locator}.
    +
    +priv_path(AppName, FileName) ->
    +    case code:priv_dir(AppName) of
    +        {error, _Error} = Error ->
    +            Error;
    +        Dir ->
    +            {ok, filename:join(Dir, FileName)}
         end.
     
    -get_from_module(Module) ->
    -    Module:data().
    +ensure_exists(FilePath) ->
    +    case filelib:is_regular(FilePath) of
    +        true ->
    +            ok;
    +        false ->
    +            {error, {notfound, FilePath}}
    --- End diff --
    
    I'll rename the function into `check_exists`.


---
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