免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 7501 | 回复: 0
打印 上一主题 下一主题

[其他] [erlang]ets中bag表类型应用 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-08-02 18:19 |只看该作者 |倒序浏览
其它参考:ets和record无逢结合

ets默认创建的为set类型表,既每条记录关健字唯一的表,但很多时候创建一个表有多个相同的关健字,如一个类似sns好友对应表:

uid friendId
-------------
101 201
101 202
101 203
....


初始化一个record,在文件 t.hrl 中:
-record(t, {uid,friendId}).
引入Eshell
>rr("t.hrl").
[t]

这时我们需要一个bag类型的ets表,以uid做主键:
>ets:new(t,[public,bag,named_table,{keypos, #t.uid}]).
t

在uid=101的关键字下新增记录:
>ets:insert(t,#t{uid=101,friendId=201}),
ets:insert(t,#t{uid=101,friendId=202}),
ets:insert(t,#t{uid=101,friendId=203}).
true

查看uid=101的所有记录:
>ets:lookup(t,101).
[#t{uid = 101,friendId = 201},
#t{uid = 101,friendId = 202},
#t{uid = 101,friendId = 203}]

删除friendId=202的记录
>ets:delete_object(t,#t{uid=101,friendId=202}).
true
再次查看是否已经删除
>ets:lookup(t,101).                           
[#t{uid = 101,friendId = 201},#t{uid = 101,friendId = 203}]

ok,friendId=202的记录已经删除.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP