User's Guide
Reference Manual
Release Notes
PDF
Top

STDLIB
Reference Manual
Version 1.18.1

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
- base64
  - Top of manual page
  - encode/1
  - encode_to_string/1
  - decode/1
  - decode_to_string/1
  - mime_decode/1
  - mime_decode_to_string/1
- beam_lib
  - Top of manual page
  - chunks/2
  - chunks/3
  - version/1
  - md5/1
  - info/1
  - cmp/2
  - cmp_dirs/2
  - diff_dirs/2
  - strip/1
  - strip_files/1
  - strip_release/1
  - format_error/1
  - crypto_key_fun/1
  - clear_crypto_key_fun/0
- 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
  - cd/1
  - flush/0
  - help/0
  - i/0
  - ni/0
  - i/3
  - l/1
  - lc/1
  - ls/0
  - ls/1
  - m/0
  - m/1
  - memory/0
  - memory/1
  - nc/1
  - nc/2
  - nl/1
  - pid/3
  - pwd/0
  - q/0
  - regs/0
  - nregs/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_local_time/1
  - now_to_universal_time/1
  - now_to_datetime/1
  - seconds_to_daystime/1
  - seconds_to_time/1
  - 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_key/2
  - map/2
  - merge/3
  - new/0
  - size/1
  - store/3
  - 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
  - open/2
  - open/3
  - close/1
  - parse_erl_form/1
  - parse_file/3
  - format_error/1
- erl_eval
  - Top of manual page
  - exprs/2
  - exprs/3
  - exprs/4
  - expr/2
  - expr/3
  - expr/4
  - expr/5
  - expr_list/2
  - expr_list/3
  - expr_list/4
  - new_bindings/0
  - bindings/1
  - binding/2
  - add_binding/3
  - del_binding/2
- 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
  - bif/2
  - guard_bif/2
  - type_test/2
  - arith_op/2
  - bool_op/2
  - comp_op/2
  - list_op/2
  - send_op/2
  - op_type/2
- erl_lint
  - Top of manual page
  - module/1
  - module/2
  - module/3
  - is_guard_test/1
  - format_error/1
- erl_parse
  - Top of manual page
  - parse_form/1
  - parse_exprs/1
  - parse_term/1
  - format_error/1
  - tokens/1
  - tokens/2
  - normalise/1
  - abstract/1
- erl_pp
  - Top of manual page
  - form/1
  - form/2
  - attribute/1
  - attribute/2
  - function/1
  - function/2
  - guard/1
  - guard/2
  - exprs/1
  - exprs/2
  - exprs/3
  - expr/1
  - expr/2
  - expr/3
  - expr/4
- erl_scan
  - Top of manual page
  - string/1
  - string/2
  - string/3
  - tokens/3
  - tokens/4
  - reserved_word/1
  - token_info/1
  - token_info/2
  - attributes_info/1
  - attributes_info/2
  - set_attribute/3
  - format_error/1
- erl_tar
  - Top of manual page
  - add/3
  - add/4
  - close/1
  - create/2
  - create/3
  - extract/1
  - extract/2
  - format_error/1
  - open/2
  - table/1
  - table/2
  - t/1
  - 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/2
  - match/3
  - match/1
  - match_delete/2
  - match_object/2
  - match_object/3
  - match_object/1
  - 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/2
  - select/3
  - select/1
  - select_count/2
  - select_delete/2
  - select_reverse/2
  - select_reverse/3
  - select_reverse/1
  - setopts/2
  - slot/2
  - tab2file/2
  - tab2file/3
  - tab2list/1
  - tabfile_info/1
  - table/1
  - table/2
  - test_ms/2
  - to_dets/2
  - update_counter/3
  - update_counter/3
  - update_counter/3
  - update_element/3
  - update_element/3
- file_sorter
  - Top of manual page
  - sort/1
  - sort/2
  - sort/3
  - keysort/2
  - keysort/3
  - keysort/4
  - merge/2
  - merge/3
  - keymerge/3
  - keymerge/4
  - check/1
  - check/2
  - keycheck/2
  - keycheck/3
- filelib
  - Top of manual page
  - ensure_dir/1
  - file_size/1
  - fold_files/5
  - is_dir/1
  - is_file/1
  - is_regular/1
  - last_modified/1
  - wildcard/1
  - wildcard/2
- filename
  - Top of manual page
  - absname/1
  - absname/2
  - absname_join/2
  - basename/1
  - basename/2
  - dirname/1
  - extension/1
  - flatten/1
  - join/1
  - join/2
  - nativename/1
  - pathtype/1
  - rootname/1
  - rootname/2
  - split/1
  - find_src/1
  - find_src/2
- gb_sets
  - Top of manual page
  - add/2
  - add_element/2
  - balance/1
  - delete/2
  - delete_any/2
  - del_element/2
  - difference/2
  - subtract/2
  - empty/0
  - new/0
  - filter/2
  - fold/3
  - from_list/1
  - from_ordset/1
  - insert/2
  - intersection/2
  - intersection/1
  - is_disjoint/2
  - is_empty/1
  - is_member/2
  - is_element/2
  - is_set/1
  - is_subset/2
  - iterator/1
  - largest/1
  - next/1
  - singleton/1
  - size/1
  - smallest/1
  - take_largest/1
  - take_smallest/1
  - to_list/1
  - union/2
  - union/1
