4

I'm storing some data in mnesia, and I'd like to be able to change most of the values involved.

The naive

change(RecordId, Slot, NewValue) ->
    [Rec] = do(qlc:q([X || X <- mnesia:table(rec), X#rec.id =:= RecordId])),
    NewRec = Rec#rec{Slot=NewValue},
    F = fun() -> mnesia:write(NewRec) end,
    {atomic, Val} = mnesia:transaction(F),
    Val.

doesn't do it; the compiler complains that Slot is not an atom or _. Is there a way to express a general slot editing function as above, or am I going to be stuck defining a whole bunch of change_slots?

A marginally better approach is to pull out the insert and find pieces

atomic_insert(Rec) ->
    F = fun() -> mnesia:write(Rec) end,
    {atomic, Val} = mnesia:transaction(F),
    Val.

find(RecordId) -> 
    [Rec] = do(qlc:q([X || X <- mnesia:table(rec), X#rec.id =:= RecordId])),
    Rec.

change(RecordId, name, NewValue) ->
    Rec = find(RecordId),
    NewRec = Rec#rec{name=NewValue},
    atomic_insert(NewRec);
change(RecordId, some_other_property, NewValue) ->
    Rec = find(RecordId),
    NewRec = Rec#rec{some_other_property=NewValue},
    ...

but there's still a bit of code duplication there. Is there any way to abstract that pattern out? Is there an established technique to allow records to be edited? Any ideas in general?

1
  • See section 9.8, "Internal Representation of Records", in erlang.org/doc/reference_manual/records.html. The field names are only available at compile time, so you can't use a variable for the field name in the #rec syntax. The record_info() function might be of help for what you're trying to do.
    – RichardC
    May 30, 2012 at 20:17

3 Answers 3

4

Since records are represented by tuples, you could try using tuple operations to set individual values.

-module(rec).
-export([field_num/1, make_rec/0, set_field/3]).
-record(rec, {slot1, slot2, slot3}).

make_rec() ->
  #rec{slot1=1, slot2=2, slot3=3}.

field_num(Field) ->
  Fields = record_info(fields, rec),
  DifField = fun (FieldName) -> Field /= FieldName end,
  case length(lists:takewhile(DifField, Fields)) of
    Length when Length =:= length(Fields) ->
      {error, not_found};
    Length ->
      Length + 2
  end.

set_field(Field, Value, Record) ->
  setelement(field_num(Field), Record, Value).

set_field will return an updated record:

Eshell V5.9.1  (abort with ^G)
1> c(rec).
{ok,rec}
2> A = rec:make_rec().
{rec,1,2,3}
3> B = rec:set_field(slot3, other_value, A).
{rec,1,2,other_value}
3

You can also define change as a macro (especially if it used only inside the module):

-define(change(RecordId, Slot, NewValue),
        begin
            [Rec] = do(qlc:q([X || X <- mnesia:table(rec), X#rec.id =:= RecordId])),
            NewRec = Rec#rec{Slot=NewValue},
            F = fun() -> mnesia:write(NewRec) end,
            {atomic, Val} = mnesia:transaction(F),
            Val
        end).

Usage:

test(R, Id) ->
    ?change(Id, name, 5).

With macro you can also pass _ as a field (good for pattern matching).

2

Another way of using that a record is really a tuple would be:

change(RecordId, Index, NewValue) ->
    [Rec] = do(qlc:q([X || X <- mnesia:table(rec), X#rec.id =:= RecordId])),
    NewRec = setelement(Index, Rec, NewValue),
    F = fun() -> mnesia:write(NewRec) end,
    {atomic, Val} = mnesia:transaction(F),
    Val.

which you could use like this:

5> Val = record:change(id58, #rec.name, new_value).

This is also a "clean" use of records as tuples as you are using the #rec.name syntax to find the index of the field in the tuple. It was the reason this syntax was added.

1
  • I ended up doing something similar to this. As a note to people reading the question, element/2 was also useful here.
    – Inaimathi
    Jun 1, 2012 at 0:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.