<commit_before>%%%-------------------------------------------------------------------
%%% File:      mediator.erl
%%% @author    <NAME> <> []
%%% @copyright 2008 <NAME>
%%% @doc  
%%% N = Replication factor of data.
%%% R = Number of hosts that need to participate in a successful read operation
%%% W = Number of hosts that need to participate in a successful write operation
%%% @end  
%%%
%%% @since 2008-04-12 by <NAME>
%%%-------------------------------------------------------------------
-module(mediator).
-author('cliff moon').

-behaviour(gen_server).

%% API
-export([start_link/1, get/1, put/3, has_key/1, delete/1, stop/0]).

%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
         terminate/2, code_change/3]).

-include("config.hrl").

-record(mediator, {config}).
  
-ifdef(TEST).
-include("etest/mediator_test.erl").
-endif.

%%====================================================================
%% API
%%====================================================================
%%--------------------------------------------------------------------
%% @spec start_link() -> {ok,Pid} | ignore | {error,Error}
%% @doc Starts the server
%% @end 
%%--------------------------------------------------------------------
start_link(Config) ->
  gen_server:start_link({local, mediator}, ?MODULE, Config, []).

get(Key) -> 
  gen_server:call(mediator, {get, Key}).
  
put(Key, Context, Value) -> 
  gen_server:call(mediator, {put, Key, Context, Value}).
  
has_key(Key) -> 
  gen_server:call(mediator, {has_key, Key}).

delete(Key) ->
  gen_server:call(mediator, {delete, Key}).
  
stop() ->
  gen_server:call(mediator, stop).



%%====================================================================
%% gen_server callbacks
%%====================================================================

%%--------------------------------------------------------------------
%% @spec init(Args) -> {ok, State} |
%%                         {ok, State, Timeout} |
%%                         ignore               |
%%                         {stop, Reason}
%% @doc Initiates the server
%% @end 
%%--------------------------------------------------------------------
init(Config) ->
    {ok, #mediator{config=configuration:get_config()}}.

%%--------------------------------------------------------------------
%% @spec 
%% handle_call(Request, From, State) -> {reply, Reply, State} |
%%                                      {reply, Reply, State, Timeout} |
%%                                      {noreply, State} |
%%                                      {noreply, State, Timeout} |
%%                                      {stop, Reason, Reply, State} |
%%                                      {stop, Reason, State}
%% @doc Handling call messages
%% @end 
%%--------------------------------------------------------------------
handle_call({get, Key}, _From, State) ->
  {reply, internal_get(Key, State), State};
  
handle_call({put, Key, Context, Value}, _From, State) ->
  {reply, internal_put(Key, Context, Value, State), State};
  
handle_call({has_key, Key}, _From, State) ->
  {reply, internal_has_key(Key, State), State};
  
handle_call({delete, Key}, _From, State) ->
  {reply, internal_delete(Key, State), State};
  
handle_call(stop, _From, State) ->
  {stop, shutdown, ok, State}.

%%--------------------------------------------------------------------
%% @spec handle_cast(Msg, State) -> {noreply, State} |
%%                                      {noreply, State, Timeout} |
%%                                      {stop, Reason, State}<commit_msg>Make mediator spawn processes to actually handle activity and async reply for better concurrency<commit_after>%%%-------------------------------------------------------------------
%%% File:      mediator.erl
%%% @author    <NAME> <> []
%%% @copyright 2008 <NAME>
%%% @doc  
%%% N = Replication factor of data.
%%% R = Number of hosts that need to participate in a successful read operation
%%% W = Number of hosts that need to participate in a successful write operation
%%% @end  
%%%
%%% @since 2008-04-12 by <NAME>
%%%-------------------------------------------------------------------
-module(mediator).
-author('cliff moon').

-behaviour(gen_server).

%% API
-export([start_link/1, get/1, put/3, has_key/1, delete/1, stop/0]).

%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
         terminate/2, code_change/3]).

-include("config.hrl").

-record(mediator, {config}).
  
-ifdef(TEST).
-include("etest/mediator_test.erl").
-endif.

%%====================================================================
%% API
%%====================================================================
%%--------------------------------------------------------------------
%% @spec start_link() -> {ok,Pid} | ignore | {error,Error}
%% @doc Starts the server
%% @end 
%%--------------------------------------------------------------------
start_link(Config) ->
  gen_server:start_link({local, mediator}, ?MODULE, Config, []).

get(Key) -> 
  gen_server:call(mediator, {get, Key}).
  
put(Key, Context, Value) -> 
  gen_server:call(mediator, {put, Key, Context, Value}).
  
has_key(Key) -> 
  gen_server:call(mediator, {has_key, Key}).

delete(Key) ->
  gen_server:call(mediator, {delete, Key}).
  
stop() ->
  gen_server:call(mediator, stop).



%%====================================================================
%% gen_server callbacks
%%====================================================================

%%--------------------------------------------------------------------
%% @spec init(Args) -> {ok, State} |
%%                         {ok, State, Timeout} |
%%                         ignore               |
%%                         {stop, Reason}
%% @doc Initiates the server
%% @end 
%%--------------------------------------------------------------------
init(Config) ->
    {ok, #mediator{config=configuration:get_config()}}.

%%--------------------------------------------------------------------
%% @spec 
%% handle_call(Request, From, State) -> {reply, Reply, State} |
%%                                      {reply, Reply, State, Timeout} |
%%                                      {noreply, State} |
%%                                      {noreply, State, Timeout} |
%%                                      {stop, Reason, Reply, State} |
%%                                      {stop, Reason, State}
%% @doc Handling call messages
%% @end 
%%--------------------------------------------------------------------
handle_call({get, Key}, From, State) ->
    spawn_link(fun() ->
                       gen_server:reply(From, internal_get(Key, State))
               end),
    {noreply, State};

handle_call({put, Key, Context, Value}, From, State) ->
    spawn_link(fun() ->
                       gen_server:reply(From, internal_put(Key, Context, Value, State))
               end),
    {noreply, State};

handle_call({has_key, Key}, From, State) ->
    spawn_link(fun() ->
                       gen_server:reply(From, internal_has_key(Key, State))
               end),
    {noreply, State};

handle_call({delete, Key}, From, State) ->
    spawn_link(fun() ->
                       gen_server:reply(From, internal_delete(Key, State))
               end),
    {noreply, State};
  
handle_call(stop, _From, State) ->
  {stop, shutdown, ok, State}.

%%--------------------------------------------------------------------
%% @spec handle_cast(Msg, State) -> {noreply, State} |
%%                                      {noreply, State, Timeout} |
%%                                      {stop, Reason, State}
{'fraction_non_alphanumeric': 0.3617327574924748, 'fraction_numerical': 0.006281900274833137, 'mean_word_length': 1.5422488356620092, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5066666', 'n_tokens_mistral': 2039, 'n_tokens_neox': 1904, 'n_words': 446}