- 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
  - keys/1
  - largest/1
  - lookup/2
  - map/2
  - next/1
  - size/1
  - smallest/1
  - take_largest/1
  - take_smallest/1
  - to_list/1
  - update/3
  - values/1
- gen_event
  - Top of manual page
  - start_link/0
  - start_link/1
  - start/0
  - start/1
  - add_handler/3
  - add_sup_handler/3
  - notify/2
  - sync_notify/2
  - call/3
  - call/4
  - delete_handler/3
  - swap_handler/3
  - swap_sup_handler/3
  - which_handlers/1
  - stop/1
  - Module:init/1
  - Module:handle_event/2
  - Module:handle_call/2
  - Module:handle_info/2
  - Module:terminate/2
  - Module:code_change/3
  - Module:format_status/2
- gen_fsm
  - Top of manual page
  - start_link/3
  - start_link/4
  - start/3
  - start/4
  - send_event/2
  - send_all_state_event/2
  - sync_send_event/2
  - sync_send_event/3
  - sync_send_all_state_event/2
  - sync_send_all_state_event/3
  - reply/2
  - send_event_after/2
  - start_timer/2
  - cancel_timer/1
  - enter_loop/4
  - enter_loop/5
  - enter_loop/5
  - enter_loop/6
  - Module:init/1
  - Module:StateName/2
  - Module:handle_event/3
  - Module:StateName/3
  - Module:handle_sync_event/4
  - Module:handle_info/3
  - Module:terminate/3
  - Module:code_change/4
  - Module:format_status/2
- gen_server
  - Top of manual page
  - start_link/3
  - start_link/4
  - start/3
  - start/4
  - call/2
  - call/3
  - multi_call/2
  - multi_call/3
  - multi_call/4
  - cast/2
  - abcast/2
  - abcast/3
  - reply/2
  - enter_loop/3
  - enter_loop/4
  - enter_loop/4
  - enter_loop/5
  - Module:init/1
  - Module:handle_call/3
  - Module:handle_cast/2
  - Module:handle_info/2
  - Module:terminate/2
  - Module:code_change/3
  - Module:format_status/2
- io
  - Top of manual page
  - columns/0
  - columns/1
  - put_chars/1
  - put_chars/2
  - nl/0
  - nl/1
  - get_chars/2
  - get_chars/3
  - get_line/1
  - get_line/2
  - getopts/0
  - getopts/1
  - setopts/1
  - setopts/2
  - write/1
  - write/2
  - read/1
  - read/2
  - read/3
  - fwrite/1
  - fwrite/2
  - fwrite/3
  - format/1
  - format/2
  - format/3
  - fread/2
  - fread/3
  - rows/0
  - rows/1
  - scan_erl_exprs/1
  - scan_erl_exprs/2
  - scan_erl_exprs/3
  - scan_erl_form/1
  - scan_erl_form/2
  - scan_erl_form/3
  - parse_erl_exprs/1
  - parse_erl_exprs/2
  - parse_erl_exprs/3
  - parse_erl_form/1
  - parse_erl_form/2
  - parse_erl_form/3
- io_lib
  - Top of manual page
  - nl/0
  - write/1
  - write/2
  - print/1
  - print/4
  - fwrite/2
  - format/2
  - fread/2
  - fread/3
  - write_atom/1
  - write_string/1
  - write_char/1
  - indentation/2
  - char_list/1
  - deep_char_list/1
  - printable_list/1
- lib
  - Top of manual page
  - flush_receive/0
  - error_message/2
  - progname/0
  - nonl/1
  - send/2
  - sendw/2
- lists
  - Top of manual page
  - all/2
  - any/2
  - append/1
  - append/2
  - concat/1
  - delete/2
  - dropwhile/2
  - duplicate/2
  - filter/2
  - flatlength/1
  - flatmap/2
  - flatten/1
  - flatten/2
  - foldl/3
  - foldr/3
  - foreach/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
  - 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
- math
  - Top of manual page
  - pi/0
  - sin/1
  - cos/1
  - tan/1
  - asin/1
  - acos/1
  - atan/1
  - atan2/2
  - sinh/1
  - cosh/1
  - tanh/1
  - asinh/1
  - acosh/1
  - atanh/1
  - exp/1
  - log/1
  - log10/1
  - pow/2
  - sqrt/1
  - erf/1
  - erfc/1
- ms_transform
  - Top of manual page
  - parse_transform/2
  - transform_from_shell/3
  - format_error/1
- 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_key/2
  - map/2
  - merge/3
  - new/0
  - size/1
  - store/3
  - to_list/1
  - update/3
  - update/4
  - update_counter/3
- ordsets
  - Top of manual page
  - new/0
  - is_set/1
  - size/1
  - to_list/1
  - from_list/1
  - is_element/2
  - add_element/2
  - del_element/2
  - union/2
  - union/1
  - intersection/2
  - intersection/1
  - is_disjoint/2
  - subtract/2
  - is_subset/2
  - fold/3
  - filter/2
