Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/paulzql/mnesia_cluster
mnesia_cluster is a simple application easy cluster mnesia and auto create mnesia table
https://github.com/paulzql/mnesia_cluster
Last synced: 2 months ago
JSON representation
mnesia_cluster is a simple application easy cluster mnesia and auto create mnesia table
- Host: GitHub
- URL: https://github.com/paulzql/mnesia_cluster
- Owner: paulzql
- License: other
- Created: 2016-01-10T10:31:26.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2021-04-19T06:10:27.000Z (over 3 years ago)
- Last Synced: 2024-10-04T20:23:26.959Z (3 months ago)
- Language: Erlang
- Size: 16.6 KB
- Stars: 5
- Watchers: 2
- Forks: 6
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
mnesia_cluster
=====mnesia_cluster is a simple application easy cluster mnesia and auto create mnesia table.
1. auto create mnesia table
2. auto merge mnesia table
3. cluster mnesiaBuild
-----$ rebar3 compile
Start/Stop
------**1. start**
make sure start mnesia_cluster before use mnesia, recommend start it after node up.mnesia_cluster:start().
**2. stop**mnesia_cluster:stop()
--------------------
Config Cluster
----
mnesia_cluster will load configed nodes and dynamic join clusterset env before start
```erlang
%% 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
```erlang
[
{mnesia_cluster, [
{mnesia_nodes, ['other@localhost','other2@localhost']}
]}
].
```--------------------
Join Cluster
----
```erlang
%% 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]')
```
--------------------
Leave cluster
----
```erlang
%% if leave successful, will delete current node's data
mnesia_cluster:leave()
```--------------------
Define table
----mnesia_cluster use module attribute 'mnesia_table' define table
**define table with tuple**
```erlang
-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**```erlang
-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)}]}
].
```----------------
Dynamic add defined table from modules
----
```erlang
%% make sure module [test1,test2] has table define
mnesia_cluster:update([test1,test2]).
```--------------------
add module to application
----
mnesia_cluster only discover modules in application file, so you need add module to application filemyapp.app
```erlang
{application, 'myapp',
[{description, "test app"},
{vsn, "0.1.0"},
....
{modules, [chat]},
]}.```