feat(ruleeng): avoid storing whole rules in topic index
Because it doesn't really give any benefit, but wastes memory by duplication.
This commit is contained in:
parent
6b9cb06334
commit
b1defa29d7
|
@ -227,7 +227,7 @@ get_rules_for_topic(Topic) ->
|
||||||
[
|
[
|
||||||
Rule
|
Rule
|
||||||
|| M <- emqx_topic_index:matches(Topic, ?RULE_TOPIC_INDEX, [unique]),
|
|| M <- emqx_topic_index:matches(Topic, ?RULE_TOPIC_INDEX, [unique]),
|
||||||
Rule <- emqx_topic_index:get_record(M, ?RULE_TOPIC_INDEX)
|
Rule <- lookup_rule(emqx_topic_index:get_id(M))
|
||||||
].
|
].
|
||||||
|
|
||||||
-spec get_rules_with_same_event(Topic :: binary()) -> [rule()].
|
-spec get_rules_with_same_event(Topic :: binary()) -> [rule()].
|
||||||
|
@ -285,11 +285,14 @@ is_of_event_name(EventName, Topic) ->
|
||||||
|
|
||||||
-spec get_rule(Id :: rule_id()) -> {ok, rule()} | not_found.
|
-spec get_rule(Id :: rule_id()) -> {ok, rule()} | not_found.
|
||||||
get_rule(Id) ->
|
get_rule(Id) ->
|
||||||
case ets:lookup(?RULE_TAB, Id) of
|
case lookup_rule(Id) of
|
||||||
[{Id, Rule}] -> {ok, Rule#{id => Id}};
|
[Rule] -> {ok, Rule};
|
||||||
[] -> not_found
|
[] -> not_found
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
lookup_rule(Id) ->
|
||||||
|
[Rule || {_Id, Rule} <- ets:lookup(?RULE_TAB, Id)].
|
||||||
|
|
||||||
load_hooks_for_rule(#{from := Topics}) ->
|
load_hooks_for_rule(#{from := Topics}) ->
|
||||||
lists:foreach(fun emqx_rule_events:load/1, Topics).
|
lists:foreach(fun emqx_rule_events:load/1, Topics).
|
||||||
|
|
||||||
|
@ -484,7 +487,7 @@ with_parsed_rule(Params = #{id := RuleId, sql := Sql, actions := Actions}, Creat
|
||||||
do_insert_rule(#{id := Id} = Rule) ->
|
do_insert_rule(#{id := Id} = Rule) ->
|
||||||
ok = load_hooks_for_rule(Rule),
|
ok = load_hooks_for_rule(Rule),
|
||||||
ok = maybe_add_metrics_for_rule(Id),
|
ok = maybe_add_metrics_for_rule(Id),
|
||||||
true = ets:insert(?RULE_TAB, {Id, maps:remove(id, Rule)}),
|
true = ets:insert(?RULE_TAB, {Id, Rule}),
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
do_delete_rule(#{id := Id} = Rule) ->
|
do_delete_rule(#{id := Id} = Rule) ->
|
||||||
|
@ -493,10 +496,10 @@ do_delete_rule(#{id := Id} = Rule) ->
|
||||||
true = ets:delete(?RULE_TAB, Id),
|
true = ets:delete(?RULE_TAB, Id),
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
do_update_rule_index(#{id := Id, from := From} = Rule) ->
|
do_update_rule_index(#{id := Id, from := From}) ->
|
||||||
ok = lists:foreach(
|
ok = lists:foreach(
|
||||||
fun(Topic) ->
|
fun(Topic) ->
|
||||||
true = emqx_topic_index:insert(Topic, Id, Rule, ?RULE_TOPIC_INDEX)
|
true = emqx_topic_index:insert(Topic, Id, [], ?RULE_TOPIC_INDEX)
|
||||||
end,
|
end,
|
||||||
From
|
From
|
||||||
).
|
).
|
||||||
|
|
Loading…
Reference in New Issue