[erlang-questions] Storing Map into DB

Danniel Condez ducondez@REDACTED
Sat Mar 25 03:49:57 CET 2017


Vans, I think the second approach you mentioned is more in-lined to what
I'm looking for cause I'm trying to stay away from the data being
relational. Instead I would like to work more with RDF triples. Thank you
so much for your help!

-Danniel

On Fri, Mar 24, 2017 at 10:25 AM, Vans S <vans_163@REDACTED> wrote:

> >  I try to stay away from normalized data so have more flexibility in
> terms of storing and retrieving data.
>
> In an ideal world I would like mnesia to support maps. Then you can drop
> the record and mnesia becomes a document db, you give it the
> primary/secondary indexes as you create the table,
> it will find those in the maps that are the content.
>
>
> > However, making relationships or pointers to other records with
> different types is still kinda vague. I'm prett ynew to erlang but I think
> I can start with this and learn further.
> This is tough for me too. If anyone has good ideas I'm all ears.
>
> The simplest approach is writing a module with the same name as the tables
> record name and putting the logic into there.  The associations would be
> %{address_ref=> uuid4, wallet_ref=> uuid4}, then to find the wallet
> associated with a particular user you would just do
> :mnesia.read({:db_wallet, wallet_ref}).
>
>
> Another approach is a seperate sql style reference table,
> db_r_user_wallet to map the uuid references.  This has the advantage of
> keeping the data cleaner as you don't have refs in there.
>
> Personally I use the first approach, and only considered the second.
>
> Mnesia's transactions either succeed or fail, so you can do complex things
> in a single transaction like touching multiple tables, and if something
> goes wrong you wont have a half committed transaction.
>
> One of my recent projects was a compute cloud and attaching disks to
> virtual machines looks like this:
>
> -module(db_disk).
>
>
> attach(UuidOwner, UuidVm, UuidDisk) ->
>     ?ENSURE_OWNER(OwnerUuid, UuidVm, UuidDisk),
>     ?ENSURE_ATTACHABLE(UuidVm, UuidDisk),
>
>     ?T(
>         ok = mnesia:write({db_disk, UuidDisk, %{attached_to=> %{UuidVm=>
> UuidVm}}})
>     ).
>
>
> -module(db_vm).
>
> disks(OwnerUuid, UuidVm) ->
>     ?ENSURE_OWNER(OwnerUuid, UuidVm),
>     mnesia:activity(transcation, fun()->
>         mnesia:match_object({db_disk, UuidVm, %{attached_to=>
> %{UuidVm=>_}}})
>     end).
>
>
> The reason 'attached_to' == %{} is because of a hack for a missing data
> type / maps functionality in erlang.
> Maps are new and selects/matches are not able to work on lists, unless
> someone can tell me how.
>
> Ideally %{UuidVm=> UuidVm} could be simplified to %{UuidVm} if erlang were
> to allow sets like usage with
> map syntax. And %{UuidVm=> _} would also become %{UuidVm}.
>
> Im not aware of a way in a match/select to do a lists:member/2.
>
> Each disk can be attached to multiple VMs in the case of network shared
> block storage.
>
>
>
> On Friday, March 24, 2017 12:28 PM, Danniel Condez <ducondez@REDACTED>
> wrote:
>
>
>
> Hi Vans,
>
> Looks like this is a pretty simple and flexible solution. I'm looking
> deeper into mnesia and found `Object based programming in mnesia`(
> http://erlang.org/doc/apps/mnesia/Mnesia_chap5.html#id82246) to be close
> to what I'm trying to achieve. I try to stay away from normalized data so
> have more flexibility in terms of storing and retrieving data. However,
> making relationships or pointers to other records with different types is
> still kinda vague. I'm prett ynew to erlang but I think I can start with
> this and learn further.
>
>
> On Fri, Mar 24, 2017 at 7:45 AM, Vans S <vans_163@REDACTED> wrote:
>
> A mnesia table with the key being the uuid of the object.
> >
> >{db_mytable, uuid4, #{}}
> >
> >You have full fold/match/select available to you now.
> >
> >[Valid] = :mnesia.match_object({:db_ mytable, _, #{auth=>#{token=>
> "353442323232"}})
> >
> >
> >
> >On Friday, March 24, 2017 10:13 AM, Steve Davis
> <steven.charles.davis@REDACTED com> wrote:
> >
> >
> >
> >Simplest (and most portable) would be:
> >
> >
> >
> >for the put/write: term_to_binary(Map), for the get/read:
> binary_to_term(Blob)
> >
> >______________________________ _________________
> >
> >erlang-questions mailing list
> >
> >erlang-questions@REDACTED
> >
> >http://erlang.org/mailman/ listinfo/erlang-questions
> >
> >
> >
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20170324/4550a807/attachment.htm>


More information about the erlang-questions mailing list