|
| 1 | +%% Start the shell as: |
| 2 | +%% erl -mnesia |
| 3 | +%% to enable mnesia support. |
| 4 | +-module(frags). |
| 5 | +-export([create/0, activate/0, deactivate/0, add_frag/0, del_frag/0]). |
| 6 | +-export([add_person/1, populate/1]). |
| 7 | +-record(person, {name, |
| 8 | + age = 0, |
| 9 | + address = unknown, |
| 10 | + salary = 0, |
| 11 | + children = []}). |
| 12 | + |
| 13 | + |
| 14 | +add_person(Name) -> |
| 15 | + T = fun() -> |
| 16 | + mnesia:write(#person{name=Name}) |
| 17 | + end, |
| 18 | + mnesia:activity(transaction, T, [], mnesia_frag). |
| 19 | + |
| 20 | +%% Populate the db with N test data |
| 21 | +populate(N) -> |
| 22 | + [add_person(integer_to_list(X)) || X <- lists:seq(1,N)]. |
| 23 | + |
| 24 | +create() -> |
| 25 | + application:stop(mnesia), |
| 26 | + mnesia:create_schema([node()|nodes()]), |
| 27 | + application:ensure_all_started(mnesia), |
| 28 | + create_tables(tables()), |
| 29 | + ok. |
| 30 | + |
| 31 | +%% Adds new fragment to the table! |
| 32 | +add_frag() -> |
| 33 | + mnesia:change_table_frag(person, {add_frag, [node()]}). |
| 34 | + |
| 35 | +%% Removes an existing frag from the table |
| 36 | +del_frag() -> |
| 37 | + mnesia:change_table_frag(person, del_frag). |
| 38 | + |
| 39 | +%% Activate table frag for this table |
| 40 | +activate() -> |
| 41 | + mnesia:change_table_frag(person, {activate,[]}). |
| 42 | + |
| 43 | +%% Deactivate table fragmentation for this table |
| 44 | +deactivate() -> |
| 45 | + mnesia:change_table_frag(person, deactivate). |
| 46 | +%% Private |
| 47 | +create_tables([]) -> ok; |
| 48 | +create_tables([{Table, Options}|Rest]) -> |
| 49 | + mnesia:create_table(Table, Options), |
| 50 | + create_tables(Rest). |
| 51 | + |
| 52 | +tables() -> |
| 53 | + [{person, |
| 54 | + [{frag_properties, [{node_pool, [node()]}, |
| 55 | + {n_fragments, 2}, {n_disc_copies, 1}]}, |
| 56 | + {attributes, record_info(fields, person)}]}]. |
0 commit comments