Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Help me understand mnesia (NoSQL) modeling

In my Quest to understanding Mnesia, I still struggle with thinking in relational terms. So I will put my struggles up here and ask for the best way to solve them.

one-to-many-relations Say I have a bunch of people,

-record(contact, {name, phone}). 

Now, I know that I can define phone to always be saved as a list, so people can have multiple phone numbers, and I suppose that's the way to do it (is it? How would I then look this up the other way around, say, finding a name to a number?).

many-to-many-relations now let's suppose I have multiple groups I can put people in. The group names don't have any significance, they are just names; the concept is "unix system groups" or "labels". Naively, I would model this membership as a proplist, like

{groups [{friends, bool()}, {family, bool()}, {work, bool()}]} %% and so on...

as a field within the "contact" record from above, for example. What is the best way to model this within mnesia if I want to be able to lookup all members of a group based on group name quickly, and also want to be able to lookup all group an individual is registered in? I also could just model this as a list containing just the group identifiers, of course. For use with mnesia, what is the best way to model this?

I apologize if this question is dumb. There's plenty of documentation on mnesia, but it's lacking (IMO) some good examples for the overall use.

like image 557
CONTRACT SAYS I'M RIGHT Avatar asked Nov 06 '10 13:11

CONTRACT SAYS I'M RIGHT


1 Answers

For the first example, consider this record:

-record(contact, {name, [phonenumber, phonenumber, ...]}).

contact is a record with two fields, name and phone where phone is a list of phone numbers. As user425720 said it could make sense to store these as something else than strings, if you have extreme requirements for small storage footprint, for example.

Now here comes the part that is hard to "get" with key-value stores: you need to also store the inverse relationship. In other words, you need something similar to the following:

-record(phone, {phonenumber, contactname}).

If you have a layer in your application to abstract away database handling, you could make it always add/change the phone records when adding/changing a contact.

--

For the second example, consider these two records:

-record(contact, {uuid, name, [group_id, group_id]}).
-record(group, {uuid, name, [contact_id, contact_id]}).

The easiest way is to just store ids pointing to the related records. As Mnesia has no concept of referential integrity, this can become out of sync if you for example delete a group without removing that group from all users.

If you need to store the type of group on the contact record, you could use the following:

-record(contact, {name, [{family, [group_id, group_id]}, {work, [..]}]}).

--

Your second problem could also be solved by using a intermediate record, which you can think of as "membership".

-record(contact, {uuid, name, ...}).
-record(group, {uuid, name, ...}).
-record(membership, {contact_uuid, group_uuid}). # must use 'bag' table type

There can be any number of "membership" records. There will be one record for every users group.

like image 50
knutin Avatar answered Nov 15 '22 01:11

knutin