Krug.DistributedMnesia (Krug v1.1.40) View Source

Utilitary module to handle Erlang Mnesia Database. Mnesia Database has single instance mode and also distributed mode that is purpose of this module. Single instance way don't allow us to improve horizontal scalability when we need.

Link to this section Summary

Functions

Add a new table in runtime execution to mnesia schema and replicate to other nodes. Return true or false.

Removes all object entries from "table_name". Return true or false.

Executes a "count" operation against a "table_name".

Delete a row identified by "id_row" in a table "table_name". Return true or false.

Almost the same that "init_cluster". The diference is that cluster_ips will be calculated to be all range of machine local network according the network mask range (/16 or /24).

Keep the "amount_to_keep" entries from "table_name" table. All other entries will be removed/deleted. Return true or false.

Retrieves an object identified by "id_row" from "table_name". Return the registry entry or nil.

Get the first element in a table "table_name". Return the stored value or nil.

Get the last element in a table "table_name". Return the stored value or nil.

Executes a "select" operation against a "table_name" filtering by "array_params"

Set last updated time of an entry on "table_name". Stores this data on metadata control table. Return true or false.

Stores an object "data_row" identified by "id_row" on "table_name". Return true or false.

Link to this section Functions

Link to this function

add_runtime_table(table)

View Source (since 1.1.26)

Add a new table in runtime execution to mnesia schema and replicate to other nodes. Return true or false.

If table already was created in runtime, keep the actual table and return true. If table has same name from a table created in initialization, the operation will fail and will return false. You should keep control about the already created tables.

Should be in a same format as when define tables for initialization:

%{
  table_name: :my_table_name, 
  table_attributes: [:attr_1, :attr_2, ...  :attr_N] 
}

Requires mnesia already be started.

Link to this function

add_runtime_table2(table)

View Source

Removes all object entries from "table_name". Return true or false.

Requires mnesia already be started.

If you wish you application be able to scalabity then should be used

init_cluster(cluster_name,cluster_ips,ips_separator,disc_copies,tables)

function on application startup.

Link to this function

count(table_name)

View Source (since 1.1.25)

Executes a "count" operation against a "table_name".

Requires mnesia already be started.

Link to this function

delete(table_name, id_row)

View Source (since 1.1.25)

Delete a row identified by "id_row" in a table "table_name". Return true or false.

Requires mnesia already be started.

Link to this function

init_auto_cluster(cluster_name, cluster_cookie, disc_copies \\ false, tables \\ [], connection_timeout \\ nil)

View Source

Almost the same that "init_cluster". The diference is that cluster_ips will be calculated to be all range of machine local network according the network mask range (/16 or /24).

Link to this function

init_cluster(cluster_name, cluster_cookie, cluster_ips, ips_separator \\ "|", disc_copies \\ false, tables \\ [], connection_timeout \\ nil)

View Source

Start the distributed mnesia cluster. To be used on application start.

Example

defmodule <Your_App_Main_Module_Name>.Application do

  @moduledoc false

  use Application

  alias Krug.DistributedMnesia


  def start(_type, _args) do
    Supervisor.start_link(children(), opts())
  end

  defp children() do
      [
        ...
        <Your_App_Main_Module_Name>.DistributedMnesiaTaskStarter, # calls Krug.DistributedMnesia.init_cluster(...)
        ...
      ]
  end

  defp opts() do 
      [strategy: :one_for_one, name: <Your_App_Main_Module_Name>.Supervisor]
  end 
  
end
defmodule <Your_App_Main_Module_Name>.DistributedMnesiaConfigTaskStarter do
  def child_spec(opts) do
    %{id: __MODULE__,start: {__MODULE__, :start_link, [opts]}}
  end

  def start_link(opts) do
    Supervisor.start_link([{<Your_App_Main_Module_Name>.DistributedMnesiaConfigTask,opts}], strategy: :one_for_one)
  end
end
defmodule <Your_App_Main_Module_Name>.DistributedMnesiaConfigTask do

  use Task
  alias Krug.DistributedMnesia

  def start_link(opts) do
    Task.start_link(__MODULE__, :run, [opts])
  end

  def run(_opts) do
    cluster_cookie = "echo"
    cluster_name = "echo"
    cluster_ips = "192.168.1.12X "
    ips_separator = "X" 
    tables = [
      %{
         table_name: :users, 
         table_attributes: [:id, :name, :email, :last_access] 
      },
      %{
         table_name: log, 
         table_attributes: [:id, :user_id, :action, :date_time] 
      },
    ]  
  
    cluster_name
      |> DistributedMnesia.init_cluster(cluster_cookie,cluster_ips,ips_separator,true,tables)
  end

end

disc_copies: true for ":disc_copies" (ram + disc), false for ":ram_copies" (only ram).

tables: list of map table configurations

%{
  table_name: :users, # atom
  table_attributes: [:id, :name, :email] # atom list | the first element is the "id_row" value/column
}

.

connected_nodes: list (of atom) nodes already connected in a erlang cluster.

Link to this function

keep_only_last_used(table_name, amount_to_keep)

View Source

Keep the "amount_to_keep" entries from "table_name" table. All other entries will be removed/deleted. Return true or false.

Usefull for caching control (limit memory usage and others). With this you could limit each table to keep only the X last recent used (stored/loaded) entries, optimizing the memory usage and getting better caching performance - keeping in cache only the entries that are more often requested.

Requires mnesia already be started.

Link to this function

load(table_name, id_row)

View Source

Retrieves an object identified by "id_row" from "table_name". Return the registry entry or nil.

Requires mnesia already be started.

If you wish you application be able to scalabity then should be used

init_cluster(cluster_name,cluster_ips,ips_separator,disc_copies,tables)

function on application startup.

Link to this function

load_first(table_name)

View Source (since 1.1.25)

Get the first element in a table "table_name". Return the stored value or nil.

Requires mnesia already be started.

Link to this function

load_last(table_name)

View Source (since 1.1.25)

Get the last element in a table "table_name". Return the stored value or nil.

Requires mnesia already be started.

Link to this function

select(table_name, array_params)

View Source (since 1.1.25)

Executes a "select" operation against a "table_name" filtering by "array_params"

Requires mnesia already be started.

Link to this function

set_updated_at(table_name, id_row)

View Source

Set last updated time of an entry on "table_name". Stores this data on metadata control table. Return true or false.

Used by internal controls, and also exposes a way to other modules change this values (for example if you are caching something and needs to delete the oldest used objects - setting new value for updated_at each time that the object is used "loaded").

Link to this function

store(table_name, id_row, data_row)

View Source

Stores an object "data_row" identified by "id_row" on "table_name". Return true or false.

Requires mnesia already be started.

If you wish you application be able to scalabity then should be used

init_cluster(cluster_name,cluster_ips,ips_separator,disc_copies,tables)

function on application startup.