AnyEvent-KVStore-Etcd
view release on metacpan or search on metacpan
lib/AnyEvent/KVStore/Etcd.pm view on Meta::CPAN
=head2 exists
Checks to see if a key exists. Here this is no less costly than read.
=cut
sub exists($$) {
my ($self, $key) = @_;
my $value = $self->cnx->range({key => $key })->{response}->{content};
$value = decode_json($value)->{kvs}->[0]->{value};
return defined $value;;
}
=head2 list($pfx)
Returns a list of keys
=cut
# adds one to the binary representation of the string for prefix searches
sub _add_one($){
my ($str) = @_;
if ($str =~ /^\xff*$/){ # for empty string too
return "\x00";
}
my $inc = $str;
$inc =~ s/([^\xff])\xff*\z/ $1 =~ tr||\x01-\xff|cr /e;
return $inc;
}
sub list($$) {
my ($self, $pfx) = @_;
my $value = $self->cnx->range({key => $pfx, range_end => _add_one($pfx)})->{response}->{content};
return map { decode_base64($_->{key} ) } @{decode_json($value)->{kvs}};
}
=head2 write($key, $value)
Writes the key to the database and returns 1 if successful, 0 if not.
=cut
sub write($$$) {
my ($self, $key, $value) = @_;
return $self->cnx->put({ key => $key, value => $value })->is_success;
}
=head2 watch($pfx, $callback)
This sets up a "watch" where notifications of changed keys are passed to the
script. This can only really be handled inside an AnyEvent loop because the
changes can come from outside the program.
The callback takes the arguments of C<($key, $value)> of the new values.
=cut
sub _portability_wrapper {
my ($sub, $result) = @_;
use Data::Dumper;
for my $e (@{decode_json($result)->{result}->{events}}){
$e = $e->{kv};
&$sub(decode_base64($e->{key}), decode_base64($e->{value}));
}
}
sub watch($$$) {
my ($self, $pfx, $subroutine ) = @_;
return $self->cnx->watch({key => $pfx, range_end => _add_one($pfx)},
sub { my ($result) = @_; _portability_wrapper($subroutine, $result) })->create;
}
=head1 AUTHOR
Chris Travers, C<< <chris.travers at gmail.com> >>
=head1 BUGS
Please report any bugs or feature requests to C<bug-anyevent-kvstore-etcd at rt.cpan.org>, or through
the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=AnyEvent-KVStore-Etcd>. I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.
=head1 SUPPORT
You can find documentation for this module with the perldoc command.
perldoc AnyEvent::KVStore::Etcd
You can also look for information at:
=over 4
=item * RT: CPAN's request tracker (report bugs here)
L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=AnyEvent-KVStore-Etcd>
=item * CPAN Ratings
L<https://cpanratings.perl.org/d/AnyEvent-KVStore-Etcd>
=item * Search CPAN
L<https://metacpan.org/release/AnyEvent-KVStore-Etcd>
=back
=head1 ACKNOWLEDGEMENTS
=head1 LICENSE AND COPYRIGHT
This software is Copyright (c) 2023 by Chris Travers.
This is free software, licensed under:
( run in 1.609 second using v1.01-cache-2.11-cpan-39bf76dae61 )