<commit_before>%% Copyright 2010 <NAME>
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%% 
%%     http://www.apache.org/licenses/LICENSE-2.0
%% 
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.

%%% -------------------------------------------------------------------
%%% Author  : <NAME> <EMAIL>
%%% Description :
%%%
%%% Created : 
%%% -------------------------------------------------------------------
-module(janga_ets_mgr).

-behaviour(gen_server).
%% --------------------------------------------------------------------
%% Include files
%% --------------------------------------------------------------------
%% --------------------------------------------------------------------
%% External exports

%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
-export([start_link/0]).
-export([start/0]).
-export([init_table/3, backup/1, restore/1, delete/1]).

%% ====================================================================
%% External functions
%% ====================================================================
init_table(Pid, Name, Data) ->
	gen_server:call(?MODULE, {init_table, Pid, Name, Data}).
backup(Table) ->
    gen_server:call(?MODULE, {backup_table, Table}).
restore(Table) ->
    gen_server:call(?MODULE, {restore_table, Table}).
delete(Table) when is_list(Table) ->
    delete(list_to_atom(Table));    
delete(Table) when is_atom(Table) ->
    gen_server:call(?MODULE, {delete_table, Table}).

%% --------------------------------------------------------------------
%% record definitions
%% --------------------------------------------------------------------
-record(state, {}).
%% ====================================================================
%% Server functions
%% ====================================================================
%%--------------------------------------------------------------------
%% Function: start_link() -> {ok,Pid} | ignore | {error,Error}
%% Description: Starts the server
%%--------------------------------------------------------------------
start_link() ->
    gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).

start() ->
	start_link().
%% --------------------------------------------------------------------
%% Function: init/1
%% Description: Initiates the server
%% Returns: {ok, State}          |
%%          {ok, State, Timeout} |
%%          ignore               |
%%          {stop, Reason}
%% --------------------------------------------------------------------
init([]) ->
	process_flag(trap_exit, true),
    {ok, #state{}}.

%% --------------------------------------------------------------------
%% Function: handle_call/3
%% Description: Handling call messages
%% Returns: {reply, Reply, State}          |
%%          {reply, Reply, State, Timeout} |
%%          {noreply, State}               |
%%          {noreply, State, Timeout}      |
%%          {stop, Reason, Reply, State}   | (terminate/2 is called)
%%          {stop, Reason, State}            (terminate/2 is called)
%% --------------------------------------------------------------------
handle_call({init_table, Pid, Name, Data}, _From, State) ->
	lager:info("init_table for name : ~p ", [Name]),
	link(Pid),
	Table_Name = case ets:info(Name) of 
		undefined ->
			lager:info("create new ets table ~p", [Name]),
		    TableId = ets:new(Name, [public, named_table]),
    		ets:insert(TableId, Data),
    		ets:setopts(TableId, {heir, self(), Data}),
    		ets:give_away(TableId, Pid, Data),
    		TableId;
    	_InfoList ->
    		lager:info("use existing ets table : ~p", [Name]),
    		ets:setopts(Name, {heir, self(), Data}),
    		ets:give_away(Name, Pid, Data),	 
    		Name
    end,
    {reply, Table_Name, State};

handle_call({backup_table, Table}, _From, State) ->
    {ok, Path}= application:get_env(horst, backup_path),
    ok = check_path(Path),
    Reply = ets:tab2file(Table, filename:join(filename:absname(Path), atom_to_list(Table))),
    {reply, Reply, State};

handle_call({restore_table, Table}, _From, State) ->
    {ok, Path}= application:get_env(horst, backup_path),
    ok = check_path(Path),
    Reply = ets:file2tab(filename:join(filename:absname(Path), atom_to_list(Table)) , [{verify,true}]), 
    {reply, Reply, State};

handle_call({delete_table, Table}, _From, State) ->
    ets:delete(Table),
    {reply, ok, State};

handle_call(_Request, _From, State) ->
    Reply = ok,
    {reply, Reply, State}.

%% --------------------------------------------------------------------
%% Function: handle_cast/2
%% Description: Handling cast messages
%% Returns: {noreply, State}          |
%%          {noreply, State, Timeout} |
%%          {stop, Reason, State}            (terminate/2 is called)
%% --------------------------------------------------------------------
handle_cast(_Msg, State) ->
    {noreply, State}.

%% --------------------------------------------------------------------
%% Function: handle_info/2
%% Description: Handling all non call/cast messages
%% Returns: {noreply, State}          |
%%          {noreply, State, Timeout} |
%%          {stop, Reason, State}            (terminate/2 is called)
%% --------------------------------------------------------------------
handle_info({'EXIT',Pid, killed}, State) ->
    lager:info("Thing (~p) !! is now dead, farewell TableId: ~n", [Pid]),
    {noreply, State};

handle_info({'EXIT',Pid, Reason}, State) -><commit_msg>added handlingg of undefined ets<commit_after>%% Copyright 2010 <NAME>
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%% 
%%     http://www.apache.org/licenses/LICENSE-2.0
%% 
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.

%%% -------------------------------------------------------------------
%%% Author  : <NAME> <EMAIL>
%%% Description :
%%%
%%% Created : 
%%% -------------------------------------------------------------------
-module(janga_ets_mgr).

-behaviour(gen_server).
%% --------------------------------------------------------------------
%% Include files
%% --------------------------------------------------------------------
%% --------------------------------------------------------------------
%% External exports

%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
-export([start_link/0]).
-export([start/0]).
-export([init_table/3, backup/1, restore/1, delete/1]).

%% ====================================================================
%% External functions
%% ====================================================================
init_table(Pid, Name, Data) ->
	gen_server:call(?MODULE, {init_table, Pid, Name, Data}).
backup(Table) ->
    gen_server:call(?MODULE, {backup_table, Table}).
restore(Table) ->
    gen_server:call(?MODULE, {restore_table, Table}).
delete(Table) when is_list(Table) ->
    delete(list_to_atom(Table));    
delete(Table) when is_atom(Table) ->
    gen_server:call(?MODULE, {delete_table, Table}).

%% --------------------------------------------------------------------
%% record definitions
%% --------------------------------------------------------------------
-record(state, {}).
%% ====================================================================
%% Server functions
%% ====================================================================
%%--------------------------------------------------------------------
%% Function: start_link() -> {ok,Pid} | ignore | {error,Error}
%% Description: Starts the server
%%--------------------------------------------------------------------
start_link() ->
    gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).

