Erlang -- ets

STDLIB

Reference Manual

Version 3.13

- User's Guide
- Reference Manual
- Release Notes
- PDF
- Top

- Expand All
- Contract All

### Table of Contents

- STDLIB (App)
- array
  - Top of manual page
  - default/1
  - fix/1
  - foldl/3
  - foldr/3
  - from_list/1
  - from_list/2
  - from_orddict/1
  - from_orddict/2
  - get/2
  - is_array/1
  - is_fix/1
  - map/2
  - new/0
  - new/1
  - new/2
  - relax/1
  - reset/2
  - resize/1
  - resize/2
  - set/3
  - size/1
  - sparse_foldl/3
  - sparse_foldr/3
  - sparse_map/2
  - sparse_size/1
  - sparse_to_list/1
  - sparse_to_orddict/1
  - to_list/1
  - to_orddict/1
- assert.hrl
- base64
  - Top of manual page
  - decode/1
  - decode_to_string/1
  - encode/1
  - encode_to_string/1
  - mime_decode/1
  - mime_decode_to_string/1
- beam_lib
  - Top of manual page
  - all_chunks/1
  - build_module/1
  - chunks/2
  - chunks/3
  - clear_crypto_key_fun/0
  - cmp/2
  - cmp_dirs/2
  - crypto_key_fun/1
  - diff_dirs/2
  - format_error/1
  - info/1
  - md5/1
  - strip/1
  - strip/2
  - strip_files/1
  - strip_files/2
  - strip_release/1
  - strip_release/2
  - version/1
- binary
  - Top of manual page
  - at/2
  - bin_to_list/1
  - bin_to_list/2
  - bin_to_list/3
  - compile_pattern/1
  - copy/1
  - copy/2
  - decode_unsigned/1
  - decode_unsigned/2
  - encode_unsigned/1
  - encode_unsigned/2
  - first/1
  - last/1
  - list_to_bin/1
  - longest_common_prefix/1
  - longest_common_suffix/1
  - match/2
  - match/3
  - matches/2
  - matches/3
  - part/2
  - part/3
  - referenced_byte_size/1
  - replace/3
  - replace/4
  - split/2
  - split/3
- c
  - Top of manual page
  - bt/1
  - c/1
  - c/2
  - c/3
  - cd/1
  - erlangrc/1
  - flush/0
  - h/1
  - h/2
  - h/3
  - help/0
  - ht/1
  - ht/2
  - ht/3
  - i/0
  - i/3
  - l/1
  - lc/1
  - lm/0
  - ls/0
  - ls/1
  - m/0
  - m/1
  - memory/0
  - memory/1
  - mm/0
  - nc/1
  - nc/2
  - ni/0
  - nl/1
  - nregs/0
  - pid/3
  - pwd/0
  - q/0
  - regs/0
  - uptime/0
  - xm/1
  - y/1
  - y/2
- calendar
  - Top of manual page
  - date_to_gregorian_days/1
  - date_to_gregorian_days/3
  - datetime_to_gregorian_seconds/1
  - day_of_the_week/1
  - day_of_the_week/3
  - gregorian_days_to_date/1
  - gregorian_seconds_to_datetime/1
  - is_leap_year/1
  - iso_week_number/0
  - iso_week_number/1
  - last_day_of_the_month/2
  - local_time/0
  - local_time_to_universal_time/1
  - local_time_to_universal_time_dst/1
  - now_to_datetime/1
  - now_to_local_time/1
  - now_to_universal_time/1
  - rfc3339_to_system_time/1
  - rfc3339_to_system_time/2
  - seconds_to_daystime/1
  - seconds_to_time/1
  - system_time_to_local_time/2
  - system_time_to_rfc3339/1
  - system_time_to_rfc3339/2
  - system_time_to_universal_time/2
  - time_difference/2
  - time_to_seconds/1
  - universal_time/0
  - universal_time_to_local_time/1
  - valid_date/1
  - valid_date/3
- dets
  - Top of manual page
  - all/0
  - bchunk/2
  - close/1
  - delete/2
  - delete_all_objects/1
  - delete_object/2
  - first/1
  - foldl/3
  - foldr/3
  - from_ets/2
  - info/1
  - info/2
  - init_table/2
  - init_table/3
  - insert/2
  - insert_new/2
  - is_compatible_bchunk_format/2
  - is_dets_file/1
  - lookup/2
  - match/1
  - match/2
  - match/3
  - match_delete/2
  - match_object/1
  - match_object/2
  - match_object/3
  - member/2
  - next/2
  - open_file/1
  - open_file/2
  - pid2name/1
  - repair_continuation/2
  - safe_fixtable/2
  - select/1
  - select/2
  - select/3
  - select_delete/2
  - slot/2
  - sync/1
  - table/1
  - table/2
  - to_ets/2
  - traverse/2
  - update_counter/3
- dict
  - Top of manual page
  - append/3
  - append_list/3
  - erase/2
  - fetch/2
  - fetch_keys/1
  - filter/2
  - find/2
  - fold/3
  - from_list/1
  - is_empty/1
  - is_key/2
  - map/2
  - merge/3
  - new/0
  - size/1
  - store/3
  - take/2
  - to_list/1
  - update/3
  - update/4
  - update_counter/3
- digraph
  - Top of manual page
  - add_edge/3
  - add_edge/4
  - add_edge/5
  - add_vertex/1
  - add_vertex/2
  - add_vertex/3
  - del_edge/2
  - del_edges/2
  - del_path/3
  - del_vertex/2
  - del_vertices/2
  - delete/1
  - edge/2
  - edges/1
  - edges/2
  - get_cycle/2
  - get_path/3
  - get_short_cycle/2
  - get_short_path/3
  - in_degree/2
  - in_edges/2
  - in_neighbours/2
  - info/1
  - new/0
  - new/1
  - no_edges/1
  - no_vertices/1
  - out_degree/2
  - out_edges/2
  - out_neighbours/2
  - vertex/2
  - vertices/1
- digraph_utils
  - Top of manual page
  - arborescence_root/1
  - components/1
  - condensation/1
  - cyclic_strong_components/1
  - is_acyclic/1
  - is_arborescence/1
  - is_tree/1
  - loop_vertices/1
  - postorder/1
  - preorder/1
  - reachable/2
  - reachable_neighbours/2
  - reaching/2
  - reaching_neighbours/2
  - strong_components/1
  - subgraph/2
  - subgraph/3
  - topsort/1
- epp
  - Top of manual page
  - close/1
  - default_encoding/0
  - encoding_to_string/1
  - format_error/1
  - open/1
  - open/2
  - open/3
  - parse_erl_form/1
  - parse_file/2
  - parse_file/3
  - read_encoding/1
  - read_encoding/2
  - read_encoding_from_binary/1
  - read_encoding_from_binary/2
  - set_encoding/1
  - set_encoding/2
- erl_anno
  - Top of manual page
  - column/1
  - end_location/1
  - file/1
  - from_term/1
  - generated/1
  - is_anno/1
  - line/1
  - location/1
  - new/1
  - set_file/2
  - set_generated/2
  - set_line/2
  - set_location/2
  - set_record/2
  - set_text/2
  - text/1
  - to_term/1
- erl_eval
  - Top of manual page
  - add_binding/3
  - binding/2
  - bindings/1
  - del_binding/2
  - expr/2
  - expr/3
  - expr/4
  - expr/5
  - expr_list/2
  - expr_list/3
  - expr_list/4
  - exprs/2
  - exprs/3
  - exprs/4
  - new_bindings/0
- erl_expand_records
  - Top of manual page
  - module/2
- erl_id_trans
  - Top of manual page
  - parse_transform/2
- erl_internal
  - Top of manual page
  - add_predefined_functions/1
  - arith_op/2
  - bif/2
  - bool_op/2
  - comp_op/2
  - guard_bif/2
  - list_op/2
  - op_type/2
  - send_op/2
  - type_test/2
- erl_lint
  - Top of manual page
  - format_error/1
  - is_guard_test/1
  - module/1
  - module/2
  - module/3
- erl_parse
  - Top of manual page
  - abstract/1
  - abstract/2
  - anno_from_term/1
  - anno_to_term/1
  - fold_anno/3
  - format_error/1
  - map_anno/2
  - mapfold_anno/3
  - new_anno/1
  - normalise/1
  - parse_exprs/1
  - parse_form/1
  - parse_term/1
  - tokens/1
  - tokens/2
- erl_pp
  - Top of manual page
  - attribute/1
  - attribute/2
  - expr/1
  - expr/2
  - expr/3
  - expr/4
  - exprs/1
  - exprs/2
  - exprs/3
  - form/1
  - form/2
  - function/1
  - function/2
  - guard/1
  - guard/2
- erl_scan
  - Top of manual page
  - category/1
  - column/1
  - end_location/1
  - format_error/1
  - line/1
  - location/1
  - reserved_word/1
  - string/1
  - string/2
  - string/3
  - symbol/1
  - text/1
  - tokens/3
  - tokens/4
- erl_tar
  - Top of manual page
  - add/3
  - add/4
  - close/1
  - create/2
  - create/3
  - extract/1
  - extract/2
  - format_error/1
  - init/3
  - open/2
  - t/1
  - table/1
  - table/2
  - tt/1
- ets
  - Top of manual page
  - all/0
  - delete/1
  - delete/2
  - delete_all_objects/1
  - delete_object/2
  - file2tab/1
  - file2tab/2
  - first/1
  - foldl/3
  - foldr/3
  - from_dets/2
  - fun2ms/1
  - give_away/3
  - i/0
  - i/1
  - info/1
  - info/2
  - init_table/2
  - insert/2
  - insert_new/2
  - is_compiled_ms/1
  - last/1
  - lookup/2
  - lookup_element/3
  - match/1
  - match/2
  - match/3
  - match_delete/2
  - match_object/1
  - match_object/2
  - match_object/3
  - match_spec_compile/1
  - match_spec_run/2
  - member/2
  - new/2
  - next/2
  - prev/2
  - rename/2
  - repair_continuation/2
  - safe_fixtable/2
  - select/1
  - select/2
  - select/3
  - select_count/2
  - select_delete/2
  - select_replace/2
  - select_reverse/1
  - select_reverse/2
  - select_reverse/3
  - setopts/2
  - slot/2
  - tab2file/2
  - tab2file/3
  - tab2list/1
  - tabfile_info/1
  - table/1
  - table/2
  - take/2
  - test_ms/2
  - to_dets/2
  - update_counter/3
  - update_counter/4
  - update_element/3
  - whereis/1
- file_sorter
  - Top of manual page
  - check/1
  - check/2
  - keycheck/2
  - keycheck/3
  - keymerge/3
  - keymerge/4
  - keysort/2
  - keysort/3
  - keysort/4
  - merge/2
  - merge/3
  - sort/1
  - sort/2
  - sort/3
- filelib
  - Top of manual page
  - ensure_dir/1
  - file_size/1
  - find_file/2
  - find_file/3
  - find_source/1
  - find_source/2
  - find_source/3
  - fold_files/5
  - is_dir/1
  - is_file/1
  - is_regular/1
  - last_modified/1
  - safe_relative_path/2
  - wildcard/1
  - wildcard/2
- filename
  - Top of manual page
  - absname/1
  - absname/2
  - absname_join/2
  - basedir/2
  - basedir/3
  - basename/1
  - basename/2
  - dirname/1
  - extension/1
  - find_src/1
  - find_src/2
  - flatten/1
  - join/1
  - join/2
  - nativename/1
  - pathtype/1
  - rootname/1
  - rootname/2
  - safe_relative_path/1
  - split/1
- gb_sets
  - Top of manual page
  - add/2
  - add_element/2
  - balance/1
  - del_element/2
  - delete/2
  - delete_any/2
  - difference/2
  - empty/0
  - filter/2
  - fold/3
  - from_list/1
  - from_ordset/1
  - insert/2
  - intersection/1
  - intersection/2
  - is_disjoint/2
  - is_element/2
  - is_empty/1
  - is_member/2
  - is_set/1
  - is_subset/2
  - iterator/1
  - iterator_from/2
  - largest/1
  - new/0
  - next/1
  - singleton/1
  - size/1
  - smallest/1
  - subtract/2
  - take_largest/1
  - take_smallest/1
  - to_list/1
  - union/1
  - union/2
- gb_trees
  - Top of manual page
  - balance/1
  - delete/2
  - delete_any/2
  - empty/0
  - enter/3
  - from_orddict/1
  - get/2
  - insert/3
  - is_defined/2
  - is_empty/1
  - iterator/1
  - iterator_from/2
  - keys/1
  - largest/1
  - lookup/2
  - map/2
  - next/1
  - size/1
  - smallest/1
  - take/2
  - take_any/2
  - take_largest/1
  - take_smallest/1
  - to_list/1
  - update/3
  - values/1
- gen_event
  - Top of manual page
  - add_handler/3
  - add_sup_handler/3
  - call/3
  - call/4
  - check_response/2
  - delete_handler/3
  - notify/2
  - send_request/3
  - start/0
  - start/1
  - start/2
  - start_link/0
  - start_link/1
  - start_link/2
  - start_monitor/0
  - start_monitor/1
  - start_monitor/2
  - stop/1
  - stop/3
  - swap_handler/3
  - swap_sup_handler/3
  - sync_notify/2
  - wait_response/2
  - which_handlers/1
  - Module:code_change/3
  - Module:format_status/2
  - Module:handle_call/2
  - Module:handle_event/2
  - Module:handle_info/2
  - Module:init/1
  - Module:terminate/2
- gen_fsm
- gen_server
  - Top of manual page
  - abcast/2
  - abcast/3
  - call/2
  - call/3
  - cast/2
  - check_response/2
  - enter_loop/3
  - enter_loop/4
  - enter_loop/4
  - enter_loop/5
  - multi_call/2
  - multi_call/3
  - multi_call/4
  - reply/2
  - send_request/2
  - start/3
  - start/4
  - start_link/3
  - start_link/4
  - start_monitor/3
  - start_monitor/4
  - stop/1
  - stop/3
  - wait_response/2
  - Module:code_change/3
  - Module:format_status/2
  - Module:handle_call/3
  - Module:handle_cast/2
  - Module:handle_continue/2
  - Module:handle_info/2
  - Module:init/1
  - Module:terminate/2
- gen_statem
  - Top of manual page
  - call/2
  - call/3
  - cast/2
  - check_response/2
  - enter_loop/4
  - enter_loop/5
  - enter_loop/6
  - reply/1
  - reply/2
  - send_request/2
  - start/3
  - start/4
  - start_link/3
  - start_link/4
  - start_monitor/3
  - start_monitor/4
  - stop/1
  - stop/3
  - wait_response/1
  - wait_response/2
  - Module:StateName/3
  - Module:StateName/3
  - Module:callback_mode/0
  - Module:code_change/4
  - Module:format_status/2
  - Module:handle_event/4
  - Module:handle_event/4
  - Module:init/1
  - Module:terminate/3
- io
  - Top of manual page
  - columns/0
  - columns/1
  - format/1
  - format/2
  - format/3
  - fread/2
  - fread/3
  - fwrite/1
  - fwrite/2
  - fwrite/3
  - get_chars/2
  - get_chars/3
  - get_line/1
  - get_line/2
  - getopts/0
  - getopts/1
  - nl/0
  - nl/1
  - parse_erl_exprs/1
  - parse_erl_exprs/2
  - parse_erl_exprs/3
  - parse_erl_exprs/4
  - parse_erl_form/1
  - parse_erl_form/2
  - parse_erl_form/3
  - parse_erl_form/4
  - printable_range/0
  - put_chars/1
  - put_chars/2
  - read/1
  - read/2
  - read/3
  - read/4
  - rows/0
  - rows/1
  - scan_erl_exprs/1
  - scan_erl_exprs/2
  - scan_erl_exprs/3
  - scan_erl_exprs/4
  - scan_erl_form/1
  - scan_erl_form/2
  - scan_erl_form/3
  - scan_erl_form/4
  - setopts/1
  - setopts/2
  - write/1
  - write/2
- io_lib
  - Top of manual page
  - build_text/1
  - char_list/1
  - deep_char_list/1
  - deep_latin1_char_list/1
  - format/2
  - format/3
  - fread/2
  - fread/3
  - fwrite/2
  - fwrite/3
  - indentation/2
  - latin1_char_list/1
  - nl/0
  - print/1
  - print/4
  - printable_latin1_list/1
  - printable_list/1
  - printable_unicode_list/1
  - scan_format/2
  - unscan_format/1
  - write/1
  - write/2
  - write_atom/1
  - write_atom_as_latin1/1
  - write_char/1
  - write_char_as_latin1/1
  - write_latin1_char/1
  - write_latin1_string/1
  - write_string/1
  - write_string_as_latin1/1
- lists
  - Top of manual page
  - all/2
  - any/2
  - append/1
  - append/2
  - concat/1
  - delete/2
  - droplast/1
  - dropwhile/2
  - duplicate/2
  - filter/2
  - filtermap/2
  - flatlength/1
  - flatmap/2
  - flatten/1
  - flatten/2
  - foldl/3
  - foldr/3
  - foreach/2
  - join/2
  - keydelete/3
  - keyfind/3
  - keymap/3
  - keymember/3
  - keymerge/3
  - keyreplace/4
  - keysearch/3
  - keysort/2
  - keystore/4
  - keytake/3
  - last/1
  - map/2
  - mapfoldl/3
  - mapfoldr/3
  - max/1
  - member/2
  - merge/1
  - merge/2
  - merge/3
  - merge3/3
  - min/1
  - nth/2
  - nthtail/2
  - partition/2
  - prefix/2
  - reverse/1
  - reverse/2
  - search/2
  - seq/2
  - seq/3
  - sort/1
  - sort/2
  - split/2
  - splitwith/2
  - sublist/2
  - sublist/3
  - subtract/2
  - suffix/2
  - sum/1
  - takewhile/2
  - ukeymerge/3
  - ukeysort/2
  - umerge/1
  - umerge/2
  - umerge/3
  - umerge3/3
  - unzip/1
  - unzip3/1
  - usort/1
  - usort/2
  - zip/2
  - zip3/3
  - zipwith/3
  - zipwith3/4
- log_mf_h
  - Top of manual page
  - init/3
  - init/4
- maps
  - Top of manual page
  - filter/2
  - find/2
  - fold/3
  - from_list/1
  - get/2
  - get/3
  - is_key/2
  - iterator/1
  - keys/1
  - map/2
  - merge/2
  - new/0
  - next/1
  - put/3
  - remove/2
  - size/1
  - take/2
  - to_list/1
  - update/3
  - update_with/3
  - update_with/4
  - values/1
  - with/2
  - without/2
- math
  - Top of manual page
  - acos/1
  - acosh/1
  - asin/1
  - asinh/1
  - atan/1
  - atan2/2
  - atanh/1
  - ceil/1
  - cos/1
  - cosh/1
  - erf/1
  - erfc/1
  - exp/1
  - floor/1
  - fmod/2
  - log/1
  - log10/1
  - log2/1
  - pi/0
  - pow/2
  - sin/1
  - sinh/1
  - sqrt/1
  - tan/1
  - tanh/1
- ms_transform
  - Top of manual page
  - format_error/1
  - parse_transform/2
  - transform_from_shell/3
- orddict
  - Top of manual page
  - append/3
  - append_list/3
  - erase/2
  - fetch/2
  - fetch_keys/1
  - filter/2
  - find/2
  - fold/3
  - from_list/1
  - is_empty/1
  - is_key/2
  - map/2
  - merge/3
  - new/0
  - size/1
  - store/3
  - take/2
  - to_list/1
  - update/3
  - update/4
  - update_counter/3
- ordsets
  - Top of manual page
  - add_element/2
  - del_element/2
  - filter/2
  - fold/3
  - from_list/1
  - intersection/1
  - intersection/2
  - is_disjoint/2
  - is_element/2
  - is_empty/1
  - is_set/1
  - is_subset/2
  - new/0
  - size/1
  - subtract/2
  - to_list/1
  - union/1
  - union/2
- pool
  - Top of manual page
  - attach/1
  - get_node/0
  - get_nodes/0
  - pspawn/3
  - pspawn_link/3
  - start/1
  - start/2
  - stop/0
- proc_lib
  - Top of manual page
  - format/1
  - format/2
  - format/3
  - hibernate/3
  - init_ack/1
  - init_ack/2
  - initial_call/1
  - spawn/1
  - spawn/2
  - spawn/3
  - spawn/4
  - spawn_link/1
  - spawn_link/2
  - spawn_link/3
  - spawn_link/4
  - spawn_opt/2
  - spawn_opt/3
  - spawn_opt/4
  - spawn_opt/5
  - start/3
  - start/4
  - start/5
  - start_link/3
  - start_link/4
  - start_link/5
  - start_monitor/3
  - start_monitor/4
  - start_monitor/5
  - stop/1
  - stop/3
  - translate_initial_call/1
- proplists
  - Top of manual page
  - append_values/2
  - compact/1
  - delete/2
  - expand/2
  - get_all_values/2
  - get_bool/2
  - get_keys/1
  - get_value/2
  - get_value/3
  - is_defined/2
  - lookup/2
  - lookup_all/2
  - normalize/2
  - property/1
  - property/2
  - split/2
  - substitute_aliases/2
  - substitute_negations/2
  - unfold/1
- qlc
  - Top of manual page
  - append/1
  - append/2
  - cursor/1
  - cursor/2
  - delete_cursor/1
  - e/1
  - e/2
  - eval/1
  - eval/2
  - fold/3
  - fold/4
  - format_error/1
  - info/1
  - info/2
  - keysort/2
  - keysort/3
  - next_answers/1
  - next_answers/2
  - q/1
  - q/2
  - sort/1
  - sort/2
  - string_to_handle/1
  - string_to_handle/2
  - string_to_handle/3
  - table/2
- queue
  - Top of manual page
  - cons/2
  - daeh/1
  - drop/1
  - drop_r/1
  - filter/2
  - from_list/1
  - get/1
  - get_r/1
  - head/1
  - in/2
  - in_r/2
  - init/1
  - is_empty/1
  - is_queue/1
  - join/2
  - lait/1
  - last/1
  - len/1
  - liat/1
  - member/2
  - new/0
  - out/1
  - out_r/1
  - peek/1
  - peek_r/1
  - reverse/1
  - snoc/2
  - split/2
  - tail/1
  - to_list/1
- rand
  - Top of manual page
  - export_seed/0
  - export_seed_s/1
  - jump/0
  - jump/1
  - normal/0
  - normal/2
  - normal_s/1
  - normal_s/3
  - seed/1
  - seed/2
  - seed_s/1
  - seed_s/2
  - uniform/0
  - uniform/1
  - uniform_real/0
  - uniform_real_s/1
  - uniform_s/1
  - uniform_s/2
- random
  - Top of manual page
  - seed/0
  - seed/1
  - seed/3
  - seed0/0
  - uniform/0
  - uniform/1
  - uniform_s/1
  - uniform_s/2
- re
  - Top of manual page
  - compile/1
  - compile/2
  - inspect/2
  - replace/3
  - replace/4
  - run/2
  - run/3
  - split/2
  - split/3
  - version/0
- sets
  - Top of manual page
  - add_element/2
  - del_element/2
  - filter/2
  - fold/3
  - from_list/1
  - intersection/1
  - intersection/2
  - is_disjoint/2
  - is_element/2
  - is_empty/1
  - is_set/1
  - is_subset/2
  - new/0
  - size/1
  - subtract/2
  - to_list/1
  - union/1
  - union/2
- shell
  - Top of manual page
  - catch_exception/1
  - history/1
  - prompt_func/1
  - results/1
  - start_restricted/1
  - stop_restricted/0
  - strings/1
- shell_default
- shell_docs
  - Top of manual page
  - normalize/1
  - render/2
  - render/3
  - render/4
  - render_callback/2
  - render_callback/3
  - render_callback/4
  - render_type/2
  - render_type/3
  - render_type/4
  - validate/1
- slave
  - Top of manual page
  - pseudo/1
  - pseudo/2
  - relay/1
  - start/1
  - start/2
  - start/3
  - start_link/1
  - start_link/2
  - start_link/3
  - stop/1
- sofs
  - Top of manual page
  - a_function/1
  - a_function/2
  - canonical_relation/1
  - composite/2
  - constant_function/2
  - converse/1
  - difference/2
  - digraph_to_family/1
  - digraph_to_family/2
  - domain/1
  - drestriction/2
  - drestriction/3
  - empty_set/0
  - extension/3
  - family/1
  - family/2
  - family_difference/2
  - family_domain/1
  - family_field/1
  - family_intersection/1
  - family_intersection/2
  - family_projection/2
  - family_range/1
  - family_specification/2
  - family_to_digraph/1
  - family_to_digraph/2
  - family_to_relation/1
  - family_union/1
  - family_union/2
  - field/1
  - from_external/2
  - from_sets/1
  - from_term/1
  - from_term/2
  - image/2
  - intersection/1
  - intersection/2
  - intersection_of_family/1
  - inverse/1
  - inverse_image/2
  - is_a_function/1
  - is_disjoint/2
  - is_empty_set/1
  - is_equal/2
  - is_set/1
  - is_sofs_set/1
  - is_subset/2
  - is_type/1
  - join/4
  - multiple_relative_product/2
  - no_elements/1
  - partition/1
  - partition/2
  - partition/3
  - partition_family/2
  - product/1
  - product/2
  - projection/2
  - range/1
  - relation/1
  - relation/2
  - relation_to_family/1
  - relative_product/1
  - relative_product/2
  - relative_product1/2
  - restriction/2
  - restriction/3
  - set/1
  - set/2
  - specification/2
  - strict_relation/1
  - substitution/2
  - symdiff/2
  - symmetric_partition/2
  - to_external/1
  - to_sets/1
  - type/1
  - union/1
  - union/2
  - union_of_family/1
  - weak_relation/1
- string
  - Top of manual page
  - casefold/1
  - centre/2
  - centre/3
  - chars/2
  - chars/3
  - chomp/1
  - chr/2
  - concat/2
  - copies/2
  - cspan/2
  - equal/2
  - equal/3
  - equal/4
  - find/2
  - find/3
  - is_empty/1
  - join/2
  - left/2
  - left/3
  - len/1
  - length/1
  - lexemes/2
  - lowercase/1
  - next_codepoint/1
  - next_grapheme/1
  - nth_lexeme/3
  - pad/2
  - pad/3
  - pad/4
  - prefix/2
  - rchr/2
  - replace/3
  - replace/4
  - reverse/1
  - right/2
  - right/3
  - rstr/2
  - slice/2
  - slice/3
  - span/2
  - split/2
  - split/3
  - str/2
  - strip/1
  - strip/2
  - strip/3
  - sub_string/2
  - sub_string/3
  - sub_word/2
  - sub_word/3
  - substr/2
  - substr/3
  - take/2
  - take/3
  - take/4
  - titlecase/1
  - to_float/1
  - to_graphemes/1
  - to_integer/1
  - to_lower/1
  - to_upper/1
  - tokens/2
  - trim/1
  - trim/2
  - trim/3
  - uppercase/1
  - words/1
  - words/2
- supervisor
  - Top of manual page
  - check_childspecs/1
  - count_children/1
  - delete_child/2
  - get_childspec/2
  - restart_child/2
  - start_child/2
  - start_link/2
  - start_link/3
  - terminate_child/2
  - which_children/1
  - Module:init/1
- supervisor_bridge
  - Top of manual page
  - start_link/2
  - start_link/3
  - Module:init/1
  - Module:terminate/2
- sys
  - Top of manual page
  - change_code/4
  - change_code/5
  - debug_options/1
  - get_debug/3
  - get_log/1
  - get_state/1
  - get_state/2
  - get_status/1
  - get_status/2
  - handle_debug/4
  - handle_system_msg/6
  - install/2
  - install/3
  - log/2
  - log/3
  - log_to_file/2
  - log_to_file/3
  - no_debug/1
  - no_debug/2
  - print_log/1
  - remove/2
  - remove/3
  - replace_state/2
  - replace_state/3
  - resume/1
  - resume/2
  - statistics/2
  - statistics/3
  - suspend/1
  - suspend/2
  - terminate/2
  - terminate/3
  - trace/2
  - trace/3
  - Module:system_code_change/4
  - Module:system_continue/3
  - Module:system_get_state/1
  - Module:system_replace_state/2
  - Module:system_terminate/4
- timer
  - Top of manual page
  - apply_after/4
  - apply_interval/4
  - cancel/1
  - exit_after/2
  - exit_after/3
  - hms/3
  - hours/1
  - kill_after/1
  - kill_after/2
  - minutes/1
  - now_diff/2
  - seconds/1
  - send_after/2
  - send_after/3
  - send_interval/2
  - send_interval/3
  - sleep/1
  - start/0
  - tc/1
  - tc/2
  - tc/3
- unicode
  - Top of manual page
  - bom_to_encoding/1
  - characters_to_binary/1
  - characters_to_binary/2
  - characters_to_binary/3
  - characters_to_list/1
  - characters_to_list/2
  - characters_to_nfc_binary/1
  - characters_to_nfc_list/1
  - characters_to_nfd_binary/1
  - characters_to_nfd_list/1
  - characters_to_nfkc_binary/1
  - characters_to_nfkc_list/1
  - characters_to_nfkd_binary/1
  - characters_to_nfkd_list/1
  - encoding_to_bom/1
- uri_string
  - Top of manual page
  - compose_query/1
  - compose_query/2
  - dissect_query/1
  - normalize/1
  - normalize/2
  - parse/1
  - recompose/1
  - resolve/2
  - resolve/3
  - transcode/2
- win32reg
  - Top of manual page
  - change_key/2
  - change_key_create/2
  - close/1
  - current_key/1
  - delete_key/1
  - delete_value/2
  - expand/1
  - format_error/1
  - open/1
  - set_value/3
  - sub_keys/1
  - value/2
  - values/1
- zip
  - Top of manual page
  - create/2
  - create/3
  - extract/1
  - extract/2
  - foldl/3
  - list_dir/1
  - list_dir/2
  - t/1
  - table/1
  - table/2
  - tt/1
  - unzip/1
  - unzip/2
  - zip/2
  - zip/3
  - zip_close/1
  - zip_get/1
  - zip_get/2
  - zip_list_dir/1
  - zip_open/1
  - zip_open/2

# ets

### Module

ets

### Module Summary

Built-in term storage.

### Description

This module is an interface to the Erlang built-in term storage BIFs. These provide the ability to store very large quantities of data in an Erlang runtime system, and to have constant access time to the data. (In the case of ordered_set, see below, access time is proportional to the logarithm of the number of stored objects.)

Data is organized as a set of dynamic tables, which can store tuples. Each table is created by a process. When the process terminates, the table is automatically destroyed. Every table has access rights set at creation.

Tables are divided into four different types, set, ordered_set, bag, and duplicate_bag. A set or ordered_set table can only have one object associated with each key. A bag or duplicate_bag table can have many objects associated with each key.

Note

The number of tables stored at one Erlang node used to be limited. This is no longer the case (except by memory usage). The previous default limit was about 1400 tables and could be increased by setting the environment variable ERL_MAX_ETS_TABLES or the command line option +e before starting the Erlang runtime system. This hard limit has been removed, but it is currently useful to set the ERL_MAX_ETS_TABLES anyway. It should be set to an approximate of the maximum amount of tables used. This since an internal table for named tables is sized using this value. If large amounts of named tables are used and ERL_MAX_ETS_TABLES hasn't been increased, the performance of named table lookup will degrade.

Notice that there is no automatic garbage collection for tables. Even if there are no references to a table from any process, it is not automatically destroyed unless the owner process terminates. To destroy a table explicitly, use function delete/1. The default owner is the process that created the table. To transfer table ownership at process termination, use option heir or call give_away/3.

Some implementation details:

- In the current implementation, every object insert and look-up operation results in a copy of the object.

- '$end_of_table' is not to be used as a key, as this atom is used to mark the end of the table when using functions first/1 and next/2.

Notice the subtle difference between matching and comparing equal, which is demonstrated by table types set and ordered_set:

- Two Erlang terms match if they are of the same type and have the same value, so that 1 matches 1, but not 1.0 (as 1.0 is a float() and not an integer()).

- Two Erlang terms compare equal if they either are of the same type and value, or if both are numeric types and extend to the same value, so that 1 compares equal to both 1 and 1.0.

- The ordered_set works on the Erlang term order and no defined order exists between an integer() and a float() that extends to the same value. Hence the key 1 and the key 1.0 are regarded as equal in an ordered_set table.

### Failures

Functions in this module fail by raising an error exception with error reason:

badarg

  If any argument has the wrong format.

badarg

  If the table identifier is invalid.

badarg

  If the operation is denied because of table access rights (protected or private).

system_limit

  Modification of a value causes it to not be representable internally in the VM. For example, incrementation of a counter past the largest integer representable.

system_limit

  If a match specification passed as argument has excessive nesting which causes scheduler stack exhaustion for the scheduler that the calling process is executing on. Scheduler stack size can be configured when starting the runtime system.

### Concurrency

This module provides some limited support for concurrent access. All updates to single objects are guaranteed to be both atomic and isolated. This means that an updating operation to a single object either succeeds or fails completely without any effect (atomicity) and that no intermediate results of the update can be seen by other processes (isolation). Some functions that update many objects state that they even guarantee atomicity and isolation for the entire operation. In database terms the isolation level can be seen as "serializable", as if all isolated operations are carried out serially, one after the other in a strict order.

### Table traversal

There are different ways to traverse through the objects of a table.

- Single-step traversal one key at at time, using first/1, next/2, last/1 and prev/2.

- Search with simple match patterns, using match/1/2/3, match_delete/2 and match_object/1/2/3.

- Search with more powerful match specifications, using select/1/2/3, select_count/2, select_delete/2, select_replace/2 and select_reverse/1/2/3.

- Table conversions, using tab2file/2/3 and tab2list/1.

None of these ways of table traversal will guarantee a consistent table snapshot if the table is also updated during the traversal. Moreover, traversals not done in a safe way, on tables where keys are inserted or deleted during the traversal, may yield the following undesired effects:

- Any key may be missed.

- Any key may be found more than once.

- The traversal may fail with badarg exception if keys are deleted.

A table traversal is safe if either

- the table is of type ordered_set.

- the entire table traversal is done within one ETS function call.

- function safe_fixtable/2 is used to keep the table fixated during the entire traversal.

Note

Even though the access of a single object is always guaranteed to be atomic and isolated, each traversal through a table to find the next key is not done with such guarantees. This is often not a problem, but may cause rare subtle "unexpected" effects if a concurrent process inserts objects during a traversal. For example, consider one process doing

    ets:new(t, [ordered_set, named_table]),
    ets:insert(t, {1}),
    ets:insert(t, {2}),
    ets:insert(t, {3}),

A concurrent call to ets:first(t), done by another process, may then in rare cases return 2 even though 2 has never existed in the table ordered as the first key. In the same way, a concurrent call to ets:next(t, 1) may return 3 even though 3 never existed in the table ordered directly after 1.

Effects like this are improbable but possible. The probability will further be reduced (if not vanish) if table option write_concurrency is not enabled. This can also only be a potential concern for ordered_set where the traversal order is defined.

Traversals using match and select functions may not need to scan the entire table depending on how the key is specified. A match pattern with a fully bound key (without any match variables) will optimize the operation to a single key lookup without any table traversal at all. For ordered_set a partially bound key will limit the traversal to only scan a subset of the table based on term order. A partially bound key is either a list or a tuple with a prefix that is fully bound. Example:

    1> T = ets:new(t,[ordered_set]), ets:insert(T, {"555-1234", "John Smith"}).
    true
    2> %% Efficient search of all with area code 555
    2> ets:match(T,{[$5,$5,$5,$- |'$1'],'$2'}).
    [["1234","John Smith"]]

### Match Specifications

Some of the functions use a match specification, match_spec. For a brief explanation, see select/2. For a detailed description, see section Match Specifications in Erlang in ERTS User's Guide.

A match specifications with excessive nesting will cause a system_limit error exception to be raised.

### Data Types

access() = public | protected | private
continuation()

Opaque continuation used by select/1,3, select_reverse/1,3, match/1,3, and match_object/1,3.

match_spec() = [{match_pattern(), [term()], [term()]}]

A match specification, see above.

comp_match_spec()

A compiled match specification.

match_pattern() = atom() | tuple()
tab() = atom() | tid()
tid()

A table identifier, as returned by new/2.

type() = set | ordered_set | bag | duplicate_bag

### Exports

all() -> [Tab] |

### Types

Tab = tab()

Returns a list of all tables at the node. Named tables are specified by their names, unnamed tables are specified by their table identifiers.

There is no guarantee of consistency in the returned list. Tables created or deleted by other processes "during" the ets:all() call either are or are not included in the list. Only tables created/deleted before ets:all() is called are guaranteed to be included/excluded.

delete(Tab) -> true |

### Types

Tab = tab()

Deletes the entire table Tab.

delete(Tab, Key) -> true |

### Types

Tab = tab()
Key = term()

Deletes all objects with key Key from table Tab.

delete_all_objects(Tab) -> true |

### Types

Tab = tab()

Delete all objects in the ETS table Tab. The operation is guaranteed to be atomic and isolated.

delete_object(Tab, Object) -> true |

### Types

Tab = tab()
Object = tuple()

Delete the exact object Object from the ETS table, leaving objects with the same key but other differences (useful for type bag). In a duplicate_bag table, all instances of the object are deleted.

file2tab(Filename) -> {ok, Tab} | {error, Reason} |

### Types

Filename = file:name()
Tab = tab()
Reason = term()

Reads a file produced by tab2file/2 or tab2file/3 and creates the corresponding table Tab.

Equivalent to file2tab(Filename, []).

file2tab(Filename, Options) -> {ok, Tab} | {error, Reason} |

### Types

Filename = file:name()
Tab = tab()
Options = [Option]
Option = {verify, boolean()}
Reason = term()

Reads a file produced by tab2file/2 or tab2file/3 and creates the corresponding table Tab.

The only supported option is {verify,boolean()}. If verification is turned on (by specifying {verify,true}), the function uses whatever information is present in the file to assert that the information is not damaged. How this is done depends on which extended_info was written using tab2file/3.

If no extended_info is present in the file and {verify,true} is specified, the number of objects written is compared to the size of the original table when the dump was started. This can make verification fail if the table was public and objects were added or removed while the table was dumped to file. To avoid this problem, either do not verify files dumped while updated simultaneously or use option {extended_info, [object_count]} to tab2file/3, which extends the information in the file with the number of objects written.

If verification is turned on and the file was written with option {extended_info, [md5sum]}, reading the file is slower and consumes radically more CPU time than otherwise.

{verify,false} is the default.

first(Tab) -> Key | '$end_of_table' |

### Types

Tab = tab()
Key = term()

Returns the first key Key in table Tab. For an ordered_set table, the first key in Erlang term order is returned. For other table types, the first key according to the internal order of the table is returned. If the table is empty, '$end_of_table' is returned.

To find subsequent keys in the table, use next/2.

foldl(Function, Acc0, Tab) -> Acc1 |

### Types

Function = fun((Element :: term(), AccIn) -> AccOut)
Tab = tab()
Acc0 = Acc1 = AccIn = AccOut = term()

Acc0 is returned if the table is empty. This function is similar to lists:foldl/3. The table elements are traversed in an unspecified order, except for ordered_set tables, where they are traversed first to last.

If Function inserts objects into the table, or another process inserts objects into the table, those objects can (depending on key ordering) be included in the traversal.

foldr(Function, Acc0, Tab) -> Acc1 |

### Types

Function = fun((Element :: term(), AccIn) -> AccOut)
Tab = tab()
Acc0 = Acc1 = AccIn = AccOut = term()

Acc0 is returned if the table is empty. This function is similar to lists:foldr/3. The table elements are traversed in an unspecified order, except for ordered_set tables, where they are traversed last to first.

If Function inserts objects into the table, or another process inserts objects into the table, those objects can (depending on key ordering) be included in the traversal.

from_dets(Tab, DetsTab) -> true |

### Types

Tab = tab()
DetsTab = dets:tab_name()

Fills an already created ETS table with the objects in the already opened Dets table DetsTab. Existing objects in the ETS table are kept unless overwritten.

If any of the tables does not exist or the Dets table is not open, a badarg exception is raised.

fun2ms(LiteralFun) -> MatchSpec |

### Types

LiteralFun = function()
MatchSpec = match_spec()

Pseudo function that by a parse_transform translates LiteralFun typed as parameter in the function call to a match specification. With "literal" is meant that the fun must textually be written as the parameter of the function, it cannot be held in a variable that in turn is passed to the function.

The parse transform is provided in the ms_transform module and the source must include file ms_transform.hrl in STDLIB for this pseudo function to work. Failing to include the hrl file in the source results in a runtime error, not a compile time error. The include file is easiest included by adding line -include_lib("stdlib/include/ms_transform.hrl"). to the source file.

The fun is very restricted, it can take only a single parameter (the object to match): a sole variable or a tuple. It must use the is_ guard tests. Language constructs that have no representation in a match specification (if, case, receive, and so on) are not allowed.

The return value is the resulting match specification.

Example:

    1> ets:fun2ms(fun({M,N}) when N > 3 -> M end).
    [{{'$1','$2'},[{'>','$2',3}],['$1']}]

Variables from the environment can be imported, so that the following works:

    2> X=3.
    3
    3> ets:fun2ms(fun({M,N}) when N > X -> M end).
    [{{'$1','$2'},[{'>','$2',{const,3}}],['$1']}]

The imported variables are replaced by match specification const expressions, which is consistent with the static scoping for Erlang funs. However, local or global function calls cannot be in the guard or body of the fun. Calls to built-in match specification functions is of course allowed:

    4> ets:fun2ms(fun({M,N}) when N > X, my_fun(M) -> M end).
    Error: fun containing local Erlang function calls
    ('my_fun' called in guard) cannot be translated into match_spec
    {error,transform_error}
    5> ets:fun2ms(fun({M,N}) when N > X, is_atom(M) -> M end).
    [{{'$1','$2'},[{'>','$2',{const,3}},{is_atom,'$1'}],['$1']}]

As shown by the example, the function can be called from the shell also. The fun must be literally in the call when used from the shell as well.

Warning

If the parse_transform is not applied to a module that calls this pseudo function, the call fails in runtime (with a badarg). The ets module exports a function with this name, but it is never to be called except when using the function in the shell. If the parse_transform is properly applied by including header file ms_transform.hrl, compiled code never calls the function, but the function call is replaced by a literal match specification.

For more information, see ms_transform(3).

give_away(Tab, Pid, GiftData) -> true |

### Types

Tab = tab()
Pid = pid()
GiftData = term()

Make process Pid the new owner of table Tab. If successful, message {'ETS-TRANSFER',Tab,FromPid,GiftData} is sent to the new owner.

The process Pid must be alive, local, and not already the owner of the table. The calling process must be the table owner.

Notice that this function does not affect option heir of the table. A table owner can, for example, set heir to itself, give the table away, and then get it back if the receiver terminates.

i() -> ok |

Displays information about all ETS tables on a terminal.

i(Tab) -> ok |

### Types

Tab = tab()

Browses table Tab on a terminal.

info(Tab) -> InfoList | undefined |

### Types

Tab = tab()
InfoList = [InfoTuple]
InfoTuple =
{compressed, boolean()} |
{decentralized_counters, boolean()} |
{heir, pid() | none} |
{id, tid()} |
{keypos, integer() >= 1} |
{memory, integer() >= 0} |
{name, atom()} |
{named_table, boolean()} |
{node, node()} |
{owner, pid()} |
{protection, access()} |
{size, integer() >= 0} |
{type, type()} |
{write_concurrency, boolean()} |
{read_concurrency, boolean()}

