[erlang-questions] swapping tables in mnesia

Chandru chandrashekhar.mullaparthi@REDACTED
Wed Nov 28 23:19:32 CET 2012


-record(my_data, {a, b, c}).

-record(sys_state, {key, value}).

mnesia:create_table(my_data_1, [{record_info(fields, my_data),
{record_name, my_data}, blah, blah]).

mnesia:create_table(my_data_2, [{record_info(fields, my_data),
{record_name, my_data}, blah, blah]).

mnesia:create_table(sys_state, [{record_info(fields, sys_state), blah,
blah}]).

mnesia:dirty_write(#sys_state{key = active_table, value = my_data_1}).

active_table() ->
    case mnesia:dirty_read(sys_state, active_table) of
       [] -> my_data_1;
       [#sys_state{value= V}] -> V
     end.

standby_table() ->
     case active_table() of
          my_data_1 -> my_data_2;
          my_data_2 -> my_data_1
     end.

swap_tables() ->
      mnesia:dirty_write(#sys_state{key = active_table, value =
standby_table()}).

upload_new_data() ->
      write_data_to_standby_table(),
      swap_tables().

read_data_from_table() ->
     mnesia:dirty_read(active_table(), blah).

cheers
Chandru

On 28 November 2012 14:06, Philip Clarke <send2philip@REDACTED> wrote:

> Hi,
>
> Is it possible to swap or replace tables in mnesia ?
>
> For example, say I have a file which is read and parsed by some erlang
> process and then stored in a mnesia table.  When it is ready, the table
> data is used by some other processes.
>
> Then at some later time, I get a new version of the file.  I don't want to
> delete the existing table and load it up from the new file (the system
> would not be able to function during the load time).
>
> I would like for example to load up to a table called e.g. temp and when
> that has finished, replace my existing table with the temp table.
>
> One option I considered is to just keep loading up to new tables e.g.
> version1, version2, version3 etc. and then have another table which keeps
> the name of the table to use.  I'm interested to hear if anyone has a
> better solution.
>
> Thanks
> Philip
>
> _______________________________________________
> 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/20121128/bac23fc1/attachment.htm>


More information about the erlang-questions mailing list