Search code examples
erlangmnesia

Erlang unique_integer returns the same integer after restart


I need to number my records so that I can always compare what came "later" from what came "before".

For this I used the unique_integer function:

Tag = erlang:unique_integer([monotonic]),

and then I would compare any two events:

Event1.tag < Event2.tag

would mean that Event1 came before Event2.

However, when I restart the erlang node, it starts numbering from the beginning again:

event1  -5555
event2  -5554
event3  -5553

-- restart the system

event4  -5555
event5  -5554
event6  -5553

Now my sorting is not working as intended.

Is there any way to generate long sequential number that picks up where it left off after restarts?

P.S.: If it is useful, I am using mnesia to store my events, and so I need an increasing row number for record.


Solution

  • This is the feature I was looking for: mnesia:dirty_update_counter.

    Thanks to this excellent post on the matter:

    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]),
    

    The output you will get is

    Id => 1 
    Id => 2 
    Id => 1
    

    A single table can be used to generate the unique ids for other tables. In this example, unique ids are generated for record_type and another_type. Posted by Dude From Mangalore at 3:19 PM