line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#!/usr/bin/perl |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
package POE::Filter::JSON::Incr; |
4
|
1
|
|
|
1
|
|
31628
|
use Any::Moose; |
|
1
|
|
|
|
|
67707
|
|
|
1
|
|
|
|
|
8
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
3290
|
use JSON; |
|
1
|
|
|
|
|
34422
|
|
|
1
|
|
|
|
|
212
|
|
7
|
1
|
|
|
1
|
|
1739
|
use POE::Filter::JSON::Incr::Error; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
39
|
|
8
|
|
|
|
|
|
|
|
9
|
1
|
|
|
1
|
|
1704
|
use namespace::clean -except => [qw(meta)]; |
|
1
|
|
|
|
|
33877
|
|
|
1
|
|
|
|
|
12
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our $VERSION = "0.03"; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
extends our @ISA, qw(POE::Filter); |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
# with qw(MooseX::Clone) |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub clone { |
18
|
1
|
|
|
1
|
1
|
8
|
my ( $self, @args ) = @_; |
19
|
|
|
|
|
|
|
|
20
|
1
|
50
|
|
|
|
5
|
die "metaclass doesn't support cloning" unless $self->meta->can("clone_object"); |
21
|
|
|
|
|
|
|
|
22
|
1
|
|
|
|
|
19
|
$self->meta->clone_object( |
23
|
|
|
|
|
|
|
$self, |
24
|
|
|
|
|
|
|
@args, |
25
|
|
|
|
|
|
|
# clear the buffers |
26
|
|
|
|
|
|
|
buffer => [], |
27
|
|
|
|
|
|
|
json => $self->_build_json, |
28
|
|
|
|
|
|
|
); |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
has buffer => ( |
32
|
|
|
|
|
|
|
#traits => [qw(NoClone)], |
33
|
|
|
|
|
|
|
isa => "ArrayRef", |
34
|
|
|
|
|
|
|
is => "rw", |
35
|
|
|
|
|
|
|
lazy_build => 1, |
36
|
|
|
|
|
|
|
); |
37
|
|
|
|
|
|
|
|
38
|
1
|
|
|
1
|
|
10
|
sub _build_buffer { [] } |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
has json => ( |
41
|
|
|
|
|
|
|
#traits => [qw(NoClone)], |
42
|
|
|
|
|
|
|
is => "rw", |
43
|
|
|
|
|
|
|
lazy_build => 1, |
44
|
|
|
|
|
|
|
handles => [qw(encode incr_parse incr_skip)], |
45
|
|
|
|
|
|
|
); |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
has json_opts => ( |
48
|
|
|
|
|
|
|
isa => "ArrayRef", |
49
|
|
|
|
|
|
|
is => "rw", |
50
|
|
|
|
|
|
|
lazy_build => 1, |
51
|
|
|
|
|
|
|
); |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub _build_json { |
54
|
2
|
|
|
2
|
|
23
|
my $self = shift; |
55
|
|
|
|
|
|
|
|
56
|
2
|
|
|
|
|
16
|
my $json = JSON->new; |
57
|
|
|
|
|
|
|
|
58
|
2
|
|
|
|
|
4
|
foreach my $opt ( @{ $self->json_opts } ) { |
|
2
|
|
|
|
|
12
|
|
59
|
6
|
|
|
|
|
29
|
$json->$opt; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
2
|
|
|
|
|
26
|
return $json; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub _build_json_opts { |
66
|
1
|
|
|
1
|
|
8
|
return [qw( |
67
|
|
|
|
|
|
|
relaxed |
68
|
|
|
|
|
|
|
allow_nonref |
69
|
|
|
|
|
|
|
utf8 |
70
|
|
|
|
|
|
|
)]; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
has error_class => ( |
74
|
|
|
|
|
|
|
isa => "ClassName", |
75
|
|
|
|
|
|
|
is => "rw", |
76
|
|
|
|
|
|
|
default => "POE::Filter::JSON::Incr::Error", |
77
|
|
|
|
|
|
|
handles => { create_error_object => "new" }, |
78
|
|
|
|
|
|
|
); |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
has errors => ( |
81
|
|
|
|
|
|
|
isa => "Bool", |
82
|
|
|
|
|
|
|
is => "rw", |
83
|
|
|
|
|
|
|
default => 0, |
84
|
|
|
|
|
|
|
); |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
sub get_one_start { |
87
|
2
|
|
|
2
|
1
|
813
|
my ( $self, $chunks ) = @_; |
88
|
2
|
50
|
|
|
|
8
|
$chunks = [ $chunks ] unless ref $chunks; |
89
|
2
|
|
|
|
|
3
|
push @{ $self->buffer }, $self->_parse($chunks); |
|
2
|
|
|
|
|
12
|
|
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub get_one { |
93
|
3
|
|
|
3
|
1
|
4
|
my $self = shift; |
94
|
3
|
|
|
|
|
4
|
return [ splice @{ $self->buffer }, 0, 1 ]; # shift returns undef, this returns empty list |
|
3
|
|
|
|
|
66
|
|
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub get { |
98
|
4
|
|
|
4
|
1
|
3154
|
my ( $self, $chunks ) = @_; |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
return [ |
101
|
4
|
|
|
|
|
7
|
splice(@{ $self->buffer }), |
|
4
|
|
|
|
|
20
|
|
102
|
|
|
|
|
|
|
$self->_parse($chunks), |
103
|
|
|
|
|
|
|
]; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
sub _parse { |
107
|
6
|
|
|
6
|
|
8
|
my ( $self, $chunks ) = @_; |
108
|
|
|
|
|
|
|
|
109
|
6
|
|
|
|
|
8
|
my @ret; |
110
|
|
|
|
|
|
|
|
111
|
6
|
|
|
|
|
10
|
foreach my $chunk ( @$chunks ) { |
112
|
16
|
|
|
|
|
19
|
local $@; |
113
|
16
|
100
|
|
|
|
22
|
if ( my @out = eval { $self->incr_parse($chunk) } ) { |
|
16
|
|
|
|
|
46
|
|
114
|
9
|
|
|
|
|
147
|
push @ret, @out; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
16
|
100
|
|
|
|
150
|
if ( $@ ) { |
118
|
1
|
|
|
|
|
6
|
$self->incr_skip; |
119
|
1
|
|
|
|
|
22
|
push @ret, $self->json_error(error => $@, chunk => $chunk); |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
6
|
|
|
|
|
29
|
return @ret; |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
sub json_error { |
127
|
1
|
|
|
1
|
0
|
4
|
my ( $self, @args ) = @_; |
128
|
|
|
|
|
|
|
|
129
|
1
|
50
|
|
|
|
6
|
if ( $self->errors ) { |
130
|
0
|
|
|
|
|
0
|
return $self->create_error_object(@args); |
131
|
|
|
|
|
|
|
} else { |
132
|
1
|
|
|
|
|
4
|
return (); |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
sub put { |
137
|
1
|
|
|
1
|
1
|
1357
|
my ( $self, $data ) = @_; |
138
|
1
|
|
|
|
|
3
|
return [ map { $self->encode($_) . "\n" } @$data ]; |
|
2
|
|
|
|
|
38
|
|
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
sub get_pending { |
142
|
2
|
|
|
2
|
1
|
9
|
my $self = shift; |
143
|
|
|
|
|
|
|
|
144
|
2
|
100
|
|
|
|
3
|
if ( my @contents = @{ $self->buffer } ) { |
|
2
|
|
|
|
|
13
|
|
145
|
1
|
|
|
|
|
7
|
return \@contents; |
146
|
|
|
|
|
|
|
} else { |
147
|
1
|
|
|
|
|
5
|
return undef; |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
__PACKAGE__ |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
__END__ |