File Coverage

blib/lib/MongoDB/Op/_KillCursors.pm
Criterion Covered Total %
statement 21 55 38.1
branch 0 10 0.0
condition n/a
subroutine 7 11 63.6
pod 0 1 0.0
total 28 77 36.3


line stmt bran cond sub pod time code
1             # Copyright 2014 - present MongoDB, Inc.
2             #
3             # Licensed under the Apache License, Version 2.0 (the "License");
4             # you may not use this file except in compliance with the License.
5             # You may obtain a copy of the License at
6             #
7             # http://www.apache.org/licenses/LICENSE-2.0
8             #
9             # Unless required by applicable law or agreed to in writing, software
10             # distributed under the License is distributed on an "AS IS" BASIS,
11             # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12             # See the License for the specific language governing permissions and
13             # limitations under the License.
14              
15 59     59   409 use strict;
  59         131  
  59         2958  
16 59     59   305 use warnings;
  59         116  
  59         2209  
17             package MongoDB::Op::_KillCursors;
18              
19             # Encapsulate a cursor kill operation; returns true
20              
21 59     59   316 use version;
  59         112  
  59         324  
22             our $VERSION = 'v2.2.1';
23              
24 59     59   4499 use Moo;
  59         127  
  59         395  
25              
26 59     59   19448 use MongoDB::_Protocol;
  59         135  
  59         1430  
27 59         465 use Types::Standard qw(
28             ArrayRef
29 59     59   313 );
  59         115  
30              
31 59     59   46555 use namespace::clean;
  59         123  
  59         366  
32              
33             has cursor_ids => (
34             is => 'ro',
35             required => 1,
36             isa => ArrayRef,
37             );
38              
39             with $_ for qw(
40             MongoDB::Role::_CollectionOp
41             MongoDB::Role::_DatabaseOp
42             MongoDB::Role::_PrivateConstructor
43             );
44              
45             sub execute {
46 0     0 0   my ( $self, $link ) = @_;
47              
48 0 0         if ( $link->supports_query_commands ) {
49             # Spec says that failures should be ignored: cursor kills often happen
50             # via destructors and users can't do anything about failure anyway.
51 0           eval {
52 0           MongoDB::Op::_Command->_new(
53             db_name => $self->db_name,
54             query => [
55             killCursors => $self->coll_name,
56             cursors => $self->cursor_ids,
57             ],
58             query_flags => {},
59             bson_codec => $self->bson_codec,
60             session => $self->session,
61             monitoring_callback => $self->monitoring_callback,
62             )->execute($link);
63             };
64             }
65             # Server never sends a reply, so ignoring failure here is automatic.
66             else {
67             my ($msg, $request_id) = MongoDB::_Protocol::write_kill_cursors(
68 0           @{ $self->cursor_ids },
  0            
69             );
70              
71 0           my $start_event;
72 0 0         $start_event = $self->_legacy_publish_command_started(
73             $link,
74             $request_id,
75             ) if $self->monitoring_callback;
76 0           my $start = time;
77              
78 0           eval {
79 0           $link->write($msg);
80             };
81              
82 0           my $duration = time - $start;
83 0 0         if (my $err = $@) {
84 0 0         $self->_legacy_publish_command_exception(
85             $start_event,
86             $duration,
87             $err,
88             ) if $self->monitoring_callback;
89 0           die $err;
90             }
91              
92 0 0         $self->_legacy_publish_command_reply($start_event, $duration)
93             if $self->monitoring_callback;
94             }
95              
96 0           return 1;
97             }
98              
99             sub _legacy_publish_command_started {
100 0     0     my ($self, $link, $request_id) = @_;
101              
102 0           my %cmd;
103 0           tie %cmd, "Tie::IxHash", (
104             killCursors => $self->coll_name,
105             cursors => $self->cursor_ids,
106             );
107              
108 0           my $event = {
109             type => 'command_started',
110             databaseName => $self->db_name,
111             commandName => 'killCursors',
112             command => \%cmd,
113             requestId => $request_id,
114             connectionId => $link->address,
115             };
116              
117 0           eval { $self->monitoring_callback->($event) };
  0            
118              
119 0           return $event;
120             }
121              
122             sub _legacy_publish_command_exception {
123 0     0     my ($self, $start_event, $duration, $err) = @_;
124              
125             my $event = {
126             type => 'command_failed',
127             databaseName => $start_event->{databaseName},
128             commandName => $start_event->{commandName},
129             requestId => $start_event->{requestId},
130             connectionId => $start_event->{connectionId},
131 0           durationSecs => $duration,
132             reply => {},
133             failure => "$err",
134             eval_error => $err,
135             };
136              
137 0           eval { $self->monitoring_callback->($event) };
  0            
138              
139 0           return;
140             }
141              
142             sub _legacy_publish_command_reply {
143 0     0     my ($self, $start_event, $duration) = @_;
144              
145             my $event = {
146             type => 'command_succeeded',
147             databaseName => $start_event->{databaseName},
148             commandName => $start_event->{commandName},
149             requestId => $start_event->{requestId},
150             connectionId => $start_event->{connectionId},
151 0           durationSecs => $duration,
152             reply => {
153             ok => 1,
154             cursorsUnknown => $self->cursor_ids,
155             },
156             };
157              
158 0           eval { $self->monitoring_callback->($event) };
  0            
159              
160 0           return;
161             }
162              
163             1;