- pg
  - Top of manual page
  - create/1
  - create/2
  - join/2
  - send/2
  - esend/2
  - members/1
- pool
  - Top of manual page
  - start/1
  - start/2
  - attach/1
  - stop/0
  - get_nodes/0
  - pspawn/3
  - pspawn_link/3
  - get_node/0
- proc_lib
  - Top of manual page
  - 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
  - init_ack/1
  - init_ack/2
  - format/1
  - initial_call/1
  - translate_initial_call/1
  - hibernate/3
- 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
  - eval/1
  - eval/2
  - e/1
  - e/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
  - new/0
  - is_queue/1
  - is_empty/1
  - len/1
  - in/2
  - in_r/2
  - out/1
  - out_r/1
  - from_list/1
  - to_list/1
  - reverse/1
  - split/2
  - join/2
  - filter/2
  - member/2
  - get/1
  - get_r/1
  - drop/1
  - drop_r/1
  - peek/1
  - peek_r/1
  - cons/2
  - head/1
  - tail/1
  - snoc/2
  - daeh/1
  - last/1
  - liat/1
  - init/1
  - lait/1
- random
  - Top of manual page
  - seed/0
  - seed/3
  - seed/1
  - seed0/0
  - uniform/0
  - uniform/1
  - uniform_s/1
  - uniform_s/2
- re
  - Top of manual page
  - compile/1
  - compile/2
  - run/2
  - run/3
  - replace/3
  - replace/4
  - split/2
  - split/3
- sets
  - Top of manual page
  - new/0
  - is_set/1
  - size/1
  - to_list/1
  - from_list/1
  - is_element/2
  - add_element/2
  - del_element/2
  - union/2
  - union/1
  - intersection/2
  - intersection/1
  - is_disjoint/2
  - subtract/2
  - is_subset/2
  - fold/3
  - filter/2
- shell
  - Top of manual page
  - history/1
  - results/1
  - catch_exception/1
  - prompt_func/1
  - start_restricted/1
  - stop_restricted/0
- shell_default
- slave
  - Top of manual page
  - start/1
  - start/2
  - start/3
  - start_link/1
  - start_link/2
  - start_link/3
  - stop/1
  - pseudo/1
  - pseudo/2
  - relay/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
  - len/1
  - equal/2
  - concat/2
  - chr/2
  - rchr/2
  - str/2
  - rstr/2
  - span/2
  - cspan/2
  - substr/2
  - substr/3
  - tokens/2
  - join/2
  - chars/2
  - chars/3
  - copies/2
  - words/1
  - words/2
  - sub_word/2
  - sub_word/3
  - strip/1
  - strip/2
  - strip/3
  - left/2
  - left/3
  - right/2
  - right/3
  - centre/2
  - centre/3
  - sub_string/2
  - sub_string/3
  - to_float/1
  - to_integer/1
  - to_lower/1
  - to_upper/1
- supervisor
  - Top of manual page
  - start_link/2
  - start_link/3
  - start_child/2
  - terminate_child/2
  - delete_child/2
  - restart_child/2
  - which_children/1
  - count_children/1
  - check_childspecs/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
  - log/2
  - log/3
  - log_to_file/2
  - log_to_file/3
  - statistics/2
  - statistics/3
  - trace/2
  - trace/3
  - no_debug/1
  - no_debug/2
  - suspend/1
  - suspend/2
  - resume/1
  - resume/2
  - change_code/4
  - change_code/5
  - get_status/1
  - get_status/2
  - install/2
  - install/3
  - remove/2
  - remove/3
  - debug_options/1
  - get_debug/3
  - handle_debug/4
  - handle_system_msg/6
  - print_log/1
  - Mod:system_continue/3
  - Mod:system_terminate/4
  - Mod:system_code_change/4
- timer
  - Top of manual page
  - start/0
  - apply_after/4
  - send_after/2
  - send_after/3
  - kill_after/1
  - kill_after/2
  - exit_after/2
  - exit_after/3
  - apply_interval/4
  - send_interval/2
  - send_interval/3
  - cancel/1
  - sleep/1
  - tc/1
  - tc/2
  - tc/3
  - now_diff/2
  - seconds/1
  - minutes/1
  - hours/1
  - hms/3
- unicode
  - Top of manual page
  - bom_to_encoding/1
  - characters_to_list/1
  - characters_to_list/2
  - characters_to_binary/1
  - characters_to_binary/2
  - characters_to_binary/3
  - encoding_to_bom/1
- 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
  - zip/2
  - zip/3
  - create/2
  - create/3
  - unzip/1
  - unzip/2
  - extract/1
  - extract/2
  - foldl/3
  - list_dir/1
  - list_dir/2
  - table/1
  - table/2
  - t/1
  - tt/1
  - zip_open/1
  - zip_open/2
  - zip_list_dir/1
  - zip_get/1
  - zip_get/2
  - zip_close/1

# 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 objects stored).

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 can have many objects associated with each key.

