且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

初次使用ets

更新时间:2022-04-08 08:17:48

 

 

一、new(Name, Options) -> tid() | atom(),创建ets表.

Options = [Option],

目测常用的属性,

{keypos, Pos}:指定key的位置,每个元组的长度必须要大约Pos,也可以使用record的字段。

named_table:ets表注册.

Type:set | ordered_set | bag | duplicate_bag:表的类型,是否允许出现重复。

Accesspublic | protected | private

 

二、查询

1.lookup(Tab, Key) -> [Object]:根据key进行查询

Types:

Tab = tab()
Key = term()
Object = tuple()

 

Returns a list of all objects with the key Key in the table Tab.

In the case of set, bag and duplicate_bag, an object is returned only if the given key matches the key of the object in the table. If the table is an ordered_set however, an object is returned if the key given compares equal to the key of an object in the table. The difference being the same as between =:= and ==. As an example, one might insert an object with the integer() 1 as a key in an ordered_set and get the object returned as a result of doing a lookup/2 with the float() 1.0 as the key to search for.

2.fun2ms(LiteralFun) -> MatchSpec.

select(Tab, MatchSpec) -> [Match]

Types:

Tab = tab()
MatchSpec = match_spec()
Match = term().

3.match(Tab, Pattern) -> [Match].

 

 

 

部分源码:

猜测:可以使用多个项元作为一个可以key,类似于复合索引
3> ets:new(my_ets,[named_table,{keypos,2},set]).
my_ets
4> ets:insert(my_ets,{1,aa,11}).                
true
5> ets:insert(my_ets,{1,{aa,11},11}).
true
6> ets:lookup(my_ets,aa).
[{1,aa,11}]
7> ets:lookup(my_ets,{aa,11}).
[{1,{aa,11},11}]