| 1 | package EV::Nats::KV; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 5 | our $VERSION = '0.07'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use EV; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use EV::Nats::JetStream; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 20 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=6 SCAR=18.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 20 | my ($class, %opts) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 20 | ▶ | my $js = delete $opts{js} || die "js (JetStream) required"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
delete $opts{'js'} || die('js (JetStream) required')
MC/DC0%0/1
delete $opts{'js'} || die('js (JetStream) required')
delete $opts{'js'}
Truth table50%1/2
A
A= delete $opts{'js'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 20 | ▶ | my $bucket = delete $opts{bucket} || die "bucket name required"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
delete $opts{'bucket'} || die('bucket name required')
MC/DC0%0/1
delete $opts{'bucket'} || die('bucket name required')
delete $opts{'bucket'}
Truth table50%1/2
A
A= delete $opts{'bucket'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | # The bucket name is interpolated into subjects verbatim; validate (nats.go rule) up front. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 20 | ▶ | die "invalid KV bucket name '$bucket'" unless $bucket =~ /\A[a-zA-Z0-9_-]+\z/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 19 | ▶ | my $timeout = delete $opts{timeout} || $js->{timeout}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
delete $opts{'timeout'} || $js->{'timeout'}
MC/DC0%0/2
delete $opts{'timeout'} || $js->{'timeout'}
delete $opts{'timeout'}
$js->{'timeout'}
Truth table66%2/3
A || B
A= delete $opts{'timeout'}B= $js->{'timeout'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 19 | warn "EV::Nats::KV::new: unknown option '$_'" for keys %opts; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 19 | bless { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | js => $js, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | bucket => $bucket, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | stream => "KV_$bucket", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | timeout => $timeout, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | }, $class; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | # Keys are interpolated into subjects verbatim: spaces are a fatal protocol | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | # error, * and > turn lookups into wildcards. Validate (nats.go rule) up front. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 28 | ▶ | sub _check_key { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_check_key: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 28 | my $key = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 28 | ▶ | die "invalid KV key '$key'" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition88%8/9
defined $key and $key =~ m[\A[-/_=.a-zA-Z0-9]+\z] and not $key =~ /\A\./ and not $key =~ /\.\z/
defined $key and $key =~ m[\A[-/_=.a-zA-Z0-9]+\z] and not $key =~ /\A\./
defined $key and $key =~ m[\A[-/_=.a-zA-Z0-9]+\z]
MC/DC75%3/4
defined $key and $key =~ m[\A[-/_=.a-zA-Z0-9]+\z] and not $key =~ /\A\./ and not $key =~ /\.\z/
defined $key
$key =~ m[\A[-/_=.a-zA-Z0-9]+\z]
not $key =~ /\A\./
not $key =~ /\.\z/
Truth table80%4/5
A and B and not(C) and not(D)
A= defined $keyB= $key =~ m[\A[-/_=.a-zA-Z0-9]+\z]C= not $key =~ /\A\./D= not $key =~ /\.\z/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | unless defined $key | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | && $key =~ /\A[-\/_=.a-zA-Z0-9]+\z/ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | && $key !~ /\A\./ && $key !~ /\.\z/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 11 | ▶ | sub get { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 11 | my ($self, $key, $cb) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 5 | ▶ | $self->get_entry($key, sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 5 | my ($entry, $err) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 5 | ▶ | $cb->($entry ? $entry->{value} : undef, $err); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 11 | }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 12 | ▶ | sub get_entry { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_entry: called CC=1 SCAR=0.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 12 | my ($self, $key, $cb) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 12 | _check_key($key); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 6 | my $subj = '$KV.' . $self->{bucket} . '.' . $key; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | $self->{js}->_json_api( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 'STREAM.MSG.GET.' . $self->{stream}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | { last_by_subj => $subj }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 6 | ▶ | sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=18.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 6 | my ($resp, $err) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 6 | ▶ | if ($err) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | # 10037 = "no message found" -- treat as missing key | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 4 | ▶ | return $cb->(undef, undef) if $err =~ /no message found|\(code 10037\)/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | return $cb->(undef, $err); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 2 | my $msg = $resp->{message}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 2 | ▶ | return $cb->(undef, undef) unless $msg; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | # DEL/PURGE tombstones surface as missing | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 2 | ▶ | return $cb->(undef, undef) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | if EV::Nats::JetStream::msg_is_tombstone($msg); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 2 | require MIME::Base64; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 2 | ▶ | my $value = MIME::Base64::decode_base64($msg->{data} || ''); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$msg->{'data'} || ''
MC/DC0%0/1
$msg->{'data'} || ''
$msg->{'data'}
Truth table50%1/2
A
A= $msg->{'data'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 2 | $cb->({ value => $value, revision => $msg->{seq} }, undef); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 6 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | ▶ | sub put { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
put: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | my ($self, $key, $value, $cb) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | _check_key($key); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | my $subj = '$KV.' . $self->{bucket} . '.' . $key; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | ▶ | $self->{js}->js_publish($subj, $value, sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | my ($ack, $err) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | ▶ | return $cb->(undef, $err) if $err; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | $cb->($ack->{seq}, undef); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 9 | ▶ | sub _expect_publish { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_expect_publish: called CC=2 SCAR=7.3
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 9 | my ($self, $subj, $headers, $value, $cb) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 9 | my $nats = $self->{js}{nats}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 9 | ▶ | return $nats->hpublish($subj, $headers, $value) unless $cb; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 7 | ▶ | $nats->hrequest($subj, $headers, $value, $self->{js}->_decoded(sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=8.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 7 | my ($ack, $err) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 7 | ▶ | $cb->($ack && $ack->{seq}, $err); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$ack && $ack->{'seq'}
MC/DC0%0/2
$ack && $ack->{'seq'}
$ack
$ack->{'seq'}
Truth table33%1/3
A && B
A= $ackB= $ack->{'seq'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 9 | }), $self->{timeout}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 5 | ▶ | sub create { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
create: called CC=2 SCAR=10.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 5 | my ($self, $key, $value, $cb) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 5 | _check_key($key); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 5 | my $subj = '$KV.' . $self->{bucket} . '.' . $key; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | $self->_expect_publish($subj, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | "NATS/1.0\r\nNats-Expected-Last-Subject-Sequence: 0\r\n\r\n", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 4 | ▶ | $value, $cb ? sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 4 | my ($seq, $err) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 4 | ▶ | return $cb->($seq, $err) unless $err && $err =~ /wrong last sequence/i; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$err and $err =~ /wrong last sequence/i
MC/DC0%0/2
$err and $err =~ /wrong last sequence/i
$err
$err =~ /wrong last sequence/i
Truth table33%1/3
A and B
A= $errB= $err =~ /wrong last sequence/i
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | $self->{js}->_json_api('STREAM.MSG.GET.' . $self->{stream}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | ▶ | { last_by_subj => $subj }, sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | my ($resp, $read_err) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | ▶ | return $cb->(undef, $read_err) if $read_err; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | my $msg = $resp->{message}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | ▶ | return $cb->(undef, $err) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | unless EV::Nats::JetStream::msg_is_tombstone($msg); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | $self->_expect_publish($subj, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | "NATS/1.0\r\nNats-Expected-Last-Subject-Sequence: $msg->{seq}\r\n\r\n", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | $value, $cb); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 5 | ▶ | } : undef); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 4 | ▶ | sub update { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
update: called CC=4 SCAR=16.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 4 | my ($self, $key, $value, $rev, $cb) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 4 | _check_key($key); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 4 | ▶ | die "invalid KV revision '" . ($rev // '') . "'" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition20%1/5
defined $rev and $rev =~ /\A[1-9][0-9]*\z/
$rev // ""
MC/DC0%0/2
defined $rev and $rev =~ /\A[1-9][0-9]*\z/
defined $rev
$rev =~ /\A[1-9][0-9]*\z/
MC/DC0%0/1
$rev // ""
$rev
Truth table33%1/3
A and B
A= defined $revB= $rev =~ /\A[1-9][0-9]*\z/
Truth table0%0/2
A
A= $rev
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | unless defined $rev && $rev =~ /\A[1-9][0-9]*\z/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 4 | my $subj = '$KV.' . $self->{bucket} . '.' . $key; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 4 | $self->_expect_publish($subj, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | "NATS/1.0\r\nNats-Expected-Last-Subject-Sequence: $rev\r\n\r\n", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | $value, $cb); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 4 | ▶ | sub delete { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
delete: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 4 | my ($self, $key, $cb) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 4 | _check_key($key); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | # KV delete = publish empty with KV-Operation: DEL header. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | # flush() ensures the tombstone reaches the server before $cb fires | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | # so a subsequent get() doesn't race the publish. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 4 | my $headers = "NATS/1.0\r\nKV-Operation: DEL\r\n\r\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 4 | my $subj = '$KV.' . $self->{bucket} . '.' . $key; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 4 | my $nats = $self->{js}{nats}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 4 | _fenced_publish($nats, $subj, $headers, $cb); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 3 | ▶ | sub purge { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
purge: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 3 | my ($self, $key, $cb) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 3 | _check_key($key); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 3 | my $headers = "NATS/1.0\r\nKV-Operation: PURGE\r\nNats-Rollup: sub\r\n\r\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 3 | my $subj = '$KV.' . $self->{bucket} . '.' . $key; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | 3 | my $nats = $self->{js}{nats}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 3 | _fenced_publish($nats, $subj, $headers, $cb); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | # Report publish and flush failures through the supplied callback. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 7 | ▶ | sub _fenced_publish { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_fenced_publish: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 7 | my ($nats, $subj, $headers, $cb) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 7 | ▶ | return $nats->hpublish($subj, $headers, '') unless $cb; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | 7 | my $ok = eval { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 7 | $nats->hpublish($subj, $headers, ''); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 3 | ▶ | $nats->flush(sub { $cb->(defined $_[0] ? undef : 1, $_[0]) }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 7 | ▶ | return if $ok; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 4 | ▶ | (my $err = $@ || 'not connected') =~ s/ at \S+ line \d+.*//s; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$@ || 'not connected'
MC/DC0%0/1
$@ || 'not connected'
$@
Truth table50%1/2
A
A= $@
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 4 | $err =~ s/\n\z//; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | 4 | ▶ | EV::Nats::JetStream::_defer($nats, sub { $cb->(undef, $err) }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 0 | ▶ | sub keys { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
keys: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | 0 | my ($self, $cb) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 0 | my $prefix = '$KV.' . $self->{bucket} . '.'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | $self->{js}->stream_info( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | $self->{stream}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | { subjects_filter => $prefix . '>' }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | 0 | ▶ | sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 0 | my ($info, $err) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 0 | ▶ | return $cb->(undef, $err) if $err; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | 0 | ▶ | my $subjects = $info->{state}{subjects} || {}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$info->{'state'}{'subjects'} || {}
MC/DC0%0/1
$info->{'state'}{'subjects'} || {}
$info->{'state'}{'subjects'}
Truth table0%0/2
A
A= $info->{'state'}{'subjects'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | 0 | my $plen = length $prefix; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | 0 | my @keys = map { substr($_, $plen) } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 0 | grep { substr($_, 0, $plen) eq $prefix } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | CORE::keys %$subjects; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | 0 | $cb->(\@keys, undef); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | 0 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | 1 | ▶ | sub watch { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
watch: called CC=4 SCAR=23.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | 1 | my ($self, $key_pattern, $cb) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | 1 | ▶ | $key_pattern //= '>'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$key_pattern //= ">"
MC/DC0%0/1
$key_pattern //= ">"
$key_pattern
Truth table50%1/2
A
A= $key_pattern
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | 1 | ▶ | die "invalid KV watch pattern '$key_pattern'" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not length $key_pattern or $key_pattern =~ /\s/
MC/DC0%0/2
not length $key_pattern or $key_pattern =~ /\s/
not length $key_pattern
$key_pattern =~ /\s/
Truth table33%1/3
not(A) or B
A= not length $key_patternB= $key_pattern =~ /\s/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | if !length $key_pattern || $key_pattern =~ /\s/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | 0 | my $subj = '$KV.' . $self->{bucket} . '.' . $key_pattern; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | 0 | ▶ | return $self->{js}{nats}->subscribe($subj, sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | 0 | my ($subject, $payload, $reply, $headers) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | 0 | my $prefix = '$KV.' . $self->{bucket} . '.'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | 0 | my $key = substr($subject, length $prefix); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | 0 | my $op = 'PUT'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | 0 | ▶ | if ($headers && $headers =~ /KV-Operation:\s*(\S+)/i) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$headers and $headers =~ /KV-Operation:\s*(\S+)/i
MC/DC0%0/2
$headers and $headers =~ /KV-Operation:\s*(\S+)/i
$headers
$headers =~ /KV-Operation:\s*(\S+)/i
Truth table0%0/3
A and B
A= $headersB= $headers =~ /KV-Operation:\s*(\S+)/i
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | 0 | $op = uc $1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | 0 | $cb->($key, $payload, $op); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | 0 | }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | 0 | ▶ | sub create_bucket { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
create_bucket: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | 0 | my ($self, $opts, $cb) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | 0 | ▶ | $opts //= {}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$opts //= {}
MC/DC0%0/1
$opts //= {}
$opts
Truth table0%0/2
A
A= $opts
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 0 | require JSON::PP; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | my $config = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | name => $self->{stream}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | subjects => ['$KV.' . $self->{bucket} . '.>'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | retention => 'limits', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | max_msgs_per_subject => $opts->{max_history} || 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | ($opts->{max_bytes} ? (max_bytes => $opts->{max_bytes}) : ()), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | ($opts->{max_age} ? (max_age => $opts->{max_age}) : ()), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | 0 | ▶ | ($opts->{replicas} ? (num_replicas => $opts->{replicas}) : ()), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition0%0/2
$opts->{'max_history'} || 1
MC/DC0%0/1
$opts->{'max_history'} || 1
$opts->{'max_history'}
Truth table0%0/2
A
A= $opts->{'max_history'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | discard => 'new', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | allow_rollup_hdrs => JSON::PP::true(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | deny_delete => JSON::PP::true(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | deny_purge => JSON::PP::false(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | 0 | ▶ | $self->{js}->stream_create($config, sub { $cb->(@_[0, 1]) }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 1 | ▶ | sub delete_bucket { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
delete_bucket: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | 1 | my ($self, $cb) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | 1 | ▶ | $self->{js}->stream_delete($self->{stream}, sub { $cb->(@_[0, 1]) }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | 0 | ▶ | sub status { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
status: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | 0 | my ($self, $cb) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 0 | ▶ | $self->{js}->stream_info($self->{stream}, sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | 0 | my ($info, $err) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | 0 | ▶ | return $cb->(undef, $err) if $err; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | $cb->({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | bucket => $self->{bucket}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | values => $info->{state}{messages}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | bytes => $info->{state}{bytes}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | history => $info->{config}{max_msgs_per_subject}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | 0 | }, undef); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | 0 | }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | 5 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | EV::Nats::KV - Key-Value store on top of NATS JetStream | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | use EV; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | use EV::Nats; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | use EV::Nats::JetStream; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | use EV::Nats::KV; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | my $nats = EV::Nats->new(host => '127.0.0.1'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | my $js = EV::Nats::JetStream->new(nats => $nats); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | my $kv = EV::Nats::KV->new(js => $js, bucket => 'config'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | $kv->create_bucket({}, sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | $kv->put('app.setting', 'on', sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | $kv->get('app.setting', sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | my ($val, $err) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | print "got: $val\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | # Live updates | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | $kv->watch('app.>', sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | my ($key, $value, $op) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | print "$op $key = $value\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | EV::run; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | A KV bucket is a JetStream stream named C<KV_E<lt>bucketE<gt>> with | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | subjects C<$KV.E<lt>bucketE<gt>.E<gt>>, history of 1, rollup headers | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | allowed, and deletes denied (purge tombstones are used instead). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | This module wraps that convention: C<put>/C<get> become single calls | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | that hide the underlying C<js_publish> + C<STREAM.MSG.GET> dance. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | Keys must match C<[-/_=.a-zA-Z0-9]+> without leading or trailing | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | dots, and are validated up front. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | All callbacks fire on the L<EV> loop, not synchronously. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | =head2 new | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | new(js => $js, bucket => $name, [timeout => $ms]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | The bucket need not exist yet; call L</create_bucket> first to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | provision it. C<timeout> defaults to the timeout of C<$js>. The | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | bucket name must match C<[a-zA-Z0-9_-]+>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | =head2 get | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | get($key, $cb) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | Fetch the latest value for C<$key>. Callback: C<($value, $err)>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | C<$value> is C<undef> if the key does not exist, or has been deleted | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | or purged (the tombstone is recognised and surfaces as a clean miss). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | =head2 get_entry | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | get_entry($key, $cb) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | Like L</get>, but the callback receives | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | C<< { value => $value, revision => $rev } >>, or C<undef> for a missing | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | key; C<revision> is what L</update> expects. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | =head2 put | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | put($key, $value, $cb) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | Set C<$key> to C<$value>. Callback: C<($seq, $err)>, where C<$seq> is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | the JetStream sequence number assigned by the server. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | =head2 create | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | create($key, $value, [$cb]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | Like C<put>, but only succeeds if C<$key> is missing. With a callback, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | deleted/purged keys can be recreated using the tombstone's revision; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | concurrent creators see a wrong-last-sequence error. Callback: C<($seq, $err)>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | Without C<$cb> the headered publish is fire-and-forget. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | =head2 update | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | update($key, $value, $rev, [$cb]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | Like C<put>, but only succeeds if C<$key>'s current revision is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | C<$rev> (compare-and-swap). Callback: C<($seq, $err)>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | =head2 delete | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | delete($key, [$cb]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | Mark C<$key> as deleted by publishing a C<KV-Operation: DEL> tombstone, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | followed by a C<flush> fence so a subsequent C<get> won't race the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | publish. Callback: C<($ok, $err)>; C<$err> is set if the connection | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | dropped before the PONG arrived, or if the client is not currently | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | connected. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | =head2 purge | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | purge($key, [$cb]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | Like C<delete>, but emits C<Nats-Rollup: sub> too -- the prior history | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | of C<$key> is rolled up and replaced by the tombstone, freeing storage. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | Callback: C<($ok, $err)>; C<$err> is set if the client is not | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | currently connected. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | =head2 keys | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | keys($cb) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | List all keys currently stored in the bucket. Callback: C<(\@keys, $err)>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | Tombstoned keys are not filtered out -- check with C<get> if needed. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | =head2 watch | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | watch($pattern, $cb) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | Subscribe to live changes. C<$pattern> is a NATS subject suffix relative | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | to the bucket (e.g. C<E<gt>> for all keys, C<app.E<gt>> for everything | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | under C<app.>); it must be non-empty and contain no whitespace. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | Callback receives C<($key, $value, $op)> where C<$op> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | is C<PUT>, C<DEL>, or C<PURGE>. Returns the underlying subscription | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | id; pass to L<EV::Nats/unsubscribe> to stop. Resubscribes on | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | reconnect; updates published while disconnected are missed. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | =head2 create_bucket | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | create_bucket(\%opts, $cb) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | Provision the underlying stream. Recognised C<\%opts>: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | =over | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | =item * C<max_history> - default 1; how many revisions to keep per key. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | =item * C<max_bytes> - bucket-wide storage cap. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | =item * C<max_age> - per-message TTL in nanoseconds. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | =item * C<replicas> - cluster replication factor. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | Callback: C<($info, $err)>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 390 | =head2 delete_bucket | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 392 | delete_bucket($cb) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 394 | Tear down the underlying stream. Callback: C<($info, $err)>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 396 | =head2 status | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 398 | status($cb) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 400 | Returns a snapshot hashref: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | { bucket => $name, values => $count, bytes => $n, history => $h } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 404 | Callback: C<(\%status, $err)>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 406 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 408 | L<EV::Nats>, L<EV::Nats::JetStream>, L<EV::Nats::ObjectStore>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 409 | L<NATS KV documentation|https://docs.nats.io/nats-concepts/jetstream/key-value-store>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 411 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||