The number of tables stored at one Erlang node is limited. The current default limit is approximately 1400 tables. The upper limit can be increased by setting the environment variable ERL_MAX_ETS_TABLES before starting the Erlang runtime system (i.e. with the -env option to erl/werl). The actual limit may be slightly higher than the one specified, but never lower.

Note that there is no automatic garbage collection for tables. Even if there are no references to a table from any process, it will not automatically be destroyed unless the owner process terminates. It can be destroyed explicitly by using delete/1. The default owner is the process that created the table. Table ownership can be transferred at process termination by using the heir option or explicitly by calling 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' should not be used as a key since this atom is used to mark the end of the table when using first/next.

Also worth noting is the subtle difference between matching and comparing equal, which is demonstrated by the different 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 there is no defined order 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.

### Failure

In general, the functions below will exit with reason badarg if any argument is of the wrong format, if the table identifier is invalid or if the operation is denied due to table access rights (protected or private).

### 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 towards a single object will either succeed or fail completely without any effect at all (atomicy). Nor can any intermediate results of the update be seen by other processes (isolation). Some functions that update several objects state that they even guarantee atomicy and isolation for the entire operation. In database terms the isolation level can be seen as "serializable", as if all isolated operations were carried out serially, one after the other in a strict order.

No other support is available within ETS that would guarantee consistency between objects. However, the safe_fixtable/2 function can be used to guarantee that a sequence of first/1 and next/2 calls will traverse the table without errors and that each existing object in the table is visited exactly once, even if another process (or the same process) simultaneously deletes or inserts objects into the table. Nothing more is guaranteed; in particular objects that are inserted or deleted during such a traversal may be visited once or not at all. Functions that internally traverse over a table, like select and match, will give the same guarantee as safe_fixtable.

### Match Specifications

Some of the functions uses a match specification, match_spec. A brief explanation is given in select/2. For a detailed description, see the chapter "Match specifications in Erlang" in ERTS User's Guide.

### DATA TYPES

continuation()

Opaque continuation used by select/1 and select/3.

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

A match specification, see above.

match_pattern() = atom() | tuple()

tab() = atom() | tid()

tid()

A table identifier, as returned by new/2.

### EXPORTS

all() -> [Tab]

Types:

Tab = tid() | atom()

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

delete(Tab) -> true

Types:

Tab = tid() | atom()

Deletes the entire table Tab.

delete(Tab, Key) -> true

Types:

Tab = tid() | atom()
Key = term()

Deletes all objects with the key Key from the table Tab.

delete_all_objects(Tab) -> true

Types:

Tab = tid() | atom()

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

delete_object(Tab,Object) -> true

Types:

Tab = tid() | atom()
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, all instances of the object will be 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 currently only supported option is {verify,boolean()}. If verification is turned on (by means of specifying {verify,true}), the function utilizes 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 might make verification fail if the table was public and objects were added or removed while the table was dumped to file. To avoid this type of problems, either do not verify files dumped while updated simultaneously or use the {extended_info, [object_count]} option to tab2file/3, which extends the information in the file with the number of objects actually written.

If verification is turned on and the file was written with the 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 = tid() | atom()
Key = term()

Returns the first key Key in the table Tab. If the table is of the ordered_set type, the first key in Erlang term order will be returned. If the table is of any other type, the first key according to the table's internal order will be returned. If the table is empty, '$end_of_table' will be returned.

Use next/2 to find subsequent keys in the table.

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 order in which the elements of the table are traversed is unspecified, except for tables of type ordered_set, for which they are traversed first to last.

