blib/lib/Async/Redis/Pipeline.pm

filter: Statementstmt 19.1% 14 / 73 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 33.3% 4 / 12 Podpod 100% 6 / 6 totaltotal 19.2% 24 / 125 CC 19 SCAR 42.1
CC
19
Cov
13%
execute
156.0
run_script
20.0
_queue
12.0
CRAP 67.4
« blib/lib/Async/Redis/Iterator.pm ↑ Summary blib/lib/Async/Redis/Telemetry.pm »
1 package Async::Redis::Pipeline;
2  
3 91 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 91 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 91 ▶use 5.018;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 91 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
10 0 my ($class, %args) = @_;
11  
12 return bless {
13 redis => $args{redis},
14 commands => [],
15 executed => 0,
16 0 ▶ max_depth => $args{max_depth} // 10000,
Condition0%0/2
$args{"max_depth"} // 10000
l!l
00
MC/DC0%0/1
$args{"max_depth"} // 10000
$args{"max_depth"}
Truth table0%0/2
A
A= $args{"max_depth"}
Aresult
11
00
17 }, $class;
18 }
19  
20 # Queue a command - returns self for chaining
21 0 ▶sub _queue {
Subroutine _queue: not called CC=3 SCAR=24.8
22 0 my ($self, $cmd, @args) = @_;
23  
24 0 ▶ die "Pipeline already executed" if $self->{executed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"executed"} 0 0 0
25  
26 0 ▶ if (@{$self->{commands}} >= $self->{max_depth}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"commands"};} >= $self->{"max_depth"}) 0 0 0
27 0 die "Pipeline depth limit ($self->{max_depth}) exceeded";
28 }
29  
30 0 push @{$self->{commands}}, [$cmd, @args];
31 0 return $self;
32 }
33  
34 # Generate AUTOLOAD to capture any command call
35 91 our $AUTOLOAD;
36  
37 0 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: not called CC=2 SCAR=17.9
38 0 my $self = shift;
39 0 my $cmd = $AUTOLOAD;
40 0 $cmd =~ s/.*:://;
41 0 ▶ return if $cmd eq 'DESTROY';
Branch: 1 branch
BranchTrueFalseTotal
if $cmd eq "DESTROY" 0 0 0
42  
43 0 return $self->_queue(uc($cmd), @_);
44 }
45  
46 # Allow explicit command() calls
47 0 ▶sub command {
Subroutine command: not called CC=1 SCAR=6.9
Pod documented
48 0 my ($self, $cmd, @args) = @_;
49 0 return $self->_queue($cmd, @args);
50 }
51  
52 # Explicit add() for backwards compatibility
53 0 ▶sub add {
Subroutine add: not called CC=1 SCAR=6.9
Pod documented
54 0 my ($self, @cmd) = @_;
55 0 return $self->_queue(@cmd);
56 }
57  
58 # Queue a registered script by name
59 # Usage: $pipe->run_script('name', @keys_then_args)
60 0 ▶sub run_script {
Subroutine run_script: not called CC=4 SCAR=30.0
Pod documented
61 0 my ($self, $name, @args) = @_;
62  
63 0 ▶ die "Pipeline already executed" if $self->{executed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"executed"} 0 0 0
64 0 ▶ die "Script name required" unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 0 0
65  
66 0 ▶ if (@{$self->{commands}} >= $self->{max_depth}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"commands"};} >= $self->{"max_depth"}) 0 0 0
67 0 die "Pipeline depth limit ($self->{max_depth}) exceeded";
68 }
69  
70 # Queue with special marker - resolved at execute time
71 0 push @{$self->{commands}}, ['__SCRIPT__', $name, @args];
72 0 return $self;
73 }
74  
75 0 ▶async sub execute {
Subroutine execute: not called CC=12 SCAR=50.5
Pod documented
76 0 my ($self) = @_;
77  
78 # Mark as executed (single-use)
79 0 ▶ if ($self->{executed}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"executed"}) 0 0 0
80 0 return [];
81 }
82 0 $self->{executed} = 1;
83  
84 0 my @commands = @{$self->{commands}};
85 0 ▶ return [] unless @commands;
Branch: 1 branch
BranchTrueFalseTotal
unless @commands 0 0 0
86  
87 0 my $redis = $self->{redis};
88  
89 # Resolve __SCRIPT__ entries to EVALSHA commands
90 0 my %scripts_used;
91 0 for my $cmd (@commands) {
92 0 ▶ if ($cmd->[0] eq '__SCRIPT__') {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd->[0] eq "__SCRIPT__") 0 0 0
93 0 my (undef, $name, @args) = @$cmd;
94  
95 0 ▶ my $script = $redis->get_script($name)
Branch: 1 branch
BranchTrueFalseTotal
my $script = $redis->get_script($name) or die "Unknown script '${name}' in pipeline - use define_command() first" 0 0 0
96 or die "Unknown script '$name' in pipeline - use define_command() first";
97  
98 0 $scripts_used{$name} = $script;
99  
100 0 my $num_keys = $script->num_keys;
101 0 ▶ if ($num_keys eq 'dynamic') {
Branch: 1 branch
BranchTrueFalseTotal
if ($num_keys eq "dynamic") 0 0 0
102 0 $num_keys = shift @args;
103 0 ▶ die "Key count required as first argument for dynamic script '$name'"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $num_keys 0 0 0
104 unless defined $num_keys;
105 }
106  
107 # Convert to EVALSHA command
108 0 @$cmd = ('EVALSHA', $script->sha, $num_keys, @args);
109 }
110 }
111  
112 # Preload any scripts used (ensures EVALSHA will work)
113 0 for my $script (values %scripts_used) {
114 0 await $redis->script_load($script->script);
115 }
116  
117 # Apply key prefixing if configured
118 0 ▶ if (defined $redis->{prefix} && $redis->{prefix} ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $redis->{"prefix"} and $redis->{"prefix"} ne "") 0 0 0
Condition0%0/3
defined $redis->{"prefix"} and $redis->{"prefix"} ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $redis->{"prefix"} and $redis->{"prefix"} ne ""
defined $redis->{"prefix"} $redis->{"prefix"} ne ""
Truth table0%0/3
A and B
A= defined $redis->{"prefix"}B= $redis->{"prefix"} ne ""
ABresult
0X0
100
111
119 0 require Async::Redis::KeyExtractor;
120 0 for my $cmd (@commands) {
121 0 my ($name, @args) = @$cmd;
122 @args = Async::Redis::KeyExtractor::apply_prefix(
123 0 $redis->{prefix}, $name, @args
124 );
125 0 @$cmd = ($name, @args);
126 }
127 }
128  
129 # Execute pipeline via Redis connection
130 0 return await $redis->_execute_pipeline(\@commands);
131 91 }
132  
133 0 ▶sub count { scalar @{shift->{commands}} }
Subroutine count: not called CC=1 SCAR=6.9
Pod documented
134  
135 91 1;
136  
137 __END__
« blib/lib/Async/Redis/Iterator.pm ↑ Summary blib/lib/Async/Redis/Telemetry.pm »