如何在 Erlang Mnesia 中生成 AUTO INCREMENT ID



我正在尝试将记录写入 mnesia,我想为每个记录分配一个唯一的 Id。Id 需要自动递增。

例如,我有一个包含 3 列的表——ID、名称、DOB。

记录 1 的 Id = 1,名称 = Joe,DOB = 03-05-1994

记录 2 应具有 Id = 2,名称 = Alice,DOB = 11-07-1991 等等...

请帮忙

看起来您必须手动跟踪最后一个 id。 您可以创建一个table_ids表,用于跟踪在给定表中插入记录时使用的最后一个 id。table_ids表可用于单独跟踪所有不同表中使用的最后一个 id。

根据 mnesia 文档,与其自己从table_ids表中读取表的最后一个 id,然后递增最后一个 id,然后将最后一个 id 写回table_ids表中,不如简单地调用 mnesia:dirty_update_counter()。 下面是一个示例:

-module(my).
-compile(export_all).
-include_lib("stdlib/include/qlc.hrl"). 
-record(person, {id, name, age}).
-record(table_id, {table_name, last_id}).
init() ->
    mnesia:create_schema([node()]),
    mnesia:start(),
    {atomic, ok} = mnesia:create_table(people, [ 
       {record_name, person},
       {attributes, record_info(fields, person)}
    ]),
    {atomic, ok} = mnesia:create_table(table_ids, [
       {record_name, table_id},
       {attributes, record_info(fields, table_id)}
    ]).
start_counter(TableName) ->
    Fun = fun() ->
                mnesia:write(table_ids, 
                  #table_id{table_name=TableName, last_id=0}, 
                  write
                )
          end,
    {atomic, ok} = mnesia:transaction(Fun).

insert(Name, Age) ->
    Index = mnesia:dirty_update_counter(table_ids, people, 1),
    Fun = fun() ->
                mnesia:write(people,
                    #person{id=Index, name=Name, age=Age},
                    write
                )
          end,
    {atomic, ok} = mnesia:transaction(Fun).
select_all() -> 
    {atomic, People} = mnesia:transaction(
         fun() ->
              qlc:eval(
                qlc:q([X || X <- mnesia:table(people)])
              )
         end
    ),
    People.

在 iex 中:

~/erlang_programs/mnesia$ rm -rf Mnesia.nonode@nohost/
~/erlang_programs/mnesia$ erl
Erlang/OTP 20 [erts-9.3] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:10] [hipe] [kernel-poll:false]
Eshell V9.3  (abort with ^G)
1> c(my).                   
my.erl:2: Warning: export_all flag enabled - all functions will be exported
{ok,my}
2> my:init().               
{atomic,ok}
3> my:start_counter(people).
{atomic,ok}
4> my:insert("Joe", 22).    
{atomic,ok}
5> my:insert("Sue", 31).    
{atomic,ok}
6> my:select_all().         
[{person,1,"Joe",22},{person,2,"Sue",31}]
7> mnesia:stop().           
stopped
8> 
=INFO REPORT==== 9-Jan-2019::23:30:02 ===
    application: mnesia
    exited: stopped
    type: temporary
(Hit Return to get back to prompt)
8> 

最新更新