如何在 Elixir 中使用 :dets 存储地图?

How to store a map using :dets in Elixir?

我希望能够使用 :dets

存储地图

目前,这是我正在尝试实施的解决方案:

# a list of strings
topics = GenServer.call(MessageBroker.TopicsProvider, {:get_topics})

# a map with each element of the list as key and an empty list as value
topics_map =
  topics
  |> Enum.chunk_every(1)
  |> Map.new(fn [k] -> {k, []} end)

{:ok, table} = :dets.open_file(:messages, type: :set)

# trying to store the map
:dets.insert(table, [topics_map])

:dets.close(table)

但是,我得到

** (EXIT) an exception was raised:
    ** (ArgumentError) argument error
        (stdlib 3.12) dets.erl:1259: :dets.insert(:messages, [%{"tweet" => [], "user" => []}])

如何做到这一点?

我用erlang测试过。您应该先将地图转换为列表。

Following from dets:insert_new() doc

insert_new(Name, Objects) -> boolean() | {error, Reason}
Types
Name = tab_name()
Objects = object() | [object()] 
Reason = term()
Inserts one or more objects into table Name. If there already exists some object with a key matching the key of any of the specified objects, the table is not updated and false is returned. Otherwise the objects are inserted and true returned.

测试代码

dets:open_file(dets_a,[{file,"/tmp/aab"}]).
Map = #{a => 2, b => 3, c=> 4, "a" => 1, "b" => 2, "c" => 4}.
List_a = maps:to_list(Map). %% <----- this line
dets:insert(dets_a,List_a).

陈宇的解决方案很好,但在得到之前我已经找到了另一种解决方案。 基本上,您可以将地图添加到元组

:dets.insert(table, {:map, topics_map})

然后,您可以使用

获取这张地图
:dets.lookup(table, :map)

据我了解您的意图,您希望将 userstweets 存储在不同的键下。为此,您需要首先构建关键字列表,而不是地图。

topics = for topic <- topics, do: {topic, []}
# or topics = Enum.map(topics, &{&1, []})
# or topics = Enum.map(topics, fn topic -> {topic, []} end)

那么您可以使用此关键字列表来创建 dets

{:ok, table} = :dets.open_file(:messages, type: :set)
:dets.insert(table, topics)
:dets.close(table)