[erlang-questions] mnesia table layout suggestion

Bryan Burgers bryan.burgers@REDACTED
Tue Jul 31 09:01:10 CEST 2007


Hi.

In part of my program, I want to store lists of objects in mnesia. So
I have these functions:
> add_to_list( User, Object, List ) -> ...
> retrieve_from_list( User, List ) -> ...


So I can do:
> lists:add_to_list( bryan, milk, shopping ).
> lists:add_to_list( bryan, eggs, shopping ).
> lists:add_to_list( bryan, todo, ask_question ).

> retrieve_from_list( bryan, shopping ).
[milk,eggs]


My question is about how to store this in mnesia. Currently, I have a:
> -record( listitem, { key, object } ).
> % where key is a tuple of a user and a list
and I am using a bag. This way, it seems that each user has her own
set of lists, and she can put as many things in the lists. (There will
never be the same item twice in a list, which is the way I want it,
and there is no sharing of lists.)

The problem comes when I want to do queries with QLC, though, because
what I want is:
> qlc:q([ X#listitem.user || X <- mnesia:tables( listitem ), ... ]).
but since I don't have a 'user' field, I can't do that.

It seems one solution would be to make my:
> -record(listitem, { key, user, list, item }).
with redundant information, but that seems like overkill. Is there a
better way to do this? I'm wondering if there is a good way to set up
my mnesia table to handle a
> -record(listitem, { user, list, item }).
with the semantics that I want? Or should I just use the redundant
information, and if I ever want to use the record outside of the
function, I can transform it:

> -record(dblistitem, { key, user, list, item } ).
> -record(listitem, { user, list, item } ).
> transform_listitem( #dblistitem{ key = {User,List}, item = Item ) ->
>   #listitem{ user = User, list = List, item = Item }.

So, do you have any good ideas on the best way to handle this situation?

Thanks in advance,

Bryan



More information about the erlang-questions mailing list