<commit_before>-module(xnest_manager).
-behaviour(gen_server).

%% API.
-export([start_link/0]).
-export([get_xnest/1]).
-export([get_xnest_count/0]).

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


-define(TAB, ?MODULE).
-define(PID_INDEX, pid_to_name).

-record(state, {
}).

%%------------------------------------------------------------------------------------------------
%% API.
%%------------------------------------------------------------------------------------------------

%% @doc Start the xnest_manager gen_server.
-spec start_link() -> {ok, pid()}.
start_link() ->
	gen_server:start_link(?MODULE, [], []).

%% @doc Get the xnest by name, a new one will be created if the specfied is not exists.
-spec get_xnest(binary()) -> {ok, pid()}.
get_xnest(XNestName) ->
	gen_server:call(?MODULE, {get_xnest, XNestName}).

%% @doc Get current xnest number.
-spec get_xnest_count() -> integer().
get_xnest_count() ->
	0.

%%------------------------------------------------------------------------------------------------
%% gen_server.
%%------------------------------------------------------------------------------------------------

%% @private
init([]) ->
	?TAB = ets:new(?TAB, [private, named_table]),
	?PID_INDEX = ets:new(?PID_INDEX, [private, named_table]),
	process_flag(trap_exit, true),
	{ok, #state{}}.

%% @private
handle_call({get_xnest, XNestName}, _From,  State) ->
	XNest = case ets:lookup(?TAB, XNestName) of
		[{_XNestName, ExistXNest}|_] ->
			ExistXNest;
		_ ->
			{ok, NewXNest} = xnest:start_link(),
			ets:insert(?TAB, [{XNestName, NewXNest}]),
			ets:insert(?PID_INDEX, [{NewXNest, XNestName}]),
			NewXNest	
	end,
	{reply, XNest, State};
handle_call(_Request, _From, State) ->
	{reply, ignored, State}.

%% @private
handle_cast(_Msg, State) ->
	{noreply, State}.

%% @private
handle_info({'EXIT', FromPid, normal}, State) ->
	remove_xnest(FromPid),
        {noreply, State};<commit_msg>start_link with {local module}, if not , gen_server:call can not  find correct server<commit_after>-module(xnest_manager).
-include("lager.hrl").
-behaviour(gen_server).

%% API.
-export([start_link/0]).
-export([get_xnest/1]).
-export([get_xnest_count/0]).

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


-define(TAB, ?MODULE).
-define(PID_INDEX, pid_to_name).

-record(state, {
}).

%%------------------------------------------------------------------------------------------------
%% API.
%%------------------------------------------------------------------------------------------------

%% @doc Start the xnest_manager gen_server.
-spec start_link() -> {ok, pid()}.
start_link() ->
	gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).

%% @doc Get the xnest by name, a new one will be created if the specfied is not exists.
-spec get_xnest(binary()) -> {ok, pid()}.
get_xnest(XNestName) ->
	XNestPid = gen_server:call(?MODULE, {get_xnest, XNestName}),
	{ok, XNestPid}.

%% @doc Get current xnest number.
-spec get_xnest_count() -> integer().
get_xnest_count() ->
	0.

%%------------------------------------------------------------------------------------------------
%% gen_server.
%%------------------------------------------------------------------------------------------------

%% @private
init([]) ->
	?TAB = ets:new(?TAB, [private, named_table]),
	?PID_INDEX = ets:new(?PID_INDEX, [private, named_table]),
	process_flag(trap_exit, true),
	{ok, #state{}}.

%% @private
handle_call({get_xnest, XNestName}, _From,  State) ->
	XNest = case ets:lookup(?TAB, XNestName) of
		[{_XNestName, ExistXNest}|_] ->
			ExistXNest;
		_ ->
			{ok, NewXNest} = xnest:start_link(),
			ets:insert_new(?TAB, {XNestName, NewXNest}),
			ets:insert_new(?PID_INDEX, {NewXNest, XNestName}),
			NewXNest	
	end,
	{reply, XNest, State};


handle_call(_Request, _From, State) ->
	{reply, ignored, State}.

%% @private
handle_cast(_Msg, State) ->
	{noreply, State}.

%% @private
handle_info({'EXIT', FromPid, normal}, State) ->
	remove_xnest(FromPid),
        {noreply, State};
{'fraction_non_alphanumeric': 0.3484452883930691, 'fraction_numerical': 0.004747211013529551, 'mean_word_length': 2.073668854850474, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6500878', 'n_tokens_mistral': 1492, 'n_tokens_neox': 1391, 'n_words': 270}