mnesia_cluster is a simple application easy cluster mnesia and auto create mnesia table.
- auto create mnesia table
- auto merge mnesia table
- cluster mnesia
$ rebar3 compile
1. start
make sure start mnesia_cluster before use mnesia, recommend start it after node up.
mnesia_cluster:start().
2. stop
mnesia_cluster:stop()
mnesia_cluster will load configed nodes and dynamic join cluster
set env before start
%% set other nodes (will find and connect other nodes)
application:set_env(mnesia_cluster, mnesia_nodes, ['node1','node2'...]).
you can also config in .config file
[
{mnesia_cluster, [
{mnesia_nodes, ['other@localhost','other2@localhost']}
]}
].
%% try to join '[email protected]' cluster
%% note: this will clean current node's data and copy data from '[email protected]'
mnesia_cluster:join('[email protected]')
%% if leave successful, will delete current node's data
mnesia_cluster:leave()
mnesia_cluster use module attribute 'mnesia_table' define table
define table with tuple
-mnesia_table({
TableName, %% the name of table
TableOptions %% the options of table (see mnesia:create_table)
}).
%% ------------------------------------------------------------
%% example:
%% ------------------------------------------------------------
-module(chat).
%% define message table
%% notice: left table copy type (ram_copies, disc_copies, disc_only_copies) with blank
%% it will auto insert node() in the list when do mnesia:create_table
-mnesia_table({message, [{type, bag}, {disc_copies, []}, {attributes, [id, uid, content]}]}).
define table by function
-mnesia_table(
func %% function name (make sure export func/0
).
@spec fun() -> [table_def()],
table_def() :: {atom(), list()}
func() ->
[].
%% ------------------------------------------------------------
%% example:
%% ------------------------------------------------------------
-module(chat).
-export([get_table/0]).
%% define message table
-mnesia_table(get_table).
get_table() ->
[
{message, [{type, bag}, {disc_copies, [node()]}, {attributes, recordinfo(message)}]},
{users, [{type, set}, {disc_only_copies, [node()]}, {attributes, record_info(fields, user)}]}
].
%% make sure module [test1,test2] has table define
mnesia_cluster:update([test1,test2]).
mnesia_cluster only discover modules in application file, so you need add module to application file
myapp.app
{application, 'myapp',
[{description, "test app"},
{vsn, "0.1.0"},
....
{modules, [chat]},
]}.