Returns information about table Tab as a list of tuples. If Tab has the correct type for a table identifier, but does not refer to an existing ETS table, undefined is returned. If Tab is not of the correct type, a badarg exception is raised.

{compressed, boolean()}

  Indicates if the table is compressed.

{decentralized_counters, boolean()}

  Indicates whether the table uses decentralized_counters.

{heir, pid() | none}

  The pid of the heir of the table, or none if no heir is set.

{id, tid()}

  The table identifier.

{keypos, integer() >= 1}

  The key position.

{memory, integer() >= 0

  The number of words allocated to the table.

{name, atom()}

  The table name.

{named_table, boolean()}

  Indicates if the table is named.

{node, node()}

  The node where the table is stored. This field is no longer meaningful, as tables cannot be accessed from other nodes.

{owner, pid()}

  The pid of the owner of the table.

{protection, access()}

  The table access rights.

{size, integer() >= 0

  The number of objects inserted in the table.

{type, type()}

  The table type.

{read_concurrency, boolean()}

  Indicates whether the table uses read_concurrency or not.

{write_concurrency, boolean()}

  Indicates whether the table uses write_concurrency.

Note

The execution time of this function is affected by the decentralized_counters table option. The execution time is much longer when the decentralized_counters option is set to true than when the decentralized_counters option is set to false.

info(Tab, Item) -> Value | undefined |

### Types

Tab = tab()
Item =
binary | compressed | decentralized_counters | fixed | heir |
id | keypos | memory | name | named_table | node | owner |
protection | safe_fixed | safe_fixed_monotonic_time | size |
stats | type | write_concurrency | read_concurrency
Value = term()

Returns the information associated with Item for table Tab, or returns undefined if Tab does not refer an existing ETS table. If Tab is not of the correct type, or if Item is not one of the allowed values, a badarg exception is raised.

In addition to the {Item,Value} pairs defined for info/1, the following items are allowed:

- Item=binary, Value=BinInfo

  BinInfo is a list containing miscellaneous information about binaries kept by the table. This Item can be changed or removed without prior notice. In the current implementation BinInfo is a list of tuples {BinaryId,BinarySize,BinaryRefcCount}.

- Item=fixed, Value=boolean()

  Indicates if the table is fixed by any process.

- Item=safe_fixed|safe_fixed_monotonic_time, Value={FixationTime,Info}|false

  If the table is fixed using safe_fixtable/2, the call returns a tuple where FixationTime is the last time when the table changed from unfixed to fixed.

  The format and value of FixationTime depends on Item:

  safe_fixed

    FixationTime corresponds to the result returned by erlang:timestamp/0 at the time of fixation. Notice that when the system uses single or multi time warp modes this can produce strange results, as the use of safe_fixed is not time warp safe. Time warp safe code must use safe_fixed_monotonic_time instead.

  safe_fixed_monotonic_time

    FixationTime corresponds to the result returned by erlang:monotonic_time/0 at the time of fixation. The use of safe_fixed_monotonic_time is time warp safe.

  Info is a possibly empty lists of tuples {Pid,RefCount}, one tuple for every process the table is fixed by now. RefCount is the value of the reference counter and it keeps track of how many times the table has been fixed by the process.

  Table fixations are not limited to safe_fixtable/2. Temporary fixations may also be done by for example traversing functions like select and match. Such table fixations are automatically released before the corresponding functions returns, but they may be seen by a concurrent call to ets:info(T,safe_fixed|safe_fixed_monotonic_time).

  If the table is not fixed at all, the call returns false.

- Item=stats, Value=tuple()

  Returns internal statistics about tables on an internal format used by OTP test suites. Not for production use.

Note

The execution time of this function is affected by the decentralized_counters table option when the second argument of the function is size or memory. The execution time is much longer when the decentralized_counters option is set to true than when the decentralized_counters option is set to false.

init_table(Tab, InitFun) -> true |

### Types

Tab = tab()
InitFun = fun((Arg) -> Res)
Arg = read | close
Res = end_of_input | {Objects :: [term()], InitFun} | term()

Replaces the existing objects of table Tab with objects created by calling the input function InitFun, see below. This function is provided for compatibility with the dets module, it is not more efficient than filling a table by using insert/2.

When called with argument read, the function InitFun is assumed to return end_of_input when there is no more input, or {Objects, Fun}, where Objects is a list of objects and Fun is a new input function. Any other value Value is returned as an error {error, {init_fun, Value}}. Each input function is called exactly once, and if an error occur, the last function is called with argument close, the reply of which is ignored.

If the table type is set and more than one object exists with a given key, one of the objects is chosen. This is not necessarily the last object with the given key in the sequence of objects returned by the input functions. This holds also for duplicated objects stored in tables of type bag.

insert(Tab, ObjectOrObjects) -> true |

### Types

Tab = tab()
ObjectOrObjects = tuple() | [tuple()]

Inserts the object or all of the objects in list ObjectOrObjects into table Tab.

- If the table type is set and the key of the inserted objects matches the key of any object in the table, the old object is replaced.

- If the table type is ordered_set and the key of the inserted object compares equal to the key of any object in the table, the old object is replaced.

- If the list contains more than one object with matching keys and the table type is set, one is inserted, which one is not defined. The same holds for table type ordered_set if the keys compare equal.

The entire operation is guaranteed to be atomic and isolated, even when a list of objects is inserted.

insert_new(Tab, ObjectOrObjects) -> boolean() |

### Types

Tab = tab()
ObjectOrObjects = tuple() | [tuple()]

Same as insert/2 except that instead of overwriting objects with the same key (for set or ordered_set) or adding more objects with keys already existing in the table (for bag and duplicate_bag), false is returned.

If ObjectOrObjects is a list, the function checks every key before inserting anything. Nothing is inserted unless all keys present in the list are absent from the table. Like insert/2, the entire operation is guaranteed to be atomic and isolated.

is_compiled_ms(Term) -> boolean() |

### Types

Term = term()

Checks if a term represent a valid compiled match specification. A compiled match specifications is only valid on the Erlang node where it was compiled by calling match_spec_compile/1.

Note

Before STDLIB 3.4 (OTP 20.0) compiled match specifications did not have an external representation. If passed through binary_to_term(term_to_binary(CMS)) or sent to another node and back, the result was always an empty binary <<>>.

After STDLIB 3.4 (OTP 20.0) compiled match specifications have an external representation as a node specific reference to the original compiled match specification. If passed through binary_to_term(term_to_binary(CMS)) or sent to another node and back, the result may or may not be a valid compiled match specification depending on if the original compiled match specification was still alive.

last(Tab) -> Key | '$end_of_table' |

### Types

Tab = tab()
Key = term()

Returns the last key Key according to Erlang term order in table Tab of type ordered_set. For other table types, the function is synonymous to first/1. If the table is empty, '$end_of_table' is returned.

To find preceding keys in the table, use prev/2.

lookup(Tab, Key) -> [Object] |

### Types

Tab = tab()
Key = term()
Object = tuple()

Returns a list of all objects with key Key in table Tab.

- For tables of type set, bag, or duplicate_bag, an object is returned only if the specified key matches the key of the object in the table.

- For tables of type ordered_set, an object is returned if the specified key compares equal to the key of an object in the table.

The difference is the same as between =:= and ==.

As an example, one can insert an object with integer() 1 as a key in an ordered_set and get the object returned as a result of doing a lookup/2 with float() 1.0 as the key to search for.

For tables of type set or ordered_set, the function returns either the empty list or a list with one element, as there cannot be more than one object with the same key. For tables of type bag or duplicate_bag, the function returns a list of arbitrary length.

Notice that the time order of object insertions is preserved; the first object inserted with the specified key is the first in the resulting list, and so on.

Insert and lookup times in tables of type set, bag, and duplicate_bag are constant, regardless of the table size. For the ordered_set datatype, time is proportional to the (binary) logarithm of the number of objects.

lookup_element(Tab, Key, Pos) -> Elem |

### Types

Tab = tab()
Key = term()
Pos = integer() >= 1
Elem = term() | [term()]

For a table Tab of type set or ordered_set, the function returns the Pos:th element of the object with key Key.

For tables of type bag or duplicate_bag, the functions returns a list with the Pos:th element of every object with key Key.

If no object with key Key exists, the function exits with reason badarg.

The difference between set, bag, and duplicate_bag on one hand, and ordered_set on the other, regarding the fact that ordered_set view keys as equal when they compare equal whereas the other table types regard them equal only when they match, holds for lookup_element/3.

match(Continuation) -> {[Match], Continuation} | '$end_of_table' |

### Types

Match = [term()]
Continuation = continuation()

Continues a match started with match/3. The next chunk of the size specified in the initial match/3 call is returned together with a new Continuation, which can be used in subsequent calls to this function.

When there are no more objects in the table, '$end_of_table' is returned.

match(Tab, Pattern) -> [Match] |

### Types

Tab = tab()
Pattern = match_pattern()
Match = [term()]

Matches the objects in table Tab against pattern Pattern.

A pattern is a term that can contain:

- Bound parts (Erlang terms)
- '_' that matches any Erlang term
- Pattern variables '$N', where N=0,1,...

The function returns a list with one element for each matching object, where each element is an ordered list of pattern variable bindings, for example:

    6> ets:match(T, '$1'). % Matches every object in table
    [[{rufsen,dog,7}],[{brunte,horse,5}],[{ludde,dog,5}]]
    7> ets:match(T, {'_',dog,'$1'}).
    [[7],[5]]
    8> ets:match(T, {'_',cow,'$1'}).
    []

If the key is specified in the pattern, the match is very efficient. If the key is not specified, that is, if it is a variable or an underscore, the entire table must be searched. The search time can be substantial if the table is very large.

For tables of type ordered_set, the result is in the same order as in a first/next traversal.

match(Tab, Pattern, Limit) -> {[Match], Continuation} | '$end_of_table' |

### Types

Tab = tab()
Pattern = match_pattern()
Limit = integer() >= 1
Match = [term()]
Continuation = continuation()

Works like match/2, but returns only a limited (Limit) number of matching objects. Term Continuation can then be used in subsequent calls to match/1 to get the next chunk of matching objects. This is a space-efficient way to work on objects in a table, which is faster than traversing the table object by object using first/1 and next/2.

If the table is empty, '$end_of_table' is returned.

Use safe_fixtable/2 to guarantee safe traversal for subsequent calls to match/1.

match_delete(Tab, Pattern) -> true |

### Types

Tab = tab()
Pattern = match_pattern()

Deletes all objects that match pattern Pattern from table Tab. For a description of patterns, see match/2.

match_object(Continuation) -> {[Object], Continuation} | '$end_of_table' |

### Types

Object = tuple()
Continuation = continuation()

Continues a match started with match_object/3. The next chunk of the size specified in the initial match_object/3 call is returned together with a new Continuation, which can be used in subsequent calls to this function.

When there are no more objects in the table, '$end_of_table' is returned.

match_object(Tab, Pattern) -> [Object] |

### Types

Tab = tab()
Pattern = match_pattern()
Object = tuple()

Matches the objects in table Tab against pattern Pattern. For a description of patterns, see match/2. The function returns a list of all objects that match the pattern.

If the key is specified in the pattern, the match is very efficient. If the key is not specified, that is, if it is a variable or an underscore, the entire table must be searched. The search time can be substantial if the table is very large.

For tables of type ordered_set, the result is in the same order as in a first/next traversal.

match_object(Tab, Pattern, Limit) -> {[Object], Continuation} | '$end_of_table' |

### Types

Tab = tab()
Pattern = match_pattern()
Limit = integer() >= 1
Object = tuple()
Continuation = continuation()

Works like match_object/2, but only returns a limited (Limit) number of matching objects. Term Continuation can then be used in subsequent calls to match_object/1 to get the next chunk of matching objects. This is a space-efficient way to work on objects in a table, which is faster than traversing the table object by object using first/1 and next/2.

If the table is empty, '$end_of_table' is returned.

Use safe_fixtable/2 to guarantee safe traversal for subsequent calls to match_object/1.

match_spec_compile(MatchSpec) -> CompiledMatchSpec |

### Types

MatchSpec = match_spec()
CompiledMatchSpec = comp_match_spec()

Transforms a match specification into an internal representation that can be used in subsequent calls to match_spec_run/2. The internal representation is opaque. To check the validity of a compiled match specification, use is_compiled_ms/1.

If term MatchSpec does not represent a valid match specification, a badarg exception is raised.

Note

This function has limited use in normal code. It is used by the dets module to perform the dets:select() operations.

match_spec_run(List, CompiledMatchSpec) -> list() |

### Types

List = [term()]
CompiledMatchSpec = comp_match_spec()

Executes the matching specified in a compiled match specification on a list of terms. Term CompiledMatchSpec is to be the result of a call to match_spec_compile/1 and is hence the internal representation of the match specification one wants to use.

The matching is executed on each element in List and the function returns a list containing all results. If an element in List does not match, nothing is returned for that element. The length of the result list is therefore equal or less than the length of parameter List.

Example:

The following two calls give the same result (but certainly not the same execution time):

    Table = ets:new...
    MatchSpec = ...
    % The following call...
    ets:match_spec_run(ets:tab2list(Table),
                       ets:match_spec_compile(MatchSpec)),
    % ...gives the same result as the more common (and more efficient)
    ets:select(Table, MatchSpec),

Note

This function has limited use in normal code. It is used by the dets module to perform the dets:select() operations and by Mnesia during transactions.

member(Tab, Key) -> boolean() |

### Types

Tab = tab()
Key = term()

Works like lookup/2, but does not return the objects. Returns true if one or more elements in the table has key Key, otherwise false.

new(Name, Options) -> tid() | atom() |

### Types

Name = atom()
Options = [Option]
Option =
Type | Access | named_table |
{keypos, Pos} |
{heir, Pid :: pid(), HeirData} |
{heir, none} |
Tweaks
Type = type()
Access = access()
Tweaks =
{write_concurrency, boolean()} |
{read_concurrency, boolean()} |
{decentralized_counters, boolean()} |
compressed
Pos = integer() >= 1
HeirData = term()

Creates a new table and returns a table identifier that can be used in subsequent operations. The table identifier can be sent to other processes so that a table can be shared between different processes within a node.

Parameter Options is a list of options that specifies table type, access rights, key position, and whether the table is named. Default values are used for omitted options. This means that not specifying any options ([]) is the same as specifying [set, protected, {keypos,1}, {heir,none}, {write_concurrency,false}, {read_concurrency,false}, {decentralized_counters,false}].

set

  The table is a set table: one key, one object, no order among objects. This is the default table type.

ordered_set

  The table is a ordered_set table: one key, one object, ordered in Erlang term order, which is the order implied by the < and > operators. Tables of this type have a somewhat different behavior in some situations than tables of other types. Most notably, the ordered_set tables regard keys as equal when they compare equal, not only when they match. This means that to an ordered_set table, integer() 1 and float() 1.0 are regarded as equal. This also means that the key used to lookup an element not necessarily matches the key in the returned elements, if float()'s and integer()'s are mixed in keys of a table.

bag

  The table is a bag table, which can have many objects, but only one instance of each object, per key.

duplicate_bag

  The table is a duplicate_bag table, which can have many objects, including multiple copies of the same object, per key.

public

  Any process can read or write to the table.

protected

  The owner process can read and write to the table. Other processes can only read the table. This is the default setting for the access rights.

private

  Only the owner process can read or write to the table.

named_table

  If this option is present, the table is registered under its Name which can then be used instead of the table identifier in subsequent operations.

  The function will also return the Name instead of the table identifier. To get the table identifier of a named table, use whereis/1.

{keypos,Pos}

  Specifies which element in the stored tuples to use as key. By default, it is the first element, that is, Pos=1. However, this is not always appropriate. In particular, we do not want the first element to be the key if we want to store Erlang records in a table.

  Notice that any tuple stored in the table must have at least Pos number of elements.

{heir,Pid,HeirData} | {heir,none}

  Set a process as heir. The heir inherits the table if the owner terminates. Message {'ETS-TRANSFER',tid(),FromPid,HeirData} is sent to the heir when that occurs. The heir must be a local process. Default heir is none, which destroys the table when the owner terminates.

{write_concurrency,boolean()}

  Performance tuning. Defaults to false, in which case an operation that mutates (writes to) the table obtains exclusive access, blocking any concurrent access of the same table until finished. If set to true, the table is optimized to concurrent write access. Different objects of the same table can be mutated (and read) by concurrent processes. This is achieved to some degree at the expense of memory consumption and the performance of sequential access and concurrent reading.

  The write_concurrency option can be combined with the options read_concurrency and decentralized_counters. You typically want to combine write_concurrency with read_concurrency when large concurrent read bursts and large concurrent write bursts are common; for more information, see option read_concurrency. The decentralized_counters option is turned on by default for tables of type ordered_set with the write_concurrency option enabled, and the decentralized_counters option is turned off by default for all other table types. For more information, see the documentation for the decentralized_counters option.

  Notice that this option does not change any guarantees about atomicity and isolation. Functions that makes such promises over many objects (like insert/2) gain less (or nothing) from this option.

  The memory consumption inflicted by both write_concurrency and read_concurrency is a constant overhead per table for set, bag and duplicate_bag. For ordered_set the memory overhead depends on the number of inserted objects and the amount of actual detected concurrency in runtime. The memory overhead can be especially large when both options are combined.

  Note

  Prior to stdlib-3.7 (OTP-22.0) write_concurrency had no effect on ordered_set.

{read_concurrency,boolean()}

  Performance tuning. Defaults to false. When set to true, the table is optimized for concurrent read operations. When this option is enabled on a runtime system with SMP support, read operations become much cheaper; especially on systems with multiple physical processors. However, switching between read and write operations becomes more expensive.

  You typically want to enable this option when concurrent read operations are much more frequent than write operations, or when concurrent reads and writes comes in large read and write bursts (that is, many reads not interrupted by writes, and many writes not interrupted by reads).

  You typically do not want to enable this option when the common access pattern is a few read operations interleaved with a few write operations repeatedly. In this case, you would get a performance degradation by enabling this option.

  Option read_concurrency can be combined with option write_concurrency. You typically want to combine these when large concurrent read bursts and large concurrent write bursts are common.

{decentralized_counters,boolean()}

  Performance tuning. Defaults to true for tables of type ordered_set with the write_concurrency option enabled, and defaults to false for all other table types. This option has no effect if the write_concurrency option is set to false.

  When this option is set to true, the table is optimized for frequent concurrent calls to operations that modify the tables size and/or its memory consumption (e.g., insert/2 and delete/2). The drawback is that calls to info/1 and info/2 with size or memory as the second argument can get much slower when the decentralized_counters option is turned on.

  When this option is enabled the counters for the table size and memory consumption are distributed over several cache lines and the scheduling threads are mapped to one of those cache lines. The erl option +dcg can be used to control the number of cache lines that the counters are distributed over.

compressed

  If this option is present, the table data is stored in a more compact format to consume less memory. However, it will make table operations slower. Especially operations that need to inspect entire objects, such as match and select, get much slower. The key element is not compressed.

next(Tab, Key1) -> Key2 | '$end_of_table' |

### Types

Tab = tab()
Key1 = Key2 = term()

Returns the next key Key2, following key Key1 in table Tab. For table type ordered_set, the next key in Erlang term order is returned. For other table types, the next key according to the internal order of the table is returned. If no next key exists, '$end_of_table' is returned.

To find the first key in the table, use first/1.

Unless a table of type set, bag, or duplicate_bag is fixated using safe_fixtable/2, a call to next/2 will fail if Key1 no longer exists in the table. For table type ordered_set, the function always returns the next key after Key1 in term order, regardless whether Key1 ever existed in the table.

prev(Tab, Key1) -> Key2 | '$end_of_table' |

### Types

Tab = tab()
Key1 = Key2 = term()

Returns the previous key Key2, preceding key Key1 according to Erlang term order in table Tab of type ordered_set. For other table types, the function is synonymous to next/2. If no previous key exists, '$end_of_table' is returned.

To find the last key in an ordered_set table, use last/1.

rename(Tab, Name) -> Name |

### Types

Tab = tab()
Name = atom()

Renames the named table Tab to the new name Name. Afterwards, the old name cannot be used to access the table. Renaming an unnamed table has no effect.

repair_continuation(Continuation, MatchSpec) -> Continuation |

### Types

Continuation = continuation()
MatchSpec = match_spec()

Restores an opaque continuation returned by select/3 or select/1 if the continuation has passed through external term format (been sent between nodes or stored on disk).

The reason for this function is that continuation terms contain compiled match specifications and may therefore be invalidated if converted to external term format. Given that the original match specification is kept intact, the continuation can be restored, meaning it can once again be used in subsequent select/1 calls even though it has been stored on disk or on another node.

Examples:

The following sequence of calls may fail:

    T=ets:new(x,[]),
    ...
    MS = ets:fun2ms(fun({N,_}=A) when (N rem 10) =:= 0 -> A end),
    {_,C} = ets:select(T, MS, 10),
    MaybeBroken = binary_to_term(term_to_binary(C)),
    ets:select(MaybeBroken).

The following sequence works, as the call to repair_continuation/2 reestablishes the MaybeBroken continuation.

    T=ets:new(x,[]),
    ...
    MS = ets:fun2ms(fun({N,_}=A) when (N rem 10) =:= 0 -> A end),
    {_,C} = ets:select(T,MS,10),
    MaybeBroken = binary_to_term(term_to_binary(C)),
    ets:select(ets:repair_continuation(MaybeBroken,MS)).

Note

This function is rarely needed in application code. It is used by Mnesia to provide distributed select/3 and select/1 sequences. A normal application would either use Mnesia or keep the continuation from being converted to external format.

The actual behavior of compiled match specifications when recreated from external format has changed and may change in future releases, but this interface remains for backward compatibility. See is_compiled_ms/1.

safe_fixtable(Tab, Fix) -> true |

### Types

Tab = tab()
Fix = boolean()

Fixes a table of type set, bag, or duplicate_bag for safe traversal using first/1 & next/2, match/3 & match/1, match_object/3 & match_object/1, or select/3 & select/1.

A process fixes a table by calling safe_fixtable(Tab, true). The table remains fixed until the process releases it by calling safe_fixtable(Tab, false), or until the process terminates.

If many processes fix a table, the table remains fixed until all processes have released it (or terminated). A reference counter is kept on a per process basis, and N consecutive fixes requires N releases to release the table.

When a table is fixed, a sequence of first/1 and next/2 calls are guaranteed to succeed even if keys are removed during the traversal. The keys for objects inserted or deleted during a traversal may or may not be returned by next/2 depending on the ordering of keys within the table and if the key exists at the time next/2 is called.

Example:

    clean_all_with_value(Tab,X) ->
        safe_fixtable(Tab,true),
        clean_all_with_value(Tab,X,ets:first(Tab)),
        safe_fixtable(Tab,false).

    clean_all_with_value(Tab,X,'$end_of_table') ->
        true;
    clean_all_with_value(Tab,X,Key) ->
        case ets:lookup(Tab,Key) of
            [{Key,X}] ->
                ets:delete(Tab,Key);
            _ ->
                true
        end,
        clean_all_with_value(Tab,X,ets:next(Tab,Key)).

Notice that deleted objects are not freed from a fixed table until it has been released. If a process fixes a table but never releases it, the memory used by the deleted objects is never freed. The performance of operations on the table also degrades significantly.

To retrieve information about which processes have fixed which tables, use info(Tab, safe_fixed_monotonic_time). A system with many processes fixing tables can need a monitor that sends alarms when tables have been fixed for too long.

Notice that safe_fixtable/2 is not necessary for table type ordered_set and for traversals done by a single ETS function call, like select/2.

select(Continuation) -> {[Match], Continuation} | '$end_of_table' |

### Types

Match = term()
Continuation = continuation()

Continues a match started with select/3. The next chunk of the size specified in the initial select/3 call is returned together with a new Continuation, which can be used in subsequent calls to this function.

When there are no more objects in the table, '$end_of_table' is returned.

select(Tab, MatchSpec) -> [Match] |

### Types

Tab = tab()
MatchSpec = match_spec()
Match = term()

Matches the objects in table Tab using a match specification. This is a more general call than match/2 and match_object/2 calls. In its simplest form, the match specification is as follows:

    MatchSpec = [MatchFunction]
    MatchFunction = {MatchHead, [Guard], [Result]}
    MatchHead = "Pattern as in ets:match"
    Guard = {"Guardtest name", ...}
    Result = "Term construct"

This means that the match specification is always a list of one or more tuples (of arity 3). The first element of the tuple is to be a pattern as described in match/2. The second element of the tuple is to be a list of 0 or more guard tests (described below). The third element of the tuple is to be a list containing a description of the value to return. In almost all normal cases, the list contains exactly one term that fully describes the value to return for each object.

The return value is constructed using the "match variables" bound in MatchHead or using the special match variables '$_' (the whole matching object) and '$$' (all match variables in a list), so that the following match/2 expression:

    ets:match(Tab,{'$1','$2','$3'})

is exactly equivalent to:

    ets:select(Tab,[{{'$1','$2','$3'},[],['$$']}])

And that the following match_object/2 call:

    ets:match_object(Tab,{'$1','$2','$1'})

is exactly equivalent to

    ets:select(Tab,[{{'$1','$2','$1'},[],['$_']}])

Composite terms can be constructed in the Result part either by simply writing a list, so that the following code:

    ets:select(Tab,[{{'$1','$2','$3'},[],['$$']}])

gives the same output as:

    ets:select(Tab,[{{'$1','$2','$3'},[],[['$1','$2','$3']]}])

That is, all the bound variables in the match head as a list. If tuples are to be constructed, one has to write a tuple of arity 1 where the single element in the tuple is the tuple one wants to construct (as an ordinary tuple can be mistaken for a Guard).

Therefore the following call:

    ets:select(Tab,[{{'$1','$2','$1'},[],['$_']}])

gives the same output as:

    ets:select(Tab,[{{'$1','$2','$1'},[],[{{'$1','$2','$3'}}]}])

This syntax is equivalent to the syntax used in the trace patterns (see the dbg(3)) module in Runtime_Tools.

The Guards are constructed as tuples, where the first element is the test name and the remaining elements are the test parameters. To check for a specific type (say a list) of the element bound to the match variable '$1', one would write the test as {is_list, '$1'}. If the test fails, the object in the table does not match and the next MatchFunction (if any) is tried. Most guard tests present in Erlang can be used, but only the new versions prefixed is_ are allowed (is_float, is_atom, and so on).

The Guard section can also contain logic and arithmetic operations, which are written with the same syntax as the guard tests (prefix notation), so that the following guard test written in Erlang:

    is_integer(X), is_integer(Y), X + Y < 4711

is expressed as follows (X replaced with '$1' and Y with '$2'):

    [{is_integer, '$1'}, {is_integer, '$2'}, {'<', {'+', '$1', '$2'}, 4711}]

For tables of type ordered_set, objects are visited in the same order as in a first/next traversal. This means that the match specification is executed against objects with keys in the first/next order and the corresponding result list is in the order of that execution.

select(Tab, MatchSpec, Limit) -> {[Match], Continuation} | '$end_of_table' |

### Types

Tab = tab()
MatchSpec = match_spec()
Limit = integer() >= 1
Match = term()
Continuation = continuation()

Works like select/2, but only returns a limited (Limit) number of matching objects. Term Continuation can then be used in subsequent calls to select/1 to get the next chunk of matching objects. This is a space-efficient way to work on objects in a table, which is still faster than traversing the table object by object using first/1 and next/2.

If the table is empty, '$end_of_table' is returned.

Use safe_fixtable/2 to guarantee safe traversal for subsequent calls to select/1.

select_count(Tab, MatchSpec) -> NumMatched |

### Types

Tab = tab()
MatchSpec = match_spec()
NumMatched = integer() >= 0

Matches the objects in table Tab using a match specification. If the match specification returns true for an object, that object considered a match and is counted. For any other result from the match specification the object is not considered a match and is therefore not counted.

This function can be described as a match_delete/2 function that does not delete any elements, but only counts them.

The function returns the number of objects matched.

select_delete(Tab, MatchSpec) -> NumDeleted |

### Types

Tab = tab()
MatchSpec = match_spec()
NumDeleted = integer() >= 0

Matches the objects in table Tab using a match specification. If the match specification returns true for an object, that object is removed from the table. For any other result from the match specification the object is retained. This is a more general call than the match_delete/2 call.

The function returns the number of objects deleted from the table.

Note

The match specification has to return the atom true if the object is to be deleted. No other return value gets the object deleted. So one cannot use the same match specification for looking up elements as for deleting them.

select_replace(Tab, MatchSpec) -> NumReplaced | OTP 20.0

### Types

Tab = tab()
MatchSpec = match_spec()
NumReplaced = integer() >= 0

Matches the objects in the table Tab using a match specification. For each matched object, the existing object is replaced with the match specification result.

The match-and-replace operation for each individual object is guaranteed to be atomic and isolated. The select_replace table traversal as a whole, like all other select functions, does not give such guarantees.

The match specifiction must be guaranteed to retain the key of any matched object. If not, select_replace will fail with badarg without updating any objects.

For the moment, due to performance and semantic constraints, tables of type bag are not yet supported.

The function returns the total number of replaced objects.

Example

For all 2-tuples with a list in second position, add atom 'marker' first in the list:

    1> T = ets:new(x,[]), ets:insert(T, {key, [1, 2, 3]}).
    true
    2> MS = ets:fun2ms(fun({K, L}) when is_list(L) -> {K, [marker | L]} end).
    [{{'$1','$2'},[{is_list,'$2'}],[{{'$1',[marker|'$2']}}]}]
    3> ets:select_replace(T, MS).
    1
    4> ets:tab2list(T).
    [{key,[marker,1,2,3]}]

A generic single object compare-and-swap operation:

    [Old] = ets:lookup(T, Key),
    New = update_object(Old),
    Success = (1 =:= ets:select_replace(T, [{Old, [], [{const, New}]}])),

select_reverse(Continuation) -> {[Match], Continuation} | '$end_of_table' | OTP R14B

### Types

Continuation = continuation()
Match = term()

Continues a match started with select_reverse/3. For tables of type ordered_set, the traversal of the table continues to objects with keys earlier in the Erlang term order. The returned list also contains objects with keys in reverse order. For all other table types, the behavior is exactly that of select/1.

Example:

    1> T = ets:new(x,[ordered_set]).
    2> [ ets:insert(T,{N}) || N <- lists:seq(1,10) ].
    ...
    3> {R0,C0} = ets:select_reverse(T,[{'_',[],['$_']}],4).
    ...
    4> R0.
    [{10},{9},{8},{7}]
    5> {R1,C1} = ets:select_reverse(C0).
    ...
    6> R1.
    [{6},{5},{4},{3}]
    7> {R2,C2} = ets:select_reverse(C1).
    ...
    8> R2.
    [{2},{1}]
    9> '$end_of_table' = ets:select_reverse(C2).
    ...

select_reverse(Tab, MatchSpec) -> [Match] | OTP R14B

### Types

Tab = tab()
MatchSpec = match_spec()
Match = term()

Works like select/2, but returns the list in reverse order for table type ordered_set. For all other table types, the return value is identical to that of select/2.

select_reverse(Tab, MatchSpec, Limit) -> {[Match], Continuation} | '$end_of_table' | OTP R14B

### Types

Tab = tab()
MatchSpec = match_spec()
Limit = integer() >= 1
Match = term()
Continuation = continuation()

Works like select/3, but for table type ordered_set traversing is done starting at the last object in Erlang term order and moves to the first. For all other table types, the return value is identical to that of select/3.

Notice that this is not equivalent to reversing the result list of a select/3 call, as the result list is not only reversed, but also contains the last Limit matching objects in the table, not the first.

setopts(Tab, Opts) -> true |

### Types

Tab = tab()
Opts = Opt | [Opt]
Opt = {heir, pid(), HeirData} | {heir, none}
HeirData = term()

Sets table options. The only allowed option to be set after the table has been created is heir. The calling process must be the table owner.

slot(Tab, I) -> [Object] | '$end_of_table' |

### Types

Tab = tab()
I = integer() >= 0
Object = tuple()

This function is mostly for debugging purposes, Normally first/next or last/prev are to be used instead.

Returns all objects in slot I of table Tab. A table can be traversed by repeatedly calling the function, starting with the first slot I=0 and ending when '$end_of_table' is returned. If argument I is out of range, the function fails with reason badarg.

Unless a table of type set, bag, or duplicate_bag is protected using safe_fixtable/2, a traversal can fail if concurrent updates are made to the table. For table type ordered_set, the function returns a list containing object I in Erlang term order.

tab2file(Tab, Filename) -> ok | {error, Reason} |

### Types

Tab = tab()
Filename = file:name()
Reason = term()

Dumps table Tab to file Filename.

Equivalent to tab2file(Tab, Filename,[])

tab2file(Tab, Filename, Options) -> ok | {error, Reason} |

### Types

Tab = tab()
Filename = file:name()
Options = [Option]
Option = {extended_info, [ExtInfo]} | {sync, boolean()}
ExtInfo = md5sum | object_count
Reason = term()

Dumps table Tab to file Filename.

When dumping the table, some information about the table is dumped to a header at the beginning of the dump. This information contains data about the table type, name, protection, size, version, and if it is a named table. It also contains notes about what extended information is added to the file, which can be a count of the objects in the file or a MD5 sum of the header and records in the file.

The size field in the header might not correspond to the number of records in the file if the table is public and records are added or removed from the table during dumping. Public tables updated during dump, and that one wants to verify when reading, needs at least one field of extended information for the read verification process to be reliable later.

Option extended_info specifies what extra information is written to the table dump:

object_count

  The number of objects written to the file is noted in the file footer, so file truncation can be verified even if the file was updated during dump.

md5sum

  The header and objects in the file are checksummed using the built-in MD5 functions. The MD5 sum of all objects is written in the file footer, so that verification while reading detects the slightest bitflip in the file data. Using this costs a fair amount of CPU time.

Whenever option extended_info is used, it results in a file not readable by versions of ETS before that in STDLIB 1.15.1

If option sync is set to true, it ensures that the content of the file is written to the disk before tab2file returns. Defaults to {sync, false}.

tab2list(Tab) -> [Object] |

### Types

Tab = tab()
Object = tuple()

Returns a list of all objects in table Tab.

tabfile_info(Filename) -> {ok, TableInfo} | {error, Reason} |

### Types

Filename = file:name()
TableInfo = [InfoItem]
InfoItem =
{name, atom()} |
{type, Type} |
{protection, Protection} |
{named_table, boolean()} |
{keypos, integer() >= 0} |
{size, integer() >= 0} |
{extended_info, [ExtInfo]} |
{version,
{Major :: integer() >= 0, Minor :: integer() >= 0}}
ExtInfo = md5sum | object_count
Type = bag | duplicate_bag | ordered_set | set
Protection = private | protected | public
Reason = term()

Returns information about the table dumped to file by tab2file/2 or tab2file/3.

The following items are returned:

name

  The name of the dumped table. If the table was a named table, a table with the same name cannot exist when the table is loaded from file with file2tab/2. If the table is not saved as a named table, this field has no significance when loading the table from file.

type

  The ETS type of the dumped table (that is, set, bag, duplicate_bag, or ordered_set). This type is used when loading the table again.

protection

  The protection of the dumped table (that is, private, protected, or public). A table loaded from the file gets the same protection.

named_table

  true if the table was a named table when dumped to file, otherwise false. Notice that when a named table is loaded from a file, there cannot exist a table in the system with the same name.

keypos

  The keypos of the table dumped to file, which is used when loading the table again.

size

  The number of objects in the table when the table dump to file started. For a public table, this number does not need to correspond to the number of objects saved to the file, as objects can have been added or deleted by another process during table dump.

extended_info

  The extended information written in the file footer to allow stronger verification during table loading from file, as specified to tab2file/3. Notice that this function only tells which information is present, not the values in the file footer. The value is a list containing one or more of the atoms object_count and md5sum.

version

  A tuple {Major,Minor} containing the major and minor version of the file format for ETS table dumps. This version field was added beginning with STDLIB 1.5.1. Files dumped with older versions return {0,0} in this field.

An error is returned if the file is inaccessible, badly damaged, or not produced with tab2file/2 or tab2file/3.

table(Tab) -> QueryHandle |

table(Tab, Options) -> QueryHandle |

### Types

Tab = tab()
QueryHandle = qlc:query_handle()
Options = [Option] | Option
Option = {n_objects, NObjects} | {traverse, TraverseMethod}
NObjects = default | integer() >= 1
TraverseMethod =
first_next | last_prev | select |
{select, MatchSpec :: match_spec()}

Returns a Query List Comprehension (QLC) query handle. The qlc module provides a query language aimed mainly at Mnesia, but ETS tables, Dets tables, and lists are also recognized by QLC as sources of data. Calling table/1,2 is the means to make the ETS table Tab usable to QLC.

When there are only simple restrictions on the key position, QLC uses lookup/2 to look up the keys. When that is not possible, the whole table is traversed. Option traverse determines how this is done:

first_next

  The table is traversed one key at a time by calling first/1 and next/2.

last_prev

  The table is traversed one key at a time by calling last/1 and prev/2.

select

  The table is traversed by calling select/3 and select/1. Option n_objects determines the number of objects returned (the third argument of select/3); the default is to return 100 objects at a time. The match specification (the second argument of select/3) is assembled by QLC: simple filters are translated into equivalent match specifications while more complicated filters must be applied to all objects returned by select/3 given a match specification that matches all objects.

{select, MatchSpec}

  As for select, the table is traversed by calling select/3 and select/1. The difference is that the match specification is explicitly specified. This is how to state match specifications that cannot easily be expressed within the syntax provided by QLC.

Examples:

An explicit match specification is here used to traverse the table:

    9> true = ets:insert(Tab = ets:new(t, []), [{1,a},{2,b},{3,c},{4,d}]),
    MS = ets:fun2ms(fun({X,Y}) when (X > 1) or (X < 5) -> {Y} end),
    QH1 = ets:table(Tab, [{traverse, {select, MS}}]).

An example with an implicit match specification:

    10> QH2 = qlc:q([{Y} || {X,Y} <- ets:table(Tab), (X > 1) or (X < 5)]).

The latter example is equivalent to the former, which can be verified using function qlc:info/1:

    11> qlc:info(QH1) =:= qlc:info(QH2).
    true

qlc:info/1 returns information about a query handle, and in this case identical information is returned for the two query handles.

take(Tab, Key) -> [Object] | OTP 18.0

### Types

Tab = tab()
Key = term()
Object = tuple()

Returns and removes a list of all objects with key Key in table Tab.

The specified Key is used to identify the object by either comparing equal the key of an object in an ordered_set table, or matching in other types of tables (for details on the difference, see lookup/2 and new/2).

test_ms(Tuple, MatchSpec) -> {ok, Result} | {error, Errors} |

### Types

Tuple = tuple()
MatchSpec = match_spec()
Result = term()
Errors = [{warning | error, string()}]

This function is a utility to test a match specification used in calls to select/2. The function both tests MatchSpec for "syntactic" correctness and runs the match specification against object Tuple.

If the match specification is syntactically correct, the function either returns {ok,Result}, where Result is what would have been the result in a real select/2 call, or false if the match specification does not match object Tuple.

If the match specification contains errors, tuple {error, Errors} is returned, where Errors is a list of natural language descriptions of what was wrong with the match specification.

This is a useful debugging and test tool, especially when writing complicated select/2 calls.

See also: erlang:match_spec_test/3.

to_dets(Tab, DetsTab) -> DetsTab |

### Types

Tab = tab()
DetsTab = dets:tab_name()

Fills an already created/opened Dets table with the objects in the already opened ETS table named Tab. The Dets table is emptied before the objects are inserted.

update_counter(Tab, Key, UpdateOp) -> Result |

update_counter(Tab, Key, UpdateOp, Default) -> Result | OTP 18.0

update_counter(Tab, Key, X3 :: [UpdateOp]) -> [Result] |

update_counter(Tab, Key, X3 :: [UpdateOp], Default) -> [Result] | OTP 18.0

update_counter(Tab, Key, Incr) -> Result |

update_counter(Tab, Key, Incr, Default) -> Result | OTP 18.0

### Types

Tab = tab()
Key = term()
UpdateOp = {Pos, Incr} | {Pos, Incr, Threshold, SetValue}
Pos = Incr = Threshold = SetValue = Result = integer()
Default = tuple()

This function provides an efficient way to update one or more counters, without the trouble of having to look up an object, update the object by incrementing an element, and insert the resulting object into the table again. The operation is guaranteed to be atomic and isolated.

This function destructively update the object with key Key in table Tab by adding Incr to the element at position Pos. The new counter value is returned. If no position is specified, the element directly following key (<keypos>+1) is updated.

If a Threshold is specified, the counter is reset to value SetValue if the following conditions occur:

- Incr is not negative (>= 0) and the result would be greater than (>) Threshold.

- Incr is negative (< 0) and the result would be less than (<) Threshold.

A list of UpdateOp can be supplied to do many update operations within the object. The operations are carried out in the order specified in the list. If the same counter position occurs more than once in the list, the corresponding counter is thus updated many times, each time based on the previous result. The return value is a list of the new counter values from each update operation in the same order as in the operation list. If an empty list is specified, nothing is updated and an empty list is returned. If the function fails, no updates are done.

The specified Key is used to identify the object by either matching the key of an object in a set table, or compare equal to the key of an object in an ordered_set table (for details on the difference, see lookup/2 and new/2).

If a default object Default is specified, it is used as the object to be updated if the key is missing from the table. The value in place of the key is ignored and replaced by the proper key value. The return value is as if the default object had not been used, that is, a single updated element or a list of them.

The function fails with reason badarg in the following situations:

- The table type is not set or ordered_set.
- No object with the correct key exists and no default object was supplied.
- The object has the wrong arity.
- The default object arity is smaller than <keypos>.
- Any field from the default object that is updated is not an integer.
- The element to update is not an integer.
- The element to update is also the key.
- Any of Pos, Incr, Threshold, or SetValue is not an integer.

update_element(Tab, Key, ElementSpec :: {Pos, Value}) -> boolean() |

update_element(Tab, Key, ElementSpec :: [{Pos, Value}]) -> boolean() |

### Types

Tab = tab()
Key = term()
Value = term()
Pos = integer() >= 1

This function provides an efficient way to update one or more elements within an object, without the trouble of having to look up, update, and write back the entire object.

This function destructively updates the object with key Key in table Tab. The element at position Pos is given the value Value.

A list of {Pos,Value} can be supplied to update many elements within the same object. If the same position occurs more than once in the list, the last value in the list is written. If the list is empty or the function fails, no updates are done. The function is also atomic in the sense that other processes can never see any intermediate results.

Returns true if an object with key Key is found, otherwise false.

The specified Key is used to identify the object by either matching the key of an object in a set table, or compare equal to the key of an object in an ordered_set table (for details on the difference, see lookup/2 and new/2).

The function fails with reason badarg in the following situations:

- The table type is not set or ordered_set.
- Pos < 1.
- Pos > object arity.
- The element to update is also the key.

whereis(TableName) -> tid() | undefined | OTP 21.0

### Types

TableName = atom()

This function returns the tid() of the named table identified by TableName, or undefined if no such table exists. The tid() can be used in place of the table name in all operations, which is slightly faster since the name does not have to be resolved on each call.

If the table is deleted, the tid() will be invalid even if another named table is created with the same name.