I have provided two possibilities. One that matches your design, and one that introduces a slight change in your record definition. See which one best suits your needs. The first one works in your design.
-record (user, {username, field1, field2, timestamp}).
%% execute the function below in a mnesia transaction
insert (#user {username = U, timestamp = _T} = User) ->
case mnesia: read ({user, U}) of
[] -> mnesia: write (User);
AllHere ->
case length (AllHere) == 500 of
false -> %% not yet 500
mnesia: write (User);
true ->
%% value has reached 500
%% get all timestamps and get the
%% oldest record and delete it
%%
OldRecord = get_oldest_stamp (AllHere),
ok = mnesia: delete_object (Record),
mnesia: write (User)
end
end.
get_oldest_stamp (UserRecords) ->
%% here you do your sorting
%% and return the record with
%% oldest timestamp
....
OldRecord.
The length/1 function inside a transaction is not optimal. Let's think about a better way. Also, I donβt know what your timestamps look like, so youβre sure that you have a way to sort them and determine the last timestamp, select the record that owns this timestamp, and then return it. I gave this solution to fit your design. I also think some where we need indexing . The following implementation seems to me better. Some, how can we change the definition of a record, and enter the oldest field, which accepts bool() , and we index it as follows:
-record (user, {
username,
field1,
field2,
timestamp
oldest %% bool (), indexed
}).
insert_user (Username, Field1, Field2) ->
User = #user {
username = Username,
field1 = Field1,
field2 = Field2,
timestamp = {date (), time ()}
}.
insert (User).
%% execute this within a mnesia transaction
insert (#user {username = U} = User) ->
case mnesia: read ({user, U}) of
[] -> mnesia: write (User # user {oldest = true});
AllHere ->
case length (AllHere) == 500 of
false ->
%% unset all existing records' oldest field
%% to false
F = fun (UserX) ->
ok = mnesia: delete_object (UserX),
ok = mnesia: write (UserX # user {oldest = false})
end
[F (XX) || XX <- AllHere],
ok = mnesia: write (User # user {oldest = true});
true ->
[OldestRec] = mnesia: index_read (user, true, oldest),
ok = mnesia: delete_object (OldestRec),
ok = mnesia: write (User # user {oldest = true})
end
end.
The above implementation seems to me better. success!