ETS和DETS
ETS
ETS == Erlang Term Storage , Erlang内存项元存储机制
ETS表示通过散列表和二叉树实现的,不同表现形式提供不同类型的聚合
ETS表列表:
ETS表类型 | 实现方式 | 描述 |
---|---|---|
Set 集合 | hash内存地址映射 | 在集合中每个关键字只出现一次,所以使用这种表作为索引的例子,意味着每个词只对应表中的一个元素 |
Ordered set 有序集合 | AVL平衡二叉树 | 元素按关键字的字段顺序存储的集合 |
Bag 袋 | hash内存地址映射 | 袋允许不同记录对应同一个关键字 |
Duplicate bag 复袋 | hash内存地址映射 | 一个复袋允许包含重复的元素,也可以有重复的关键字 |
函数ets:new(Name, Options) 可以创建ets表
new(Name, Options) -> tid() | atom()
Types:
Name = atom()
Options = [Option]
Option =
Type |
Access |
named_table |
{keypos, Pos} |
{heir, Pid :: pid(), HeirData} |
{heir, none} |
Tweaks
Type = set / ordered_set / bag / duplicate_bag
Access = public 都可读可写 / protected Owner可读写,别的只读 / private 只有Owner可读写
Tweaks =
{write_concurrency, boolean()} |
{read_concurrency, boolean()} |
compressed
Pos = integer() >= 1
HeirData = term()
操作Example:
ets:insert(TabId,{alison, sweden}).
% true
ets:lookup(TabId,alison).
% [{alison, sweden}]
更多函数参考模块ets
tv:start() 将会可视化显示当前节点和连接的节点所有用的表
DETS
DETS == Disk Erlang Term Storage , Erlang磁盘项元存储机制
DETS 类似ETS,只不过存储与磁盘文件,并且单个表不能超过2G.
创建或打开DETS表函数:
open_file(Name, Args) -> {ok, Name} | {error, Reason}
Types:
Name = tab_name()
Args = [OpenArg]
OpenArg =
{access, read | read_write } |
{auto_save, infinity | integer() >= 0} | % 表周期刷新时间
{estimated_no_objects, integer() >= 0} |
{file, file:name()} |
{max_no_slots, integer() >= 0 | default} | % 表分段设置,默认200万,最大3200万
{min_no_slots, integer() >= 0 | default} |
{keypos, integer() >= 1} |
{ram_file, boolean()} |
{repair, boolean() | force} |
{type, bag | duplicate_bag | set} | % 不支持有序集合
{version, 8 | 9 | default}
Reason = term()