Skip to content

Commit 03b9e16

Browse files
committed
Example of mnesia fragmentation including activating dactivating it and adding and removing fragments
0 parents  commit 03b9e16

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Mnesia.nonode@nohost
2+
*.beam

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Mnesia Table Fragmentation
2+
==========================
3+
4+
Following these two resources:
5+
6+
- https://erlangcentral.org/wiki/index.php/Mnesia_Table_Fragmentation
7+
- http://erlang.org/doc/apps/mnesia/Mnesia_chap5.html#id79999

frags.erl

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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

Comments
 (0)