register/spawn で並列化してみた。

compile(export_all) は、どうせ本気では使わないとおもうので。
それにしても、ほんっとこつがつかめない言語です、
おそらくこの書き方もまた中途半端なんだろうなと思います。
これにネットワークが関係すると、さらに面倒になる気がします。
本来は、do() も並列化すべきだとおもいますが ... そのうちやります。

たいして面白くないソースですが。

mnesia_test.erl

%% vim: fileencoding=utf8 sts=4 sw=4

-module(mnesia_test).
%-compile(export_all).
-export([init/0, start/0, auth_demo/1]).
-include_lib("stdlib/include/qlc.hrl").
-record(users, {user_name, user_password}).

% > c(mnesia_test).
% > mnesia_test:init().
% > mnesia_test:start().
% > mnesia_test:auth_demo(accept).
% > mnesia_test:auth_demo(reject).
% > mnesia_test:auth_demo({"ham", "XQQS5R91"}).

% > timer:tc(mnesia_test, auth_demo, [{"eggs", "bgOYF0bT"}]).
% {9,{user,<<"eggs">>,<<"bgOYF0bT">>}}
% accept
% > timer:tc(mnesia_test, auth_demo, [{"bacon", "XQQS5R91"}]).
% {9,{user,<<"bacon">>,<<"XQQS5R91">>}}
% reject


users_table() ->
    [
        {users, <<"spam">>, <<"eubIZs47">>},
        {users, <<"eggs">>, <<"bgOYF0bT">>},
        {users, <<"bacon">>, <<"QekmMgcP">>},
        {users, <<"ham">>, <<"XQQS5R91">>}
    ].

insert_users_table() ->
    F = fun() ->
            lists:foreach(fun mnesia:write/1, users_table())
        end,
    mnesia:transaction(F).

init() ->
    mnesia:create_schema([node()]),
    mnesia:start(),
    mnesia:create_table(users, [{attributes, record_info(fields, users)}]),
    mnesia:stop().

start() ->
    mnesia:start(),
    mnesia:clear_table(users),
    case mnesia:clear_table(users) of
        {aborted, _} ->
            aboarted;
        {atomic, ok} ->
            insert_users_table()
    end,
    register(spam, spawn(fun() -> auth_spam() end)),
    register(eggs, spawn(fun() -> auth_eggs() end)),
    register(do, spawn(fun() -> do() end)),
    register(loop, spawn(fun() -> loop() end)).

% mnesia_test:auth_demo({"soundkitchen", "mitszo"}).
auth_demo({UserName, Password}) ->
    spam ! {user, list_to_binary(UserName),
                  list_to_binary(Password)};
auth_demo(accept) ->
    spam ! {user, <<"spam">>, <<"eubIZs47">>};
auth_demo(reject) ->
    spam ! {user, <<"spam">>, <<"spam">>}.

loop() ->
    receive
        reject ->
            io:format("~p~n", [reject]),
            loop();
        accept ->
            io:format("~p~n", [accept]),
            loop()
    end.

auth_spam() ->
    receive
        {user, UserName, UserPassword} ->
            eggs ! {user, UserName, UserPassword},
            auth_spam();
        {result, Result} ->
            case Result of
                [] ->
                    loop ! reject;
                [{_, _}] ->
                    loop ! accept
            end,
            auth_spam()
    end.

auth_eggs() ->
    receive
        {user, UserName, UserPassword} ->
            Q = qlc:q([{User#users.user_name, User#users.user_password} || 
                                    User <- mnesia:table(users),
                                    User#users.user_name =:= UserName,
                                    User#users.user_password =:= UserPassword],
                                    {cache, ets}),
            do ! Q,
            auth_eggs()
    end.

do() ->
    receive
        Q ->
            F = fun() -> qlc:e(Q) end,
            {atomic, Result} = mnesia:transaction(F),
            spam ! {result, Result},
            do()
    end.