Skip to content

Commit

Permalink
Merge pull request #322 from tom-binary/feature/storage_set_methods
Browse files Browse the repository at this point in the history
Support for unordered set methods
  • Loading branch information
tom-binary authored Jun 21, 2024
2 parents ffb2b65 + 665e759 commit 92ac8a7
Show file tree
Hide file tree
Showing 3 changed files with 364 additions and 3 deletions.
153 changes: 150 additions & 3 deletions lib/Myriad/Role/Storage.pm
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,27 @@ a concrete implementation - instead, see classes such as:
=cut

our @WRITE_METHODS = qw(set getset getdel incr push unshift pop shift hash_set hash_add orderedset_add orderedset_remove_member orderedset_remove_byscore del unlink set_unless_exists);
our @WRITE_METHODS = qw(
set
getset
getdel
incr
push
unshift
pop
shift
hash_set
hash_add
orderedset_add
orderedset_remove_member
orderedset_remove_byscore
unorderedset_add
unorderedset_remove
unorderedset_replace
del
unlink
set_unless_exists
);

=head2 set
Expand Down Expand Up @@ -216,7 +236,7 @@ Returns a L<Future>.

method orderedset_add;

=head2 orderedset_remove_memeber
=head2 orderedset_remove_member
Removes a member from an orderedset structure
Takes the following parameters:
Expand Down Expand Up @@ -256,6 +276,63 @@ Returns a L<Future>.

method orderedset_remove_byscore;

=head2 unorderedset_add
Adds members to a set.
Takes the following parameters:
=over 4
=item * C<< $k >> - the relative key in storage
=item * C<< $members >> - an arrayref holding zero or more members to add
=back
Returns a L<Future>.
=cut

method unorderedset_add;

=head2 unorderedset_remove
Removes members from a set.
Takes the following parameters:
=over 4
=item * C<< $k >> - the relative key in storage
=item * C<< $members >> - an arrayref holding zero or more members to remove
=back
Returns a L<Future>.
=cut

method unorderedset_remove;

=head2 unorderedset_replace
Atomically replace all members in a set.
Takes the following parameters:
=over 4
=item * C<< $k >> - the relative key in storage
=item * C<< $members >> - an arrayref holding zero or more members to form the new set
=back
Returns a L<Future>.
=cut

method unorderedset_replace;

method del;
method unlink;
method set_unless_exists;
Expand All @@ -264,7 +341,23 @@ method set_unless_exists;
=cut

our @READ_METHODS = qw(get observe watch_keyspace hash_get hash_keys hash_values hash_exists hash_count hash_as_list orderedset_member_count orderedset_members when_key_changed);
our @READ_METHODS = qw(
get
observe
watch_keyspace
hash_get
hash_keys
hash_values
hash_exists
hash_count
hash_as_list
orderedset_member_count
orderedset_members
unorderedset_is_member
unorderedset_member_count
unorderedset_members
when_key_changed
);

=head2 get
Expand Down Expand Up @@ -431,6 +524,60 @@ Returns a L<Future>.

method orderedset_members;

=head2 unorderedset_is_member
Returns true if the given key is a member in the set.
Takes the following parameters:
=over 4
=item * C<< $k >> - the relative key in storage
=item * C<< $value >> - the value to check for presence in the set
=back
Returns a L<Future>.
=cut

method unorderedset_is_member;

=head2 unorderedset_member_count
Returns the count of all members.
Takes the following parameters:
=over 4
=item * C<< $k >> - the relative key in storage
=back
Returns a L<Future>.
=cut

method unorderedset_member_count;

=head2 unorderedset_members
Returns a list of all members in the set.
Takes the following parameters:
=over 4
=item * C<< $k >> - the relative key in storage
=back
Returns a L<Future>.
=cut

method unorderedset_members;

method when_key_changed;

1;
Expand Down
100 changes: 100 additions & 0 deletions lib/Myriad/Storage/Implementation/Memory.pm
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,62 @@ async method orderedset_remove_byscore : Defer ($k, $min, $max) {
return 0 + @keys_before - @keys_after;
}

=head2 unorderedset_add
Takes the following parameters:
=over 4
=item * C<< $k >> - the relative key in storage
=item * C<< $m >> - the scalar member value
=back
Note that references are currently B<not> supported - attempts to write an arrayref, hashref
or object will fail.
Returns a L<Future> which will resolve on completion.
=cut

async method unorderedset_add : Defer ($k, $m) {
$m = [ $m ] unless ref($m) eq 'ARRAY';
die 'set member values cannot be a reference for key:' . $k . ' - ' . ref($_) for grep { ref } $m->@*;
$data{$k} = {} unless defined $data{$k};
return @{$data{$k}}{$m->@*} = ();
}

=head2 unorderedset_remove
Takes the following parameters:
=over 4
=item * C<< $k >> - the relative key in storage
=item * C<< $m >> - the scalar member value
=back
Returns a L<Future> which will resolve on completion.
=cut

async method unorderedset_remove : Defer ($k, $m) {
$m = [ $m ] unless ref($m) eq 'ARRAY';
my $keys_before = 0 + keys $data{$k}->%*;
delete @{$data{$k}}{$m->@*};
return $keys_before - keys $data{$k}->%*;
}

async method unorderedset_replace : Defer ($k, $m) {
$m = [ $m ] unless ref($m) eq 'ARRAY';
delete @{$data{$k}}{keys $data{$k}->%*};
@{$data{$k}}{$m->@*} = ();
return 0 + keys $data{$k}->%*;
}

async method unlink : Defer (@keys) {
delete @data{@keys};
$key_change->{$_}->done for grep { $key_change->{$_} } @keys;
Expand Down Expand Up @@ -548,6 +604,50 @@ async method orderedset_members : Defer ($k, $min = '-inf', $max = '+inf', $with
return [ map { ($_ >= $min and $_ <= $max ) ? $with_score ? ($data{$k}->{$_}, $_) : ($data{$k}->{$_}) : () } sort keys $data{$k}->%* ];
}

=head2 unorderedset_member_count
Takes the following parameters:
=over 4
=item * C<< $k >> - the relative key in storage
=back
Returns a L<Future> which will resolve on completion.
=cut

async method unorderedset_member_count : Defer ($k) {
return 0 + keys $data{$k}->%*;
}

=head2 unorderedset_members
Takes the following parameters:
=over 4
=item * C<< $k >> - the relative key in storage
=item * C<< $min >> - minimum score for selection
=item * C<< $max >> - maximum score for selection
=back
Returns a L<Future> which will resolve on completion.
=cut

async method unorderedset_members : Defer ($k) {
return [ keys $data{$k}->%* ];
}

async method unorderedset_is_member : Defer ($k, $m) {
return exists $data{$k}{$m};
}

1;

=head1 AUTHOR
Expand Down
Loading

0 comments on commit 92ac8a7

Please sign in to comment.