More explicit error-reporting on search failure, to find problem reported by overnigh...
[irspy-moved-to-github.git] / lib / ZOOM / IRSpy / Task / Search.pm
1 # $Id: Search.pm,v 1.16 2007-12-18 11:59:42 mike Exp $
2
3 package ZOOM::IRSpy::Task::Search;
4
5 use 5.008;
6 use strict;
7 use warnings;
8
9 use ZOOM::IRSpy::Task;
10 our @ISA = qw(ZOOM::IRSpy::Task);
11
12 =head1 NAME
13
14 ZOOM::IRSpy::Task::Search - a searching task for IRSpy
15
16 =head1 SYNOPSIS
17
18  ## to follow
19
20 =head1 DESCRIPTION
21
22  ## to follow
23
24 =cut
25
26 sub new {
27     my $class = shift();
28     my $qtype = shift();
29     my $qstr = shift();
30     die "$class: unrecognised query type '$qtype'"
31         if !grep { $qtype eq $_ } qw(pqf cql);
32
33     my $this = $class->SUPER::new(@_);
34     $this->{qtype} = $qtype;
35     $this->{qstr} = $qstr;
36     $this->{rs} = undef;
37     return $this;
38 }
39
40 sub run {
41     my $this = shift();
42
43     $this->set_options();
44
45     my $conn = $this->conn();
46     $conn->connect($conn->option("host"));
47
48     my $qtype = $this->{qtype};
49     my $qstr = $this->{qstr};
50     $this->irspy()->log("irspy_task", $conn->option("host"),
51                         " searching for '$qtype:$qstr'");
52     die "task $this has resultset?!" if defined $this->{rs};
53
54     my $query;
55     if ($qtype eq "pqf") {
56         $query = new ZOOM::Query::PQF($qstr);
57     } elsif ($qtype eq "cql") {
58         $query = new ZOOM::Query::CQL($qstr);
59     } else {
60         die "Huh?!";
61     }
62
63     ### Note well that when this task runs, it creates a result-set
64     #   object which MUST BE DESTROYED in order to prevent large-scale
65     #   memory leakage.  So when creating a Task::Search, it is the
66     #   APPLICATION'S RESPONSIBILITY to ensure that the callback
67     #   invoked on success OR FAILURE makes arrangements for the set
68     #   to be destroyed.
69     eval {
70         $this->{rs} = $conn->search($query);
71     }; if ($@) {
72         die "remote search '$query' had error: '$@'";
73     }
74
75     warn "no ZOOM-C level events queued by $this"
76         if $conn->is_idle();
77
78     $this->set_options();
79 }
80
81 # Unique to Task::Search, used only for logging
82 sub render_query {
83     my $this = shift();
84     return $this->{qtype} . ":" . $this->{qstr}    
85 }
86
87 sub render {
88     my $this = shift();
89     return ref($this) . "(" . $this->render_query() . ")";
90 }
91
92 use overload '""' => \&render;
93
94
95 =head1 SEE ALSO
96
97 ZOOM::IRSpy
98
99 =head1 AUTHOR
100
101 Mike Taylor, E<lt>mike@indexdata.comE<gt>
102
103 =head1 COPYRIGHT AND LICENSE
104
105 Copyright (C) 2006 by Index Data ApS.
106
107 This library is free software; you can redistribute it and/or modify
108 it under the same terms as Perl itself, either Perl version 5.8.7 or,
109 at your option, any later version of Perl 5 you may have available.
110
111 =cut
112
113 1;