Skip to content

Instantly share code, notes, and snippets.

@shino
Created August 7, 2010 16:22
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shino/512938 to your computer and use it in GitHub Desktop.
Save shino/512938 to your computer and use it in GitHub Desktop.
-module(mnesia_repl_and_frag).
-compile([export_all]).
-record(store, {key, value}).
%% Usage:
%% Start secondary node, at first. Not a typo :-)
%% Then start primary at another node.
primary() ->
net_kernel:start(['a@localhost', shortnames]),
erlang:set_cookie(node(),'secret'),
mnesia:start(),
mnesia:change_config(extra_db_nodes, ['b@localhost']),
mnesia:delete_table(store),
case mnesia:create_table(store,
[{record_name, store}, {type, set},
{frag_properties,
[{node_pool, ['a@localhost', 'b@localhost']},
{n_ram_copies, 2},
{n_fragments, 8}]},
{attributes, record_info(fields, store)}]) of
{atomic, ok} ->
add_data(1, 1000),
{ok, created};
{aborted, {already_exists,store}} ->
{ok, already_exists};
{aborted, _Reason} ->
{aborted, _Reason}
end.
secondary() ->
net_kernel:start(['b@localhost', shortnames]),
erlang:set_cookie(node(),'secret'),
mnesia:start(),
ok.
table_info(Item) ->
mnesia:activity(async_dirty, fun mnesia:table_info/2,
[store, Item],
mnesia_frag).
add_data(Start, Stop) ->
F = fun(Key) ->
mnesia:activity(async_dirty, fun mnesia:write/3,
[store,
#store{key = Key, value = Key},
dirty_write],
mnesia_frag)
end,
lists:foreach(F, lists:seq(Start, Stop)),
ok.
add_data_BAD_EXAMPLE(Start, Stop) ->
%% NOTE: dirty operations are NOT delegated to access module.
%% So this is BAD example.
F = fun(Key) ->
mnesia:activity(async_dirty, fun mnesia:dirty_write/2,
[store,
#store{key = Key, value = Key}],
mnesia_frag)
end,
lists:foreach(F, lists:seq(Start, Stop)),
ok.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment