[Erl_question19] efficiency in reading the config file of Erlang

Source: Internet
Author: User
Several options of the Erlang configuration file:
Method Advantages Disadvantages
1. Read the configuration file to the storage medium (ETS table or MySQL) Unified management to facilitate cross-Node Distribution of reload,
Erlang application is to read the configuration file to an ETS table named ac_tab.
If you frequently read a large number of Global tables, consider the table performance.
2. Use the configuration file as *. xml or *. config, and use Erlang to directly read XML or config Configuring XML makes it easy for configuration personnel (both the front and back ends are XML and easy to use)
Configured as config, Erlang reading program is very simple
Erlang is not suitable for reading XML
Reading a hard disk file frequently is obviously bad.
3. Convert the configuration file to the *. Beam file. High query efficiency Almost none. Of course, distributed deployment is a little complicated, but this function is useless.
Thus: configure the conversion by yourself ( You are welcome to find bugs. You cannot find them because the Code has no bugs. Haha): You only need to put the config file in the specified file path:
% Convert all config files in get_config_dir (). You can check whether the format of each config file is duplicate or whether the format is {key, value} 1> configtobeam: config_to_beam (). check config syntax error "test_duplicate_error.config": reason: {error,
Key_duplicate,
1}
Check config syntax error "test_format_error.config": reason: {error,
Format_error,
{2, 3, Test2 }}
[{OK, test_best },
{Error, "test_duplicate_error.config "},
{Error, "test_format_error.config"}]
% Returns all {key, value} 2> test_best: All (). [{4, test4}, {3, test3}, {2, Test2}, {1, test}] % returns value3> test_best: Lookup (2) of the corresponding key ). test2 % if the key does not exist, undefined4> test_best: Lookup (12) is returned ). undefined
Put my test data directory in
1 code: root_dir () ++ "/config" 2 3% % contains four files: 4% skip test.txt skip 5% % test_best.config without checking the config file. This is normal configuration 6% % test_format_error.config not {key, value} configuration 7% % test_duplicate_error.config key has repeated Configuration

 

The source code is as follows:
1% % --------------------------------------------------------------------- 2% % @ author [email protected] 3% % @ Doc put the corresponding. convert the config file. beam file and updated to memory 4% % configname: All () return all key_value 5% % configname: Lookup (key) return value or undefined 6% % 1. the file set in the directory is 7% % 2. traverse and convert each file 8% % 2.1 read the file into check format 9% % 2.2 convert config to ERL file 10% % 2.2 compile ERL file into beam file 11% % 3. load beam file to memory 12% % @ end 13% % creat ED: 22. aug 2014 PM 14% % ----------------------------------------------------------------------- 15-module (configtobeam ). 16 17-compile (export_all ). 18% % API 19-Export ([config_to_beam/0, config_to_beam/1, get_config_dir/0]). 20 21% % @ Doc convert all the config files in the specified directory. beam file and load it to memory 22 config_to_beam ()-> 23 allconfigname = get_all_config_name (), 24 [begin 25 config_to_beam (configname) 26 end | confignam E <-allconfigname]. 27 28% % @ Doc convert the config file named configname in the specified directory. beam file and load it to memory 29 config_to_beam (configname)-> 30 case check_syntax (configname) of 31 {OK, termlist}-> 32 term_to_beam (configname --". config ", termlist); 33 reason-> 34 IO: Format (" Check config syntax error ~ P: reason :~ P ~ N ", [configname, reason]), 35 {reason, configname} 36 end. 37 38% % @ todo is changed to your storage. config file directory 39 get_config_dir ()-> 40 Code: root_dir () ++ "/config ". 41 42% % % % 43% % internal function 44% % % % 45 term_to_beam (modulename, term List)-> 46 ERL = term_to_erl (modulename, termlist), 47 modulename2 = modulename ++ ". erl ", 48 file: write_file (modulename2, ERL, [write, binary, {encoding, utf8}]), 49 compile: file (modulename2 ), 50 modulename3 = list_to_atom (modulename), 51 code: purge (modulename3), 52 code: load_file (modulename3), 53 {OK, modulename3 }. 54 55 term_to_erl (modulename, termlist)-> 56 strvalue = lists: flatten (term_to_erl2 (T Ermlist, ""), 57 strlist = lists: flatten (io_lib: Format ("~ W \ n ", [termlist]), 58" 59-module ("++ modulename ++ "). 60-Export ([All/0, lookup/1]). 61 62 All ()-> "++ strlist ++ ". 63 64 Lookup (key)-> 65 case key of 66 "++ strvalue ++" 67 _-> undefined 68 end. 69 ". 70 71 term_to_erl2 ([], sum)-> 72 Sum; 73 term_to_erl2 ([{key, value} | left], ACC)-> 74 term_to_erl2 (left, 75 io_lib: format ("~ W-> ~ W; \ n ", [key, value]) ++ ACC ). 76 77 get_all_config_name ()-> 78 {OK, allfilename} = file: list_dir (get_config_dir (), 79 lists: Filter (fun (filename)-> 80 case lists: reverse (filename) of 81 "gifnoc. "++ _-> true; 82 _-> false 83 end 84 end, allfilename ). 85 86 check_syntax (configname)-> 87 case file: Consult (joint_path (configname) of 88 {OK, termlist = [_ | _]}-> 89 check_fromat_duplicate (termlist, []); 90 reason-> 91 {error, reason} 92 end. 93 94 joint_path (configname)-> 95 get_config_dir () ++ "/" ++ configname. 96 97 check_fromat_duplicate ([], ACC)-> 98 {OK, ACC}; 99 check_fromat_duplicate ([{key, _ value} = term | left], ACC) -> 100 Case lists: keymember (Key, 1, ACC) of101 true-> {error, key_duplicate, key}; 102 false-> check_fromat_duplicate (left, [term | ACC]) 103 end; 104 check_fromat_duplicate ([term | _], _ ACC)-> 105 {error, format_error, term }.

 

Life is hard, but Lao Tzu is so cute ^

[Erl_question19] efficiency in reading the config file of Erlang

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.