返回首页
当前位置: 主页 > erlang >

step by step开发一个简易股票交易系统(五)---数据库

时间:2009-04-08 20:54来源:javaeye.com 作者:mooner 点击:
Step5:使用mnesia代替进程字典 1.创建mnesia表init_db.erl -module(init_db). -import(lists,[ foreach /2]). -compile(export_all). -include_lib( stdlib/include/qlc.hrl ). -record(stock,{id,pid}). do_this_once()- mnesia:create_schema(
  

Step5:使用mnesia代替进程字典
1.创建mnesia表init_db.erl

 
 

  1. -module(init_db).     
  2. -import(lists, [foreach/2]).     
  3. -compile(export_all).     
  4.     
  5. -include_lib("stdlib/include/qlc.hrl").     
  6. -record(stock,{id,pid}).     
  7.     
  8. do_this_once() ->     
  9.     mnesia:create_schema([node()]),     
  10.     mnesia:start(),     
  11.     mnesia:create_table(stock,[{ram_copies, [node()]},{attributes, record_info(fields,stock)}]),     
  12.     mnesia:stop().    

2.stock_server.erl

 
 

  1. -module(stock_server).     
  2.     
  3. -behaviour(gen_server).     
  4.     
  5. -export([list/1, sell/2, buy/2, start/0,stop/0,start_trade/1,stop_trade/1]).     
  6.     
  7. -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).     
  8.     
  9. -record(stock,{id,pid}).     
  10.     
  11. -import(lists, [foreach/2]).     
  12. -import(stock,[start/1,stop/1]).     
  13.     
  14. -include_lib("stdlib/include/qlc.hrl").     
  15.     
  16. start()->gen_server:start_link({local,stock_server},stock_server,[],[]).     
  17. stop()->gen_server:cast(?MODULE,stop).     
  18. sell(Id,Trade)->gen_server:cast(?MODULE,{sell,Id,Trade}).     
  19. buy(Id,Trade)->gen_server:cast(?MODULE,{buy,Id,Trade}).     
  20. list(Id)->gen_server:cast(?MODULE,{list,Id}).     
  21. start_trade(Id)->gen_server:cast(?MODULE,{start_trade,Id}).     
  22. stop_trade(Id)->gen_server:cast(?MODULE,{stop_trade,Id}).     
  23.     
  24. init([]) ->     
  25.     mnesia:start(),     
  26.     mnesia:wait_for_tables([stock], 20000),     
  27.     foreach(fun start_stock/1,getStock()),     
  28.     {ok, []}.     
  29.     
  30. handle_call(_Request, _From, State) ->     
  31.     Reply = ok,     
  32.     {reply, Reply, State}.     
  33.     
  34.     
  35. handle_cast(stop,State) ->     
  36.     {stop,normal,State};     
  37.     
  38. handle_cast({start_trade,Id},State) ->     
  39.     start_stock(Id),     
  40.     {noreply,  State};     
  41. handle_cast({stop_trade,Id},State) ->     
  42.     case getPid(Id) of     
  43.         []->     
  44.             io:format("stock Id=~w no such stock~n",[Id]);     
  45.         [stop]->     
  46.             io:format("stock Id=~w have no start trade~n",[Id]);     
  47.              
  48.         [Pid]->     
  49.             gen_server:cast(Pid,stop),     
  50.             removeId(Id)         
  51.     end,     
  52.     {noreply, State};     
  53.     
  54. handle_cast({list,Id}, State) ->     
  55.     case getPid(Id) of     
  56.         []->     
  57.             io:format("stock Id=~w no such stock~n",[Id]);     
  58.         [stop]->     
  59.             io:format("stock Id=~w have no start trade~n",[Id]);     
  60.         [Pid]->     
  61.             gen_server:cast(Pid,{list,5})     
  62.     end,     
  63.     {noreply,  State};     
  64.     
  65. handle_cast({buy,Id,Trade}, State) ->     
  66.     case getPid(Id) of     
  67.         []->     
  68.             io:format("stock Id=~w no such stock~n",[Id]);     
  69.         [stop]->     
  70.             io:format("stock Id=~w have no start trade~n",[Id]);     
  71.         [Pid]->     
  72.                  
  73.             gen_server:cast(Pid,{buy,Trade})     
  74.     end,     
  75.     {noreply,  State};     
  76.     
  77. handle_cast({sell,Id,Trade}, State) ->     
  78.     case getPid(Id) of     
  79.         []->     
  80.             io:format("stock Id=~w no such stock~n",[Id]);     
  81.         [stop]->     
  82.             io:format("stock Id=~w have no start trade~n",[Id]);     
  83.         [Pid]->     
  84.                  
  85.             gen_server:cast(Pid,{sell,Trade})     
  86.     end,     
  87.     {noreply,  State};     
  88.     
  89. handle_cast({ok,Msg}, State) ->     
  90.     io:format("ok: ~w~n",[Msg]),     
  91.     {noreply, State};     
  92.     
  93. handle_cast(_Msg, State) ->     
  94.     {noreply, State}.     
  95.     
  96. handle_info(_Info, State) ->     
  97.     {noreply, State}.     
  98.     
  99. terminate(_Reason, _State) ->     
  100.     foreach(fun stop_stock/1,getStock()),       
  101.     mnesia:stop(),         
  102.     ok.     
  103.     
  104. code_change(_OldVsn, State, _Extra) ->     
  105.     {ok, State}.     
  106.     
  107. %% --------------------------------------------------------------------     
  108. %%% Internal functions     
  109. %% --------------------------------------------------------------------     
  110. removeId(Item) ->     
  111.     Oid = {stock, Item},     
  112.     F = fun() ->     
  113.                 mnesia:delete(Oid)     
  114.         end,     
  115.     mnesia:transaction(F).     
  116.     
  117. saveId(Id,Pid)->     
  118.     Row = #stock{id=Id,pid=Pid},     
  119.     F = fun() ->     
  120.                 mnesia:write(Row)     
  121.         end,     
  122.     mnesia:transaction(F).     
  123.     
  124. getStock()->     
  125.     do(qlc:q([{X#stock.id,X#stock.pid} || X <- mnesia:table(stock)])).     
  126.     
  127. getPid(Id)->     
  128.     do(qlc:q([X#stock.pid || X <- mnesia:table(stock),     
  129.                              X#stock.id =:=Id])).     
  130.     
  131. do(Q) ->     
  132.     F = fun() -> qlc:e(Q) end,     
  133.     {atomic, Val} = mnesia:transaction(F),     
  134.     Val.     
  135.     
  136. start_stock({Id,_})->     
  137.     case gen_server:start(stock,[],[]) of     
  138.         {ok,Pid}->saveId(Id,Pid);     
  139.         Any->     
  140.             io:format("start_trade error:~w~n",[Any])     
  141.     end.     
  142.     
  143. stop_stock({Id,Pid})->         
  144.     case Pid of     
  145.         stop->     
  146.             ok;     
  147.         Pid->     
  148.             gen_server:cast(Pid, stop)     
  149.     end,     
  150.     saveId(Id,stop).    

顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
最新评论 查看所有评论
发表评论 查看所有评论
发布者资料
amw 查看详细资料 发送留言 加为好友 用户等级:高级会员 注册时间:2009-03-30 13:03 最后登录:2012-01-17 11:01
推荐内容