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