DEV Community

voluntas
voluntas

Posted on

4 1

Mnesia の dirty_update_counter/3 を使う

mnesia に カウンターを扱う関数があるので使って見たメモ。

  • レコードが無ければ作ってくれる
  • 加算後の値を返してくれる
$ erl
Erlang/OTP 17 [erts-6.1] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]

Eshell V6.1  (abort with ^G)
1> mnesia:start().
ok
2> rd(store, {key, value}).
store
3> mnesia:create_table(store, []).
{atomic,ok}
4> mnesia:dirty_update_counter(store, <<"spam">>, 1).
1
5> mnesia:dirty_update_counter(store, <<"spam">>, 1).
2
6> mnesia:dirty_update_counter(store, <<"spam">>, 1).
3
7> mnesia:dirty_update_counter(store, <<"spam">>, 1).
4
8> mnesia:dirty_read(store, <<"spam">>).
[#store{key = <<"spam">>,value = 4}]
9> mnesia:dirty_update_counter(store, <<"egg">>, 1).
1
10> mnesia:dirty_read(store, <<"egg">>).
[#store{key = <<"egg">>,value = 1}]
10> timer:tc(fun() -> mnesia:dirty_update_counter(store, <<"spam">>, 1) end).
{45,6}
Enter fullscreen mode Exit fullscreen mode

シングルノードであれば 45 マイクロセカンドと高速です。ただ ets だと一桁台なので、カウンターの性能に引っ張られる可能性もありますね。

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay