Data-Shared
view release on metacpan or search on metacpan
lib/Data/Shared.pm view on Meta::CPAN
C<freeze_all>, C<unlink_all>) and native C<unlink>, C<destroy> and
C<freeze> through C<shared_call> croak on any read-only root. A handle
taken with C<handle> is the leaf itself and carries no root flag: every
native method on it, those three included, keeps whatever permissions
that leaf's own file gives it.
C<< strict => 1 >>, off by default, requires C<new_readonly> or
immutability (PerfectHash) throughout the declared tree, including lazy
composite leaves. Unsupported backends are rejected before any handle is
opened. Raw handles expose the backend's API; filesystem permissions govern
direct writes.
=head2 $root->mount($name, $spec, @ctor_args)
Mounts a structure under C<$name> (which may itself contain C</>, for
nested namespaces; longest-prefix wins). C<$spec> may be:
=over 4
=item * a short spec: C<'hashmap'>, C<'hashmap:ii'>, C<'buffer:i64'>,
C<'queue:int'>, C<'sync:semaphore'>, C<'sortedset:strings'>, etc.
Short specs accept mixed case and resolve to lowercase. See
L<Data::Shared::Registry> for the full inventory.
=item * a full class name: C<'Data::HashMap::Shared::SS'>.
=item * a hashref: C<< { spec => 'hashmap:ss', args => [...] } >> or
with an explicit C<ctor> (for C<new_weighted>/C<new_decayed> style
constructors, also available as C<'reservoir:weighted'> /
C<'topk:decayed'>).
=item * an already-constructed handle (transient; not written to the
manifest).
=item * a coderef factory called as C<$factory-E<gt>($root, $name)>
(transient).
=item * a composite spec (arrayref or hashref with C<child>): see
L</DEEP PATHS (COMPOSITES)>.
=back
Returns the native handle (for a composite mount, a
L<Data::Shared::Node> for the mount). C<$root-E<gt>handle($path)>
also returns it, and any native method can always be reached that
way or via C<shared_call>.
Configuration arrays and hashes are copied before construction. Cycles are
rejected; a constructor argument JSON has no form for at all -- a 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 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
the loaded handle as the spec, as for any other type. C<sortedset:strings> takes
C<< mount('names', 'sortedset:strings', { max => 100_000 }) >>;
C<set>/C<keys> backing paths are auto-filled for file-backed roots.
Explicit backing paths must be absolute. Derived paths follow the root's
directory spelling and are relative exactly when that directory is.
Backing paths already owned by another mount are rejected before
construction, including aliases produced by filename sanitization
(e.g. C<a/b> and C<a__b>) and string SortedSet sidecars. Mount the
same native handle under another name to share it intentionally; an
C<unmount> that would unlink storage another mount still names is
refused, so retire the aliases first.
=head2 $root->unmount($name), $root->mounts, $root->mount_info($name)
Lifecycle and introspection for mounts. Names are canonicalized as
C<mount> canonicalizes them, so C</a/> and C<a> name one mount.
C<unmount> returns 1 when it removed a mount and 0 when there was none;
C<mount_info> returns C<undef> for an absent name, and otherwise a
hashref of C<name>, C<class>, C<key>, C<dist>, C<path_class>, C<file>,
C<transient> and C<composite>.
C<mount_names> is a synonym for C<mounts>; C<available_types> (class or
root method) lists the 39 short spec names and C<registry> returns the
L<Data::Shared::Registry> table itself.
C<unmount> retains storage in a manifest ledger; remounting the same name
adopts it. To reclaim it while serving other mounts, remount it with the
same spec and C<< unmount($name, unlink => 1) >>. C<unlink_all> also reclaims
retained storage from any process, but removes the whole store. Each retained
store costs a manifest entry; use C<< unlink => 1 >> when rotating fresh names.
C<< unmount($name, unlink => 1) >> publishes the removal and retires storage
under the same directory lock. Native C<unlink> and C<destroy> through
C<shared_call> at a mount do the same. See L</SHARING A ROOT> for what
other processes observe.
=head2 $root->dir, $root->readonly, $root->manifest_path
Accessors: the root's directory, whether it was opened read-only, and
the path of its manifest. The directory and the manifest path are both
C<undef> for an anonymous root.
=head2 $root->sync_all, $root->freeze_all, $root->unlink_all, $root->stats, $root->frozen
C<sync_all>, C<freeze_all> and C<unlink_all> visit supported handles in
mount order. Sync and freeze include a composite's parent, child index and
every registered child, open or not. C<freeze_all> counts newly sealed
handles; 0 means nothing was left to seal.
C<stats> returns a hashref keyed by mount name, containing native stats or
( run in 1.535 second using v1.01-cache-2.11-cpan-60884a8df76 )