<commit_before>%%%-------------------------------------------------------------------
%%% File    : etorrent_torrent.erl
%%% Author  : <NAME> <>
%%% Description : Library for manipulating the torrent table.
%%%
%%% Created : 15 Jun 2008 by <NAME> <>
%%%-------------------------------------------------------------------
-module(etorrent_torrent).

-include_lib("stdlib/include/qlc.hrl").
-include("etorrent_mnesia_table.hrl").

%% API
-export([new/3, delete/1, get_by_id/1, get_all/0, statechange/2,
	 get_num_pieces/1, decrease_not_fetched/1,
	 is_endgame/1, get_mode/1]).

%%====================================================================
%% API
%%====================================================================
%%--------------------------------------------------------------------
%% Function: new(Id, LoadData, NPieces) -> NPieces
%% Description: Initialize a torrent entry for Id with the tracker
%%   state as given. Pieces is the number of pieces for this torrent.
%% Precondition: The #piece table has been filled with the torrents pieces.
%%--------------------------------------------------------------------
new(Id, {{uploaded, U}, {downloaded, D}, {left, L}, {total, T}}, NPieces) ->
    F = fun() ->
		State = case L of 0 -> seeding; _ -> leeching end,
		mnesia:write(#torrent { id = Id,
					left = L,
					total = T,
					uploaded = U,
					downloaded = D,
					pieces = NPieces,
					state = State })
	end,
    {atomic, _} = mnesia:transaction(F),
    Missing = etorrent_piece:get_num_not_fetched(Id),
    mnesia:dirty_update_counter(torrent_c_pieces, Id, Missing).

%%--------------------------------------------------------------------
%% Function: get_mode(Id) -> seeding | endgame | leeching
%% Description: Return the current mode of the torrent.
%%--------------------------------------------------------------------
get_mode(Id) ->
    [#torrent { state = S}] = mnesia:dirty_read(torrent, Id),
    S.

%%--------------------------------------------------------------------
%% Function: delete(Id) -> transaction
%% Description: Remove the torrent identified by Id. Id is either a
%%   an integer, or the Record itself we want to remove.
%%--------------------------------------------------------------------
delete(Id) when is_integer(Id) ->
    [R] = mnesia:dirty_read(torrent, Id),
    delete(R);
delete(Torrent) when is_record(Torrent, torrent) ->
    mnesia:dirty_delete_object(Torrent).

%%--------------------------------------------------------------------
%% Function: get_by_id(Id, Pid) -> Rows
%% Description: Return the torrent identified by Id
%%--------------------------------------------------------------------
get_by_id(Id) ->
    mnesia:dirty_read(torrent, Id).

%%--------------------------------------------------------------------
%% Function: get_all() -> Rows
%% Description: Return all torrents, sorted by Id
%%--------------------------------------------------------------------
get_all() ->
    get_all(#torrent.id).

%%--------------------------------------------------------------------<commit_msg>When deleting a torrent, clean up its associated #torrent_c_pieces table.<commit_after>%%%-------------------------------------------------------------------
%%% File    : etorrent_torrent.erl
%%% Author  : <NAME> <>
%%% Description : Library for manipulating the torrent table.
%%%
%%% Created : 15 Jun 2008 by <NAME> <>
%%%-------------------------------------------------------------------
-module(etorrent_torrent).

-include_lib("stdlib/include/qlc.hrl").
-include("etorrent_mnesia_table.hrl").

%% API
-export([new/3, delete/1, get_by_id/1, get_all/0, statechange/2,
	 get_num_pieces/1, decrease_not_fetched/1,
	 is_endgame/1, get_mode/1]).

%%====================================================================
%% API
%%====================================================================
%%--------------------------------------------------------------------
%% Function: new(Id, LoadData, NPieces) -> NPieces
%% Description: Initialize a torrent entry for Id with the tracker
%%   state as given. Pieces is the number of pieces for this torrent.
%% Precondition: The #piece table has been filled with the torrents pieces.
%%--------------------------------------------------------------------
new(Id, {{uploaded, U}, {downloaded, D}, {left, L}, {total, T}}, NPieces) ->
    F = fun() ->
		State = case L of 0 -> seeding; _ -> leeching end,
		mnesia:write(#torrent { id = Id,
					left = L,
					total = T,
					uploaded = U,
					downloaded = D,
					pieces = NPieces,
					state = State })
	end,
    {atomic, _} = mnesia:transaction(F),
    Missing = etorrent_piece:get_num_not_fetched(Id),
    mnesia:dirty_update_counter(torrent_c_pieces, Id, Missing).

%%--------------------------------------------------------------------
%% Function: get_mode(Id) -> seeding | endgame | leeching
%% Description: Return the current mode of the torrent.
%%--------------------------------------------------------------------
get_mode(Id) ->
    [#torrent { state = S}] = mnesia:dirty_read(torrent, Id),
    S.

%%--------------------------------------------------------------------
%% Function: delete(Id) -> transaction
%% Description: Remove the torrent identified by Id. Id is either a
%%   an integer, or the Record itself we want to remove.
%%--------------------------------------------------------------------
delete(Id) when is_integer(Id) ->
    [R] = mnesia:dirty_read(torrent, Id),
    delete(R);
delete(Torrent) when is_record(Torrent, torrent) ->
    mnesia:dirty_delete(torrent_c_pieces, Torrent#torrent.id),
    mnesia:dirty_delete_object(Torrent).

%%--------------------------------------------------------------------
%% Function: get_by_id(Id, Pid) -> Rows
%% Description: Return the torrent identified by Id
%%--------------------------------------------------------------------
get_by_id(Id) ->
    mnesia:dirty_read(torrent, Id).

%%--------------------------------------------------------------------
%% Function: get_all() -> Rows
%% Description: Return all torrents, sorted by Id
%%--------------------------------------------------------------------
get_all() ->
    get_all(#torrent.id).

%%--------------------------------------------------------------------
{'fraction_non_alphanumeric': 0.4297838270616493, 'fraction_numerical': 0.005124099279423539, 'mean_word_length': 1.3876146788990826, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6259010', 'n_tokens_mistral': 1678, 'n_tokens_neox': 1529, 'n_words': 401}