Skip to content

Commit

Permalink
Merge pull request #3 from nekrograve/master
Browse files Browse the repository at this point in the history
Merged with @manpages
  • Loading branch information
dmitriid committed Jul 11, 2012
2 parents 0b11e2f + b38f303 commit 78e8340
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 68 deletions.
32 changes: 6 additions & 26 deletions apps/hint_search/src/hs_engine_mfa.erl
Expand Up @@ -7,8 +7,7 @@
%%
%% API
%%
-export([ mfa_to_ets/2
, modules_matching/2
-export([ modules_matching/2
]).

%%%
Expand All @@ -22,26 +21,13 @@
%%% External
%%%

mfa_to_ets(MFAList, {M2FA, F2MA}) ->
{Ms, Fs} = lists:foldl(fun({M, F, A}, {Mods, Funs}) ->
ets:insert(M2FA, {F, A, M}),
ets:insert(F2MA, {M, A, F}),
Mods1 = sets:add_element(atom_to_binary(M, utf8), Mods),
Funs1 = sets:add_element(atom_to_binary(F, utf8), Funs),
{Mods1, Funs1}
end, {sets:new(), sets:new()}, MFAList),
{Ms, Fs}.

modules_matching(Module, MFAs) ->
%?debugFmt(">MODULE>>> ~p", [Module]),
case Module of
[] -> MFAs;
_ ->
ModuleString = atom_to_list(Module),
ModuleUniversum = atoms_to_utf_set([element(1, X) || X <- MFAs]), % I don't do drugs, honestly
%?debugFmt(">RELEVANT MODULES>>>~n~p", [sets:to_list(ModuleUniversum)]), % it's just time for spawnfest running out
MM = do_modules_matching(ModuleUniversum, hint_mfa_req:fuzzy_matches(ModuleString)), % ~jonn mostovoy
%?debugFmt("~n!!!!!!!????? MFA ~p", [[MFA || {M,_,_}=MFA <- MFAs, lists:member(M, MM)]]),
ModuleUniversum = atoms_to_utf_set([element(1, X) || X <- MFAs]),
MM = do_modules_matching(ModuleUniversum, hint_mfa_req:fuzzy_matches(ModuleString)),
[MFA || {M,_,_}=MFA <- MFAs, lists:member(M, MM)]
end
.
Expand All @@ -51,19 +37,13 @@ modules_matching(Module, MFAs) ->
%%%

do_modules_matching(RelevantModules, ModExprs) ->
_ModExact = sets:filter(fun(Element) -> %% ModExact, your hour will come.
list_to_binary(proplists:get_value(exact, ModExprs)) == Element
end, RelevantModules),
[_ModStartsWith, ModContains] = [sets:filter(fun(Element) ->
case re:run(Element, proplists:get_value(ReMode, ModExprs), []) of
ModContains = sets:filter(fun(Element) ->
case re:run(Element, proplists:get_value(contains, ModExprs), []) of
nomatch -> false;
_ -> true
end
end, RelevantModules) || ReMode <- [starts_with, contains]],
%?debugMsg("Still Alive"),
% Треш, угар, содомия
end, RelevantModules),
[binary_to_atom(B,latin1)||B<-sets:to_list(ModContains)].
%{sets:to_list(ModExact), sets:to_list(ModStartsWith), sets:to_list(ModContains)}.


-spec atoms_to_utf_set([atom()]) -> set().
Expand Down
51 changes: 15 additions & 36 deletions apps/hint_search/src/plt_cache_server.erl
Expand Up @@ -33,7 +33,7 @@
%%%

-define(POOL, pool_plt_cache_server).
-record(state, {plt, ets, ets_m2fa, ets_f2ma, fs, ms, file}).
-record(state, {plt, ets, file}).

%%%
%%% Import
Expand Down Expand Up @@ -83,7 +83,7 @@ apply(MFA) ->
%%
%% Lookup arity
%%
-spec lookup(atom(), integer()) -> any().
-spec lookup(atom(), byte()) -> any().
lookup(Module, Arity) ->
Worker = poolboy:checkout(?POOL),
Res = gen_server:call(Worker, {lookup, {Module, Arity}}, infinity),
Expand All @@ -97,27 +97,22 @@ lookup(Module, Arity) ->
init(Args) ->
File = get_file(Args),
Plt = dialyzer_plt:from_file(File),
InitProplist = create_and_update_ets(Plt),
{Ets, M2FA, F2MA} = proplists:get_value(ets, InitProplist),
Ms = proplists:get_value(ms, InitProplist),
Fs = proplists:get_value(fs, InitProplist),
{ok, #state{plt = Plt, ets = Ets, file = File, ms = Ms, fs = Fs, ets_m2fa = M2FA, ets_f2ma = F2MA}}.
Ets = create_and_update_ets(Plt),
{ok, #state{plt = Plt, ets = Ets, file = File}}.

handle_call({load, File}, _From, #state{ets=Ets, ets_m2fa=M2FA, ets_f2ma=F2MA} = State) ->
handle_call({load, File}, _From, #state{ets=Ets} = State) ->
Plt = dialyzer_plt:from_file(File),
clean_update(Ets, M2FA, F2MA, Plt),
clean_update(Ets, Plt),
{reply, ok, State#state{plt=Plt, file=File}, hibernate};

handle_call(update, _From, #state{ets=Ets, ets_m2fa=M2FA, ets_f2ma=F2MA, file=File} = State) ->
handle_call(update, _From, #state{ets=Ets, file=File} = State) ->
Plt = dialyzer_plt:from_file(File),
clean_update(Ets, M2FA, F2MA, Plt),
clean_update(Ets, Plt),
{reply, ok, State, hibernate};

handle_call({lookup, {Module, Arity}}, _From, #state{ets=Ets, ms=Modules} = State) ->
%?debugFmt("=LOOKUP===~n~pets:lookup", [ets:lookup(Ets, Arity)]),
handle_call({lookup, {Module, Arity}}, _From, #state{ets=Ets} = State) ->
MFA = ets:lookup(Ets, Arity),
Matched = hs_engine_mfa:modules_matching(Module, MFA),
%?debugFmt("=LOOKED UP===~n~p", [Mss]),
{reply, Matched, State};

handle_call({apply, {M, F, A}}, _From, #state{plt=Plt} = State) ->
Expand Down Expand Up @@ -162,39 +157,23 @@ create_and_update_ets(Plt) ->
, {keypos, 3}
, {write_concurrency,true}
, {read_concurrency,true}]),
M2FA = ets:new(plt_cache_m2fa, [ set %% having all those bottlenecks, I see
, private %% those ets tables as premature optimization
, {keypos, 3} %% ~manpages
, {write_concurrency,true}
, {read_concurrency,true}]),
F2MA = ets:new(plt_cache_f2ma, [ bag
, private
, {keypos, 3}
, {write_concurrency,true}
, {read_concurrency,true}]),
{Ms, Fs} = update_ets(Ets, M2FA, F2MA, Plt),
[ {ets, {Ets, M2FA, F2MA}}
, {ms, Ms}
, {fs, Fs}
].
update_ets(Ets, Plt),
Ets.

clean_update(Ets, M2FA, F2MA, Plt) ->
clean_update(Ets, Plt) ->
ets:delete_all_objects(Ets),
ets:delete_all_objects(M2FA),
ets:delete_all_objects(F2MA),
update_ets(Ets, M2FA, F2MA, Plt).
update_ets(Ets, Plt).

update_ets(Ets0, M2FA, F2MA, Plt) ->
update_ets(Ets0, Plt) ->
AllModulesSet = dialyzer_plt:all_modules(Plt),
AllModules = sets:to_list(AllModulesSet),
% wrong heuristic :)
Modules = [M || M <- AllModules,
string:equal(atom_to_list(M),
string:to_lower(atom_to_list(M)))],
MFAList = get_mfas(Modules, Plt),
{Ms, Fs} = hs_engine_mfa:mfa_to_ets(MFAList, {M2FA, F2MA}),
ets:insert(Ets0, MFAList),
{Ms, Fs}.
ok.

get_mfas(Modules, Plt) -> get_mfas(Modules, Plt, []).

Expand Down
8 changes: 2 additions & 6 deletions site/src/saloon_main.erl
Expand Up @@ -7,10 +7,10 @@
-include_lib("eunit/include/eunit.hrl").

init({_Any, http}, Req, []) ->
saloon_init:prepare(Req),
{ok, Req, 0}.

handle(Req, State) ->
saloon_init:prepare(Req),
%?debugFmt(
% "erlydtl:compile: ~p~n",
% [
Expand All @@ -22,9 +22,6 @@ handle(Req, State) ->
% ]
%),
{ok, Rendered} = main_dtl:render([
%{name, <<"Forever Alone Guy">>},
%{friends, []},
%{primes, ["2", <<"3">>, 5, 7]}
{me, [
{category, <<"">>},
{controller, <<"saloon_main">>}
Expand All @@ -35,11 +32,10 @@ handle(Req, State) ->
[{name, <<"landing">>}]
]}
]),
?debugFmt("=RENDERING===~n~p~n", [Rendered]),
{ok, Rep} = cowboy_http_req:reply(
200, [], Rendered, Req
),
%{ok , Rep} = cowboy_http_req:reply(200, [], "ok", Req),

{ok, Rep, State+1}.

terminate(_R, _S) ->
Expand Down
2 changes: 2 additions & 0 deletions site/src/saloon_search.erl
Expand Up @@ -7,6 +7,7 @@
-include_lib("eunit/include/eunit.hrl").

init(_, Req, _) ->
saloon_init:prepare(Req),
{ok, Req, 0}.

handle(Req, State) ->
Expand All @@ -18,6 +19,7 @@ handle(Req, State) ->
case hint_search:q(Q) of
{error, _Reason} -> render(error);
{ok, Data} ->
?debugFmt("Results for query:~n~p", [Data]),
Data1 = lists:sublist(Data, 10),
render(Data1)
end;
Expand Down

0 comments on commit 78e8340

Please sign in to comment.