Search code examples
erlangets

Are ETS bulk operations atomic?


Specifically, :ets.tab2list and :ets.file2tab. Does these functions "snapshot" the table state, or can other operations interleave reads and writes while these functions complete?


Solution

  • Based on the documentation here:

    Functions that internally traverse over a table, like select and match, give the same guarantee as safe_fixtable.

    Where

    [...] function safe_fixtable can be used to guarantee that a sequence of first/1 and next/2 calls traverse the table without errors and that each existing object in the table is visited exactly once, even if another (or the same) process simultaneously deletes or inserts objects into the table.

    And specifically related to your question:

    Nothing else is guaranteed; in particular objects that are inserted or deleted during such a traversal can be visited once or not at all.


    EDIT

    ets:tab2list/1 calls ets:match_object/2 which is a built-in function (BIF) implemented in C. The implementation here is using the BIF ets_select2, which is the implementation for ets:select/2.

    ets:file2tab ends up calling load_table/3 which simply uses ets:insert/2.

    The code for ets:tab2file/3 in ets.erl, uses ets:select/3 to get the first chunk and then ets:select/1 to get the rest of the chunks in the table.