Преглед изворни кода

refactor: no need for a exception for resource not_found state

zmstone пре 1 година
родитељ
комит
1c6ea54a17

+ 3 - 12
apps/emqx_resource/src/emqx_resource_cache.erl

@@ -17,7 +17,7 @@
 -module(emqx_resource_cache).
 
 %% CRUD APIs
--export([new/0, write/3, is_exist/1, read/1, safe_read/1, erase/1]).
+-export([new/0, write/3, is_exist/1, read/1, erase/1]).
 %% For Config management
 -export([all_ids/0, list_all/0, group_ids/1]).
 %% For health checks etc.
@@ -98,17 +98,8 @@ write(ManagerPid, Group, Data) ->
 %% @doc Read cached pieces and return a externalized map.
 %% NOTE: Do not call this in hot-path.
 %% TODO: move `group' into `resource_data()'.
--spec read(resource_id()) -> {resource_group(), resource_data()}.
+-spec read(resource_id()) -> [{resource_group(), resource_data()}].
 read(ID) ->
-    case safe_read(ID) of
-        [] ->
-            error({not_found, ID});
-        [{G, D}] ->
-            {G, D}
-    end.
-
--spec safe_read(resource_id()) -> [{resource_group(), resource_data()}].
-safe_read(ID) ->
     case ets:lookup(?RESOURCE_STATE_CACHE, ID) of
         [] ->
             [];
@@ -156,7 +147,7 @@ list_all() ->
     IDs = all_ids(),
     lists:foldr(
         fun(ID, Acc) ->
-            case safe_read(ID) of
+            case read(ID) of
                 [] ->
                     Acc;
                 [{_G, Data}] ->

+ 6 - 7
apps/emqx_resource/src/emqx_resource_manager.erl

@@ -381,17 +381,16 @@ lookup(ResId) ->
 %% @doc Lookup the group and data of a resource from the cache
 -spec lookup_cached(resource_id()) -> {ok, resource_group(), resource_data()} | {error, not_found}.
 lookup_cached(ResId) ->
-    try read_cache(ResId) of
-        {Group, Data} ->
-            {ok, Group, Data}
-    catch
-        error:{not_found, _} ->
+    case read_cache(ResId) of
+        [{Group, Data}] ->
+            {ok, Group, Data};
+        [] ->
             {error, not_found}
     end.
 
 %% @doc Check if the resource is cached.
 is_exist(ResId) ->
-    {error, not_found} =/= lookup_cached(ResId).
+    emqx_resource_cache:is_exist(ResId).
 
 %% @doc Get the metrics for the specified resource
 get_metrics(ResId) ->
@@ -732,7 +731,7 @@ handle_event(EventType, EventData, State, Data) ->
     keep_state_and_data.
 
 log_status_consistency(Status, #data{status = Status} = Data0) ->
-    {_Group, Cached} = read_cache(Data0#data.id),
+    [{_Group, Cached}] = read_cache(Data0#data.id),
     Data = data_record_to_external_map(Data0),
     log_cache_consistency(Cached, Data);
 log_status_consistency(Status, Data) ->