If Function inserts objects into the table, or another process inserts objects into the table, those objects may (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 order in which the elements of the table are traversed is unspecified, except for tables of type ordered_set, for which they are traversed last to first.

If Function inserts objects into the table, or another process inserts objects into the table, those objects may (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 named DetsTab. The existing objects of the ETS table are kept unless overwritten.

Throws a badarg error if any of the tables does not exist or the dets table is not open.

fun2ms(LiteralFun) -> MatchSpec

Types:

LiteralFun = function()
MatchSpec = match_spec()

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

The parse transform is implemented in the module ms_transform and the source must include the file ms_transform.hrl in STDLIB for this pseudo function to work. Failing to include the hrl file in the source will result in a runtime error, not a compile time ditto. The include file is easiest included by adding the 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 needs to use the is_XXX guard tests. Language constructs that have no representation in a match_spec (like if, case, receive etc) are not allowed.

The return value is the resulting match_spec.

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 this 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 will be replaced by match_spec const expressions, which is consistent with the static scoping for Erlang funs. Local or global function calls can not be in the guard or body of the fun however. Calls to builtin match_spec functions of course is allowed:

    4> ets:fun2ms(fun({M,N}) when N > X, is_atomm(M) -> M end).
    Error: fun containing local Erlang function calls
    ('is_atomm' 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 can be seen by the example, the function can be called from the shell too. The fun needs to be literally in the call when used from the shell as well. Other means than the parse_transform are used in the shell case, but more or less the same restrictions apply (the exception being records, as they are not handled by the shell).

Warning

If the parse_transform is not applied to a module which calls this pseudo function, the call will fail in runtime (with a badarg). The module ets actually exports a function with this name, but it should never really be called except for when using the function in the shell. If the parse_transform is properly applied by including the ms_transform.hrl header file, compiled code will never call the function, but the function call is replaced by a literal match_spec.

For more information, see ms_transform(3).

give_away(Tab, Pid, GiftData) -> true

Types:

Tab = tid() | atom()
Pid = pid()
GiftData = term()

Make process Pid the new owner of table Tab. If successful, the message {'ETS-TRANSFER',Tab,FromPid,GiftData} will be 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.

Note that give_away does not at all affect the heir option of the table. A table owner can for example set the heir to itself, give the table away and then get it back in case the receiver terminates.

i() -> ok

Displays information about all ETS tables on tty.

i(Tab) -> ok

Types:

Tab = tab()

Browses the table Tab on tty.

info(Tab) -> [{Item, Value}] | undefined

Types:

Tab = tid() | atom()
Item = atom(), see below
Value = term(), see below

Returns information about the table Tab as a list of {Item, Value} 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, this function fails with reason badarg.

- Item=memory, Value=integer()
  The number of words allocated to the table.
- Item=owner, Value=pid()
  The pid of the owner of the table.
- Item=heir, Value=pid()|none
  The pid of the heir of the table, or none if no heir is set.
- Item=name, Value=atom()
  The name of the table.
- Item=size, Value=integer()
  The number of objects inserted in the table.
- Item=node, Value=atom()
  The node where the table is stored. This field is no longer meaningful as tables cannot be accessed from other nodes.
- Item=named_table, Value=true|false
  Indicates if the table is named or not.
- Item=type, Value=set|ordered_set|bag|duplicate_bag
  The table type.
- Item=keypos, Value=integer()
  The key position.
- Item=protection, Value=public|protected|private
  The table access rights.
- Item=compressed, Value=true|false
  Indicates if the table is compressed or not.

info(Tab, Item) -> Value | undefined

Types:

Tab = tid() | atom()
Item, Value - see below

Returns the information associated with Item for the 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, this function fails with reason badarg.

Warning

In R11B and earlier, this function would not fail but return undefined for invalid values for Item.

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

- Item=fixed, Value=true|false
  Indicates if the table is fixed by any process or not.

- Item=safe_fixed, Value={FirstFixed,Info}|false

  If the table has been fixed using safe_fixtable/2, the call returns a tuple where FirstFixed is the time when the table was first fixed by a process, which may or may not be one of the processes it is fixed by right now.

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

  If the table never has been fixed, the call returns false.

- Item=stats, Value=tuple()
  Returns internal statistics about set, bag and duplicate_bag tables on an internal format used by OTP test suites. Not for production use.

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 the 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 ets:insert/2.

When called with the 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 will be called exactly once, and should an error occur, the last function is called with the argument close, the reply of which is ignored.

If the type of the table is set and there is more than one object 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 = tid() | atom()
ObjectOrObjects = tuple() | [tuple()]

Inserts the object or all of the objects in the list ObjectOrObjects into the table Tab. If the table is a set and the key of the inserted objects matches the key of any object in the table, the old object will be replaced. If the table is an ordered_set and the key of the inserted object compares equal to the key of any object in the table, the old object is also replaced. If the list contains more than one object with matching keys and the table is a set, one will be inserted, which one is not defined. The same thing holds for ordered_set, but will also happen 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 = tid() | atom()
ObjectOrObjects = tuple() | [tuple()]

This function works exactly like insert/2, with the exception that instead of overwriting objects with the same key (in the case of set or ordered_set) or adding more objects with keys already existing in the table (in the case of bag and duplicate_bag), it simply returns false. If ObjectOrObjects is a list, the function checks every key prior to inserting anything. Nothing will be inserted if not 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()

This function is used to check if a term is a valid compiled match_spec. The compiled match_spec is an opaque datatype which can not be sent between Erlang nodes nor be stored on disk. Any attempt to create an external representation of a compiled match_spec will result in an empty binary (<<>>). As an example, the following expression:

    ets:is_compiled_ms(ets:match_spec_compile([{'_',[],[true]}])).

will yield true, while the following expressions:

    MS = ets:match_spec_compile([{'_',[],[true]}]),
    Broken = binary_to_term(term_to_binary(MS)),
    ets:is_compiled_ms(Broken).

will yield false, as the variable Broken will contain a compiled match_spec that has passed through external representation.

Note

The fact that compiled match_specs has no external representation is for performance reasons. It may be subject to change in future releases, while this interface will still remain for backward compatibility reasons.

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

Types:

Tab = tid() | atom()
Key = term()

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

Use prev/2 to find preceding keys in the table.

lookup(Tab, Key) -> [Object]

Types:

Tab = tid() | atom()
Key = term()
Object = tuple()

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

In the case of set, bag and duplicate_bag, an object is returned only if the given key matches the key of the object in the table. If the table is an ordered_set however, an object is returned if the key given compares equal to the key of an object in the table. The difference being the same as between =:= and ==. As an example, one might insert an object with the integer() 1 as a key in an ordered_set and get the object returned as a result of doing a lookup/2 with the float() 1.0 as the key to search for.

If the table is 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. If the table is of type bag or duplicate_bag, the function returns a list of arbitrary length.

Note that the time order of object insertions is preserved; the first object inserted with the given key will be first in the resulting list, and so on.

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

lookup_element(Tab, Key, Pos) -> Elem

Types:

Tab = tid() | atom()
Key = term()
Pos = integer()
Elem = term() | [term()]

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

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

If no object with the key Key exists, the function will exit 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's view keys as equal when they compare equal whereas the other table types only regard them equal when they match, naturally holds for lookup_element as well as for lookup.

match(Tab, Pattern) -> [Match]

Types:

Tab = tid() | atom()
Pattern = tuple()
Match = [term()]

Matches the objects in the table Tab against the pattern Pattern.

A pattern is a term that may contain:

- bound parts (Erlang terms),
- '_' which matches any Erlang term, and
- 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. An example:

    6> ets:match(T, '$1'). % Matches every object in the 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, i.e. 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.

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

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

Types:

Tab = tid() | atom()
Pattern = tuple()
Match = [term()]
Continuation = term()

Works like ets:match/2 but only returns a limited (Limit) number of matching objects. The Continuation term can then be used in subsequent calls to ets: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 still faster than traversing the table object by object using ets:first/1 and ets:next/1.

'$end_of_table' is returned if the table is empty.

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

Types:

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

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

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

match_delete(Tab, Pattern) -> true

Types:

Tab = tab()
Pattern = match_pattern()

Deletes all objects which match the pattern Pattern from the table Tab. See match/2 for a description of patterns.

match_object(Tab, Pattern) -> [Object]

Types:

Tab = tid() | atom()
Pattern = Object = tuple()

Matches the objects in the table Tab against the pattern Pattern. See match/2 for a description of patterns. The function returns a list of all objects which match the pattern.

If the key is specified in the pattern, the match is very efficient. If the key is not specified, i.e. 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.

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

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

Types:

Tab = tid() | atom()
Pattern = tuple()
Match = [term()]
Continuation = term()

Works like ets:match_object/2 but only returns a limited (Limit) number of matching objects. The Continuation term can then be used in subsequent calls to ets: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 still faster than traversing the table object by object using ets:first/1 and ets:next/1.

'$end_of_table' is returned if the table is empty.

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

Types:

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

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

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

match_spec_compile(MatchSpec) -> CompiledMatchSpec

Types:

MatchSpec = match_spec()
CompiledMatchSpec = comp_match_spec()

This function transforms a match_spec into an internal representation that can be used in subsequent calls to ets:match_spec_run/2. The internal representation is opaque and can not be converted to external term format and then back again without losing its properties (meaning it can not be sent to a process on another node and still remain a valid compiled match_spec, nor can it be stored on disk). The validity of a compiled match_spec can be checked using ets:is_compiled_ms/1.

If the term MatchSpec can not be compiled (does not represent a valid match_spec), a badarg fault is thrown.

Note

This function has limited use in normal code, it is used by Dets to perform the dets:select operations.

match_spec_run(List,CompiledMatchSpec) -> list()

Types:

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

This function executes the matching specified in a compiled match_spec on a list of tuples. The CompiledMatchSpec term should be the result of a call to ets:match_spec_compile/1 and is hence the internal representation of the match_spec one wants to use.

The matching will be 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 the length of the parameter List. The two calls in the following example will 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)),
    % ...will give 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 Dets to perform the dets:select operations and by Mnesia during transactions.

member(Tab, Key) -> true | false

Types:

Tab = tid() | atom()
Key = term()

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

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

Types:

Name = atom()
Options = [Option]
Option = Type | Access | named_table | {keypos,Pos} | {heir,pid(),HeirData} | {heir,none} | Tweaks
Type = set | ordered_set | bag | duplicate_bag
Access = public | protected | private
Tweaks = {write_concurrency,boolean()} | {read_concurrency,boolean()} | compressed
Pos = integer()
HeirData = term()

Creates a new table and returns a table identifier which 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.

The parameter Options is a list of atoms which specifies table type, access rights, key position and if the table is named or not. If one or more options are left out, the default values are used. This means that not specifying any options ([]) is the same as specifying [set, protected, {keypos,1}, {heir,none}, {write_concurrency,false}, {read_concurrency,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 the 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, the integer() 1 and the 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 elements returned, 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 may 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 name Name is associated with the table identifier. The name can then be used instead of the table identifier in subsequent operations.

- {keypos,Pos} Specfies which element in the stored tuples should be used as key. By default, it is the first element, i.e. 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.

  Note 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 will inherit the table if the owner terminates. The message {'ETS-TRANSFER',tid(),FromPid,HeirData} will be sent to the heir when that happens. The heir must be a local process. Default heir is none, which will destroy the table when the owner terminates.

- {write_concurrency,boolean()} Performance tuning. Default is false, in which case an operation that mutates (writes to) the table will obtain exclusive access, blocking any concurrent access of the same table until finished. If set to true, the table is optimized towards 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 sequential access and concurrent reader performance. The write_concurrency option can be combined with the read_concurrency option. You typically want to combine these when large concurrent read bursts and large concurrent write bursts are common (see the documentation of the read_concurrency option for more information). Note that this option does not change any guarantees about atomicy and isolation. Functions that makes such promises over several objects (like insert/2) will gain less (or nothing) from this option.

  Table type ordered_set is not affected by this option in current implementation.

- {read_concurrency,boolean()} Performance tuning. Default is 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 (i.e., lots of reads not interrupted by writes, and lots of 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 will get a performance degradation by enabling this option. The read_concurrency option can be combined with the write_concurrency option. You typically want to combine these when large concurrent read bursts and large concurrent write bursts are common.

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

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

Types:

Tab = tid() | atom()
Key1 = Key2 = term()

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

Use first/1 to find the first key in the table.

Unless a table of type set, bag or duplicate_bag is protected using safe_fixtable/2, see below, a traversal may fail if concurrent updates are made to the table. If the table is of type ordered_set, the function returns the next key in order, even if the object does no longer exist.

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

Types:

Tab = tid() | atom()
Key1 = Key2 = term()

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

Use last/1 to find the last key in the table.

rename(Tab, Name) -> Name

Types:

Tab = Name = atom()

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

repair_continuation(Continuation, MatchSpec) -> Continuation

Types:

Continuation = continuation()
MatchSpec = match_spec()

This function can be used to restore an opaque continuation returned by ets:select/3 or ets: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_specs and therefore will be invalidated if converted to external term format. Given that the original match_spec is kept intact, the continuation can be restored, meaning it can once again be used in subsequent ets:select/1 calls even though it has been stored on disk or on another node.

As an example, the following sequence of calls will fail:

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

...while the following sequence will work:

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

...as the call to ets:repair_continuation/2 will reestablish the (deliberately) invalidated continuation Broken.

Note

This function is very rarely needed in application code. It is used by Mnesia to implement 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 reason for not having an external representation of a compiled match_spec is performance. It may be subject to change in future releases, while this interface will remain for backward compatibility.

safe_fixtable(Tab, true|false) -> true

Types:

Tab = tid() | atom()

Fixes a table of the set, bag or duplicate_bag table type for safe traversal.

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 several processes fix a table, the table will remain 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 actually release the table.

When a table is fixed, a sequence of first/1 and next/2 calls are guaranteed to succeed and each object in the table will only be returned once, even if objects are removed or inserted during the traversal. The keys for new objects inserted during the traversal may be returned by next/2 (it depends on the internal ordering of the keys). An 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)).

Note that no deleted objects are actually removed 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 will never be freed. The performance of operations on the table will also degrade significantly.

Use info/2 to retrieve information about which processes have fixed which tables. A system with a lot of processes fixing tables may need a monitor which sends alarms when tables have been fixed for too long.

Note that for tables of the ordered_set type, safe_fixtable/2 is not necessary as calls to first/1 and next/2 will always succeed.

select(Tab, MatchSpec) -> [Match]

Types:

Tab = tid() | atom()
Match = term()
MatchSpec = match_spec()

Matches the objects in the table Tab using a match_spec. This is a more general call than the ets:match/2 and ets:match_object/2 calls. In its simplest forms the match_specs look like this:

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

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

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

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

is exactly equivalent to:

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

- and the following ets: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 this code:

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

gives the same output as:

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

i.e. 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 with the single element in the tuple being the tuple one wants to construct (as an ordinary tuple could 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 dbg(3)).

The Guards are constructed as tuples where the first element is the name of the test and the rest of the elements are the parameters of the test. 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 will not match and the next MatchFunction (if any) will be tried. Most guard tests present in Erlang can be used, but only the new versions prefixed is_ are allowed (like is_float, is_atom etc).

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 a guard test written in Erlang looking like this:

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

is expressed like this (X replaced with '$1' and Y with '$2'):

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

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

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

Types:

Tab = tid() | atom()
Match = term()
MatchSpec = match_spec()
Continuation = term()

Works like ets:select/2 but only returns a limited (Limit) number of matching objects. The Continuation term can then be used in subsequent calls to ets: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 ets:first/1 and ets:next/1.

'$end_of_table' is returned if the table is empty.

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

Types:

Match = term()
Continuation = term()

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

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

select_count(Tab, MatchSpec) -> NumMatched

Types:

Tab = tid() | atom()
Object = tuple()
MatchSpec = match_spec()
NumMatched = integer()

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

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

The function returns the number of objects matched.

select_delete(Tab, MatchSpec) -> NumDeleted

Types:

Tab = tid() | atom()
Object = tuple()
MatchSpec = match_spec()
NumDeleted = integer()

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

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

Note

The match_spec has to return the atom true if the object is to be deleted. No other return value will get the object deleted, why one can not use the same match specification for looking up elements as for deleting them.

select_reverse(Tab, MatchSpec) -> [Match]

Types:

Tab = tid() | atom()
Match = term()
MatchSpec = match_spec()

Works like select/2, but returns the list in reverse order for the ordered_set table type. 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'

Types:

Tab = tid() | atom()
Match = term()
MatchSpec = match_spec()
Continuation = term()

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

Note 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.

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

Types:

Match = term()
Continuation = term()

Continues a match started with ets:select_reverse/3. If the table is an ordered_set, the traversal of the table will continue towards objects with keys earlier in the Erlang term order. The returned list will also contain objects with keys in reverse order.

For all other table types, the behaviour is exatly 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).
    ...

setopts(Tab, Opts) -> true

Types:

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

Set table options. The only option that currently is allowed 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 = tid() | atom()
I = integer()
Object = tuple()

This function is mostly for debugging purposes, Normally one should use first/next or last/prev instead.

Returns all objects in the I:th slot of the 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. The function will fail with reason badarg if the I argument is out of range.

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

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

Types:

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

Dumps the table Tab to the 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]}
ExtInfo = md5sum | object_count
Reason = term()

