我需要对我的记录进行编号,以便我可以随时比较之前“之前”的内容。
为此,我使用了unique_integer
函数:
Tag = erlang:unique_integer([monotonic]),
然后我会比较任何两个事件:
Event1.tag < Event2.tag
意味着Event1来自Event2。
然而,当我重新启动 erlang节点时,它再次开始从开始编号:
event1 -5555
event2 -5554
event3 -5553
-- restart the system
event4 -5555
event5 -5554
event6 -5553
现在我的排序没有按预期工作。
有没有办法生成长序列号,它会在重启后从中断处继续?
P.S。:如果它有用,我使用mnesia来存储我的事件,所以我需要增加行号来记录。
答案 0 :(得分:3)
erlang:unique_integer() - &gt;整数()强>
在当前运行时系统实例上生成并返回唯一的整数。与调用erlang相同:unique_integer([])。
所以你所看到的与定义的行为一致。
我不清楚你需要什么,正确的解决方案取决于你的确切需求。假设您有快速进入的活动并且您计划在到达时间之前使用ordered_set
将它们存储在Mnesia中,我可以考虑一些方法:
erlang:system_time(nano_seconds)
生成一个独特的{erlang:system_time(nano_seconds), make_ref()}
生成密钥,以确保即使时间戳相同也具有唯一性答案 1 :(得分:3)
这是我正在寻找的功能:mnesia:dirty_update_counter
。
感谢关于此事的excellent post:
mnesia:dirty_update_counter(unique_ids, record_type, 1), io:format("Id => ~p~n", [Id]), Id1 = mnesia:dirty_update_counter(unique_ids,record_type, 1), io:format( "Id => ~p~n", [Id1]), Id2 = mnesia:dirty_update_counter(unique_ids, another_type, 1), io:format("Id => ~p~n", [Id2]),
您将获得的输出是
Id => 1 Id => 2 Id => 1
单个表可用于为其他表生成唯一ID 表。在此示例中,为record_type和生成唯一ID another_type。发贴者:Dude来自Mangalore,时间是下午3:19
答案 2 :(得分:1)
如果您希望在一秒钟内不重启,请使用{erlang:system_time(seconds), erlang:unique_integer([monotonic])}
。否则{erlang:system_time(), erlang:unique_integer([monotonic])}
是安全的。
您可以在系统或流程启动时存储erlang:system_time(秒)的值并保存调用erlang:system_time/0,1
。