Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

set_env can now also update mnesia

  • Loading branch information...
commit d3226b539c66242089bf13bc95eb69e8d051f3b1 1 parent f10bfaf
Ulf Wiger authored committed
Showing with 36 additions and 5 deletions.
  1. +20 −1 src/gproc.erl
  2. +16 −4 test/gproc_tests.erl
View
21 src/gproc.erl
@@ -471,6 +471,8 @@ is_valid_set_strategy([{os_env, _}|T], Value) ->
is_string(Value) andalso is_valid_set_strategy(T, Value);
is_valid_set_strategy([app_env|T], Value) ->
is_valid_set_strategy(T, Value);
+is_valid_set_strategy([{mnesia,_Type,_Oid,_Pos}|T], Value) ->
+ is_valid_set_strategy(T, Value);
is_valid_set_strategy([], _) ->
true;
is_valid_set_strategy(_, _) ->
@@ -483,7 +485,24 @@ set_strategy([H|T], App, Key, Value) ->
os_env ->
os:putenv(os_env_key(Key), Value);
{os_env, ENV} ->
- os:putenv(ENV, Value)
+ os:putenv(ENV, Value);
+ {mnesia,Type,Oid,Pos} ->
+ mnesia:activity(
+ Type,
+ fun() ->
+ Rec = case mnesia:read(Oid) of
+ [] ->
+ {Tab,K} = Oid,
+ Tag = mnesia:table_info(Tab, record_name),
+ Attrs = mnesia:table_info(Tab, attributes),
+ list_to_tuple(
+ [Tag,K |
+ [undefined || _ <- tl(Attrs)]]);
+ [Old] ->
+ Old
+ end,
+ mnesia:write(setelement(Pos, Rec, Value))
+ end)
end,
set_strategy(T, App, Key, Value);
set_strategy([], _, _, Value) ->
View
20 test/gproc_tests.erl
@@ -25,10 +25,12 @@
reg_test_() ->
{setup,
fun() ->
- application:start(gproc)
+ application:start(gproc),
+ application:start(mnesia)
end,
fun(_) ->
- application:stop(gproc)
+ application:stop(gproc),
+ application:stop(mnesia)
end,
[
{spawn, ?_test(t_simple_reg())}
@@ -295,7 +297,6 @@ t_get_env() ->
?assertEqual("s3", gproc:get_env(l, gproc, ssss, [{os_env,"TTTT"}])),
?assertEqual("s4", gproc:get_env(l, gproc, ssss, [{default,"s4"}])),
%%
- ?assertEqual(ok, application:start(mnesia)),
?assertEqual({atomic,ok}, mnesia:create_table(t, [{ram_copies, [node()]}])),
?assertEqual(ok, mnesia:dirty_write({t, foo, bar})),
?assertEqual(bar, gproc:get_env(l, gproc, some_env, [{mnesia,transaction,
@@ -321,7 +322,18 @@ t_set_env() ->
?assertEqual("s1", os:getenv("SSSS")),
?assertEqual(true, os:putenv("SSSS", "s0")),
?assertEqual([{self(),"s1"}],
- gproc:lookup_values({p,l,{gproc_env,gproc,ssss}})).
+ gproc:lookup_values({p,l,{gproc_env,gproc,ssss}})),
+ %%
+ ?assertEqual({atomic,ok}, mnesia:create_table(t_set_env,
+ [{ram_copies,[node()]}])),
+ ?assertEqual(ok, mnesia:dirty_write({t_set_env, a, 1})),
+ ?assertEqual(2, gproc:set_env(l, gproc, a, 2, [{mnesia,async_dirty,
+ {t_set_env,a},3}])),
+ ?assertEqual([{t_set_env,a,2}], mnesia:dirty_read({t_set_env,a})),
+ %% non-existing mnesia obj
+ ?assertEqual(3, gproc:set_env(l, gproc, b, 3, [{mnesia,async_dirty,
+ {t_set_env,b},3}])),
+ ?assertEqual([{t_set_env,b,3}], mnesia:dirty_read({t_set_env,b})).
t_get_env_inherit() ->
P = spawn_link(fun() ->
Please sign in to comment.
Something went wrong with that request. Please try again.