Dumps the table Tab to the file Filename.

When dumping the table, certain 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's 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 actual 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.

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

object_count

  The number of objects actually written to the file is noted in the file footer, why verification of file truncation is possible 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 will detect the slightest bitflip in the file data. Using this costs a fair amount of CPU time.

Whenever the extended_info option is used, it results in a file not readable by versions of ets prior to that in stdlib-1.15.1

tab2list(Tab) -> [Object]

Types:

Tab = tab()
Object = tuple()

Returns a list of all objects in the 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 at all when loading the table from file.

type
  The ets type of the dumped table (i.e. set, bag, duplicate_bag or ordered_set). This type will be used when loading the table again.
protection
  The protection of the dumped table (i.e. private, protected or public). A table loaded from the file will get the same protection.
named_table
  true if the table was a named table when dumped to file, otherwise false. Note 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 will be used when loading the table again.
size
  The number of objects in the table when the table dump to file started, which in case of a public table need not correspond to the number of objects actually saved to the file, as objects might 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. Note 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 will return {0,0} in this field.

An error is returned if the file is inaccessible, badly damaged or not an file 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 QLC (Query List Comprehension) query handle. The module qlc implements a query language aimed mainly at Mnesia but ETS tables, Dets tables, and lists are also recognized by QLC as sources of data. Calling ets: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 ets:lookup/2 to look up the keys, but when that is not possible the whole table is traversed. The option traverse determines how this is done:

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

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

