ETS表选择所有记录匹配ID

发布于 2025-01-27 03:24:56 字数 1157 浏览 2 评论 0原文

嘿,我有一个ETS表交易,表的一部分看起来像这样。

[
  {"txn_05h0f14877a091yj7358a",
   %{
     account_id: "acc_f5f3f6y7t8s7a5htj78ty",
     amount: 289,
     date: ~D[2022-03-16],
     details: %{
       details: %{
         category: "transport",
         counterparty: %{name: "UBER", type: "organization"},
         processing_status: "complete"
       }
     },
     links: %{
       account: "http://localhost:4000/accounts/acc_f5f3f6y7t8s7a5htj78ty",
       self: "http://localhost:4000/accounts/acc_f5f3f6y7t8s7a5htj78ty/balances/transactions/txn_f5f3f6y7t8s7a5htj78ty"
     },
     running_balance: 100000,
     status: "posted",
     type: "card_payment"
   }}
]

我想选择与account_id匹配的所有记录,我正在做这样的记录

iex(23)> fun = :ets.fun2ms(fn {_, {account_id}} when account_id == "acc_f5f3f6y7t8s7a5htj78ty" -> account_id end)
[{{:_, {:"$1"}}, [{:==, :"$1", "acc_f5f3f6y7t8s7a5htj78ty"}], [:"$1"]}]
iex(24)> :ets.select(:transactions, fun)
[]

,但是查询无法正常工作。选择匹配account_id的所有记录的正确方法是什么?

Hey I have an ets table transactions, part of the table is looking like this.

[
  {"txn_05h0f14877a091yj7358a",
   %{
     account_id: "acc_f5f3f6y7t8s7a5htj78ty",
     amount: 289,
     date: ~D[2022-03-16],
     details: %{
       details: %{
         category: "transport",
         counterparty: %{name: "UBER", type: "organization"},
         processing_status: "complete"
       }
     },
     links: %{
       account: "http://localhost:4000/accounts/acc_f5f3f6y7t8s7a5htj78ty",
       self: "http://localhost:4000/accounts/acc_f5f3f6y7t8s7a5htj78ty/balances/transactions/txn_f5f3f6y7t8s7a5htj78ty"
     },
     running_balance: 100000,
     status: "posted",
     type: "card_payment"
   }}
]

I would like to select all records that are matching account_id and I'm doing something like this

iex(23)> fun = :ets.fun2ms(fn {_, {account_id}} when account_id == "acc_f5f3f6y7t8s7a5htj78ty" -> account_id end)
[{{:_, {:"$1"}}, [{:==, :"$1", "acc_f5f3f6y7t8s7a5htj78ty"}], [:"$1"]}]
iex(24)> :ets.select(:transactions, fun)
[]

but the query is not working properly. What will be a proper way of selecting all records matching account_id?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

假情假意假温柔 2025-02-03 03:24:56

您的方法有效,但您的模式与数据的形状不符:它期望形状{“ TXN_05H0F14877A091YJ7358A”,{“ ACC_F5F3F6Y7T8S7A5HTJ78TY}}}

您需要查找地图:account_id键:

iex> fun = :ets.fun2ms(fn {_, transaction} when transaction.account_id == "acc_f5f3f6y7t8s7a5htj78ty" -> transaction end)
[
  {{:_, :"$1"},
   [{:==, {:map_get, :account_id, :"$1"}, "acc_f5f3f6y7t8s7a5htj78ty"}],
   [:"$1"]}
]
iex> :ets.select(:transactions, fun)
[
  %{
    account_id: "acc_f5f3f6y7t8s7a5htj78ty",
    amount: 289,
    ...
  }
]

注意:transaction.account_id == guard需要elixir> = 1.11,您还可以使用何时: erlang.map_get(:account_id,transaction)==如果您在较旧版本上。

Your approach works but your pattern doesn't match the shape of the data: it expects records of the shape {"txn_05h0f14877a091yj7358a", {"acc_f5f3f6y7t8s7a5htj78ty"}}.

You need to lookup the map :account_id key:

iex> fun = :ets.fun2ms(fn {_, transaction} when transaction.account_id == "acc_f5f3f6y7t8s7a5htj78ty" -> transaction end)
[
  {{:_, :"$1"},
   [{:==, {:map_get, :account_id, :"$1"}, "acc_f5f3f6y7t8s7a5htj78ty"}],
   [:"$1"]}
]
iex> :ets.select(:transactions, fun)
[
  %{
    account_id: "acc_f5f3f6y7t8s7a5htj78ty",
    amount: 289,
    ...
  }
]

Note: The when transaction.account_id == guard needs Elixir >= 1.11, you can also use when :erlang.map_get(:account_id, transaction) == if you are on an older version.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文