1 %%%-------------------------------------------------------------------
2 %%% File : etorrent_tracking_map.erl
3 %%% Author : Jesper Louis Andersen <>
4 %%% Description : Tracking Map manipulations
6 %%% Created : 15 Jun 2008 by Jesper Louis Andersen <>
7 %%%-------------------------------------------------------------------
8 -module(etorrent_tracking_map
).
10 -include_lib("stdlib/include/qlc.hrl").
11 -include("etorrent_mnesia_table.hrl").
14 -export([all
/0, new
/3, delete
/1, select
/1, statechange
/2,
15 is_ready_for_checking
/1]).
17 %%====================================================================
19 %%====================================================================
20 %%--------------------------------------------------------------------
21 %% Function: new(Filename, Supervisor) -> ok
22 %% Description: Add a new torrent given by File with the Supervisor
23 %% pid as given to the database structure.
24 %%--------------------------------------------------------------------
25 new(File
, Supervisor
, Id
) when is_integer(Id
), is_pid(Supervisor
), is_list(File
) ->
26 mnesia:dirty_write(#tracking_map
{ id
= Id
,
28 supervisor_pid
= Supervisor
,
32 %%--------------------------------------------------------------------
34 %% Description: Return everything we are currently tracking
35 %%--------------------------------------------------------------------
39 qlc:e(qlc:q([T
|| T
<- mnesia:table(tracking_map
)]))
42 %%--------------------------------------------------------------------
43 %% Function: select(Id) -> [#tracking_map]
44 %% Args: Id ::= integer() | {filename, FN} | {infohash, IH}
48 %% Description: Find tracking map matching the filename in question.
49 %%--------------------------------------------------------------------
50 select(Id
) when is_integer(Id
) ->
53 mnesia:read(tracking_map
, Id
, read
)
55 select({filename
,Filename
}) ->
58 Query
= qlc:q([T
|| T
<- mnesia:table(tracking_map
),
59 T#tracking_map
.filename
== Filename
]),
62 select({infohash
, InfoHash
}) ->
65 Q
= qlc:q([T
|| T
<- mnesia:table(tracking_map
),
66 T#tracking_map
.info_hash
=:= InfoHash
]),
70 %%--------------------------------------------------------------------
71 %% Function: delete(Id) -> ok
72 %% Description: Clean out all references to torrents matching Pid
73 %%--------------------------------------------------------------------
74 delete(Id
) when is_integer(Id
) ->
75 mnesia:dirty_delete(tracking_map
, Id
).
77 %%--------------------------------------------------------------------
78 %% Function: statechange(Id, What) -> ok
79 %% Description: Alter the state of the Tracking map identified by Id
80 %% by What (see alter_map/2).
81 %%--------------------------------------------------------------------
82 statechange(Id
, What
) ->
84 [TM
] = mnesia:read(tracking_map
, Id
, write
),
85 mnesia:write(alter_map(TM
, What
))
87 {atomic
, _
} = mnesia:transaction(F
),
90 %%--------------------------------------------------------------------
91 %% Function: is_ready_for_checking(Id) -> bool()
92 %% Description: Attempt to mark the torrent for checking. If this
93 %% succeeds, returns true, else false
94 %%--------------------------------------------------------------------
95 is_ready_for_checking(Id
) ->
97 Q
= qlc:q([TM
|| TM
<- mnesia:table(tracking_map
),
98 TM#tracking_map
.state
=:= checking
]),
99 case length(qlc:e(Q
)) of
101 [TM
] = mnesia:read(tracking_map
, Id
, write
),
102 mnesia:write(TM#tracking_map
{ state
= checking
}),
108 {atomic
, T
} = mnesia:transaction(F
),
111 %%====================================================================
112 %% Internal functions
113 %%====================================================================
115 alter_map(TM
, What
) ->
118 TM#tracking_map
{ info_hash
= IH
};
120 TM#tracking_map
{ state
= started
};
122 TM#tracking_map
{ state
= stopped
}