Erlang ETS Table

 不须要显示用锁,插入和查询时间不只快并且控制为常量,这就是Erlang的ETS Table.php

   为何而设计?html

   Erlang中能够用List表达集合数据,可是若是数据量特别大的话在List中访问元素就会变慢了;这种主要是因为List的绝大部分操做都是基于遍历完成的.node

Erlang的设计目标是软实时(参考:http://en.wikipedia.org/wiki/Real-time_computing),在大量数据中检索的时间不只要快并且要求是常量.为了解决快速查git

询的问题,Erlang提供的机制就是ETS(Erlang Term Storage)和DETS(Disk Erlang Term Storage).本文只关注ETS.shell

 ETS基础     数据库

  1. ETS查询时间是常量,例外是若是使用ordered_set查询时间与logN成正比(N为存储的数据量)
  2. ETS 存储数据的格式是Tuple,下面的测试代码中咱们能够看到细节
  3. ETS Table由进程建立,进程销毁ETS Table也随着销毁,在使用Shell作ETS实验的时候要注意一下,Table的拥有关系能够give_away 转交给其它进程
  4. 一个Erlang节点的ETS表的数量是有限制的,默认是1400个表,在启动erlang节点以前修改 ERL_MAX_ETS_TABLES参数能够修改这个限制ejabberd社区站点上总结的性能调优中提到了这一点,点击这里查看: 

    http://www.ejabberd.im/tuning
  5. ETS表不在GC的管理范围内,除非拥有它的进程死掉它才会终止;能够经过delete删除数据 
  6. 目前版本,insert和lookup操做都会致使对象副本的建立,insert和lookup时间对于set bag duplicate_bag都是常量值与表大小无关.
  7. 并发控制:全部针对一个对象的更新都被保证是原子的、隔离的:修改要么所有成功要么失败。也没有其它的中间结果被其它的进程使用。有些方法能够在处理多个对象的时候保证这种原子性和隔离性。
    在数据库术语中隔离级别被称做序列化,就好像全部隔离的操做一个接一个严格按照顺序执行。
  8. 在遍历过程当中,可使用safe_fixtable来保证遍历过程当中不出现错误,全部数据项只被访问一遍.用到逐一遍历的场景就不多,使用safe_fixtable的情景就更少。不过这个机制是很是有用的,
    还记得在.net中版本中很麻烦的一件事情就是遍历在线玩家用户列表.因为玩家登陆退出的变化,这里的异常几乎是不可避免的.select match内部实现的时候都会使用safe_fixtable


查看ETS Tableexpress

     Erlang提供了一个可视化的ETS查看工具 The Table Visualizer,启动tv:start(),界面比较简单.值得一提的是,这个工具能够跨节点查看ETS信息,在File菜单里面有一个nodes选项,服务器

打开会给出和当前节点互相连通的节点列表,点击节点会显示这个节点上的ETS Table信息.数据结构

 

在没有可视化工具的时候咱们如何查看ETS的信息?并且这仍是比较常见的状况,在文本模式操做服务器的状况下,Table Visualizer根本无法使用.下面的命令能够达到一样的效果:并发

    ets:all() %列出全部的ETS Table 

    ets:i() %给出一个ETS Table的清单 包含表的类型,数据量,使用内存,全部者信息

    ets:i(zen_ets) % 输出zen_ets表的数据,我的感受这个很是方便比tv还要简单快捷,若是表数据量很大,它还提供了一个分页显示的功能

    ets:info(zen_ets) %单独查看一个ETS Table的详细信息也可使用这个方法,若是怀疑这个表被锁了可使用ets:info(zen_ets,fixed)查看,ets:info(zen_ets,safe_fixed) 能够 

    得到更多的信息,这样比较容易定位是哪一个模块出了问题.

    ets:member(Tab, Key) -> true | false %看表里面是否存在键值为Key的数据项.

  

建立 删除ETS Table插入数据

  上面已经提到了ETS存储数据的格式是Tuples,咱们动手写一些测试代码看一下ETS的常规操做:  

      %快速建立一个ETS Table 并填充数据

       T = ets:new(x,[ordered_set]).

       [ ets:insert(T,{N}) || N <- lists:seq(1,10) ].   

       TableID = ets:new(temp_table , []), %Create New ETS Table

        ets:insert(TableID,{1,2} ),                  % insert one Item to Table

        Result= ets:lookup(TableID ,1),

        io:format("ets:lookup(TableID ,1) Result: ~p ~n  " ,[ Result  ]),  

         ets:insert(TableID,{1,3} ),

         Result2 = ets:lookup(TableID, 1 ),

         io:format("ets:lookup(TableID ,1) Result2: ~p ~n  ", [ Result2 ]),

         ets:delete(TableID),

         BagTableID =  ets:new(temp_table, [bag]),

         ets:insert(BagTableID,{1,2} ),

         ets:insert(BagTableID,{1,3} ),

         ets:insert(BagTableID,{1,4} ),

       %Note that the time order of object insertions is preserved; 

       %The first object inserted with the given key will be first in the resulting list, and so on.

         Result3 = ets:lookup(BagTableID, 1 ),

         io:format("ets:lookup(BagTableID ,1) Result3: ~p ~n  ", [ Result3 ])

         %建立ETS表 注意参数named_table,咱们能够经过countries原子来标识这个ETS Table

         ets:new(countries, [bag,named_table]), 

        %插入几条数据

         ets:insert(countries,{yves,france,cook}),  

         ets:insert(countries,{sean,ireland,bartender}),

         ets:insert(countries,{marco,italy,cook}),

         ets:insert(countries,{chris,ireland,tester}).

我不明白为何宁愿相信别人的话,也不肯意本身动手写一段测试代码看看;别人说对了还好,若是说错了呢?快速的构建一个测试模型,去验证本身的想法,这种能力会在一次次实验中不断强化;好比有人问到"ETS INSERT是每次都新加一个仍是会更新一个",在Erlang Shell中就能够验证它(下面默认建立的是Set类型):
复制代码
复制代码
Eshell V5.9  (abort with ^G)
1> ets:new(test,[named_table]).
test
2> [ets:insert(test,{Item}) || Item <-[1,2,3,4,5,6]].
[true,true,true,true,true,true]
3> [ets:insert(test,{Item}) || Item <-[1,2,3,4,5,6]].
[true,true,true,true,true,true]
4> ets:i(test).
<1 > {5}
<2 > {3}
<3 > {2}
<4 > {1}
<5 > {4}
<6 > {6}
EOT (q)uit (p)Digits (k)ill /Regexp -->q

ok
复制代码
复制代码


 

 

分页从ETS中提取数据

      有时候匹配的数据量很大,若是一次性把全部的数据都取出来,处理会很是慢;一个处理方法就是分批次处理,这也就要求咱们可以分屡次

从ETS Table中取数据.这和作网页分页很像.ets类库中提供了一系列方法来实现这个功能这里咱们以match为例:

match(Tab, Pattern, Limit) -> {[Match],Continuation} | '$end_of_table'

参数Limit就是每一次查询的数量限制,若是实际匹配的数据量超过了Limit就会返回{[Match],Continuation}的结果,Match表明查询的结果集,能够推测

Continuation包含分页的信息,若是继续取下一页的结果集使用下面的方法:

match(Continuation) -> {[Match],Continuation} | '$end_of_table'

咱们经过demo看一下分页查询的结果,特别是Continuation的数据结构,首先咱们先填充一些测试数据:

  

View Code

  

咱们每页10条数据,执行4次,代码以下:

 {M,C}=ets:match(zen_ets,'$1',10). %第一页

 {M2,C2} = ets:match(C).               %第二页

 {M3,C3} = ets:match(C2).             %第三页

 {M4,C4} = ets:match(C3).            %没有数据了看异常是什么?

展开下面的代码查看调用结果:

执行结果

相似的还有:

match_object(Tab, Pattern, Limit) -> {[Match],Continuation} | '$end_of_table'

match_object(Continuation) -> {[Match],Continuation} | '$end_of_table'

select(Tab, MatchSpec, Limit) -> {[Match],Continuation} | '$end_of_table'

select(Continuation) -> {[Match],Continuation} | '$end_of_table'

只获取匹配数据的数量: select_count(Tab, MatchSpec) -> NumMatched

ETS 使用Match specifications 查询

    match方法进行匹配最简单, '$数字'表明占位符,'_'表明通配符;'$数字'这种表示方式,数字的大小表明什么?

从下面的代码示例中能够看出数字控制的是输出结果顺序,数字相对大小表明相对位置顺序;

       

%'_' 通配符
A= ets:match(countries, {'$1','_','_' } ) ,
io:format(" ets:match(countries, {'$1','_','_' } ) Result : ~p ~n " ,[ A ]), 
B= ets:match(countries , {'$1', '$0' ,'_' } ),
io:format(" ets:match(countries , {'$1', '$0' ,'_' } ), Result : ~p ~n " ,[ B ]),
C= ets:match(countries , {'$11', '$9' ,'_' } ),
io:format(" C= ets:match(countries , {'$11', '$9' ,'_' } ), Result : ~p ~n " ,[ C ]),
D= ets:match(countries , {'$11', '$99' ,'_' } ),
io:format(" ets:match(countries , {'$11', '$99' ,'_' } ), Result : ~p ~n " ,[ D ]),
E= ets:match(countries , {'$101', '$9' ,'_' } ),
io:format("ets:match(countries , {'$101', '$9' ,'_' } ), Result : ~p ~n " ,[ E ]),
F= ets:match(countries,{'$2',ireland,'_'}),
G= ets:match(countries,{'_',ireland,'_'}), % [[],[]] 若是没有数字占位符 是没有结果输出的 只是空列表
H= ets:match(countries,{'$2',cook,'_'}),
I= ets:match(countries,{'$0','$1',cook}),
J= ets:match(countries,{'$0','$0',cook}),

  

     若是是须要全部字段,提取整个数据项,那就直接使用match_object,

            K= ets:match_object(countries,{'_',ireland,'_'}),

            io:format(" ets:match_object(countries,{'_',ireland,'_'}),  Result : ~p ~n " ,[ K ]),

         L= ets:match(countries ,'$1' ),

         io:format(" ets:match(countries ,'$1' ),  Result: ~p ~n " ,[ L ]),

        Result=ets:match_delete(countries,{'_','_',cook}),

         io:format("ets:match_delete(countries,{'_','_',cook}),   Result : ~p ~n " ,[ Result ]),

        上面的例子countries这个结构很简单,可是若是是一个字段稍多写的结构呢?很容易出现相似ets:match(zen_ets, {'$1','_','_','_','_','_' } )  .这样的代码,不只可读性差,并且一旦字段顺序发生

变化,这里就容易出错.解决方法在[Erlang 0006] Erlang中的record与宏  一文中已经提到过,使用record能够规避掉tuple字段增减,顺序的问题.

例如:    ets:match_delete(zen_ets, #t{age=24,iabn=1,_='_'}), 

有时候咱们须要表达更为复杂的匹配条件,这就须要使用Match specifications了,ms的解析依赖ms_transform模块,因此首先咱们在模块头添加

include_lib("stdlib/include/ms_transform.hrl").增长对ms_transform.hrl头文件的引用.Match specifications的详细说明参见这里: http://www.erlang.org/doc/apps/erts/match_spec.html     

        

MS = ets:fun2ms(fun({ Name,Country , Position }  ) when Position /=cook -> [Country,Name ] end   ),

        MSResult = ets:select(countries, MS ),

        io:format("ets:fun2ms(fun({ Name,Country , Position }  ) when Position /=cook -> [Country,Name ] end   ), MSResult:~p~n " , [MSResult ]),

        

        MS2 =ets:fun2ms(fun(Data ={Name, Country ,Position } )  when Position /=cook -> Data end   ),

        MSResult2 = ets:select(countries , MS2),

        io:format("ets:fun2ms(fun(Data ={Name, Country ,Position } )  when Position /=cook -> Data end   ),   Result : ~p ~n " ,[ MSResult2 ]),

        

        %当咱们使用的是Tuple的时候这里必须使用彻底匹配 

       MS3  = ets:fun2ms(fun(Data ={Name, Country ,Position } )  when Position /=cook -> Data end   ), 

       MSResult2 = ets:select(countries , MS3),

       

在实战操做中,咱们遇到这样一个问题,下面的MS MS2是等效的么?  ets:fun2ms(fun(#t{id =ID , name =Name, _='_'  } ) when ID >30 -> Name  end ),亮点是红色标记的部分.能够运行一下下面的

代码看,二者是生成的ms是同样的.

         

    MS = ets:fun2ms(fun(#t{id =ID , name =Name  } ) when ID >30 -> Name  end ),

    io:format(" ets:fun2ms(fun(#t{id =ID , name =Name  } ) when ID >30 -> Name  end ),  MS:  ~p ~n   " , [ MS ]),  

        

    MS2 = ets:fun2ms(fun(#t{id =ID , name =Name, _='_'  } ) when ID >30 -> Name  end ),

    io:format(" ets:fun2ms(fun(#t{id =ID , name =Name, _='_'  } ) when ID >30 -> Name  end ), MS2: ~p ~n    " ,[  MS2 ]),

    

     io:format("MS==MS2 ? Result : ~p ~n  " , [ MS==MS2 ]),

    

    MSResult = ets:select(zen_ets , MS ),

在使用MS的过程当中,还有一个特殊的状况,若是要返回完整的record应该怎么写呢?仔细阅读ETS文档,能够看到这么一句:The return value is constructed using the "match variables" bound in

 the MatchHead or using the special match variables '$_' (the whole matching object) and '$$' (all match variables in a list), so that the following ets:match/2 expression:

再翻看http://www.erlang.org/doc/apps/erts/match_spec.html,能够看到下面的说明:

ExprMatchVariable ::= MatchVariable (bound in the MatchHead) | '$_' | '$$'

也就是说只要这样'$_'就能够了,试验了一下MS3 = ets:fun2ms(fun(T=#t{id =ID , name =Name, _='_'  } ) when ID >30 -> T  end )生成的ms是:

,MS3: [{{t, '$1',  '_','$2', '_',  '_'},      [{'>', '$1', 30}],['$_']}]

拓展阅读:

2003年的论文 <<Erlang ETS Table的实现与性能研究>>

Study of Erlang ETS Table Implementation and Performance. [点此下载]

Scott Lystig Fritchie. 
Second ACM SIGPLAN Erlang Workshop. 
Uppsala, Sweden, August 29, 2003.

详见:ligaoren博园

相关文章
相关标签/搜索