Algorithm-MinPerfHashTwoLevel
view release on metacpan or search on metacpan
lib/Tie/Hash/MinPerfHashTwoLevel/OnDisk.pm view on Meta::CPAN
my ($file, %opts)= @_;
tie my %tied, __PACKAGE__, $file, %opts;
return \%tied;
}
sub mph2l_make_file {
my ($file,%opts)= @_;
return __PACKAGE__->make_file(file => $file, %opts);
}
sub mph2l_validate_file {
my ($file, %opts)= @_;
return __PACKAGE__->validate_file(file => $file, %opts);
}
sub new {
my ($class, %opts)= @_;
$opts{flags} ||= 0;
$opts{flags} |= MPH_F_VALIDATE if $opts{validate};
my $error;
my $mount= mount_file($opts{file},$error,$opts{flags});
my $error_rsv= delete $opts{error_rsv};
if ($error_rsv) {
$$error_rsv= $error;
}
if (!defined($mount)) {
if ($error_rsv) {
return;
} else {
lib/Tie/Hash/MinPerfHashTwoLevel/OnDisk.pm view on Meta::CPAN
or die "Failed to open $tmp_file for output";
print $ofh $buf
or die "failed to print to '$tmp_file': $!";
close $ofh
or die "failed to close '$tmp_file': $!";
rename $tmp_file, $ofile
or die "failed to rename '$tmp_file' to '$ofile': $!";
return $ofile;
}
sub validate_file {
my ($class, %opts)= @_;
my $file= $opts{file}
or die "file is a mandatory option to validate_file";
my $verbose= $opts{verbose};
my ($variant,$msg);
my $error_sv;
my $self= $class->new(file => $file, flags => MPH_F_VALIDATE, error_rsv => \$error_sv);
if ($self) {
$msg= sprintf "file '%s' is a valid '%s' file\n"
. " variant: %d\n"
. " keys: %d\n"
. " hash-state: %s\n"
lib/Tie/Hash/MinPerfHashTwoLevel/OnDisk.pm view on Meta::CPAN
The list of supported variants is as follows:
5 - Xor, siphash, with inthash, 8 byte alignment, one checksum.
In version 0.15 we switched hash functions to use SipHash(1-3), which
unfortunately made supporting variants prior to 5 impossible.
=back
=item validate_file
Validate the file specified by the 'file' argument. Returns a list of
two values, 'variant' and 'message'. If the file fails validation the 'variant'
will be undef and the 'message' will contain an error message. If the file
passes validation the 'variant' will specify the variant of the file
(currently only 0 is valid), and 'message' will contain some basic information
about the file, such as how many keys it contains, the comment it was
created with, etc.
=back
lib/Tie/Hash/MinPerfHashTwoLevel/OnDisk.pm view on Meta::CPAN
=over 4
=item mph2l_tied_hashref
Simple wrapper to replace the cumbersome
tie my %hash, "Tie::Hash::MinPerfHashTwoLevel::OnDisk", $file;
with a simple sub that can be imported
my $hashref= mph2l_tied_hashref($file,$validate);
The validate flag causes MPH_F_VALIDATE validations to occur on load.
=item mph2l_make_file
Sub form of L<make_file>. Eg:
use Tie::Hash::MinPerfHashTwoLevel::OnDisk;
Tie::Hash::MinPerfHashTwoLevel::OnDisk->make_file(@args);
is identical to
use Tie::Hash::MinPerfHashTwoLevel::OnDisk qw(mph2l_make_file);
mph2l_make_file(@args);
Sub form of C<make_file()>.
=item mph2l_validate_file
Sub form of C<validate_file()>. Eg:
use Tie::Hash::MinPerfHashTwoLevel::OnDisk;
Tie::Hash::MinPerfHashTwoLevel::OnDisk->validate_file(@args);
is identical to
use Tie::Hash::MinPerfHashTwoLevel::OnDisk qw(mph2l_validate_file);
mph2l_validate_file(@args);
=back
=head2 TIED INTERFACE
my %hash;
tie %hash, "Tie::Hash::MinPerfHashTwoLevel::OnDisk", $some_file, $flags;
will setup %hash to read from the mmapped image on disk as created by make_file().
The underlying image is never altered, and copies of the keys and values are made
utf8n_to_uvchr_error|||n
utf8n_to_uvchr||5.007001|n
utf8n_to_uvuni||5.007001|
utilize|||
uvchr_to_utf8_flags||5.007003|
uvchr_to_utf8||5.007001|
uvoffuni_to_utf8_flags||5.019004|
uvuni_to_utf8_flags||5.007003|
uvuni_to_utf8||5.007001|
valid_utf8_to_uvchr|||n
validate_suid|||
variant_under_utf8_count|||n
varname|||
vcmp||5.009000|
vcroak||5.006000|
vdeb||5.007003|
vform||5.006000|
visit|||
vivify_defelem|||
vivify_ref|||
vload_module|5.006000||p
t/Corruption.t view on Meta::CPAN
substr( $data, $pos, 1, chr( ord($chr) ^ ( 1 << rand(8) ) ) );
my $fn= sprintf "$tmpdir/test_%03d.mph2l", $pos+1;
open my $ofh, ">", $fn or die "failed to open '$fn' for write: $!";
print $ofh $data;
close $ofh;
substr($data,$pos,1,$chr);
}
ok(1,"constructed files ok");
for my $pos (0 .. length($data)) {
my $fn= sprintf "$tmpdir/test_%03d.mph2l", $pos;
my $got= eval { mph2l_tied_hashref($fn,validate=>1); 1 };
my $error= $got ? "" : "Error: $@";
if ($pos) {
ok( !$got, sprintf "munging offset %d is noticed", $pos-1 );
ok( $error=~/Error: Failed to mount/, sprintf "munging offset %d produces an error of sorts", $pos-1 );
} else {
ok( $got, "loaded base image ok" );
ok ( !$error, "No error loading base image");
}
}
done_testing();
t/OnDisk.pl view on Meta::CPAN
if ($corpus_file) {
if (!-e $corpus_file and $ENV{CREATE_CORPUS}) {
require File::Copy;
File::Copy::copy($test_file,$corpus_file);
}
#use File::Copy qw(copy); copy($test_file, $corpus_file);
ok(files_eq($test_file,$corpus_file),"file is as expected ($title)");
}
ok(defined($seed_arg),"seed_arg is defined after make_file() ($title)");
is( $got_file,$test_file, "make_file returned as expected ($title)" );
my ($got_variant,$got_message)= $class->validate_file(file=>$test_file);
ok( defined $got_variant, "file validates ok ($title)")
or diag $got_message;
is( $got_variant, $variant, "file variant ok ($title)");
my %tied_hash;
tie %tied_hash, $class, $test_file;
my $scalar= scalar(%tied_hash);
ok($scalar,"scalar works");
my $obj= tied(%tied_hash);
is($obj->get_comment, $this_comment, "comment works as expected");
is($obj->get_hdr_variant, $variant, "variant is as expected");
is($obj->get_hdr_num_buckets, 0+keys %$source_hash,"num_buckets is as expected");
( run in 0.251 second using v1.01-cache-2.11-cpan-4d50c553e7e )