Handle HAVE_ALL and HAVE_NONE. Cleanup the BITFIELD message.
[etorrent.git] / lib / etorrent-1.0 / src / etorrent_piece_diskstate.erl
blob5599aa52f59ba96dc10c16166979fa4542b2c017
1 %%%-------------------------------------------------------------------
2 %%% File : etorrent_piece_diskstate.erl
3 %%% Author : Jesper Louis Andersen <jlouis@ogre.home>
4 %%% Description : Manipulation of the #piece_diskstate table.
5 %%%
6 %%% Created : 16 Jul 2008 by Jesper Louis Andersen <jlouis@ogre.home>
7 %%%-------------------------------------------------------------------
8 -module(etorrent_piece_diskstate).
10 -include_lib("stdlib/include/qlc.hrl").
11 -include("etorrent_mnesia_table.hrl").
13 %% API
14 -export([new/2, prune/1, select/1]).
16 %%====================================================================
17 %% API
18 %%====================================================================
19 %%--------------------------------------------------------------------
20 %% Function:
21 %% Description:
22 %%--------------------------------------------------------------------
24 %%--------------------------------------------------------------------
25 %% Function: new/2 -> ok
26 %% Args: Filename ::= string() (filename of torrent)
27 %% State ::= seeding | {bitfield, BF}
29 %% BF ::= binary() (bitfield)
30 %% Description: Add/Update the Filenames persistent disk state
31 %%--------------------------------------------------------------------
32 new(Filename, State) ->
33 mnesia:dirty_write(#piece_diskstate { filename = Filename,
34 state = State }).
36 %%--------------------------------------------------------------------
37 %% Function: prune/1 -> ok
38 %% Args: Filenames ::= set() (of filenames)
39 %% Description: Prune the table for any entry not in the FN set.
40 %%--------------------------------------------------------------------
41 prune(Filenames) ->
42 {atomic, Kill} =
43 mnesia:transaction(
44 fun () ->
45 Q = qlc:q([E#piece_diskstate.filename
46 || E <- mnesia:table(piece_diskstate),
47 sets:is_element(E#piece_diskstate.filename,
48 Filenames)]),
49 qlc:e(Q)
50 end),
51 lists:foreach(fun (T) -> mnesia:dirty_delete(piece_diskstate, T) end,
52 Kill),
53 ok.
55 %%--------------------------------------------------------------------
56 %% Function: select/1
57 %% Args: Filename ::= string()
58 %% Description: Select the row matching the filename
59 %%--------------------------------------------------------------------
60 select(FN) ->
61 mnesia:dirty_read(piece_diskstate, FN).
63 %%====================================================================
64 %% Internal functions
65 %%====================================================================