- select. The table is traversed by calling ets:select/3 and ets:select/1. The 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_spec (the second argument of select/3) is assembled by QLC: simple filters are translated into equivalent match_specs while more complicated filters have to be applied to all objects returned by select/3 given a match_spec that matches all objects.

- {select, MatchSpec}. As for select the table is traversed by calling ets:select/3 and ets:select/1. The difference is that the match_spec is explicitly given. This is how to state match_specs that cannot easily be expressed within the syntax provided by QLC.

The following example uses an explicit match_spec 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 implicit match_spec:

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

The latter example is in fact equivalent to the former which can be verified using the 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.

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_spec used in calls to ets:select/2. The function both tests MatchSpec for "syntactic" correctness and runs the match_spec against the object Tuple. If the match_spec contains errors, the tuple {error, Errors} is returned where Errors is a list of natural language descriptions of what was wrong with the match_spec. If the match_spec is syntactically OK, the function returns {ok,Result} where Result is what would have been the result in a real ets:select/2 call or false if the match_spec does not match the object Tuple.

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

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]) -> [Result]
update_counter(Tab, Key, Incr) -> Result

Types:

Tab = tid() | atom()
Key = term()
UpdateOp = {Pos,Incr} | {Pos,Incr,Threshold,SetValue}
Pos = Incr = Threshold = SetValue = Result = integer()

