1 %%%-------------------------------------------------------------------
2 %%% File : etorrent_path_map.erl
3 %%% Author : Jesper Louis Andersen <>
4 %%% Description : Manipulate the #path_map table
6 %%% Created : 6 Jul 2008 by Jesper Louis Andersen <>
7 %%%-------------------------------------------------------------------
8 -module(etorrent_path_map
).
10 -include_lib("stdlib/include/qlc.hrl").
11 -include("etorrent_mnesia_table.hrl").
14 -export([select
/2, delete
/1]).
16 %%====================================================================
18 %%====================================================================
19 %%--------------------------------------------------------------------
20 %% Function: select(Path) -> Id,
22 %% Description: Bi-directionally lookup on {Path, Id} pairs. If a non-
23 %% exsisting path is selected, we add it as a side-effect
24 %%--------------------------------------------------------------------
25 select(Id
, TorrentId
) when is_integer(Id
) ->
26 [R
] = mnesia:dirty_read(path_map
, {Id
, TorrentId
}),
28 select(Path
, TorrentId
) when is_list(Path
) ->
29 case mnesia:dirty_index_read(path_map
, Path
, #path_map
.path
) of
31 Id
= etorrent_counters:next(path_map
),
32 ok
= mnesia:dirty_write(#path_map
{ id
= {Id
, TorrentId
},
36 {Id
, _TorrentId
} = R#path_map
.id
,
40 %%--------------------------------------------------------------------
41 %% Function: delete(TorrentId) -> ok
42 %% Description: Delete entries from the pathmap based on the TorrentId
43 %%--------------------------------------------------------------------
44 delete(TorrentId
) when is_integer(TorrentId
) ->
45 MatchHead
= #path_map
{ id
= {'_', TorrentId
}, _
= '_' },
46 lists:foreach(fun(Obj
) -> mnesia:dirty_delete_object(Obj
) end,
47 mnesia:dirty_select(path_map
, [{MatchHead
, [], ['$_']}])),
50 %%====================================================================
52 %%====================================================================