line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
21
|
|
|
21
|
|
128125
|
use strict; |
|
21
|
|
|
|
|
58
|
|
|
21
|
|
|
|
|
717
|
|
2
|
21
|
|
|
21
|
|
113
|
use warnings; |
|
21
|
|
|
|
|
41
|
|
|
21
|
|
|
|
|
1202
|
|
3
|
|
|
|
|
|
|
package MetaCPAN::Client::Scroll; |
4
|
|
|
|
|
|
|
# ABSTRACT: A MetaCPAN::Client scroller |
5
|
|
|
|
|
|
|
$MetaCPAN::Client::Scroll::VERSION = '2.028000'; |
6
|
21
|
|
|
21
|
|
673
|
use Moo; |
|
21
|
|
|
|
|
11862
|
|
|
21
|
|
|
|
|
225
|
|
7
|
21
|
|
|
21
|
|
8129
|
use Carp; |
|
21
|
|
|
|
|
54
|
|
|
21
|
|
|
|
|
1288
|
|
8
|
21
|
|
|
21
|
|
131
|
use Ref::Util qw< is_hashref >; |
|
21
|
|
|
|
|
58
|
|
|
21
|
|
|
|
|
1062
|
|
9
|
21
|
|
|
21
|
|
1101
|
use JSON::MaybeXS qw< decode_json encode_json >; |
|
21
|
|
|
|
|
14596
|
|
|
21
|
|
|
|
|
1348
|
|
10
|
|
|
|
|
|
|
|
11
|
21
|
|
|
21
|
|
8868
|
use MetaCPAN::Client::Types qw< Str Int Time ArrayRef HashRef Bool >; |
|
21
|
|
|
|
|
76
|
|
|
21
|
|
|
|
|
16579
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
has ua => ( |
14
|
|
|
|
|
|
|
is => 'ro', |
15
|
|
|
|
|
|
|
required => 1, |
16
|
|
|
|
|
|
|
); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
has size => ( |
19
|
|
|
|
|
|
|
is => 'ro', |
20
|
|
|
|
|
|
|
isa => Str, |
21
|
|
|
|
|
|
|
); |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
has time => ( |
24
|
|
|
|
|
|
|
is => 'ro', |
25
|
|
|
|
|
|
|
isa => Time, |
26
|
|
|
|
|
|
|
); |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
has base_url => ( |
29
|
|
|
|
|
|
|
is => 'ro', |
30
|
|
|
|
|
|
|
isa => Str, |
31
|
|
|
|
|
|
|
required => 1, |
32
|
|
|
|
|
|
|
); |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
has type => ( |
35
|
|
|
|
|
|
|
is => 'ro', |
36
|
|
|
|
|
|
|
isa => Str, |
37
|
|
|
|
|
|
|
required => 1, |
38
|
|
|
|
|
|
|
); |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
has body => ( |
41
|
|
|
|
|
|
|
is => 'ro', |
42
|
|
|
|
|
|
|
isa => HashRef, |
43
|
|
|
|
|
|
|
required => 1, |
44
|
|
|
|
|
|
|
); |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
has _id => ( |
47
|
|
|
|
|
|
|
is => 'ro', |
48
|
|
|
|
|
|
|
isa => Str, |
49
|
|
|
|
|
|
|
); |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
has _buffer => ( |
52
|
|
|
|
|
|
|
is => 'ro', |
53
|
|
|
|
|
|
|
isa => ArrayRef, |
54
|
|
|
|
|
|
|
default => sub { [] }, |
55
|
|
|
|
|
|
|
); |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
has _done => ( |
58
|
|
|
|
|
|
|
is => 'rw', |
59
|
|
|
|
|
|
|
isa => Bool, |
60
|
|
|
|
|
|
|
default => sub { 0 }, |
61
|
|
|
|
|
|
|
); |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
has total => ( |
64
|
|
|
|
|
|
|
is => 'ro', |
65
|
|
|
|
|
|
|
isa => Int, |
66
|
|
|
|
|
|
|
); |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
has aggregations => ( |
69
|
|
|
|
|
|
|
is => 'ro', |
70
|
|
|
|
|
|
|
isa => HashRef, |
71
|
|
|
|
|
|
|
default => sub { +{} }, |
72
|
|
|
|
|
|
|
); |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub BUILDARGS { |
75
|
11
|
|
|
11
|
1
|
56815
|
my ( $class, %args ) = @_; |
76
|
11
|
|
100
|
|
|
74
|
$args{time} //= '5m'; |
77
|
11
|
|
50
|
|
|
61
|
$args{size} //= '100'; |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
my ( $ua, $base_url, $type, $body, $time, $size ) = |
80
|
11
|
|
|
|
|
72
|
@args{qw< ua base_url type body time size >}; |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
# fetch scroller from server |
83
|
|
|
|
|
|
|
|
84
|
11
|
|
|
|
|
581
|
my $res = $ua->post( |
85
|
|
|
|
|
|
|
sprintf( '%s/%s/_search?scroll=%s&size=%s', $base_url, $type, $time, $size ), |
86
|
|
|
|
|
|
|
{ content => encode_json $body } |
87
|
|
|
|
|
|
|
); |
88
|
|
|
|
|
|
|
|
89
|
11
|
50
|
|
|
|
3028096
|
if ( $res->{status} != 200 ) { |
90
|
0
|
|
|
|
|
0
|
my $msg = "failed to create a scrolled search"; |
91
|
0
|
0
|
|
|
|
0
|
$args{debug} and $msg .= "\n(" . $res->{content} . ")"; |
92
|
0
|
|
|
|
|
0
|
croak $msg; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
11
|
|
|
|
|
133692
|
my $content = decode_json $res->{content}; |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
# read response content --> object params |
98
|
|
|
|
|
|
|
|
99
|
11
|
|
|
|
|
149
|
$args{_id} = $content->{_scroll_id}; |
100
|
11
|
|
|
|
|
43
|
$args{total} = $content->{hits}{total}; |
101
|
11
|
|
|
|
|
40
|
$args{_buffer} = $content->{hits}{hits}; |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
$args{aggregations} = $content->{aggregations} |
104
|
11
|
50
|
33
|
|
|
79
|
if $content->{aggregations} and is_hashref( $content->{aggregations} ); |
105
|
|
|
|
|
|
|
|
106
|
11
|
|
|
|
|
1976
|
return \%args; |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
sub next { |
110
|
109
|
|
|
109
|
1
|
5193
|
my $self = shift; |
111
|
109
|
|
|
|
|
236
|
my $buffer = $self->_buffer; |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
# We're exhausted and will do no more. |
114
|
109
|
50
|
|
|
|
2375
|
return if $self->_done; |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
# Refill the buffer if it's empty. |
117
|
109
|
100
|
|
|
|
782
|
@$buffer = @{ $self->_fetch_next } |
|
4
|
|
|
|
|
25
|
|
118
|
|
|
|
|
|
|
unless @$buffer; |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
# Grab the next result from the buffer. If there's no result, then that's |
121
|
|
|
|
|
|
|
# all, folks! |
122
|
109
|
|
|
|
|
206
|
my $next = shift @$buffer; |
123
|
|
|
|
|
|
|
|
124
|
109
|
100
|
|
|
|
370
|
$self->_done(1) unless $next; |
125
|
|
|
|
|
|
|
|
126
|
109
|
|
|
|
|
357
|
return $next; |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
sub _fetch_next { |
130
|
4
|
|
|
4
|
|
14
|
my $self = shift; |
131
|
|
|
|
|
|
|
|
132
|
4
|
|
|
|
|
154
|
my $res = $self->ua->post( |
133
|
|
|
|
|
|
|
sprintf( '%s/_search/scroll?scroll=%s&size=%s', $self->base_url, $self->time, $self->size ), |
134
|
|
|
|
|
|
|
{ content => $self->_id } |
135
|
|
|
|
|
|
|
); |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
croak "failed to fetch next scrolled batch" |
138
|
4
|
50
|
|
|
|
525140
|
unless $res->{status} == 200; |
139
|
|
|
|
|
|
|
|
140
|
4
|
|
|
|
|
7869
|
my $content = decode_json $res->{content}; |
141
|
|
|
|
|
|
|
|
142
|
4
|
|
|
|
|
64
|
return $content->{hits}{hits}; |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
sub DEMOLISH { |
146
|
11
|
|
|
11
|
1
|
23354
|
my $self = shift; |
147
|
|
|
|
|
|
|
|
148
|
11
|
|
|
|
|
439
|
$self->ua->delete( |
149
|
|
|
|
|
|
|
sprintf( '%s/_search/scroll?scroll=%s', $self->base_url, $self->time ), |
150
|
|
|
|
|
|
|
{ content => $self->_id } |
151
|
|
|
|
|
|
|
); |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
1; |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
__END__ |