New API for constructor: now takes new additional first argument, a
[irspy-moved-to-github.git] / lib / ZOOM / IRSpy / Task / Search.pm
1 # $Id: Search.pm,v 1.14 2007-05-01 15:32:06 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::Prefix($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     $this->{rs} = $conn->search($query);
70     warn "no ZOOM-C level events queued by $this"
71         if $conn->is_idle();
72
73     $this->set_options();
74 }
75
76 sub render {
77     my $this = shift();
78     return ref($this) . "(" . $this->{qtype} . ":" . $this->{qstr} . ")";
79 }
80
81 use overload '""' => \&render;
82
83
84 =head1 SEE ALSO
85
86 ZOOM::IRSpy
87
88 =head1 AUTHOR
89
90 Mike Taylor, E<lt>mike@indexdata.comE<gt>
91
92 =head1 COPYRIGHT AND LICENSE
93
94 Copyright (C) 2006 by Index Data ApS.
95
96 This library is free software; you can redistribute it and/or modify
97 it under the same terms as Perl itself, either Perl version 5.8.7 or,
98 at your option, any later version of Perl 5 you may have available.
99
100 =cut
101
102 1;