anchor.diff:          orddict:new(), AllSubs),
       orddict:to_list(Merged).
   
  -%% ======================================================================
  -%% Misc
  +
  +%% @doc
  +%% Gets the Pids of the Subs matching the given topic
  +%% @end
  +-spec get_matching_subs(Topic::topic()) -> [{Filter::binary(),Pid::pid()}].
  +
  +get_matching_subs(Topic) ->
  +    Patterns = mqtt_topic:explode(Topic),
  +    Spec = [{
  +        #mqtt_sub{filter = '$1', pid = '$2', _ = '_'},
  +        [{'=:=', '$1', P}],
  +        ['$2']
  +    } || P <- Patterns],
  +    mnesia:dirty_select(?SUB_RECORD, Spec).
  +
  +clear(Filter) ->
  +    claim(Filter,undefined).
  +
  +claim(Filter,Pid) ->
  +    Fun =
  +        fun() ->
  +            case mnesia:read(?SUB_RECORD,Filter,write) of
  +                [] ->
  +                    ok = mnesia:write(#mqtt_sub{filter = Filter,pid = Pid});
  +                [S = #mqtt_sub{pid = OldPid}] when OldPid =/= Pid ->
  +                    ok = mnesia:write(S#mqtt_sub{pid = Pid})
  +            end
  +        end,
  +    mnesia_do(Fun).
  +
  +get_sub(Filter) ->
  +    Spec = {
  +        #mqtt_sub{filter = '$1', pid = '$2', _ = '_'},
  +        [{'=:=', '$1', Filter}],
  +        ['$2']
  +    },
  +    case mnesia:dirty_select(?SUB_RECORD, Spec) of
  +        [] -> error;
  +        [Pid] -> {ok,Pid}
  +    end.
  +
  +
  +%% ======================================================================
  +%% PRIVATE Misc
   %% ======================================================================
   
   mnesia_do(Fun) ->
  
positive.code: -record(mqtt_sub, {
      filter,
      subs,
      topic,
      client_id,
      qos
  }).
  
  
  
negative.code: -include_lib("stdlib/include/qlc.hrl").
  
  %% API
  -export([create_tables/2, wait_for_tables/0,
      add_sub/3, remove_sub/2,
      get_matches/1, get_matching_subs/1,
      clear/1, load/1, get_sub/1]).
  
  -ifdef(TEST).
      -export([clear_tables/0,delete_tables/0]).
  