|
46 | 46 | -export([ensure_ok/2]). |
47 | 47 | -export([localnode/1, tcp_name/3]). |
48 | 48 | -export([intersperse/2, upmap/2, map_in_order/2]). |
| 49 | +-export([table_foreach/2]). |
49 | 50 | -export([dirty_read_all/1, dirty_foreach_key/2, dirty_dump_log/1]). |
50 | 51 | -export([append_file/2, ensure_parent_dirs_exist/1]). |
51 | 52 | -export([format_stderr/2]). |
|
97 | 98 | -spec(intersperse/2 :: (A, [A]) -> [A]). |
98 | 99 | -spec(upmap/2 :: (fun ((A) -> B), [A]) -> [B]). |
99 | 100 | -spec(map_in_order/2 :: (fun ((A) -> B), [A]) -> [B]). |
| 101 | +-spec(table_foreach/2 :: (fun ((any()) -> any()), atom()) -> 'ok'). |
100 | 102 | -spec(dirty_read_all/1 :: (atom()) -> [any()]). |
101 | 103 | -spec(dirty_foreach_key/2 :: (fun ((any()) -> any()), atom()) -> |
102 | 104 | 'ok' | 'aborted'). |
@@ -295,6 +297,21 @@ map_in_order(F, L) -> |
295 | 297 | lists:reverse( |
296 | 298 | lists:foldl(fun (E, Acc) -> [F(E) | Acc] end, [], L)). |
297 | 299 |
|
| 300 | +%% For each entry in a table, execute a function in a transaction. |
| 301 | +%% This is often far more efficient than wrapping a tx around the lot. |
| 302 | +%% |
| 303 | +%% We ignore entries that have been modified or removed. |
| 304 | +table_foreach(F, TableName) -> |
| 305 | + lists:foreach( |
| 306 | + fun (E) -> execute_mnesia_transaction( |
| 307 | + fun () -> case mnesia:match_object(TableName, E, read) of |
| 308 | + [] -> ok; |
| 309 | + _ -> F(E) |
| 310 | + end |
| 311 | + end) |
| 312 | + end, dirty_read_all(TableName)), |
| 313 | + ok. |
| 314 | + |
298 | 315 | dirty_read_all(TableName) -> |
299 | 316 | mnesia:dirty_select(TableName, [{'$1',[],['$1']}]). |
300 | 317 |
|
|
0 commit comments