<commit_before>package MR::Mapper;
use Moo;
use Storable qw(nfreeze thaw);
use Time::HiRes qw(sleep);

with 'MR::Redis';
with 'MR::Daemon';

sub run {
    my ($self) = @_;
    
    MR->info( "Mapper $$ started." );
    
    my $redis = $self->redis;
    
    my %mapper;
    
    while (1) {
        my $names = $redis->hkeys('mapper');
                
        for my $name (@$names) {
            if ( !$mapper{$name} ) {
                my $code = $redis->hget( mapper => $name );
                
                MR->debug( "Got mapper for %s: %s", $name, $code );
                
                local $@;
                
                {                
                    $mapper{$name} = eval 'sub ' . $code;
                    
                    die "Failed to compile mapper for $name: $@"
                        if $@;
                }
            }
            
            $self->_run_mapper($name, $mapper{$name});
        }
    }
}

sub _run_mapper {
    my ($self, $name, $mapper) = @_;
    
    my $redis = $self->redis;
    
    return if $redis->llen( $name.'-input' ) == 0;
    
    $redis->set( $name.'-mapping', 1 );
    
    my $input = $redis->rpop( $name.'-input' );
    
    if (!defined $input) {
        $redis->set( $name.'-mapping', 0);
        return;
    }
    
    my $value = thaw($input);
    
    MR->debug( "Got input '%s'", $value->{key} );
    
    die 'Mapper is undefined? for ' . $$
        if !defined $mapper;
    
    my $mapped = $mapper->($self, $value);
    
    if (defined $mapped && defined $mapped->{key}) {
        $redis->lpush( $name.'-mapped', nfreeze($mapped) );
    }
    
    MR->debug( "Mapped is '%s'", $mapped->{key} );

    
    $redis->set( $name.'-mapping', 0 );
}

1;

<commit_msg>Fix debugging statement to not run for an undefined value<commit_after>package MR::Mapper;
use Moo;
use Storable qw(nfreeze thaw);
use Time::HiRes qw(sleep);

with 'MR::Redis';
with 'MR::Daemon';

sub run {
    my ($self) = @_;
    
    MR->info( "Mapper $$ started." );
    
    my $redis = $self->redis;
    
    my %mapper;
    
    while (1) {
        my $names = $redis->hkeys('mapper');
                
        for my $name (@$names) {
            if ( !$mapper{$name} ) {
                my $code = $redis->hget( mapper => $name );
                
                MR->debug( "Got mapper for %s: %s", $name, $code );
                
                local $@;
                
                {                
                    $mapper{$name} = eval 'sub ' . $code;
                    
                    die "Failed to compile mapper for $name: $@"
                        if $@;
                }
            }
            
            $self->_run_mapper($name, $mapper{$name});
        }
    }
}

sub _run_mapper {
    my ($self, $name, $mapper) = @_;
    
    my $redis = $self->redis;
    
    return if $redis->llen( $name.'-input' ) == 0;
    
    $redis->set( $name.'-mapping', 1 );
    
    my $input = $redis->rpop( $name.'-input' );
    
    if (!defined $input) {
        $redis->set( $name.'-mapping', 0);
        return;
    }
    
    my $value = thaw($input);
    
    MR->debug( "Got input '%s'", $value->{key} );
    
    die 'Mapper is undefined? for ' . $$
        if !defined $mapper;
    
    my $mapped = $mapper->($self, $value);
    
    if (defined $mapped && defined $mapped->{key}) {
        $redis->lpush( $name.'-mapped', nfreeze($mapped) );
        
        MR->debug( "Mapped is '%s'", $mapped->{key} );
    }
    
    $redis->set( $name.'-mapping', 0 );
}

1;


{'fraction_non_alphanumeric': 0.17681728880157171, 'fraction_numerical': 0.0033679483581251753, 'mean_word_length': 1.3775850567044696, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '458937', 'n_tokens_mistral': 1250, 'n_tokens_neox': 1214, 'n_words': 271}