zoukankan      html  css  js  c++  java
  • [erlnag] ETS & DETS

    erlang提供的k-v存储 ets保存在内存里 dets保存在磁盘上

    ETS表与正常的进程存储空间是分离的,其存储区域与普通进程无关,ETS表隶属于创建它的进程,当这个进程死掉或者调用了ets:delete,ETS表就被删掉了

    测试&示例代码

     1 -module(test_ets).
     2 -compile(export_all).
     3 
     4 start() ->
     5     %% 4种不同表的类型
     6     lists:foreach(fun test/1,[set,ordered_set,bag,duplicate_bag]).
     7 
     8 test(Mode) ->
     9     %% 表类型 set,ordered_set,bag,duplicate_bag 
    10     %% 访问控制 private,protected,public
    11     %% named_table 后续操作使用tablename来操作 如 ets:inert(tablename,{a,1234}).
    12     TableId = ets:new(tablename,[Mode,named_table]),
    13     ets:insert(TableId,{a,1}),
    14     ets:insert(TableId,{b,2}),
    15     ets:insert(tablename,{a,1}), %% 使用tablename
    16     ets:insert(TableId,{a,3}),
    17     List = ets:tab2list(tablename),
    18     io:format("~-13w => ~p~n",[Mode,List]),
    19     ets:delete(tablename).
    20 
    21 test_new() ->
    22     ets:new(tablename,[set,public,named_table]).
    23 
    24 test_insert(Key,Value) ->
    25     ets:insert(tablename,{Key,Value}).
    26 
    27 test_lookup(Key) ->
    28     ets:lookup(tablename,Key).
    29 
    30 test_delete() ->
    31     ets:delete(tablename).
    32 
    33 %% dets
    34 %% 文件大小不能超过2G
    35 open(File) ->
    36     io:format("dets opened:~p~n",[File]),
    37     Bool = filelib:is_file(File),
    38     case dets:open_file(?MODULE,[{file,File}]) of
    39         {ok,?MODULE} ->
    40             case Bool of
    41                 true -> void;
    42                 false -> ok = dets:insert(?MODULE,{free,1})
    43             end,
    44             true;
    45         {error,Reason} ->
    46             io:format("cannot open dets table:~p~n",[Reason]),
    47             exit(eDetsOpen)
    48     end.
    49 
    50 close() -> dets:close(?MODULE).
    51 
    52 test_dets_insert(Key,Value) ->
    53     dets:insert(?MODULE,{Key,Value}).
    54 
    55 test_dets_lookup(Key) ->
    56     dets:lookup(?MODULE,Key).

    运行结果

    Eshell V5.8.4  (abort with ^G)
    1> c(test_ets).
    {ok,test_ets}
    2> test_ets:start().
    set           => [{b,2},{a,3}]
    ordered_set   => [{a,3},{b,2}]
    bag           => [{b,2},{a,1},{a,3}]
    duplicate_bag => [{b,2},{a,1},{a,1},{a,3}]
    ok
    3> T = ets:new(test,[public,set]).
    32784
    4> ets:insert(T,{key,23456}).
    true
    5> ets:lookup(T,key).        
    [{key,23456}]
    6> c(test_ets).                   
    {ok,test_ets}
    7> test_ets:open("test.bin").     
    dets opened:"test.bin"
    true
    8> test_ets:test_dets_insert(a,b).
    ok
    9> test_ets:test_dets_lookup(a).  
    [{a,b}]
  • 相关阅读:
    ES6新特性
    CSS + HTML 鼠标在图片上悬停时的显示文字,移走时文字消失
    APICloud closeToWin和closeWin的使用
    基于vue-cli配置移动端自适应
    Cookies,localStorage,sessionStorage,Web SQL Database(客户端)会话存储学习
    select标签默认选择选项
    MySQL数据库的配置
    jsp定义 ,JSON对象
    eclipse配置
    VS2017 C++编译时出现 "the windows sdk version for 8.1 was not found"解决办法(亲测有效)
  • 原文地址:https://www.cnblogs.com/bluefrog/p/2501965.html
Copyright © 2011-2022 走看看