<commit_before>%% -------------------------------------------------------------------
%%
%% Copyright (c) 2007-2011 Basho Technologies, Inc.  All Rights Reserved.
%%
%% -------------------------------------------------------------------

%% @doc Module to manage storage of objects and files

-module(riak_moss_delete_fsm).

-behaviour(gen_fsm).

-include("riak_moss.hrl").

-ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").

-endif.

%% API
-export([start_link/5,
         block_deleted/2]).

%% gen_fsm callbacks
-export([init/1,
         prepare/2,
         deleting/2,
         handle_event/3,
         handle_sync_event/4,
         handle_info/3,
         terminate/3,
         code_change/4]).

-record(state, {bucket :: binary(),
                key :: binary(),
                uuid :: binary(),
                manifest :: lfs_manifest(),
                riakc_pid :: pid(),
                timer_ref :: timer:tref(),
                delete_blocks_remaining :: ordsets:ordset(integer()),
                unacked_deletes=ordsets:new(),
                mani_pid :: pid(),
                all_delete_works :: list(pid()),
                free_deleters :: ordsets:new()}).

-type state() :: #state{}.

%% ===================================================================
%% Public API
%% ===================================================================

%% @doc Start a `riak_moss_delete_fsm'.
start_link(Bucket, Key, UUID, RiakcPid, Options) ->
    Args = [Bucket, Key, UUID, RiakcPid, Options],
    gen_fsm:start_link(?MODULE, Args, []).

block_deleted(Pid, BlockID) ->
    gen_fsm:send_event(Pid, {block_deleted, BlockID, self()}).

%% ====================================================================
%% gen_fsm callbacks
%% ====================================================================

init([Bucket, Key, UUID, RiakcPid, _Options]) ->
    %% TODO:
    %% gproc register
    %% or fail
    {ok, prepare, #state{bucket=Bucket,
                         key=Key,
                         uuid=UUID,
                         riakc_pid=RiakcPid},
                     0}.

prepare(timeout, State=#state{bucket=Bucket,
                              key=Key,
                              uuid=UUID,
                              riakc_pid=RiakcPid}) ->

    ManiPid = riak_moss_manifest_fsm:start_link(Bucket, Key, RiakcPid),

    %% TODO:
    %% handle the case where the manifest
    %% is not found
    {ok, Manifest} = riak_moss_manifest_fsm:get_specific_manifest(UUID),

    %% Based on the manifest,
    %% fill in the delete_blocks_remaining
    {NewManifest, BlocksToDelete} = blocks_to_delete_from_manifest(Manifest),

    %% TODO:
    %% launch the delete workers
    AllDeleteWorkers = [],

    %% start the save_manifest
    %% timer
    %% TODO:
    %% this time probably
    %% shouldn't be hardcoded,
    %% and if it is, what should
    %% it be?
    {ok, TRef} = timer:send_interval(60000, self(), save_manifest),

    {next_state, deleting, State#state{mani_pid=ManiPid,
                                       manifest=NewManifest,
                                       all_delete_works=AllDeleteWorkers,
                                       delete_blocks_remaining=BlocksToDelete,
                                       timer_ref=TRef}}.

deleting({block_deleted, BlockID, DeleterPid},<commit_msg>Start delete block servers in delete_fsm<commit_after>%% -------------------------------------------------------------------
%%
%% Copyright (c) 2007-2011 Basho Technologies, Inc.  All Rights Reserved.
%%
%% -------------------------------------------------------------------

%% @doc Module to manage storage of objects and files

-module(riak_moss_delete_fsm).

-behaviour(gen_fsm).

-include("riak_moss.hrl").

-ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").

-endif.

%% API
-export([start_link/5,
         block_deleted/2]).

%% gen_fsm callbacks
-export([init/1,
         prepare/2,
         deleting/2,
         handle_event/3,
         handle_sync_event/4,
         handle_info/3,
         terminate/3,
         code_change/4]).

-record(state, {bucket :: binary(),
                key :: binary(),
                uuid :: binary(),
                manifest :: lfs_manifest(),
                riakc_pid :: pid(),
                timer_ref :: timer:tref(),
                delete_blocks_remaining :: ordsets:ordset(integer()),
                unacked_deletes=ordsets:new(),
                mani_pid :: pid(),
                all_delete_works :: list(pid()),
                free_deleters :: ordsets:new()}).

-type state() :: #state{}.

%% ===================================================================
%% Public API
%% ===================================================================

%% @doc Start a `riak_moss_delete_fsm'.
start_link(Bucket, Key, UUID, RiakcPid, Options) ->
    Args = [Bucket, Key, UUID, RiakcPid, Options],
    gen_fsm:start_link(?MODULE, Args, []).

block_deleted(Pid, BlockID) ->
    gen_fsm:send_event(Pid, {block_deleted, BlockID, self()}).

%% ====================================================================
%% gen_fsm callbacks
%% ====================================================================

init([Bucket, Key, UUID, RiakcPid, _Options]) ->
    %% TODO:
    %% gproc register
    %% or fail
    {ok, prepare, #state{bucket=Bucket,
                         key=Key,
                         uuid=UUID,
                         riakc_pid=RiakcPid},
                     0}.

prepare(timeout, State=#state{bucket=Bucket,
                              key=Key,
                              uuid=UUID,
                              riakc_pid=RiakcPid}) ->

    ManiPid = riak_moss_manifest_fsm:start_link(Bucket, Key, RiakcPid),

    %% TODO:
    %% handle the case where the manifest
    %% is not found
    {ok, Manifest} = riak_moss_manifest_fsm:get_specific_manifest(UUID),

    %% Based on the manifest,
    %% fill in the delete_blocks_remaining
    {NewManifest, BlocksToDelete} = blocks_to_delete_from_manifest(Manifest),

    %% TODO:
    %% launch the delete workers
    AllDeleteWorkers =
    riak_moss_block_server:start_block_servers(RiakcPid,
        riak_moss_lfs_utils:delete_concurrency()),

    %% start the save_manifest
    %% timer
    %% TODO:
    %% this time probably
    %% shouldn't be hardcoded,
    %% and if it is, what should
    %% it be?
    {ok, TRef} = timer:send_interval(60000, self(), save_manifest),

    {next_state, deleting, State#state{mani_pid=ManiPid,
                                       manifest=NewManifest,
                                       all_delete_works=AllDeleteWorkers,
                                       delete_blocks_remaining=BlocksToDelete,
                                       timer_ref=TRef}}.

deleting({block_deleted, BlockID, DeleterPid},
{'fraction_non_alphanumeric': 0.2272260525157694, 'fraction_numerical': 0.007041220478216224, 'mean_word_length': 2.004847950639048, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7031503', 'n_tokens_mistral': 2065, 'n_tokens_neox': 1915, 'n_words': 366}