2016-03-15 30 views
2

以下連同LYSE這本書,在關於Mnesia的一章中,作者提供了一個使用fun2ms的查詢。出於某種原因,給我一個錯誤:fun2ms應該被轉換嗎?

$ rebar3 ct 
[0;32m===> Verifying dependencies... 
[0m[0;32m===> Compiling mafiapp 
[0m[0;32m===> Running Common Test suites... 
[0m%%% mafiapp_SUITE ==> add_service: [0;32mOK[0m[0m 

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
mnesia:wrap_trans failed on line 399 
Reason: aborted 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

%%% mafiapp_SUITE ==> friend_by_name: [0;31mFAILED[0m[0m 
%%% mafiapp_SUITE ==> [0;31m{failed,{aborted,{badarg,{ets,fun2ms, 
           [function,called,with,real,'fun',should,be, 
           transformed,with,parse_transform,'or',called, 
           with,a,'fun',generated,in,the,shell]}}}}[0m[0m 

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
mnesia:wrap_trans failed on line 399 
Reason: aborted 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

%%% mafiapp_SUITE ==> friend_with_services: [0;31mFAILED[0m[0m 
%%% mafiapp_SUITE ==> [0;31m{failed,{aborted,{badarg,{ets,fun2ms, 
           [function,called,with,real,'fun',should,be, 
           transformed,with,parse_transform,'or',called, 
           with,a,'fun',generated,in,the,shell]}}}}[0m[0m 
Failed 2 tests. Passed 1 tests. 
Results written to "d:/Projects/vms/erlang/projects/erlang-patterns/src/examples/mafiapp/_build/test/logs/index.html". 
[0;31m===> Failures occured running tests: 2 
[0m 

find_services功能如下mafiapp.erl文件:

-module(mafiapp). 

%% API exports 
-export([install/1, add_friend/4, add_service/4, friend_by_name/1]). 

%% Records 
-record(mafiapp_friends, {name, 
          contact=[], 
          info=[], 
          expertise}). 
-record(mafiapp_services, {from, 
          to, 
          date, 
          description}). 

%%==================================================================== 
%% API 
%%==================================================================== 
add_friend(Name, Contact, Info, Expertise) -> 
    F = fun() -> 
     mnesia:write(#mafiapp_friends{name=Name, contact=Contact, info=Info, expertise=Expertise}) 
    end, 
    mnesia:activity(transaction, F). 

add_service(From, To, Date, Description) -> 
    F = fun() -> 
     case mnesia:read({mafiapp_friends, From}) =:= [] orelse 
      mnesia:read({mafiapp_friends, To}) =:= [] of 
      true -> {error, unknown_friend}; 
      false -> mnesia:write(#mafiapp_services{from=From, to=To, date=Date, description=Description}) 
     end 
    end, 
    mnesia:activity(transaction, F). 

friend_by_name(Name) -> 
    F = fun() -> 
     case mnesia:read({mafiapp_friends, Name}) of 
      [#mafiapp_friends{contact=C, info=I, expertise=E}] -> 
       {Name,C,I,E,find_services(Name)}; 
      [] -> undefined 
     end 
    end, 
    mnesia:activity(transaction, F). 

install(Nodes) -> 
    ok = mnesia:create_schema(Nodes), 
    rpc:multicall(Nodes, application, start, [mnesia]), 
    mnesia:create_table(mafiapp_friends, 
     [{attributes, record_info(fields, mafiapp_friends)}, 
     {index, [#mafiapp_friends.expertise]}, 
     {disc_copies, Nodes}]), 
    mnesia:create_table(mafiapp_services, 
     [{attributes, record_info(fields, mafiapp_services)}, 
     {index, [#mafiapp_services.to]}, 
     {disc_copies, Nodes}, 
     {type, bag}]), 
    rpc:multicall(Nodes, application, stop, [mnesia]). 

%% Private functions 
find_services(Name) -> 
    Match = ets:fun2ms(
      fun(#mafiapp_services{from=From, to=To, date=D, description=Desc}) 
       when From =:= Name -> 
        {to, To, D, Desc}; 
       (#mafiapp_services{from=From, to=To, date=D, description=Desc}) 
       when To =:= Name -> 
        {from, From, D, Desc} 
      end 
    ), 
    mnesia:select(mafiapp_services, Match). 

請幫助。

+0

[erlang:用真正的'fun'調用的函數應該被轉換爲parse \ _transform?](http://stackoverflow.com/questions/6655064/erlang-function-called-with-real-fun-應待轉化的與 - 解析-TRANSFO) – Amiramix

回答

0

您必須將ms_transform解析轉換插入模塊以使ets:fun2ms正常工作。

-include_lib("stdlib/include/ms_transform.hrl"). 

有關更多詳細信息,請參閱ets:fun2ms/1文檔。

相關問題