start() ->
	start_link().
%% --------------------------------------------------------------------
%% Function: init/1
%% Description: Initiates the server
%% Returns: {ok, State}          |
%%          {ok, State, Timeout} |
%%          ignore               |
%%          {stop, Reason}
%% --------------------------------------------------------------------
init([]) ->
	process_flag(trap_exit, true),
    {ok, #state{}}.

%% --------------------------------------------------------------------
%% Function: handle_call/3
%% Description: Handling call messages
%% Returns: {reply, Reply, State}          |
%%          {reply, Reply, State, Timeout} |
%%          {noreply, State}               |
%%          {noreply, State, Timeout}      |
%%          {stop, Reason, Reply, State}   | (terminate/2 is called)
%%          {stop, Reason, State}            (terminate/2 is called)
%% --------------------------------------------------------------------
handle_call({init_table, Pid, Name, Data}, _From, State) ->
	lager:info("init_table for name : ~p ", [Name]),
	link(Pid),
	Table_Name = case ets:info(Name) of 
		undefined ->
			lager:info("create new ets table ~p", [Name]),
		    TableId = ets:new(Name, [public, named_table]),
    		ets:insert(TableId, Data),
    		ets:setopts(TableId, {heir, self(), Data}),
    		ets:give_away(TableId, Pid, Data),
    		TableId;
    	_InfoList ->
    		lager:info("use existing ets table : ~p", [Name]),
    		ets:setopts(Name, {heir, self(), Data}),
    		ets:give_away(Name, Pid, Data),	 
    		Name
    end,
    {reply, Table_Name, State};

handle_call({backup_table, Table}, _From, State) ->
    {ok, Path}= application:get_env(horst, backup_path),
    ok = check_path(Path),
    Reply = ets:tab2file(Table, filename:join(filename:absname(Path), atom_to_list(Table))),
    {reply, Reply, State};

handle_call({restore_table, Table}, _From, State) ->
    {ok, Path}= application:get_env(horst, backup_path),
    ok = check_path(Path),
    Reply = ets:file2tab(filename:join(filename:absname(Path), atom_to_list(Table)) , [{verify,true}]), 
    {reply, Reply, State};

handle_call({delete_table, undefined}, _From, State) ->
    {reply, ok, State};

handle_call({delete_table, Table}, _From, State) ->
    ets:delete(Table),
    {reply, ok, State};

handle_call(_Request, _From, State) ->
    Reply = ok,
    {reply, Reply, State}.

%% --------------------------------------------------------------------
%% Function: handle_cast/2
%% Description: Handling cast messages
%% Returns: {noreply, State}          |
%%          {noreply, State, Timeout} |
%%          {stop, Reason, State}            (terminate/2 is called)
%% --------------------------------------------------------------------
handle_cast(_Msg, State) ->
    {noreply, State}.

%% --------------------------------------------------------------------
%% Function: handle_info/2
%% Description: Handling all non call/cast messages
%% Returns: {noreply, State}          |
%%          {noreply, State, Timeout} |
%%          {stop, Reason, State}            (terminate/2 is called)
%% --------------------------------------------------------------------
handle_info({'EXIT',Pid, killed}, State) ->
    lager:info("Thing (~p) !! is now dead, farewell TableId: ~n", [Pid]),
    {noreply, State};

handle_info({'EXIT',Pid, Reason}, State) ->
{'fraction_non_alphanumeric': 0.37043330501274424, 'fraction_numerical': 0.005097706032285472, 'mean_word_length': 1.7342624854819977, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 48, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7519238', 'n_tokens_mistral': 3140, 'n_tokens_neox': 2995, 'n_words': 755}