This function provides an efficient way to update one or more counters, without the hassle of having to look up an object, update the object by incrementing an element and insert the resulting object into the table again. (The update is done atomically; i.e. no process can access the ets table in the middle of the operation.)

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

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

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

A list of UpdateOp can be supplied to do several 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 one time in the list, the corresponding counter will thus be updated several 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 should fail, no updates will be done at all.

The given 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 (see lookup/2 and new/2 for details on the difference).

The function will fail with reason badarg if:

- the table is not of type set or ordered_set,
- no object with the right key exists,
- the object has the wrong arity,
- the element to update is not an integer,
- the element to update is also the key, or,
- any of Pos, Incr, Threshold or SetValue is not an integer

update_element(Tab, Key, {Pos,Value}) -> true | false
update_element(Tab, Key, [{Pos,Value}]) -> true | false

Types:

Tab = tid() | atom()
Key = Value = term()
Pos = integer()

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

It will destructively update the object with key Key in the table Tab. The element at the Pos:th position will be given the value Value.

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

The function returns true if an object with the key Key was found, false otherwise.

The given 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 (see lookup/2 and new/2 for details on the difference).

The function will fail with reason badarg if:

- the table is not of type set or ordered_set,
- Pos is less than 1 or greater than the object arity, or,
- the element to update is also the key