List 在Erlang ETS中存储列表
我试图在ETS中插入一个列表,以便稍后退出,但出于某种原因,它表示这是一个错误的arg。我不确定我是否插错了 难道不能在ETS中插入列表吗 有问题的行是List 在Erlang ETS中存储列表,list,erlang,ets,List,Erlang,Ets,我试图在ETS中插入一个列表,以便稍后退出,但出于某种原因,它表示这是一个错误的arg。我不确定我是否插错了 难道不能在ETS中插入列表吗 有问题的行是ets:insert(表,[{parsed_file,UUIDs}]) 代码如下: readUUID(Id, Props) -> fun () -> %%TableBool = proplists:get_value(table_bool, Props, <<"">>),
ets:insert(表,[{parsed_file,UUIDs}])
代码如下:
readUUID(Id, Props) ->
fun () ->
%%TableBool = proplists:get_value(table_bool, Props, <<"">>),
[{_, Parsed}] = ets:lookup(table, parsed_bool),
case Parsed of
true ->
{uuids, UUIDs} = ets:lookup(table, parsed_bool),
Index = random:uniform(length(UUIDs)),
list_to_binary(lists:nth(Index, UUIDs));
false ->
[{_, Dir}] = ets:lookup(table, config_dir),
File = proplists:get_value(uuid_file, Props, <<"">>),
UUIDs = parse_file(filename:join([Dir, "config", File])),
ets:insert(table, [{parsed_file, {uuids, UUIDs}}]),
ets:insert(table, [{parsed_bool, true}]),
Index = random:uniform(length(UUIDs)),
list_to_binary(lists:nth(Index, UUIDs))
end
end.
parse_file(File) ->
{ok, Data} = file:read_file(File),
parse(Data, []).
parse([], Done) ->
lists:reverse(Done);
parse(Data, Done) ->
{Line, Rest} = case re:split(Data, "\n", [{return, list}, {parts, 2}]) of
[L,R] -> {L,R};
[L] -> {L,[]}
end,
parse(Rest, [Line|Done]).
readUUID(Id,Props)->
乐趣()->
%%TableBool=proplists:get_value(table_bool,Props,),
[{uu,Parsed}]=ets:lookup(表,Parsed_bool),
案例解析
正确->
{uuids,uuids}=ets:lookup(表,解析布尔),
索引=随机:均匀(长度(UUID)),
列表到二进制(列表:第n个(索引,uuid));
错误->
[{uu,Dir}]=ets:lookup(表,配置目录),
File=proplist:get_值(uuid_文件,Props,),
UUIDs=parse_文件(文件名:join([Dir,“config”,file]),
ets:insert(表,[{parsed_file,{uuids,uuids}]),
ets:insert(表,[{parsed_bool,true}]),
索引=随机:均匀(长度(UUID)),
列表到二进制(列表:第n个(索引,UUIDs))
结束
结束。
解析_文件(文件)->
{ok,Data}=file:read_file(file),
解析(数据,[])。
解析([],完成)->
列表:反向(完成);
解析(数据,完成)->
{Line,Rest}=case re:split(数据,“\n”,[{return,list},{parts,2}])的
[L,R]>{L,R};
[五十] ->{L,[]}
完,,
解析(Rest,[Line | Done])。
如果您在同一进程中使用以下内容创建表
ets:new(table, [set, named_table, public]).
那你应该没事了。默认权限受保护,只有创建进程才能写入 作为我对仅包含元组的ets表的评论的后续内容,以及
ets:lookup/2
在代码中返回以下行:
{uuids, UUIDs} = ets:lookup(table, parsed_bool),
将始终生成错误,因为ets:lookup/2
返回一个列表。以上三行通话可能会成功。似乎您正试图使用键parsed\u bool
在表中进行两次查找,并期望得到两种不同类型的答案:{{uu,parsed}
和{uuids,uuids}
。请记住,ETS不提供键值表,而是提供元组表,其中一个元素(默认为第一个元素)是键,执行ETS:lookup/2
返回具有该键的元组列表。可以取回多少取决于表的属性
查看的文档。它适合我。。。您确定调用insert的进程是ets或表的所有者是公共进程吗?我没有看到负责创建它的代码。您应该知道,实际上只能将元组放入ETS表,而不能放入列表。因此,当您执行ets:insert(table,[…])
时,实际上是在列表中插入元组,而不是列表本身。当您执行ets:lookup(table,Key)
时,您将返回具有该键的元组列表。