Rolling.
[ZOOM-Perl-moved-to-github.git] / lib / ZOOM.pod
1 # $Id: ZOOM.pod,v 1.10 2005-11-18 17:55:08 mike Exp $
2
3 use strict;
4 use warnings;
5
6 =head1 NAME
7
8 ZOOM - Perl extension implementing the ZOOM API for Information Retrieval
9
10 =head1 SYNOPSIS
11
12  use ZOOM;
13  eval {
14      $conn = new ZOOM::Connection($host, $port)
15      $conn->option(preferredRecordSyntax => "usmarc");
16      $rs = $conn->search_pqf('@attr 1=4 dinosaur');
17      $n = $rs->size();
18      print $rs->record(0)->render();
19  };
20  if ($@) {
21      print "Error ", $@->code(), ": ", $@->message(), "\n";
22  }
23
24 =head1 DESCRIPTION
25
26 This module provides a nice, Perlish implementation of the ZOOM
27 Abstract API described and documented at http://zoom.z3950.org/api/
28
29 the ZOOM module is implemented as a set of thin classes on top of the
30 non-OO functions provided by this distribution's C<Net::Z3950::ZOOM>
31 module, which in 
32 turn is a thin layer on top of the ZOOM-C code supplied as part of
33 Index Data's YAZ Toolkit.  Because ZOOM-C is also the underlying code
34 that implements ZOOM bindings in C++, Visual Basic, Scheme, Ruby, .NET
35 (including C#) and other languages, this Perl module works compatibly
36 with those other implementations.  (Of course, the point of a public
37 API such as ZOOM is that all implementations should be compatible
38 anyway; but knowing that the same code is running is reassuring.)
39
40 The ZOOM module provides two enumerations (C<ZOOM::Error> and
41 C<ZOOM::Event>), a single utility function C<diag_str()> in the C<ZOOM>
42 package itself, and eight classes:
43 C<ZOOM::Exception>,
44 C<ZOOM::Options>,
45 C<ZOOM::Connection>,
46 C<ZOOM::Query>,
47 C<ZOOM::ResultSet>,
48 C<ZOOM::Record>,
49 C<ZOOM::ScanSet>
50 and
51 C<ZOOM::Package>.
52 Of these, the Query class is abstract, and has two concrete
53 subclasses:
54 C<ZOOM::Query::CQL>
55 and
56 C<ZOOM::Query::PQF>.
57 Many useful ZOOM applications can be built using only the Connection,
58 ResultSet, Record and Exception classes, as in the example
59 code-snippet above.
60
61 A typical application will begin by creating an Connection object,
62 then using that to execute searches that yield ResultSet objects, then
63 fetching records from the result-sets to yield Record objects.  If an
64 error occurs, an Exception object is thrown and can be dealt with.
65
66 More sophisticated applications might also browse the server's indexes
67 to create a ScanSet, from which indexed terms may be retrieved; others
68 might send ``Extended Services'' Packages to the server, to achieve
69 non-standard tasks such as database creation and record update.
70 Searching using a query syntax other than PQF can be done using an
71 query object of one of the Query subclasses.  Finally, sets of options
72 may be manipulated independently of the objects they are associated
73 with using an Options object.
74
75 In general, method calls throw an exception if anything goes wrong, so
76 you don't need to test for success after each call.  See the section
77 below on the Exception class for details.
78
79 =head1 UTILITY FUNCTION
80
81 =head2 ZOOM::diag_str()
82
83  $msg = ZOOM::diag_str(ZOOM::Error::INVALID_QUERY);
84
85 Returns a human-readable English-language string corresponding to the
86 error code that is its own parameter.  This works for any error-code
87 returned from
88 C<ZOOM::Exception::code()>,
89 C<ZOOM::Connection::error_x()>
90 or
91 C<ZOOM::Connection::errcode()>,
92 irrespective of whether it is a member of the C<ZOOM::Error>
93 enumeration or drawn from the BIB-1 diagnostic set.
94
95 =head1 CLASSES
96
97 The eight ZOOM classes are described here in ``sensible order'':
98 first, the four commonly used classes, in the he order that they will
99 tend to be used in most programs (Connection, ResultSet, Record,
100 Exception); then the four more esoteric classes in descending order of
101 how often they are needed.
102
103 With the exception of the Options class, which is an extension to the
104 ZOOM model, the introduction to each class includes a link to the
105 relevant section of the ZOOM Abstract API.
106
107 =head2 ZOOM::Connection
108
109  $conn = new ZOOM::Connection("indexdata.dk:210/gils");
110  print("server is '", $conn->option("serverImplementationName"), "'\n");
111  $conn->option(preferredRecordSyntax => "usmarc");
112  $rs = $conn->search_pqf('@attr 1=4 mineral');
113  $ss = $conn->scan('@attr 1=1003 a');
114  if ($conn->errcode() != 0) {
115     die("somthing went wrong: " . $conn->errmsg())
116  }
117  $conn->destroy()
118
119 This class represents a connection to an information retrieval server,
120 using an IR protocol such as ANSI/NISO Z39.50, SRW (the
121 Search/Retrieve Webservice), SRU (the Search/Retrieve URL) or
122 OpenSearch.  Not all of these protocols require a low-level connection
123 to be maintained, but the Connection object nevertheless provides a
124 location for the necessary cache of configuration and state
125 information, as well as a uniform API to the connection-oriented
126 facilities (searching, index browsing, etc.), provided by these
127 protocols.
128
129 See the description of the C<Connection> class in the ZOOM Abstract
130 API at
131 http://zoom.z3950.org/api/zoom-current.html#3.2
132
133 =head3 Methods
134
135 =head4 new()
136
137  $conn = new ZOOM::Connection("indexdata.dk", 210);
138  $conn = new ZOOM::Connection("indexdata.dk:210/gils");
139  $conn = new ZOOM::Connection("tcp:indexdata.dk:210/gils");
140  $conn = new ZOOM::Connection("http:indexdata.dk:210/gils");
141
142 Creates a new Connection object, and immediately connects it to the
143 specified server.  If you want to make a new Connection object but
144 delay forging the connection, use the C<create()> and C<connect()>
145 methods instead.
146
147 This constructor can be called with two arguments or a single
148 argument.  In the former case, the arguments are the name and port
149 number of the Z39.50 server to connect to; in the latter case, the
150 single argument is a YAZ service-specifier string of the form
151
152 =over 4
153
154 =item
155
156 [I<scheme>:]I<host>[:I<port>][/I<databaseName>]
157
158 =back
159
160 In which the I<host> and I<port> parts are as in the two-argument
161 form, the I<databaseName> if provided specifies the name of the
162 database to be used in subsequent searches on this connection, and the
163 optional I<scheme> (default C<tcp>) indicates what protocol should be
164 used.  At present, the following schemes are supported:
165
166 =over 4
167
168 =item tcp
169
170 Z39.50 connection.
171
172 =item ssl
173
174 Z39.50 connection encrypted using SSL (Secure Sockets Layer).  Not
175 many servers support this, but Index Data's Zebra is one that does.
176
177 =item unix
178
179 Z39.50 connection on a Unix-domain (local) socket, in which case the
180 I<hostname> portion of the string is instead used as a filename in the
181 local filesystem.
182
183 =item http
184
185 SRW connection using SOAP over HTTP.
186
187 =back
188
189 Support for SRU will follow in the fullness of time.
190
191 If an error occurs, an exception is thrown.  This may indicate a
192 networking problem (e.g. the host is not found or unreachable), or a
193 protocol-level problem (e.g. a Z39.50 server rejected the Init
194 request).
195
196 =head4 create() / connect()
197
198  $options = new ZOOM::Options();
199  $options->option(implementationName => "my client");
200  $conn = create ZOOM::Connection($options)
201  $conn->connect($host, 0);
202
203 The usual Connection constructor, C<new()> brings a new object into
204 existence and forges the connection to the server all in one
205 operation, which is often what you want.  For applications that need
206 more control, however, these two method separate the two steps,
207 allowing additional steps in between such as the setting of options.
208
209 C<create()> creates and returns a new Connection object, which is
210 I<not> connected to any server.  It may be passed an options block, of
211 type C<ZOOM::Options> (see below), into which options may be set
212 before or after the creation of the Connection.  The connection to the
213 server may then be forged by the C<connect()> method, the arguments of
214 which are the same as those of the C<new()> constructor.
215
216 =head4 error_x() / errcode() / errmsg() / addinfo() / diagset()
217
218  ($errcode, $errmsg, $addinfo, $diagset) = $conn->error_x();
219  $errcode = $conn->errcode();
220  $errmsg = $conn->errmsg();
221  $addinfo = $conn->addinfo();
222  $diagset = $conn->diagset();
223
224 These methods may be used to obtain information about the last error
225 to have occurred on a connection - although typically they will not
226 been used, as the same information is available through the
227 C<ZOOM::Exception> that is thrown when the error occurs.  The
228 C<errcode()>,
229 C<errmsg()>,
230 C<addinfo()>
231 and
232 C<diagset()>
233 methods each return one element of the diagnostic, and
234 C<error_x()>
235 returns all four at once.
236
237 See the C<ZOOM::Exception> for the interpretation of these elements.
238
239 =head4 option() / option_binary()
240
241  print("server is '", $conn->option("serverImplementationName"), "'\n");
242  $conn->option(preferredRecordSyntax => "usmarc");
243  $conn->option_binary(iconBlob => "foo\0bar");
244  die if length($conn->option_binary("iconBlob") != 7);
245
246 Objects of the Connection, ResultSet, ScanSet and Package classes
247 carry with them a set of named options which affect their behaviour in
248 certain ways.  See the ZOOM-C options documentation for details:
249
250 =over 4
251
252 =item *
253
254 Connection options are listed at
255 http://indexdata.com/yaz/doc/zoom.tkl#zoom.connections
256
257 =item *
258
259 ScanSet options are listed at
260 http://indexdata.com/yaz/doc/zoom.scan.tkl
261 I<### move this obvservation down to the appropriate place>
262
263 =item *
264
265 Package options are listed at
266 http://indexdata.com/yaz/doc/zoom.ext.html
267 I<### move this obvservation down to the appropriate place>
268
269 =back
270
271 These options are set and fetched using the C<option()> method, which
272 may be called with either one or two arguments.  In the two-argument
273 form, the option named by the first argument is set to the value of
274 the second argument, and its old value is returned.  In the
275 one-argument form, the value of the specified option is returned.
276
277 For historical reasons, option values are not binary-clean, so that a
278 value containing a NUL byte will be returned in truncated form.  The
279 C<option_binary()> method behaves identically to C<option()> except
280 that it is binary-clean, so that values containing NUL bytes are set
281 and returned correctly.
282
283 =head4 search() / search_pqf()
284
285  $rs = $conn->search(new ZOOM::Query::CQL('title=dinosaur'));
286  # The next two lines are equivalent
287  $rs = $conn->search(new ZOOM::Query::PQF('@attr 1=4 dinosaur'));
288  $rs = $conn->search_pqf('@attr 1=4 dinosaur');
289
290 The principal purpose of a search-and-retrieve protocol is searching
291 (and, er, retrieval), so the principal method used on a Connection
292 object is C<search()>.  It accepts a single argument, a C<ZOOM::Query>
293 object (or, more precisely, an object of a subclass of this class);
294 and it creates and returns a new ResultSet object representing the set
295 of records resulting from the search.
296
297 Since queries using PQF (Prefix Query Format) are so common, we make
298 them a special case by providing a C<search_prefix()> method.  This is
299 identical to C<search()> except that it accepts a string containing
300 the query rather than an object, thereby obviating the need to create
301 a C<ZOOM::Query::PQF> object.  See the documentation of that class for
302 information about PQF.
303
304 =head4 scan()
305
306 Many Z39.50 servers allow you to browse their indexes to find terms to
307 search for.  This is done using the C<scan> method, which creates and
308 returns a new ScanSet object representing the set of terms resulting
309 from the scan.
310
311 C<scan()> takes a single argument, but it has to work hard: it
312 specifies both what index to scan for terms, and where in the index to
313 start scanning.  What's more, the specification of what index to scan
314 includes multiple facets, such as what database fields it's an index
315 of (author, subject, title, etc.) and whether to scan for whole fields
316 or single words (e.g. the title ``I<The Empire Strikes Back>'', or the
317 four words ``Back'', ``Empire'', ``Strikes'' and ``The'', interleaved
318 with words from other titles in the same index.
319
320 All of this is done by using a single term from the PQF query as the
321 C<scan()> argument.  (At present, only PQF is supported, although
322 there is no reason in principle why CQL and other query syntaxes
323 should not be supported in future).  The attributes associated with
324 the term indicate which index is to be used, and the term itself
325 indicates the point in the index at which to start the scan.  For
326 example, if the argument is C<@attr 1=4 fish>, then
327
328 =over 4
329
330 =item @attr 1=4
331
332 This is the BIB-1 attribute with type 1 (meaning access-point, which
333 specifies an index), and type 4 (which means ``title'').  So the scan
334 is in the title index.
335
336 =item fish
337
338 Start the scan from the lexicographically earliest term that is equal
339 to or falls after ``fish''.
340
341 =back
342
343 The argument C<@attr 1=4 @attr 6=3 fish> would behave similarly; but
344 the BIB-1 attribute 6=3 mean completeness=``complete field'', so the
345 scan would be for complete titles rather than for words occurring in
346 titles.
347
348 This takes a bit of getting used to.
349
350 The behaviour is C<scan()> is affected by the following options, which
351 may be set on the Connection through which the scan is done:
352
353 =over 4
354
355 =item number [default: 10]
356
357 Indicates how many terms should be returned in the ScanSet.  The
358 number actually returned may be less, if the start-point is near the
359 end of the index, but will not be greater.
360
361 =item position [default: 1]
362
363 A 1-based index specifying where in the returned list of terms the
364 seed-term should appear.  By default it should be the first term
365 returned, but C<position> may be set, for example, to zero (requesting
366 the next terms I<after> the seed-term), or to the same value as
367 C<number> (requesting the index terms I<before> the seed term).
368
369 =item stepSize [default: 0]
370
371 An integer indicating how many indexed terms are to be skipped between
372 each one returned in the ScanSet.  By default, no terms are skipped,
373 but overriding this can be useful to get a high-level overview of the
374 index.
375
376 =back
377
378 =head4 package()
379
380  $p = $conn->package();
381  $o = new ZOOM::Options();
382  $o->option(databaseName => "newdb");
383  $p = $conn->package($o);
384
385 Creates and returns a new C<ZOOM::Package>, to be used in invoking an
386 Extended Service.  An options block may optionally be passed in.  See
387 the C<ZOOM::Package> documentation.
388
389 =head4 destroy()
390
391  $conn->destroy()
392
393 Destroys a Connection object, tearing down any low-level connection
394 associated with it and freeing its resources.  It is an error to reuse
395 a Connection that has been C<destroy()>ed.
396
397 =head2 ZOOM::ResultSet
398
399  $rs = $conn->search_pqf('@attr 1=4 mineral');
400  $n = $rs->size();
401  for $i (1 .. $n) {
402      $rec = $rs->record($i-1);
403      print $rec->render();
404  }
405
406 A ResultSet object represents the set of zero or more records
407 resulting from a search, and is the means whereby these records can be
408 retrieved.  A ResultSet object may maintain client side cache or some,
409 less, none, all or more of the server's records: in general, this is
410 supposed to an implementaton detail of no interest to a typical
411 application, although more sophisticated applications do have
412 facilities for messing with the cache.  Most applications will only
413 need the C<size()>, C<record()> and C<sort()> methods.
414
415 There is no C<new()> method nor any other explicit constructor.  The
416 only way to create a new ResultSet is by using C<search()> (or
417 C<search_prefix()>) on a Connection.
418
419 See the description of the C<Result Set> class in the ZOOM Abstract
420 API at
421 http://zoom.z3950.org/api/zoom-current.html#3.4
422
423 =head3 Methods
424
425 =head4 option()
426
427  $conn->option(elementSetName => "f");
428
429 Allows options to be set into, and read from a ResultSet, just like
430 the Connection class's C<option()> method.  There is no
431 C<option_binary()> method for ResultSet objects.
432
433 ResultSet options are listed at
434 http://indexdata.com/yaz/doc/zoom.resultsets.tkl
435
436 =head4 size()
437
438  print "Found ", $rs->size(), " records\n";
439
440 Returns the number of records in the result set.
441
442 =head4 record(), record_immediate()
443
444  $rec = $rs->record(0);
445  $rec2 = $rs->record_immediate(0);
446  $rec3 = $rs->record_immediate(1)
447      or print "second record wasn't in cache\n";
448
449 The C<record()> method returns a C<ZOOM::Record> object representing
450 a record from result-set, whose position is indicated by the argument
451 passed in.  This is a zero-based index, so that legitimate values
452 range from zero to C<$rs->size()-1>.
453
454 The C<record_immediate()> API is identical, but it never invokes a
455 network operation, merely returning the record from the ResultSet's
456 cache if it's already there, or an undefined value otherwise.  So if
457 you use this method, B<you must always check the return value>.
458
459 =head4 records()
460
461  $rs->records(0, 10, 0);
462  for $i (0..10) {
463      print $rs->record_immediate($i)->render();
464  }
465
466  @nextseven = $rs->records(10, 7, 1);
467
468 The C<record_immediate()> method only fetches records from the cache,
469 whereas C<record()> fetches them from the server if they have not
470 already been cached; but the ZOOM module has to guess what the most
471 efficient strategy for this is.  It might fetch each record, alone
472 when asked for: that's optimal in an application that's only
473 interested in the top hit from each search, but pessimal for one that
474 wants to display a whole list of results.  Conversely, the software's
475 strategy might be always to ask for blocks of a twenty records:
476 that's great for assembling long lists of things, but wasteful when
477 only one record is wanted.  The problem is that the ZOOM module can't
478 tell, when you call C<$rs->record()>, what your intention is.
479
480 But you can tell it.  The C<records()> method fetches a sequence of
481 records, all in one go.  It takes three arguments: the first is the
482 zero-based index of the first record in the sequence, the second is
483 the number of records to fetch, and the third is a boolean indication
484 of whether or not to return the retrieved records as well as adding
485 them to the cache.  (You can always pass 1 for this if you like, and
486 Perl will discard the unused return value, but there is a small
487 efficiency gain to be had by passing 0.)
488
489 Once the records have been retrieved from the server
490 (i.e. C<records()> has completed without throwing an exception), they
491 can be fetched much more efficiently using C<record()> - or
492 C<record_immediate(), which is then guaranteed to succeed.
493
494 =head4 cache_reset()
495
496  $rs->cache_reset()
497
498 Resets the ResultSet's record cache, so that subsequent invocations of
499 C<record_immediate()> will fail.  I struggle to imagine a real
500 scenario where you'd want to do this.
501
502 =head4 sort()
503
504  if ($rs->sort("yaz", "1=4 >i") < 0) {
505      die "sort failed";
506  }
507
508 Sorts the ResultSet in place ###
509
510 =head4 destroy()
511
512  $rs->destroy()
513
514 Destroys a ResultSet object, freeing its resources.  It is an error to
515 reuse a ResultSet that has been C<destroy()>ed.
516
517 =head2 ZOOM::Record
518
519 I<###>
520
521 =head2 ZOOM::Exception
522
523 In general, method calls throw an exception (of class
524 C<ZOOM::Exception>) if anything goes wrong, so you don't need to test
525 for success after each call.  Exceptions are caught by enclosing the
526 main code in an C<eval{}> block and checking C<$@> on exit from that
527 block, as in the code-sample above.
528
529 There are a small number of exceptions to this rule: the three
530 record-fetching methods in the C<ZOOM::ResultSet> class,
531 C<record()>,
532 C<record_immediate()>,
533 and
534 C<records()>
535 can all return undefined values for legitimate reasons, under
536 circumstances that do not merit throwing an exception.  For this
537 reason, the return values of these methods should be checked.  See the
538 individual methods' documentation for details.
539
540 =head3 Methods
541
542 I<###>
543
544 =head2 ZOOM::ScanSet
545
546 I<###>
547
548 =head2 ZOOM::Package
549
550 I<###>
551
552 =head2 ZOOM::Query
553
554 I<###>
555
556 =head2 ZOOM::Options
557
558 I<###>
559
560 =head1 ENUMERATIONS
561
562 The ZOOM module provides two enumerations that list possible return
563 values from particular functions.  They are described in the following
564 sections.
565
566 =head2 ZOOM::Error
567
568  if ($@->code() == ZOOM::Error::QUERY_PQF) {
569      return "your query was not accepted";
570  }
571
572 This class provides a set of manifest constants representing some of
573 the possible error codes that can be raised by the ZOOM module.  The
574 methods that return error-codes are
575 C<ZOOM::Exception::code()>,
576 C<ZOOM::Connection::error_x()>
577 and
578 C<ZOOM::Connection::errcode()>.
579
580 The C<ZOOM::Error> class provides the constants
581 C<NONE>,
582 C<CONNECT>,
583 C<MEMORY>,
584 C<ENCODE>,
585 C<DECODE>,
586 C<CONNECTION_LOST>,
587 C<INIT>,
588 C<INTERNAL>,
589 C<TIMEOUT>,
590 C<UNSUPPORTED_PROTOCOL>,
591 C<UNSUPPORTED_QUERY>,
592 C<INVALID_QUERY>,
593 C<CREATE_QUERY>,
594 C<QUERY_CQL>,
595 C<QUERY_PQF>,
596 C<SORTBY>,
597 C<CLONE>,
598 C<PACKAGE>
599 and
600 C<SCANTERM>,
601 each of which specifies a client-side error.  Since errors may also be
602 diagnosed by the server, and returned to the client, error codes may
603 also take values from the BIB-1 diagnostic set of Z39.50, listed at
604 the Z39.50 Maintenance Agency's web-site at
605 http://www.loc.gov/z3950/agency/defns/bib1diag.html
606
607 All error-codes, whether client-side from the C<ZOOM::Error>
608 enumeration or server-side from the BIB-1 diagnostic set, can be
609 translated into human-readable messages by passing them to the
610 C<ZOOM::diag_str()> utility function.
611
612 =head2 ZOOM::Event
613
614  if ($conn->last_event() == ZOOM::Event::CONNECT) {
615      print "Connected!\n";
616  }
617
618 In applications that need it - mostly complex multiplexing
619 applications - The C<ZOOM::Connection::last_event()> method is used to
620 return an indication of the last event that occurred on a particular
621 connection.  It always returns a value drawn from this enumeration,
622 that is, one of C<NONE>, C<CONNECT>, C<SEND_DATA>, C<RECV_DATA>,
623 C<TIMEOUT>, C<UNKNOWN>, C<SEND_APDU>, C<RECV_APDU>, C<RECV_RECORD> or
624 C<RECV_SEARCH>.
625
626 You almost certainly don't need to know about this.  Frankly, I'm not
627 sure how to use it myself.
628
629 =head1 SEE ALSO
630
631 The ZOOM abstract API,
632 http://zoom.z3950.org/api/zoom-current.html
633
634 The C<Net::Z3950::ZOOM> module, included in the same distribution as this one.
635
636 The C<Net::Z3950> module, which this one supersedes.
637 http://perl.z3950.org/
638
639 The documentation for the ZOOM-C module of the YAZ Toolkit, which this
640 module is built on.  Specifically, its lists of options are useful.
641 http://indexdata.com/yaz/doc/zoom.tkl
642
643 The BIB-1 diagnostic set of Z39.50,
644 http://www.loc.gov/z3950/agency/defns/bib1diag.html
645
646 =head1 AUTHOR
647
648 Mike Taylor, E<lt>mike@indexdata.comE<gt>
649
650 =head1 COPYRIGHT AND LICENCE
651
652 Copyright (C) 2005 by Index Data.
653
654 This library is free software; you can redistribute it and/or modify
655 it under the same terms as Perl itself, either Perl version 5.8.4 or,
656 at your option, any later version of Perl 5 you may have available.
657
658 =cut
659
660 1;