summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon MacMullen <simon@rabbitmq.com>2012-03-21 18:11:06 +0000
committerSimon MacMullen <simon@rabbitmq.com>2012-03-21 18:11:06 +0000
commit753c485a48d8f8a6ac3ebf391ad025f52750e7e5 (patch)
tree356d73660e0ed2d66762a744eda18711b3956f16
parenta32778828783ff861092e5615dcfb9377c3d0c01 (diff)
downloadrabbitmq-server-git-753c485a48d8f8a6ac3ebf391ad025f52750e7e5.tar.gz
lookup/3.
-rw-r--r--src/rabbit_cluster_config.erl33
1 files changed, 25 insertions, 8 deletions
diff --git a/src/rabbit_cluster_config.erl b/src/rabbit_cluster_config.erl
index 7fd963e8e8..50680ffe72 100644
--- a/src/rabbit_cluster_config.erl
+++ b/src/rabbit_cluster_config.erl
@@ -18,7 +18,9 @@
-include("rabbit.hrl").
--export([set/3, clear/2, list/0, info_keys/0]).
+-export([set/3, clear/2, list/0, lookup/3, info_keys/0]).
+
+-define(TABLE, rabbit_cluster_config).
%%---------------------------------------------------------------------------
@@ -29,11 +31,7 @@ set(AppName, Key, Value) ->
Module:validate(Key, Term),
ok = rabbit_misc:execute_mnesia_transaction(
fun () ->
- ok = mnesia:write(
- rabbit_cluster_config,
- #cluster_config{key = {AppName, Key},
- value = Term},
- write)
+ ok = mnesia:write(?TABLE, c(AppName, Key, Term), write)
end),
Module:notify(Key, Term),
ok.
@@ -41,16 +39,35 @@ set(AppName, Key, Value) ->
clear(AppName, Key) ->
rabbit_misc:execute_mnesia_transaction(
fun () ->
- ok = mnesia:delete(rabbit_cluster_config, {AppName, Key}, write)
+ ok = mnesia:delete(?TABLE, {AppName, Key}, write)
end).
list() ->
- All = rabbit_misc:dirty_read_all(rabbit_cluster_config),
+ All = rabbit_misc:dirty_read_all(?TABLE),
[[{app_name, AppName},
{key, Key},
{value, Value}] || #cluster_config{key = {AppName, Key},
value = Value} <- All].
+lookup(AppName, Key, Default) ->
+ case mnesia:dirty_read(?TABLE, {AppName, Key}) of
+ [] -> lookup_missing(AppName, Key, Default);
+ [R] -> R#cluster_config.value
+ end.
+
+lookup_missing(AppName, Key, Default) ->
+ rabbit_misc:execute_mnesia_transaction(
+ fun () ->
+ case mnesia:read(?TABLE, {AppName, Key}) of
+ [] -> mnesia:write(?TABLE, c(AppName, Key, Default), write),
+ Default;
+ [R] -> R#cluster_config.value
+ end
+ end).
+
+c(AppName, Key, Default) -> #cluster_config{key = {AppName, Key},
+ value = Default}.
+
info_keys() -> [app_name, key, value].
%%---------------------------------------------------------------------------