<commit_before>package Diversion::BlobStore;
use Moo;
use Digest::SHA1 qw(sha1_hex);
use File::Spec;
use File::Path qw(make_path);
use PerlIO::via::gzip;

has root => (
    is => "rw",
    required => 1,
);

has digest_function => (
    is => "ro",
    default => sub {
        return \&sha1_hex
    }
);

sub _fh_ro {
    my ($self, $digest) = @_;
    my $fh;
    my $f = $self->_filename($digest);
    my $f_gz = $f . ".gz";
    if (-f $f_gz) {
	open $fh, "<:via(gzip)", $f_gz;
    } elsif (-f $f) {
	open $fh, "<", $f;
    }
    return $fh;
}

sub _fh_rw {
    my ($self, $digest) = @_;
    my ($path, $f) = $self->_filename($digest);
    my $f_gz = $f . ".gz";
    return undef if -f $f_gz || -f $f;
    make_path($path) unless -d $path;
    open(my $fh, ">", $f) or die $!;
    return $fh;
}

sub _filename {
    my ($self, $digest) = @_;
    my @xs = grep { $_ ne "" } split /(....)/, $digest;
    my $x = pop @xs;
    my $path = File::Spec->catdir($self->root, @xs);
    my $file = File::Spec->catfile($path, $x);
    return ($path, $file);
}

sub put {
    my ($self, $data) = @_;
    my $digest = $self->digest_function->($data);
    if (my $fh = $self->_fh_rw($digest)) {
	print $fh $data;
    }
    return $digest;
}

sub get {
    my ($self, $digest) = @_;
    my $fh = $self->_fh_ro($digest);
    if ($fh) {
	local $/ = undef;
	return scalar <$fh>;
    }<commit_msg>change prefix dir to avoid wasting inodes.<commit_after>package Diversion::BlobStore;
use Moo;
use Digest::SHA1 qw(sha1_hex);
use File::Spec;
use File::Path qw(make_path);
use PerlIO::via::gzip;

has root => (
    is => "rw",
    required => 1,
);

has digest_function => (
    is => "ro",
    default => sub {
        return \&sha1_hex
    }
);

sub _fh_ro {
    my ($self, $digest) = @_;
    my $fh;
    my $f = $self->_filename($digest);
    my $f_gz = $f . ".gz";
    if (-f $f_gz) {
	open $fh, "<:via(gzip)", $f_gz;
    } elsif (-f $f) {
	open $fh, "<", $f;
    }
    return $fh;
}

sub _fh_rw {
    my ($self, $digest) = @_;
    my ($path, $f) = $self->_filename($digest);
    my $f_gz = $f . ".gz";
    return undef if -f $f_gz || -f $f;
    make_path($path) unless -d $path;
    open(my $fh, ">", $f) or die $!;
    return $fh;
}

sub _filename {
    my ($self, $digest) = @_;
    my $pre  = substr($digest,0,4);
    my $suf = substr($digest,4);
    my $dir = File::Spec->catdir($self->root, $pre);
    my $file = File::Spec->catfile($dir, $suf);
    return ($dir, $file);
}

sub put {
    my ($self, $data) = @_;
    my $digest = $self->digest_function->($data);
    if (my $fh = $self->_fh_rw($digest)) {
	print $fh $data;
    }
    return $digest;
}

sub get {
    my ($self, $digest) = @_;
    my $fh = $self->_fh_ro($digest);
    if ($fh) {
	local $/ = undef;
	return scalar <$fh>;
    }
{'fraction_non_alphanumeric': 0.21878373515653113, 'fraction_numerical': 0.00395825836631882, 'mean_word_length': 2.554987212276215, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 37, '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': '4090626', 'n_tokens_mistral': 1209, 'n_tokens_neox': 1144, 'n_words': 294}