Data-SortedSet-Shared
view release on metacpan or search on metacpan
lib/Data/SortedSet/Shared/Strings.pm view on Meta::CPAN
my @top = $z->rev_range_by_rank(0, 9); # ("alice", "bob", ...)
my $score = $z->score("alice");
my ($who, $sc) = $z->pop_min; # remove + return the lowest
=head1 DESCRIPTION
A string-keyed sorted set in shared memory: the same API as
L<Data::SortedSet::Shared> but with B<string members> instead of int64 ids. It is
a thin layer composing two shared structures -- a L<Data::SortedSet::Shared> for
the (id, score) ordering and a L<Data::Intern::Shared> mapping each string key to
a dense id. Keys are interned on the way in and decoded back to strings on the way
out.
Because both backing stores live in shared memory, the set works B<across
processes>: every process resolves a key to the same id, so a string-keyed
leaderboard, priority queue, or rate limiter can be shared by many workers.
Ties among equal scores break by interning id (roughly insertion order of
first-seen keys), B<not> lexicographically. Keys are interned by byte content and
stay interned until C<clear> (see L<Data::Intern::Shared/LIMITS>). B<Linux-only>, 64-bit Perl.
t/06-strings.t view on Meta::CPAN
is_deeply [$z->peek_max], ['bob', 1000], 'peek_max';
my $st = $z->stats;
ok exists $st->{set} && exists $st->{keys}, 'stats has set + keys sections';
is $st->{set}{count}, $z->count, 'stats set count matches';
cmp_ok $st->{keys}{count}, '>=', $z->count, 'stats keys count >= members (permanent interning)';
my @each;
$z->each(sub { push @each, [@_] });
is scalar(@each), $z->count, 'each visits every member';
is_deeply [map { $_->[0] } @each], [qw(carol bob)], 'each in score order, decoded to strings';
ok $z->remove("bob"), 'remove returns true';
ok !$z->exists("bob"), 'removed';
ok !$z->remove("bob"), 'remove absent returns false';
# add_many + clear
is $z->add_many([ ['m1', 5], ['m2', 6], 'bad', ['m3', 7] ]), 3, 'add_many counts new (skips malformed)';
$z->clear;
is $z->count, 0, 'clear empties the set';
ok !$z->exists("carol"), 'clear forgot the keys too';
( run in 1.300 second using v1.01-cache-2.11-cpan-600a1bdf6e4 )