Feng Lee 9 лет назад
Родитель
Сommit
a1ed02499c
2 измененных файлов с 21 добавлено и 19 удалено
  1. 10 9
      include/emqttd.hrl
  2. 11 10
      src/emqttd_router.erl

+ 10 - 9
include/emqttd.hrl

@@ -37,22 +37,23 @@
 -define(SHARE,  <<"$share/">>). %% Shared Topic
 
 %%--------------------------------------------------------------------
-%% MQTT Topic
+%% PubSub
 %%--------------------------------------------------------------------
--record(mqtt_topic, {
-            topic   :: binary(),
-                flags   :: [retained | static]
-            }).
 
--type mqtt_topic() :: #mqtt_topic{}.
+-type(pubsub() :: publish | subscribe).
+
+-define(PUBSUB(PS), (PS =:= publish orelse PS =:= subscribe)).
 
 %%--------------------------------------------------------------------
-%% PubSub
+%% MQTT Topic
 %%--------------------------------------------------------------------
 
--type(pubsub() :: publish | subscribe).
+-record(mqtt_topic, {
+    topic      :: binary(),
+    flags = [] :: [retained | static]
+}).
 
--define(PUBSUB(PS), (PS =:= publish orelse PS =:= subscribe)).
+-type(mqtt_topic() :: #mqtt_topic{}).
 
 %%--------------------------------------------------------------------
 %% MQTT Client

+ 11 - 10
src/emqttd_router.erl

@@ -53,20 +53,19 @@
 %%--------------------------------------------------------------------
 
 mnesia(boot) ->
+    ok = emqttd_mnesia:create_table(mqtt_topic, [
+                {ram_copies, [node()]},
+                {record_name, mqtt_topic},
+                {attributes, record_info(fields, mqtt_topic)}]),
     ok = emqttd_mnesia:create_table(mqtt_route, [
                 {type, bag},
                 {ram_copies, [node()]},
                 {record_name, mqtt_route},
-                {attributes, record_info(fields, mqtt_route)}]),
-
-    ok = emqttd_mnesia:create_table(topic, [
-                {ram_copies, [node()]},
-                {record_name, mqtt_topic},
-                {attributes, record_info(fields, mqtt_topic)}]);
+                {attributes, record_info(fields, mqtt_route)}]);
 
 mnesia(copy) ->
-    ok = emqttd_mnesia:copy_table(mqtt_route, ram_copies),
-    ok = emqttd_mnesia:copy_table(topic).
+    ok = emqttd_mnesia:copy_table(topic),
+    ok = emqttd_mnesia:copy_table(mqtt_route, ram_copies).
 
 %%--------------------------------------------------------------------
 %% Start the Router
@@ -126,7 +125,8 @@ add_route_(Route = #mqtt_route{topic = Topic}) ->
                 true  -> emqttd_trie:insert(Topic);
                 false -> ok
             end,
-            mnesia:write(Route);
+            mnesia:write(Route),
+            mnesia:write(#mqtt_topic{topic = Topic});
         Records ->
             case lists:member(Route, Records) of
                 true  -> ok;
@@ -164,7 +164,8 @@ del_route_(Route = #mqtt_route{topic = Topic}) ->
             case emqttd_topic:wildcard(Topic) of
                 true  -> emqttd_trie:delete(Topic);
                 false -> ok
-            end;
+            end,
+            mnesia:delete({mqtt_topic, Topic});
         _More ->
             %% Remove route only
             mnesia:delete_object(Route)