erlang ets写入mysql_[Erlang]配置文件读取方式(ETS,BEAM)性能研究

1 %%%-------------------------------------------------------------------

2 %%% @author zhongwencool@gmail.com

3 %%% @doc 把对应的.config文件转为.beam文件并更新到内存

4 %%% configName:all() return all key_value

5 %%% configName:lookup(Key) return value or undefined

6 %%% 1.得到目录下文件集合

7 %%% 2.遍历对每个文件进行转换

8 %%% 2.1 把文件读入 检查格式

9 %%% 2.2 把config转化为erl文件

10 %%% 2.2 把erl文件编译成beam文件

11 %%% 3.加载beam文件到内存

12 %%% @end

13 %%% Created : 22. Aug 2014 4:08 PM

14 %%%-------------------------------------------------------------------

15 -module(configToBeam).16

17 -compile(export_all).18 %%API

19 -export([config_to_beam/0,config_to_beam/1,get_config_dir/0]).20

21 %%@doc 把指定目录下全部的config文件都转为.beam文件并加载到内存

22 config_to_beam() ->

23 AllConfigName =get_all_config_name(),24 [begin

25 config_to_beam(ConfigName)26 end||ConfigName

28 %%@doc 把指定目录下名为ConfigName的config文件转为.beam文件并加载到内存

29 config_to_beam(ConfigName) ->

30 case check_syntax(ConfigName) of

31 {ok, TermList} ->

32 term_to_beam(ConfigName -- ".config", TermList);33 Reason ->

34 io:format("check Config syntax error~p:Reason:~p~n",[ConfigName, Reason]),35 {Reason,ConfigName}36 end.37

38 %%@todo 改成你存放.config文件的目录

39 get_config_dir() ->

40 code:root_dir()++"/config".41

42 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

43 %%% Internal Function

44 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

45 term_to_beam(ModuleName,TermList) ->

46 Erl =term_to_erl(ModuleName,TermList),47 ModuleName2 = ModuleName++".erl",48 file:write_file(ModuleName2, Erl, [write, binary, {encoding, utf8}]),49 compile:file(ModuleName2),50 ModuleName3 =list_to_atom(ModuleName),51 code:purge(ModuleName3),52 code:load_file(ModuleName3),53 {ok,ModuleName3}.54

55 term_to_erl(ModuleName,TermList) ->

56 StrValue = lists:flatten(term_to_erl2(TermList,"")),57 StrList = lists:flatten(io_lib:format(" ~w

", [TermList])),58 "59 -module(" ++ ModuleName ++ ").60 -export([all/0,lookup/1]).61

62 all()->"++ StrList ++".63

64 lookup(Key) ->

65 case Key of

66 " ++ StrValue ++ "

67 _ ->undefined68 end.69 ".

70

71 term_to_erl2([],Sum) ->

72 Sum;73 term_to_erl2([{Key, Value}|Left],Acc) ->

74 term_to_erl2(Left,75 io_lib:format(" ~w -> ~w;

",[Key, Value])++Acc).76

77 get_all_config_name() ->

78 {ok,AllFileName} =file:list_dir(get_config_dir()),79 lists:filter(fun(FileName) ->

80 case lists:reverse(FileName) of

81 "gifnoc." ++_ -> true;82 _ -> false

83 end

84 end,AllFileName).85

86 check_syntax(ConfigName) ->

87 case file:consult(joint_path(ConfigName)) of

88 {ok, TermList = [_|_]} ->

89 check_fromat_duplicate(TermList, []);90 Reason ->

91 {error, Reason}92 end.93

94 joint_path(ConfigName) ->

95 get_config_dir() ++"/" ++ConfigName.96

97 check_fromat_duplicate([], Acc) ->

98 {ok, Acc};99 check_fromat_duplicate([{Key, _Value} = Term|Left], Acc) ->

100 case lists:keymember(Key, 1, Acc) of

101 true ->{error, key_duplicate, Key};102 false -> check_fromat_duplicate(Left, [Term|Acc])103 end;104 check_fromat_duplicate([Term|_],_Acc) ->

105 {error,format_error,Term}.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值