"Erlang / OTP concurrent programming combat" Chapter IX with Mnesia increase support for distributed cache

  1. Mnesia is a lightweight soft real-time distributed data storage systems, redundant replication and transactional, discrete Erlang suitable for storing data blocks, especially good at data stored in RAM.
  2. Mnesia for low redundant smaller size data storage requirements. Run-time data for a moderate size (disk-based) persistent data, or need cross-process sharing, Mnesia are a good choice.
  3. The step of establishing a database:
    1. Initialization Mnesia
    2. Start node
    3. The establishment of a database schema
    4. Starting Mnesia
    5. Establishment of a database table
    6. The new table entry of data
    7. To do some basic query data
  4. Initialize the database:
    1. Start node:
      erl -mnesia dir '"/tmp/mnesia_store"' -name mynode

       

    2. The establishment of a database schema:
      the so-called database schema is some descriptive information, which records details of what the table, then the database table there and how.
      To build a distributed database on multiple nodes, you must deposit a copy of the pattern on all nodes to allow the nodes to understand the general structure of its own stored data.
      mnesia:create_schema([node()]).    % 在本地节点建立空数据库模式

      If this fails, it may be because the current node can not establish communication with a node in the list, it could be on a node has Mnesia in operation, or which remain in the old database schema on a node (you can call mnesia : delete_schema (Nodes) to clean up the old model).

    3. Starting Mnesia

      mnesia:start().
      mnesia:info().    % 查看数据库信息
    4. Built table:
      mnesia:create_table(Name, Options).
      
      -record(user, {id, name}).
      mnesia:create_table(user, [{attributes, record_info(fields, user)}, {type, bag}]).
      
      mnesia:write(#user{id=Id, name=Name}).
      mnasia:read(user, Id).
      mnesia:transaction(Fun).
      mnesia:dirty_write(#user{id=Id, name=Name}).
      
      
      %% record_info/2 不是真正意义上的函数,它只在编译器有效(和记录语法中的#一样),在运行期或在 Erlang shell 中无法调用它。
      
      其他的默认选项:
      1、表既可读也可写
      2、表仅驻留于 RAM 中
      3、表中存储的记录与表同名
      4、表的类型为 set
      5、加载优先级为0
      6、local_content 标记被置为 false
      
      Mnesia 表类型:
      1、set
      2、ordered_set
      3、bag
      
      Mnesia 存储类型:
      1、ram_copies
      2、disc_copies
      3、disc_only_copies    % 不支持ordered_set
      
      不同节点上的表可以有不同的存储类型,甚至支持运行时修改。

      Options is a {Name, Value} list of options, among all the options, the most important one is the Attributes, which option is used to specify the field names of the records stored in the table.
      If it is not, the Mnesia assumes only two fields recorded, respectively, and key val.
      Table's primary key is always the first field of the record.

  5. Inquire:

    mnesia:select(user, [{#user{id='$1', name=zh}, [], ['$1']}]).
    
    {Head, Condition, Results}
    Condition 罗列作用于该匹配条件上的额外约束条件
    Result 描述要从匹配到的每条记录中生成什么样的结果项式
    
    '_'    仅限于在 Head 部分使用,无所谓,任意值都可以
    '$_'   仅限于在 Result 和 Condition 中使用,与查询条件相匹配的整条记录
    '$$'   仅限于在 Result 和 Condition 中使用,等价于依此罗列出在 Head 部分匹配的所有变量

     

  6. Query list velocity structure (QLC):
    -include_lib("stdlib/include/qlc.hrl").
    
    Table = mnesia:table(user),
    QueryHandle = qlc:q([U#user.id || U <- Table, U#user.name =:= zh]),
    qlc:eval(QueryHandle).

    QLC is a common query interface, applicable to all kinds of things having the features table ETS tables, tables, etc. the Mnesia.

发布了42 篇原创文章 · 获赞 2 · 访问量 1万+

Guess you like

Origin blog.csdn.net/sanmao123456_/article/details/103489083