File Coverage

blib/lib/Elastijk/oo.pm
Criterion Covered Total %
statement 39 99 39.3
branch 6 28 21.4
condition 7 30 23.3
subroutine 13 21 61.9
pod 0 15 0.0
total 65 193 33.6


line stmt bran cond sub pod time code
1             package Elastijk::oo;
2 3     3   19 use strict;
  3         6  
  3         74  
3 3     3   11 use warnings;
  3         5  
  3         73  
4 3     3   15 use Elastijk;
  3         3  
  3         149  
5              
6             sub new {
7 100005     100005 0 97030 my $class = shift;
8 100005         206415 return bless { host => "localhost", port => "9200", @_ }, $class;
9             }
10              
11             {
12 3     3   13 no warnings 'redefine';
  3         4  
  3         3091  
13 100002     100002   209598 *Elastijk::new = sub { shift; Elastijk::oo->new(@_) };
  100002         113165  
14             };
15              
16             sub __fleshen_request_args {
17 16     16   18 my ($self, $args) = @_;
18 16   33     24 $args->{$_} ||= $self->{$_} for grep { defined $self->{$_} } qw(host port index type head socket_cache on_connect connect_timeout read_timeout);
  144         270  
19             }
20              
21             sub request {
22 16     16 0 48 my ($self, %args) = @_;
23 16         34 __fleshen_request_args($self, \%args);
24 16         42 return Elastijk::request(\%args);
25             }
26              
27             sub request_raw {
28 0     0 0 0 my ($self, %args) = @_;
29 0         0 __fleshen_request_args($self, \%args);
30 0         0 return Elastijk::request_raw(\%args);
31             }
32              
33             sub index {
34 1     1 0 730 my ($self, %args) = @_;
35 1 50       6 return $self->request(method => ( exists $args{id} ? "PUT" : "POST" ), %args);
36             }
37              
38             sub get {
39 1     1 0 5 my $self = shift;
40 1         2 return $self->request(method => "GET", @_);
41             }
42              
43             sub put {
44 0     0 0 0 my $self = shift;
45 0         0 $self->request(method => "PUT", @_);
46             }
47              
48             sub delete {
49 1     1 0 883 my $self = shift;
50 1         3 return $self->request(method => "DELETE", @_);
51             }
52              
53             sub head {
54 0     0 0 0 my $self = shift;
55 0         0 return $self->request(method => "HEAD", @_);
56             }
57              
58             sub post {
59 0     0 0 0 my $self = shift;
60 0         0 return $self->request(method => "POST", @_);
61             }
62              
63             sub exists {
64 6     6 0 2728 my ($self, %args) = @_;
65 6 100       14 my $index = exists($args{index}) ? $args{index} : $self->{index};
66              
67 6         8 my ($status,$res);
68              
69 6 100 66     30 if ($index && exists($args{type}) && !exists($args{id})) {
      100        
70             # Type exists API
71             # https://www.elastic.co/guide/en/elasticsearch/reference/6.0/indices-types-exists.html
72              
73 2         5 $res = $self->request( method => 'GET', path => '/', index => undef );
74 2 50 50     9 if ( ($res->{version}{number} || '') ge '5') {
75 0         0 my $path = '/' . $index . '/_mappings/' . $args{type};
76 0         0 ($status,$res) = $self->request(method => "GET", path => $path);
77             } else {
78 2         6 ($status,$res) = $self->request(method => "HEAD", %args);
79             }
80             } else {
81 4         9 ($status,$res) = $self->request(method => "HEAD", %args);
82             }
83              
84 6         23 return ($status,'2' eq substr($status,0,1));
85             }
86              
87             sub search {
88 4     4 0 2088 my $self = shift;
89 4         9 return $self->request(command => "_search", method => "GET", @_);
90             }
91              
92             sub count {
93 0     0 0   my $self = shift;
94 0           my ($status,$res) = $self->request(command => "_count", method => "GET", @_);
95 0           return ($status, $res->{count});
96             }
97              
98              
99             sub bulk {
100 0     0 0   my ($self, %args) = @_;
101 0           $args{body} = join("", map { $Elastijk::JSON->encode($_)."\n" } @{$args{body}});
  0            
  0            
102 0           my ($status,$res) = $self->request_raw(method => "POST", command => "_bulk", %args);
103 0 0         $res = $Elastijk::JSON->decode($res) if $res;
104 0           return ($status, $res);
105             }
106              
107             sub scan_scroll {
108 0     0 0   my ($self, %args) = @_;
109 0           my $on_response_callback = delete $args{on_response};
110              
111 0 0         my %uri_param = %{ delete($args{uri_param}) || {} };
  0            
112 0           $uri_param{search_type} = "scan";
113 0   0       $uri_param{scroll} ||= "10m";
114 0           my $scroll_id;
115 0           my ($status, $res) = $self->get(%args, command => "_search", uri_param => \%uri_param);
116 0 0         if (substr($status,0,1) ne '2') {
117 0           return;
118             }
119              
120 0           while (1) {
121 0           $scroll_id = $res->{_scroll_id};
122             ($status,$res) = $self->get(
123             index => "_search", type => "scroll", #WTF
124 0           uri_param => { scroll => $uri_param{scroll}, scroll_id => $scroll_id }
125             );
126 0 0 0       if (substr($status,0,1) eq '2' && @{$res->{hits}{hits}} > 0) {
  0            
127 0           my $r = $on_response_callback->($status, $res);
128 0 0 0       last if defined($r) && !$r;
129 0           $scroll_id = $res->{_scroll_id};
130             } else {
131 0           last;
132             }
133             }
134             }
135              
136             sub search_scroll {
137 0     0 0   my ($self, %args) = @_;
138 0           my $on_response_callback = delete $args{on_response};
139              
140 0 0         my %uri_param = %{ delete($args{uri_param}) || {} };
  0            
141 0   0       $uri_param{scroll} ||= "10m";
142 0           my $scroll_id;
143 0           my ($status, $res) = $self->post(%args, command => "_search", uri_param => \%uri_param);
144 0 0         if (substr($status,0,1) ne '2') {
145 0           return;
146             }
147              
148 0           my $r = $on_response_callback->($status, $res);
149 0 0 0       return if defined($r) && !$r;
150              
151 0           while (1) {
152 0           $scroll_id = $res->{_scroll_id};
153             ($status,$res) = $self->post(
154             path => "/_search/scroll",
155 0           body => { scroll => $uri_param{scroll}, scroll_id => $scroll_id }
156             );
157 0 0 0       if (substr($status,0,1) eq '2' && @{$res->{hits}{hits}} > 0) {
  0            
158 0           my $r = $on_response_callback->($status, $res);
159 0 0 0       last if defined($r) && !$r;
160 0           $scroll_id = $res->{_scroll_id};
161             } else {
162 0           last;
163             }
164             }
165             }
166              
167             1;