blib/lib/Bot/Cobalt/Plugin/RDB/AsyncSearch/Worker.pm untested

filter: Statementstmt 0.0% 0 / 54 Branchbran 0.0% 0 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 81 CC 11 SCAR 45.1
CC
11
Cov
0%
worker
132.0
BEGIN
2.0
BEGIN
2.0
CRAP 91.4
1 package Bot::Cobalt::Plugin::RDB::AsyncSearch::Worker;
2 $Bot::Cobalt::Plugin::RDB::AsyncSearch::Worker::VERSION = '0.021003';
3 use strict; use warnings;
4  
5 use Storable ();
6  
7 use bytes;
8  
9 use Bot::Cobalt::DB;
10  
11 sub worker {
12 binmode STDOUT;
13 binmode STDIN;
14
15 select(STDOUT);
16 $|++;
17
18 my $buf = '';
19 my $read_bytes;
20
21 while (1) {
22 if (defined $read_bytes) {
23 if (length $buf >= $read_bytes) {
24 my $input = Storable::thaw( substr($buf, 0, $read_bytes, '') );
25 $read_bytes = undef;
26 my ($dbpath, $tag, $regex) = @$input;
27  
28 my $db = Bot::Cobalt::DB->new($dbpath);
29  
30 $SIG{$_} = sub {
31 $db->dbclose if $db->is_open;
32 die "Terminal signal, closed cleanly"
33 } for qw/INT TERM QUIT HUP/;
34  
35 die "Failed database open"
36 unless $db->dbopen(ro => 1, timeout => 30);
37  
38 $regex = qr/$regex/i;
39 my @matches;
40 KEY: while (my ($dbkey, $ref) = each %{ $db->Tied }) {
41 next KEY unless $ref;
42 my $str = ref $ref eq 'HASH' ? $ref->{String} : $ref->[0] ;
43 push @matches, $dbkey if $str =~ $regex;
44 }
45
46 $db->dbclose;
47  
48 ## Return:
49 ## - DB path
50 ## - Unique ID
51 ## - List of matching item IDs
52 my $frozen = Storable::nfreeze( [ $dbpath, $tag, @matches ] );
53 my $stream = length($frozen) . chr(0) . $frozen ;
54 my $written = syswrite(STDOUT, $stream);
55 die $! unless $written == length $stream;
56 exit 0
57 }
58 } elsif ($buf =~ s/^(\d+)\0//) {
59 $read_bytes = $1;
60 next
61 }
62  
63 my $readb = sysread(STDIN, $buf, 4096, length $buf);
64 last unless $readb;
65 }
66
67 exit 0
68 }
69  
70 1;