File Coverage

blib/lib/Elastijk/oo.pm
Criterion Covered Total %
statement 33 71 46.4
branch 1 12 8.3
condition 1 11 9.0
subroutine 13 20 65.0
pod 0 14 0.0
total 48 128 37.5


line stmt bran cond sub pod time code
1             package Elastijk::oo;
2 3     3   9 use strict;
  3         4  
  3         70  
3 3     3   8 use warnings;
  3         3  
  3         51  
4 3     3   9 use Elastijk;
  3         3  
  3         150  
5              
6             sub new {
7 100005     100005 0 60337 my $class = shift;
8 100005         163809 return bless { host => "localhost", port => "9200", @_ }, $class;
9             }
10              
11             {
12 3     3   10 no warnings 'redefine';
  3         1  
  3         1770  
13 100002     100002   148524 *Elastijk::new = sub { shift; Elastijk::oo->new(@_) };
  100002         87230  
14             };
15              
16             sub __fleshen_request_args {
17 14     14   13 my ($self, $args) = @_;
18 14   33     17 $args->{$_} ||= $self->{$_} for grep { exists $self->{$_} } qw(host port index type head socket_cache on_connect connect_timeout read_timeout);
  126         217  
19             }
20              
21             sub request {
22 14     14 0 42 my ($self, %args) = @_;
23 14         18 __fleshen_request_args($self, \%args);
24 14         30 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 408 my ($self, %args) = @_;
35 1 50       5 return $self->request(method => ( exists $args{id} ? "PUT" : "POST" ), %args);
36             }
37              
38             sub get {
39 1     1 0 4 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 481 my $self = shift;
50 1         4 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 1642 my $self = shift;
65 6         10 my ($status,$res) = $self->request(method => "HEAD", @_);
66 6         16 return ($status,'2' eq substr($status,0,1));
67             }
68              
69             sub search {
70 4     4 0 1250 my $self = shift;
71 4         8 return $self->request(command => "_search", method => "GET", @_);
72             }
73              
74             sub count {
75 0     0 0   my $self = shift;
76 0           my ($status,$res) = $self->request(command => "_count", method => "GET", @_);
77 0           return ($status, $res->{count});
78             }
79              
80              
81             sub bulk {
82 0     0 0   my ($self, %args) = @_;
83 0           $args{body} = join("", map { $Elastijk::JSON->encode($_)."\n" } @{$args{body}});
  0            
  0            
84 0           my ($status,$res) = $self->request_raw(method => "POST", command => "_bulk", %args);
85 0 0         $res = $Elastijk::JSON->decode($res) if $res;
86 0           return ($status, $res);
87             }
88              
89             sub scan_scroll {
90 0     0 0   my ($self, %args) = @_;
91 0           my $on_response_callback = delete $args{on_response};
92              
93 0 0         my %uri_param = %{ delete($args{uri_param}) || {} };
  0            
94 0           $uri_param{search_type} = "scan";
95 0   0       $uri_param{scroll} ||= "10m";
96 0           my $scroll_id;
97 0           my ($status, $res) = $self->get(%args, command => "_search", uri_param => \%uri_param);
98 0 0         if (substr($status,0,1) ne '2') {
99 0           return;
100             }
101              
102 0           while (1) {
103 0           $scroll_id = $res->{_scroll_id};
104             ($status,$res) = $self->get(
105             index => "_search", type => "scroll", #WTF
106 0           uri_param => { scroll => $uri_param{scroll}, scroll_id => $scroll_id }
107             );
108 0 0 0       if (substr($status,0,1) eq '2' && @{$res->{hits}{hits}} > 0) {
  0            
109 0           my $r = $on_response_callback->($status, $res);
110 0 0 0       last if defined($r) && !$r;
111 0           $scroll_id = $res->{_scroll_id};
112             } else {
113 0           last;
114             }
115             }
116             }
117              
118             1;