Data-Shared

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN

    code reference, or an object that is not a JSON::PP boolean -- makes the
    mount transient rather than refusing it. Failed mount publication
    restores the previous namespace and removes backing paths created by
    that attempt. Existing files are retained. Factories manage their own
    external side effects. Nested mount mutations inside a factory are
    rejected, and so is reopening the same root: the factory runs while its
    caller holds the manifest lock, which no second root in the same process
    can acquire. Use the $root it is given.

    Mount arguments must survive manifest replay, so a value JSON would
    record as something else is refused at mount time instead: "Inf", "NaN"
    and lone surrogates, a stringified "Inf" or "NaN" number included; a
    plain string such as 'Inf' is kept. Strings are recorded as bytes; a
    character string (perl's UTF8 flag on) is encoded to UTF-8 first,
    uniformly from U+0080 up, so "\xe9" after "utf8::upgrade" and the plain
    byte "\xe9" name two different files.

    Special cases: "perfecthash" is an immutable build-then-load structure,
    so a 'perfecthash' spec takes either a built file path or a "{ build_int
    => \@keys, path => $file }" / "build_str" hashref. A build spec creates
    a missing file once; an existing file is loaded without rebuilding. To
    supply one some other way, pass the factory or the loaded handle as the

lib/Data/Shared.pm  view on Meta::CPAN

transient rather than refusing it. Failed mount publication restores the
previous namespace and removes backing paths created by that attempt.
Existing files are retained. Factories manage their own external side
effects. Nested mount mutations inside a factory are rejected, and so is
reopening the same root: the factory runs while its caller holds the
manifest lock, which no second root in the same process can acquire. Use
the C<$root> it is given.

Mount arguments must survive manifest replay, so a value JSON would
record as something else is refused at mount time instead: C<Inf>,
C<NaN> and lone surrogates, a stringified C<Inf> or C<NaN> number
included; a plain string such as C<'Inf'> is kept. Strings are recorded
as bytes; a character string (perl's UTF8 flag on) is encoded to UTF-8
first, uniformly from U+0080 up, so C<"\xe9"> after C<utf8::upgrade> and
the plain byte C<"\xe9"> name two different files.

Special cases: C<perfecthash> is an immutable build-then-load structure,
so a C<'perfecthash'> spec takes either a built file path or a
C<< { build_int => \@keys, path => $file } >> / C<build_str> hashref. A
build spec creates a missing file once; an existing file is loaded
without rebuilding. To supply one some other way, pass the factory or

t/31_manifest_codec.t  view on Meta::CPAN

        $root->freeze_all;
    }
    my $ro = eval { Data::Shared->open_readonly($dir) };
    ok $ro, 'a frozen store with a byte path opens read-only' or diag $@;
    is eval { shared_get($ro, '/x/k') }, 'v', 'and reads back' if $ro;
};

subtest 'an unrepresentable mount argument is refused at mount time' => sub {
    plan skip_all => $HASHMAP if $HASHMAP;
    my @bad = (
        ['NaN capacity', sub { $_[0]->mount('x', 'hashmap:ss', undef, 'NaN' + 0) },
         qr/not a serializable mount argument/],
        ['Inf capacity', sub { $_[0]->mount('x', 'hashmap:ss', undef, 'Inf' + 0) },
         qr/not a serializable mount argument/],
        ['lone surrogate in a path', sub {
            no warnings 'utf8';
            $_[0]->mount('x', 'hashmap:ss', $_[0]->dir . '/a' . chr(0xD800) . '.shm', 100) },
         qr/not representable in a manifest/],
    );
    for my $case (@bad) {
        my ($label, $code, $like) = @$case;

t/31_manifest_codec.t  view on Meta::CPAN

    for my $value (1e15, 1000000000000000, 2**50) {
        my $dir = tempdir(CLEANUP => 1);
        my $root = Data::Shared->new($dir);
        $root->mount('h', 'histogram', undef, 1, $value, 3);
        ok eval { $root->mount('m', 'hashmap:ss', undef, 100); 1 },
            "$value: a later mount still works" or diag $@;
        ok eval { Data::Shared->open($dir); 1 }, "$value: the store reopens";
    }
};

subtest 'only a real Inf or NaN is refused, not a string that looks like one' => sub {
    # The unused `+ 0` and "$x" copies set flags on their source: before perl
    # 5.40 a numified string and a stringified real Inf carry identical flags.
    my $looks_numeric = 'inf';
    my $forced = $looks_numeric + 0;
    my ($kept) = Data::Shared::Manifest::snapshot($looks_numeric);
    is $kept, 'inf', 'a string used numerically is kept as a string';
    my $lower_nan = 'nan';
    my $forced_nan = $lower_nan + 0;
    my ($kept_nan) = Data::Shared::Manifest::snapshot($lower_nan);
    is $kept_nan, 'nan', 'and so is one that numifies to NaN';
    # Nothing separates perl's own spelling from a stringified real Inf.
    my $canonical = 'Inf';
    my $forced_canon = $canonical + 0;
    ok !eval { Data::Shared::Manifest::snapshot($canonical); 1 },
        "a string spelled exactly as perl spells Inf is refused with it";
    for my $label ('nan', 'NaN.shm', 'Infinity.shm', '-inf') {
        my ($keep) = Data::Shared::Manifest::snapshot($label);
        is $keep, $label, "the string '$label' is kept";
    }
    my $real_inf = 'Inf' + 0;
    my $stringified = "$real_inf";
    ok !eval { Data::Shared::Manifest::snapshot($real_inf); 1 },
        'a real Inf is refused even after being stringified';
    ok !eval { Data::Shared::Manifest::snapshot('NaN' + 0); 1 }, 'and a real NaN';
    my $rounded = 9**9**9;
    my $rounded_pv = "$rounded";
    my $rounded_nan = 'NaN' + 0;
    my $nan_pv = "$rounded_nan";
    my $lookalike = 'inf';
    my $lookalike_nv = $lookalike + 0;
    ok !eval { Data::Shared::Manifest::snapshot($rounded); 1 },
        'an Inf written as 9**9**9 and printed is refused too';
    ok !eval { Data::Shared::Manifest::snapshot($rounded_nan); 1 }, 'and such a NaN';
    my ($still) = Data::Shared::Manifest::snapshot($lookalike);
    is $still, 'inf', 'while the lookalike string is still kept';
};

subtest 'an upgraded string argument is published as a string' => sub {
    my $canonical = 'Inf';
    my $forced = $canonical + 0;
    utf8::upgrade($canonical);
    ok !eval { Data::Shared::Manifest::snapshot($canonical); 1 },
        'an upgraded canonical Inf spelling is refused like the downgraded one';



( run in 1.136 second using v1.01-cache-2.11-cpan-60884a8df76 )