8437586176c3644dff47df4f0b9c8cdde13c5e68
[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     die "task $this has resultset?!" if defined $this->{rs};
52
53     my $query;
54     if ($qtype eq "pqf") {
55         $query = new ZOOM::Query::PQF($qstr);
56     } elsif ($qtype eq "cql") {
57         $query = new ZOOM::Query::CQL($qstr);
58     } else {
59         die "Huh?!";
60     }
61
62     ### Note well that when this task runs, it creates a result-set
63     #   object which MUST BE DESTROYED in order to prevent large-scale
64     #   memory leakage.  So when creating a Task::Search, it is the
65     #   APPLICATION'S RESPONSIBILITY to ensure that the callback
66     #   invoked on success OR FAILURE makes arrangements for the set
67     #   to be destroyed.
68     eval {
69         $this->{rs} = $conn->search($query);
70     }; if ($@) {
71         die "remote search '$query' had error: '$@'";
72     }
73
74     warn "no ZOOM-C level events queued by $this"
75         if $conn->is_idle();
76
77     $this->set_options();
78 }
79
80 # Unique to Task::Search, used only for logging
81 sub render_query {
82     my $this = shift();
83     return $this->{qtype} . ":" . $this->{qstr}    
84 }
85
86 sub render {
87     my $this = shift();
88     return ref($this) . "(" . $this->render_query() . ")";
89 }
90
91 use overload '""' => \&render;
92
93
94 =head1 SEE ALSO
95
96 ZOOM::IRSpy
97
98 =head1 AUTHOR
99
100 Mike Taylor, E<lt>mike@indexdata.comE<gt>
101
102 =head1 COPYRIGHT AND LICENSE
103
104 Copyright (C) 2006 by Index Data ApS.
105
106 This library is free software; you can redistribute it and/or modify
107 it under the same terms as Perl itself, either Perl version 5.8.7 or,
108 at your option, any later version of Perl 5 you may have available.
109
110 =cut
111
112 1;