Erlang---ETS table

Source: Internet
Author: User
Tags types of tables

Both ETS and process dictionaries are unique to Erlang.

ETS is an acronym for Erlang term Storage, a memory-based KV (Key Value) Table that supports large data volumes and efficient queries.

To use the ETS table, you first need to create a new ETS table.

1.ets:new (? Ets_sys_building,[{keypos, #ets_sys_building. Sysbuildingid},named_table,public,set])which? Ets_sys_building: Is the table name{keypos, #ets_sys_building. sysbuilding}: Set key to #ets_sys_building.sysbuildingid (can be omitted)Note: The ETS table is based on KV, that is, key value is key-->{key,value}, where value is a tuple, and key defaults to the first element of value, and now the key is set to #ets_sys_ Building.sysbuildingid, as can be seen from key, the value of this ETS table is a tuple starting with ets_sys_building, so to know the value of the ETS table, you can see Ets_sys_ The definition of the building record. table names and value do not have the necessary links, but for ease of operation, the table and record names are generally used as the same name. named_table: If you specify the Named_table attribute, you can use the table name (that is, the first parameter of the new function name) to manipulate the table without using TableID. Public : Specifies the access permissions for the table, and if public means that all process can read and write to the table (as long as you know TableID or TableName), Private means that only the process that creates the table can read and write to the table, while protected indicates that all the process can read the tables, but only the process that created the table can write to the table (which can be omitted, Default is protected). set: Specifies the type of table created 2.ets:insert (tab,objectorobjects)-Trueinserts an object data or an object list data objectorobjects into the ETS table Tab. If the table is a set type of table, and the key of the inserted object data can match the data in the table, then the old object data will be replaced. If the table is a table of type Ordered_set and has the same key as the inserted object data, the old object data will be replaced. If there are multiple instances of the same key in the inserted object list data, and the table is a set type table, then only one object data can be inserted, but it is not sure which one. For tables of type Ordered_set, if the key is the same, the operation is the same as above. The entire operation remains atomic and independent, even when multiple object data insertions are in progress. 3.ets:tab2list (Tab), [Object]returns a list of all object data for an ETS table. 4.ets:lookup (Tab,key), [Object]

Returns a list of all object data in the Table tab key as key, note that this must be matched with key and cannot be used in other fields.

In the type set, bag, and Duplicate_bag tables, only object data that matches key keys is returned. If it is a table of type Ordered_set, it will only be returned if the key is equal. The difference between the two is like =:= and = =.

In the case of a set or ordered_set type table, the function returns an empty list or a list of elements because it is not possible for these types of tables to have multiple object data of the same key. If it is a bag or duplicate_bag type table, then the function returns a list of any length.

The order in which the data is inserted is saved, the first inserted data, and the return time is also ranked first.

in the type is set, bag, Duplicate_bag table, the time to insert and find is constant, regardless of the size of the table. For tables of type Ordered_set, the time to insert and find is proportional to the size of the table. 5.ets:fun2ms (literalfun), MatchSpecuse an listeralfun of a parse transformation to pass as a parameter to the function to convert a matching specification. "literal" means that the function must be in the form of text as a function parameter, and it cannot be a variable passed to the function in turn. Matching Specification Resolution transformation is implemented in the Ms_transform module, the source code must be introduced in the standard library (STDLIB) "Ms_transform.hrl" header file in order to make the functor function. The introduction of the header file in the source code fails, it will report a run-time error, not a compile-time error. Package This file is simple, just add the following line to the code file: -include_lib( "stdlib/include/ms_transform.hrl" ). the pseudo-function literalfun has very restrictive conditions that provide only one parameter (for matching objects): a single variable or a tuple. And you must use a similar assertion detection like is_xxx. For example, the syntax results, such as if, case, receive, are not allowed in the matching specification. The value returned by the function is a matching specification. Erlang built-in functions can be called by matching canonical functions. select( Tab 6.ets : MatchSpec ) -> [ Match ] use a matching description to match the object from the table Tab. ets:fun2ms functions and ets:select functions are generally used together,For example:get_sys_award_info (Sysactiveid)MS = ets:fun2ms (Fun (t) when (T#ets_sys_activity_award.sysactivityid = = Sysactiveid), T end), ets:select (? Ets_sys_activity_award, MS).7.ets:i (tab), ' OK ': Prints the Tab information for the specified ETS table on the output sideets:i (), ' OK ': Print the information on the output showing all ETS tables8.ets delete_all_objects( Tab : ) -> true deletes all object data in the table Tab. This operation maintains the atomicity (atomic) and independence (isolated) of the data.

Erlang---ETS table

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.