Created
August 7, 2010 16:22
-
-
Save shino/512938 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-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