Merge Entries for Mnesia

I am trying to refactor the code that I have for software that collects the current state of agents in a call queue. Currently, for each of the 6 or so events that I listen to, I check the Mnesia table if the agent exists and changes some values ​​in the row depending on the event or adds it as new if the agent does not exist. I currently have a Mnesia transaction in each case and, of course, this is a bunch of duplicate code to check for agents, etc.

I am trying to change it so that there is one function, for example change_agent / 2, which I call from events that handle this for me.

My problems are, of course, recordings ... I find no way to dynamically create them or combine 2 of them together or something else. Preferably there would be a function that I could name:

change_agent("001", #agent(id = "001", name = "Steve")).
change_agent("001", #agent(id = "001", paused = 0, talking_to = "None")).
+3
source share
2 answers

I wrote code a while ago that combines two entries. Not quite dynamic, but with macros you can easily use it for multiple entries.

: merge/2 ( . "" ), /4, A, , B, , , , ( ).

(, StackOverflow Erlang ):

%%%----------------------------------------------------------------------------
%%% @spec merge(RecordA, RecordB) -> #my_record{}
%%%     RecordA = #my_record{}
%%%     RecordB = #my_record{}
%%%
%%% @doc Merges two #my_record{} instances. The first takes precedence.
%%% @end
%%%----------------------------------------------------------------------------
merge(RecordA, RecordB) when is_record(RecordA, my_record),
                             is_record(RecordB, my_record) ->
    list_to_tuple(
        lists:append([my_record],
                     merge(tl(tuple_to_list(RecordA)),
                           tl(tuple_to_list(RecordB)),
                           tl(tuple_to_list(#my_record{})),
                           []))).

%%%----------------------------------------------------------------------------
%%% @spec merge(A, B, Default, []) -> [term()]
%%%     A = [term()]
%%%     B = [term()]
%%%     Default = [term()]
%%%
%%% @doc Merges the lists `A' and `B' into to a new list taking
%%% default values from `Default'.
%%%
%%% Each element of `A' and `B' are compared against the elements in
%%% `Default'. If they match the default, the default is used. If one
%%% of them differs from the other and the default value, that element is
%%% chosen. If both differs, the element from `A' is chosen.
%%% @end
%%%----------------------------------------------------------------------------
merge([D|ATail], [D|BTail], [D|DTail], To) ->
    merge(ATail, BTail, DTail, [D|To]); % If default, take from D
merge([D|ATail], [B|BTail], [D|DTail], To) ->
    merge(ATail, BTail, DTail, [B|To]); % If only A default, take from B
merge([A|ATail], [_|BTail], [_|DTail], To) ->
    merge(ATail, BTail, DTail, [A|To]); % Otherwise take from A
merge([],        [],        [],        To) ->
    lists:reverse(To).

.

+2

. 'exprecs, .

, , :

-compile({parse_transform, exprecs}).
-export_records([...]).  % name the records that you want to 'export'

, '. , , . (

+3

Source: https://habr.com/ru/post/1696860/


All Articles