我试图将一个列表插入 ETS 以便稍后取出,但由于某种原因,它说这是一个错误的参数。我不确定我是否插入错误。
是否无法将列表插入 ETS?
有问题的行是 ets:insert(table, [{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]).
最佳答案
如果您使用类似的内容在同一过程中创建表
ets:new(table, [set, named_table, public]).
那你应该没问题了。默认权限受到保护,只有创建进程才能写入。
关于list - 在 Erlang ETS 中存储列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27559382/