Add descriptions of ZOOM::event() and ZOOM::Connection::last_event(),
[ZOOM-Perl-moved-to-github.git] / lib / ZOOM.pod
1 # $Id: ZOOM.pod,v 1.21 2005-12-13 16:22:41 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>), two utility functions C<diag_str()> and C<event()> in
42 the C<ZOOM> 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 FUNCTIONS
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 =head2 ZOOM::event()
96
97 B<Warning: lark's vomit.  Do not read this.>
98
99  $which = ZOOM::event([ $conn1, $conn2, $conn3 ]);
100
101 Used only in complex asynchronous applications, this function takes a
102 reference to a list of Connection objects, waits until an event
103 occurs on any one of them, and returns an integer indicating which of
104 the connections it occurred on.  The return value is a 1-based index
105 into the list; 0 is returned if no event occurs within the longest
106 timeout specified by the C<timeout> options of all the connections.
107
108 B<This function is not yet implemented.>
109
110 =head1 CLASSES
111
112 The eight ZOOM classes are described here in ``sensible order'':
113 first, the four commonly used classes, in the he order that they will
114 tend to be used in most programs (Connection, ResultSet, Record,
115 Exception); then the four more esoteric classes in descending order of
116 how often they are needed.
117
118 With the exception of the Options class, which is an extension to the
119 ZOOM model, the introduction to each class includes a link to the
120 relevant section of the ZOOM Abstract API.
121
122 =head2 ZOOM::Connection
123
124  $conn = new ZOOM::Connection("indexdata.dk:210/gils");
125  print("server is '", $conn->option("serverImplementationName"), "'\n");
126  $conn->option(preferredRecordSyntax => "usmarc");
127  $rs = $conn->search_pqf('@attr 1=4 mineral');
128  $ss = $conn->scan('@attr 1=1003 a');
129  if ($conn->errcode() != 0) {
130     die("somthing went wrong: " . $conn->errmsg())
131  }
132  $conn->destroy()
133
134 This class represents a connection to an information retrieval server,
135 using an IR protocol such as ANSI/NISO Z39.50, SRW (the
136 Search/Retrieve Webservice), SRU (the Search/Retrieve URL) or
137 OpenSearch.  Not all of these protocols require a low-level connection
138 to be maintained, but the Connection object nevertheless provides a
139 location for the necessary cache of configuration and state
140 information, as well as a uniform API to the connection-oriented
141 facilities (searching, index browsing, etc.), provided by these
142 protocols.
143
144 See the description of the C<Connection> class in the ZOOM Abstract
145 API at
146 http://zoom.z3950.org/api/zoom-current.html#3.2
147
148 =head3 Methods
149
150 =head4 new()
151
152  $conn = new ZOOM::Connection("indexdata.dk", 210);
153  $conn = new ZOOM::Connection("indexdata.dk:210/gils");
154  $conn = new ZOOM::Connection("tcp:indexdata.dk:210/gils");
155  $conn = new ZOOM::Connection("http:indexdata.dk:210/gils");
156
157 Creates a new Connection object, and immediately connects it to the
158 specified server.  If you want to make a new Connection object but
159 delay forging the connection, use the C<create()> and C<connect()>
160 methods instead.
161
162 This constructor can be called with two arguments or a single
163 argument.  In the former case, the arguments are the name and port
164 number of the Z39.50 server to connect to; in the latter case, the
165 single argument is a YAZ service-specifier string of the form
166
167 =over 4
168
169 =item
170
171 [I<scheme>:]I<host>[:I<port>][/I<databaseName>]
172
173 =back
174
175 In which the I<host> and I<port> parts are as in the two-argument
176 form, the I<databaseName> if provided specifies the name of the
177 database to be used in subsequent searches on this connection, and the
178 optional I<scheme> (default C<tcp>) indicates what protocol should be
179 used.  At present, the following schemes are supported:
180
181 =over 4
182
183 =item tcp
184
185 Z39.50 connection.
186
187 =item ssl
188
189 Z39.50 connection encrypted using SSL (Secure Sockets Layer).  Not
190 many servers support this, but Index Data's Zebra is one that does.
191
192 =item unix
193
194 Z39.50 connection on a Unix-domain (local) socket, in which case the
195 I<hostname> portion of the string is instead used as a filename in the
196 local filesystem.
197
198 =item http
199
200 SRW connection using SOAP over HTTP.
201
202 =back
203
204 Support for SRU will follow in the fullness of time.
205
206 If an error occurs, an exception is thrown.  This may indicate a
207 networking problem (e.g. the host is not found or unreachable), or a
208 protocol-level problem (e.g. a Z39.50 server rejected the Init
209 request).
210
211 =head4 create() / connect()
212
213  $options = new ZOOM::Options();
214  $options->option(implementationName => "my client");
215  $conn = create ZOOM::Connection($options)
216  $conn->connect($host, 0);
217
218 The usual Connection constructor, C<new()> brings a new object into
219 existence and forges the connection to the server all in one
220 operation, which is often what you want.  For applications that need
221 more control, however, these two method separate the two steps,
222 allowing additional steps in between such as the setting of options.
223
224 C<create()> creates and returns a new Connection object, which is
225 I<not> connected to any server.  It may be passed an options block, of
226 type C<ZOOM::Options> (see below), into which options may be set
227 before or after the creation of the Connection.  The connection to the
228 server may then be forged by the C<connect()> method, the arguments of
229 which are the same as those of the C<new()> constructor.
230
231 =head4 error_x() / errcode() / errmsg() / addinfo() / diagset()
232
233  ($errcode, $errmsg, $addinfo, $diagset) = $conn->error_x();
234  $errcode = $conn->errcode();
235  $errmsg = $conn->errmsg();
236  $addinfo = $conn->addinfo();
237  $diagset = $conn->diagset();
238
239 These methods may be used to obtain information about the last error
240 to have occurred on a connection - although typically they will not
241 been used, as the same information is available through the
242 C<ZOOM::Exception> that is thrown when the error occurs.  The
243 C<errcode()>,
244 C<errmsg()>,
245 C<addinfo()>
246 and
247 C<diagset()>
248 methods each return one element of the diagnostic, and
249 C<error_x()>
250 returns all four at once.
251
252 See the C<ZOOM::Exception> for the interpretation of these elements.
253
254 =head4 option() / option_binary()
255
256  print("server is '", $conn->option("serverImplementationName"), "'\n");
257  $conn->option(preferredRecordSyntax => "usmarc");
258  $conn->option_binary(iconBlob => "foo\0bar");
259  die if length($conn->option_binary("iconBlob") != 7);
260
261 Objects of the Connection, ResultSet, ScanSet and Package classes
262 carry with them a set of named options which affect their behaviour in
263 certain ways.  See the ZOOM-C options documentation for details:
264
265 Connection options are listed at
266 http://indexdata.com/yaz/doc/zoom.tkl#zoom.connections
267
268 These options are set and fetched using the C<option()> method, which
269 may be called with either one or two arguments.  In the two-argument
270 form, the option named by the first argument is set to the value of
271 the second argument, and its old value is returned.  In the
272 one-argument form, the value of the specified option is returned.
273
274 For historical reasons, option values are not binary-clean, so that a
275 value containing a NUL byte will be returned in truncated form.  The
276 C<option_binary()> method behaves identically to C<option()> except
277 that it is binary-clean, so that values containing NUL bytes are set
278 and returned correctly.
279
280 =head4 search() / search_pqf()
281
282  $rs = $conn->search(new ZOOM::Query::CQL('title=dinosaur'));
283  # The next two lines are equivalent
284  $rs = $conn->search(new ZOOM::Query::PQF('@attr 1=4 dinosaur'));
285  $rs = $conn->search_pqf('@attr 1=4 dinosaur');
286
287 The principal purpose of a search-and-retrieve protocol is searching
288 (and, er, retrieval), so the principal method used on a Connection
289 object is C<search()>.  It accepts a single argument, a C<ZOOM::Query>
290 object (or, more precisely, an object of a subclass of this class);
291 and it creates and returns a new ResultSet object representing the set
292 of records resulting from the search.
293
294 Since queries using PQF (Prefix Query Format) are so common, we make
295 them a special case by providing a C<search_prefix()> method.  This is
296 identical to C<search()> except that it accepts a string containing
297 the query rather than an object, thereby obviating the need to create
298 a C<ZOOM::Query::PQF> object.  See the documentation of that class for
299 information about PQF.
300
301 =head4 scan()
302
303 Many Z39.50 servers allow you to browse their indexes to find terms to
304 search for.  This is done using the C<scan> method, which creates and
305 returns a new ScanSet object representing the set of terms resulting
306 from the scan.
307
308 C<scan()> takes a single argument, but it has to work hard: it
309 specifies both what index to scan for terms, and where in the index to
310 start scanning.  What's more, the specification of what index to scan
311 includes multiple facets, such as what database fields it's an index
312 of (author, subject, title, etc.) and whether to scan for whole fields
313 or single words (e.g. the title ``I<The Empire Strikes Back>'', or the
314 four words ``Back'', ``Empire'', ``Strikes'' and ``The'', interleaved
315 with words from other titles in the same index.
316
317 All of this is done by using a single term from the PQF query as the
318 C<scan()> argument.  (At present, only PQF is supported, although
319 there is no reason in principle why CQL and other query syntaxes
320 should not be supported in future).  The attributes associated with
321 the term indicate which index is to be used, and the term itself
322 indicates the point in the index at which to start the scan.  For
323 example, if the argument is C<@attr 1=4 fish>, then
324
325 =over 4
326
327 =item @attr 1=4
328
329 This is the BIB-1 attribute with type 1 (meaning access-point, which
330 specifies an index), and type 4 (which means ``title'').  So the scan
331 is in the title index.
332
333 =item fish
334
335 Start the scan from the lexicographically earliest term that is equal
336 to or falls after ``fish''.
337
338 =back
339
340 The argument C<@attr 1=4 @attr 6=3 fish> would behave similarly; but
341 the BIB-1 attribute 6=3 mean completeness=``complete field'', so the
342 scan would be for complete titles rather than for words occurring in
343 titles.
344
345 This takes a bit of getting used to.
346
347 The behaviour is C<scan()> is affected by the following options, which
348 may be set on the Connection through which the scan is done:
349
350 =over 4
351
352 =item number [default: 10]
353
354 Indicates how many terms should be returned in the ScanSet.  The
355 number actually returned may be less, if the start-point is near the
356 end of the index, but will not be greater.
357
358 =item position [default: 1]
359
360 A 1-based index specifying where in the returned list of terms the
361 seed-term should appear.  By default it should be the first term
362 returned, but C<position> may be set, for example, to zero (requesting
363 the next terms I<after> the seed-term), or to the same value as
364 C<number> (requesting the index terms I<before> the seed term).
365
366 =item stepSize [default: 0]
367
368 An integer indicating how many indexed terms are to be skipped between
369 each one returned in the ScanSet.  By default, no terms are skipped,
370 but overriding this can be useful to get a high-level overview of the
371 index.
372
373 =back
374
375 =head4 package()
376
377  $p = $conn->package();
378  $o = new ZOOM::Options();
379  $o->option(databaseName => "newdb");
380  $p = $conn->package($o);
381
382 Creates and returns a new C<ZOOM::Package>, to be used in invoking an
383 Extended Service.  An options block may optionally be passed in.  See
384 the C<ZOOM::Package> documentation.
385
386 =head4 last_event()
387
388  if ($conn->last_event() == ZOOM::Event::CONNECT) {
389      print "Connected!\n";
390  }
391
392 Returns a C<ZOOM::Event> enumerated value indicating the type of the
393 last event that occurred on the connection.  This is used only in
394 complex asynchronous applications - see the section below on
395 <ZOOM::Event> for more information.
396
397 B<Beware - this method has not been tested>
398
399 =head4 destroy()
400
401  $conn->destroy()
402
403 Destroys a Connection object, tearing down any low-level connection
404 associated with it and freeing its resources.  It is an error to reuse
405 a Connection that has been C<destroy()>ed.
406
407 =head2 ZOOM::ResultSet
408
409  $rs = $conn->search_pqf('@attr 1=4 mineral');
410  $n = $rs->size();
411  for $i (1 .. $n) {
412      $rec = $rs->record($i-1);
413      print $rec->render();
414  }
415
416 A ResultSet object represents the set of zero or more records
417 resulting from a search, and is the means whereby these records can be
418 retrieved.  A ResultSet object may maintain client side cache or some,
419 less, none, all or more of the server's records: in general, this is
420 supposed to an implementaton detail of no interest to a typical
421 application, although more sophisticated applications do have
422 facilities for messing with the cache.  Most applications will only
423 need the C<size()>, C<record()> and C<sort()> methods.
424
425 There is no C<new()> method nor any other explicit constructor.  The
426 only way to create a new ResultSet is by using C<search()> (or
427 C<search_prefix()>) on a Connection.
428
429 See the description of the C<Result Set> class in the ZOOM Abstract
430 API at
431 http://zoom.z3950.org/api/zoom-current.html#3.4
432
433 =head3 Methods
434
435 =head4 option()
436
437  $rs->option(elementSetName => "f");
438
439 Allows options to be set into, and read from, a ResultSet, just like
440 the Connection class's C<option()> method.  There is no
441 C<option_binary()> method for ResultSet objects.
442
443 ResultSet options are listed at
444 http://indexdata.com/yaz/doc/zoom.resultsets.tkl
445
446 =head4 size()
447
448  print "Found ", $rs->size(), " records\n";
449
450 Returns the number of records in the result set.
451
452 =head4 record() / record_immediate()
453
454  $rec = $rs->record(0);
455  $rec2 = $rs->record_immediate(0);
456  $rec3 = $rs->record_immediate(1)
457      or print "second record wasn't in cache\n";
458
459 The C<record()> method returns a C<ZOOM::Record> object representing
460 a record from result-set, whose position is indicated by the argument
461 passed in.  This is a zero-based index, so that legitimate values
462 range from zero to C<$rs->size()-1>.
463
464 The C<record_immediate()> API is identical, but it never invokes a
465 network operation, merely returning the record from the ResultSet's
466 cache if it's already there, or an undefined value otherwise.  So if
467 you use this method, B<you must always check the return value>.
468
469 =head4 records()
470
471  $rs->records(0, 10, 0);
472  for $i (0..10) {
473      print $rs->record_immediate($i)->render();
474  }
475
476  @nextseven = $rs->records(10, 7, 1);
477
478 The C<record_immediate()> method only fetches records from the cache,
479 whereas C<record()> fetches them from the server if they have not
480 already been cached; but the ZOOM module has to guess what the most
481 efficient strategy for this is.  It might fetch each record, alone
482 when asked for: that's optimal in an application that's only
483 interested in the top hit from each search, but pessimal for one that
484 wants to display a whole list of results.  Conversely, the software's
485 strategy might be always to ask for blocks of a twenty records:
486 that's great for assembling long lists of things, but wasteful when
487 only one record is wanted.  The problem is that the ZOOM module can't
488 tell, when you call C<$rs->record()>, what your intention is.
489
490 But you can tell it.  The C<records()> method fetches a sequence of
491 records, all in one go.  It takes three arguments: the first is the
492 zero-based index of the first record in the sequence, the second is
493 the number of records to fetch, and the third is a boolean indication
494 of whether or not to return the retrieved records as well as adding
495 them to the cache.  (You can always pass 1 for this if you like, and
496 Perl will discard the unused return value, but there is a small
497 efficiency gain to be had by passing 0.)
498
499 Once the records have been retrieved from the server
500 (i.e. C<records()> has completed without throwing an exception), they
501 can be fetched much more efficiently using C<record()> - or
502 C<record_immediate()>, which is then guaranteed to succeed.
503
504 =head4 cache_reset()
505
506  $rs->cache_reset()
507
508 Resets the ResultSet's record cache, so that subsequent invocations of
509 C<record_immediate()> will fail.  I struggle to imagine a real
510 scenario where you'd want to do this.
511
512 =head4 sort()
513
514  if ($rs->sort("yaz", "1=4 >i 1=21 >s") < 0) {
515      die "sort failed";
516  }
517
518 Sorts the ResultSet in place (discarding any cached records, as they
519 will in general be sorted into a different position).  There are two
520 arguments: the first is a string indicating the type of the
521 sort-specification, and the second is the specification itself.
522
523 The C<sort()> method returns 0 on success, or -1 if the
524 sort-specification is invalid.
525
526 At present, the only supported sort-specification type is C<yaz>.
527 Such a specification consists of a space-separated sequence of keys,
528 each of which itself consists of two space-separated words (so that
529 the total number of words in the sort-specification is even).  The two
530 words making up each key are a field and a set of flags.  The field
531 can take one of two forms: if it contains an C<=> sign, then it is a
532 BIB-1 I<type>=I<value> pair specifying which field to sort
533 (e.g. C<1=4> for a title sort); otherwise it is sent for the server to
534 interpret as best it can.  The word of flags is made up from one or
535 more of the following: C<s> for case sensitive, C<i> for case
536 insensitive; C<<> for ascending order and C<E<gt>> for descending
537 order.
538
539 For example, the sort-specification in the code-fragment above will
540 sort the records in C<$rs> case-insensitively in descending order of
541 title, with records having equivalent titles sorted case-sensitively
542 in ascending order of subject.  (The BIB-1 access points 4 and 21
543 represent title and subject respectively.)
544  
545 =head4 destroy()
546
547  $rs->destroy()
548
549 Destroys a ResultSet object, freeing its resources.  It is an error to
550 reuse a ResultSet that has been C<destroy()>ed.
551
552 =head2 ZOOM::Record
553
554  $rec = $rs->record($i);
555  print $rec->render();
556  $raw = $rec->raw();
557  $marc = new_from_usmarc MARC::Record($raw);
558  print "Record title is: ", $marc->title(), "\n";
559
560 A Record object represents a record that has been retrived from the
561 server.
562
563 There is no C<new()> method nor any other explicit constructor.  The
564 only way to create a new Record is by using C<record()> (or
565 C<record_immediate()>, or C<records()>) on a ResultSet.
566
567 In general, records are ``owned'' by their result-sets that they were
568 retrieved from, so they do not have to be explicitly memory-managed:
569 they are deallocated (and therefore can no longer be used) when the
570 result-set is destroyed.
571
572 See the description of the C<Record> class in the ZOOM Abstract
573 API at
574 http://zoom.z3950.org/api/zoom-current.html#3.5
575
576 =head3 Methods
577
578 =head4 render()
579
580  print $rec->render()
581
582 Returns a human-readable representation of the record.  Beyond that,
583 no promises are made: careful programs should not make assumptions
584 about the format of the returned string.
585
586 This method is useful mostly for debugging.
587
588 =head4 raw()
589
590  use MARC::Record
591  $raw = $rec->raw();
592  $marc = new_from_usmarc MARC::Record($raw);
593
594 Returns an opaque blob of data that is the raw form of the record.
595 Exactly what this is, and what you can do with it, varies depending on
596 the record-syntax.  For example, XML records will be returned as,
597 well, XML; MARC records will be returned as ISO 2709-encoded blocks
598 that can be decoded by software such as the fine C<Marc::Record>
599 module; GRS-1 record will be ... gosh, what an interesting question.
600 But no-one uses GRS-1 any more, do they?
601
602 =head4 clone() / destroy()
603
604  $rec = $rs->record($i);
605  $newrec = $rec->clone();
606  $rs->destroy();
607  print $newrec->render();
608  $newrec->destroy();
609
610 Usually, it's convenient that Record objects are owned by their
611 ResultSets and go away when the ResultSet is destroyed; but
612 occasionally you need a Record to outlive its parent and destroy it
613 later, explicitly.  To do this, C<clone()> the record, keep the new
614 Record object that is returned, and C<destroy()> it when it's no
615 longer needed.  This is B<only> situation in which a Record needs to
616 be destroyed.
617
618 =head2 ZOOM::Exception
619
620 In general, method calls throw an exception (of class
621 C<ZOOM::Exception>) if anything goes wrong, so you don't need to test
622 for success after each call.  Exceptions are caught by enclosing the
623 main code in an C<eval{}> block and checking C<$@> on exit from that
624 block, as in the code-sample above.
625
626 There are a small number of exceptions to this rule: the three
627 record-fetching methods in the C<ZOOM::ResultSet> class,
628 C<record()>,
629 C<record_immediate()>,
630 and
631 C<records()>
632 can all return undefined values for legitimate reasons, under
633 circumstances that do not merit throwing an exception.  For this
634 reason, the return values of these methods should be checked.  See the
635 individual methods' documentation for details.
636
637 An exception carries the following pieces of information:
638
639 =over 4
640
641 =item error-code
642
643 A numeric code that specifies the type of error.  This can be checked
644 for equality with known values, so that intelligent applications can
645 take appropriate action.
646
647 =item error-message
648
649 A human-readable message corresponding with the code.  This can be
650 shown to users, but its value should not be tested, as it could vary
651 in different versions or under different locales.
652
653 =item additional information [optional]
654
655 A string containing information specific to the error-code.  For
656 example, when the error-code is the BIB-1 diagnostic 109 ("Database
657 unavailable"), the additional information is the name of the database
658 that the application tried to use.  For some error-codes, there is no
659 additional information at all; for some others, the additional
660 information is undefined and may just be an human-readable string.
661
662 =item diagnostic set [optional]
663
664 A short string specifying the diagnostic set from which the error-code
665 was drawn: for example, C<ZOOM> for a ZOOM-specific error such as
666 C<ZOOM::Error::MEMORY> ("out of memory"), and C<BIB-1> for a Z39.50
667 error-code drawn from the BIB-1 diagnostic set.
668
669 =back
670
671 In theory, the error-code should be interpreted in the context of the
672 diagnostic set from which it is drawn; in practice, nearly all errors
673 are from either the ZOOM or BIB-1 diagnostic sets, and the codes in
674 those sets have been chosen so as not to overlap, so the diagnostic
675 set can usually be ignored.
676
677 See the description of the C<Exception> class in the ZOOM Abstract
678 API at
679 http://zoom.z3950.org/api/zoom-current.html#3.7
680
681 =head3 Methods
682
683 =head4 new()
684
685  die new ZOOM::Exception($errcode, $errmsg, $addinfo, $diagset);
686
687 Creates and returns a new Exception object with the specified
688 error-code, error-message, additional information and diagnostic set.
689 Applications will not in general need to use this, but may find it
690 useful to simulate ZOOM exceptions.  As is usual with Perl, exceptions
691 are thrown using C<die()>.
692
693 =head4 code() / message() / addinfo() / diagset()
694
695  print "Error ", $@->code(), ": ", $@->message(), "\n";
696  print "(addinfo '", $@->addinfo(), "', set '", $@->diagset(), "')\n";
697
698 These methods, of no arguments, return the exception's error-code,
699 error-message, additional information and diagnostic set respectively.
700
701 =head4 render()
702
703  print $@->render();
704
705 Returns a human-readable rendition of an exception.  The C<"">
706 operator is overloaded on the Exception class, so that an Exception
707 used in a string context is automatically rendered.  Among other
708 consequences, this has the useful result that a ZOOM application that
709 died due to an uncaught exception will emit an informative message
710 before exiting.
711
712 =head2 ZOOM::ScanSet
713
714  $ss = $conn->scan('@attr 1=1003 a');
715  $n = $ss->size();
716  ($term, $occ) = $ss->term($n-1);
717  $rs = $conn->search_pqf('@attr 1=1003 "' . $term . "'");
718  assert($rs->size() == $occ);
719
720 A ScanSet represents a set of candidate search-terms returned from an
721 index scan.  Its sole purpose is to provide access to those term, to
722 the corresponding display terms, and to the occurrence-counts of the
723 terms.
724
725 There is no C<new()> method nor any other explicit constructor.  The
726 only way to create a new ScanSet is by using C<scan()> on a
727 Connection.
728
729 See the description of the C<Scan Set> class in the ZOOM Abstract
730 API at
731 http://zoom.z3950.org/api/zoom-current.html#3.6
732
733 =head3 Methods
734
735 =head4 size()
736
737  print "Found ", $ss->size(), " terms\n";
738
739 Returns the number of terms in the scan set.  In general, this will be
740 the scan-set size requested by the C<number> option in the Connection
741 on which the scan was performed [default 10], but it may be fewer if
742 the scan is close to the end of the index.
743
744 =head4 term() / display_term()
745
746  $ss = $conn->scan('@attr 1=1004 whatever');
747  ($term, $occurrences) = $ss->term(0);
748  ($displayTerm, $occurrences2) = $ss->display_term(0);
749  assert($occurrences == $occurrences2);
750  if (user_likes_the_look_of($displayTerm)) {
751      $rs = $conn->search_pqf('@attr 1=4 "' . $term . '"');
752      assert($rs->size() == $occurrences);
753  }
754
755 These methods return the scanned terms themselves.  C<term()> returns
756 the term is a form suitable for submitting as part of a query, whereas
757 C<display_term()> returns it in a form suitable for displaying to a
758 user.  Both versions also return the number of occurrences of the term
759 in the index, i.e. the number of hits that will be found if the term
760 is subsequently used in a query.
761
762 In most cases, the term and display term will be identical; however,
763 they may be different in cases where punctuation or case is
764 normalised, or where identifiers rather than the original document
765 terms are indexed.
766
767 =head4 option()
768
769  print "scan status is ", $ss->option("scanStatus");
770
771 Allows options to be set into, and read from, a ScanSet, just like
772 the Connection class's C<option()> method.  There is no
773 C<option_binary()> method for ScanSet objects.
774
775 ScanSet options are also described, though not particularly
776 informatively, at
777 http://indexdata.com/yaz/doc/zoom.scan.tkl
778
779 =head4 destroy()
780
781  $ss->destroy()
782
783 Destroys a ScanSet object, freeing its resources.  It is an error to
784 reuse a ScanSet that has been C<destroy()>ed.
785
786 =head2 ZOOM::Package
787
788  $p = $conn->package();
789  $p->option(action => "specialUpdate");
790  $p->option(recordIdOpaque => 145);
791  $p->option(record => content_of("/tmp/record.xml"));
792  $p->send("update");
793  $p->destroy();
794
795 This class represents an Extended Services Package: an instruction to
796 the server to do something not covered by the core parts of the Z39.50
797 standard (or the equivalent in SRW or SRU).  Since the core protocols
798 are read-only, such requests are often used to make changes to the
799 database, such as in the record update example above.
800
801 Requesting an extended service is a four-step process: first, create a
802 package associated with the connection to the relevant database;
803 second, set options on the package to instruct the server on what to
804 do; third, send the package (which may result in an exception being
805 thrown if the server cannot execute the requested operations; and
806 finally, destroy the package.
807
808 Package options are listed at
809 http://indexdata.com/yaz/doc/zoom.ext.html
810
811 The particular options that have meaning are determined by the
812 top-level operation string specified as the argument to C<send()>.
813 For example, when the operation is C<update> (the most commonly used
814 extended service), the C<action> option may be set to any of
815 C<recordInsert>
816 (add a new record, failing if that record already exists),
817 C<recordDelete>
818 (delete a record, failing if it is not in the database).
819 C<recordReplace>
820 (replace a record, failing if an old version is not already present)
821 or
822 C<specialUpdate>
823 (add a record, replacing any existing version that may be present).
824
825 For update, the C<record> option should be set to the full text of the
826 XML record to added, deleted or replaced.  Depending on how the server
827 is configured, it may extract the record's unique ID from the text
828 (i.e. from a known element such as the C<001> field of a MARCXML
829 record), or it may require the unique ID to passed in explicitly using
830 the C<recordIdOpaque> option.
831
832 Extended services packages are B<not currently described> in the ZOOM
833 Abstract API at
834 http://zoom.z3950.org/api/zoom-current.html
835 They will be added in a forthcoming version, and will function much
836 as those implemented in this module.
837
838 =head3 Methods
839
840 =head4 option()
841
842  $p->option(recordIdOpaque => "46696f6e61");
843
844 Allows options to be set into, and read from, a Package, just like
845 the Connection class's C<option()> method.  There is no
846 C<option_binary()> method for Package objects.
847
848 Package options are listed at
849 http://indexdata.com/yaz/doc/zoom.ext.tkl
850
851 =head4 send()
852
853  $p->send("createdb");
854
855 Sends a package to the server associated with the Connection that
856 created it.  Problems are reported by throwing an exception.  The
857 single parameter indicates the operation that the server is being
858 requested to perform, and controls the interpretation of the package's
859 options.  Valid operations include:
860
861 =over 4
862
863 =item itemorder
864
865 Request a copy of a nominated object, e.g. place an ILL request.
866
867 =item create
868
869 Create a new database, the name of which is specified by the
870 C<databaseName> option.
871
872 =item drop
873
874 Drop an existing database, the name of which is specified by the
875 C<databaseName> option.
876
877 =item commit
878
879 Commit changes made to the database within a transaction.
880
881 =item update
882
883 Modify the contents of the database by adding, deleting or replacing
884 records (as described above in the overview of the C<ZOOM::Package>
885 class).
886
887 =item xmlupdate
888
889 I have no idea what this does.
890
891 =back
892
893 Although the module is capable of I<making> all these requests, not
894 all servers are capable of I<executing> them.  Refusal is indicated by
895 throwing an exception.  Problems may also be caused by lack of
896 privileges; so C<send()> must be used with caution, and is perhaps
897 best wrapped in a clause that checks for execptions, like so:
898
899  eval { $p->send("create") };
900  if ($@ && $@->isa("ZOOM::Exception")) {
901      print "Oops!  ", $@->message(), "\n";
902      return $@->code();
903  }
904
905 =head4 destroy()
906
907  $p->destroy()
908
909 Destroys a Package object, freeing its resources.  It is an error to
910 reuse a Package that has been C<destroy()>ed.
911
912 =head2 ZOOM::Query
913
914  $q = new ZOOM::Query::CQL("creator=pike and subject=unix");
915  $q->sortby("1=4 >i 1=21 >s");
916  $rs = $conn->search($q);
917  $q->destroy();
918
919 C<ZOOM::Query> is a virtual base class from which various concrete
920 subclasses can be derived.  Different subclasses implement different
921 types of query.  The sole purpose of a Query object is to be used in a
922 C<search()> on a Connection; because PQF is such a common special
923 case, the shortcut Connection method C<search_pqf()> is provided.
924
925 The following Query subclasses are provided, both of the providing the
926 same set of methods described below:
927
928 =over 4
929
930 =item ZOOM::Query::PQF
931
932 Implements Prefix Query Format (PQF), also sometimes known as Prefix
933 Query Notation (PQN).  This esoteric but rigorous and expressive
934 format is described in the YAZ Manual at
935 http://indexdata.com/yaz/doc/tools.tkl#PQF
936
937 =item ZOOM::Query::CQL
938
939 Implements the Common Query Language (CQL) of SRU, the Search/Retrieve
940 URL.  CQL is a much friendlier notation than PQF, using a simple infix
941 notation.  The queries are passed ``as is'' to the server rather than
942 being compiled into a Z39.50 Type-1 query, so only CQL-compliant
943 servers can support such querier.  CQL is described at
944 http://www.loc.gov/standards/sru/cql/
945 and in a slight out-of-date but nevertheless useful tutorial at
946 http://zing.z3950.org/cql/intro.html
947
948 =back
949
950 See the description of the C<Query> class in the ZOOM Abstract
951 API at
952 http://zoom.z3950.org/api/zoom-current.html#3.3
953
954 =head3 Methods
955
956 =head4 new()
957
958  $q = new ZOOM::Query::CQL('title=dinosaur'));
959  $q = new ZOOM::Query::PQF('@attr 1=4 dinosaur'));
960
961 Creates a new query object, compiling the query passed as its argument
962 according to the rules of the particular query-type being
963 instantiated.  If compilation fails, an exception is thrown.
964 Otherwise, the query may be passed to the C<Connection> method
965 <search()>.
966
967 =head4 sortby()
968
969  $q->sortby("1=4 >i 1=21 >s");
970
971 Sets a sort specification into the query, so that when a C<search()>
972 is run on the query, the result is automatically sorted.  The sort
973 specification language is the same as the C<yaz> sort-specification
974 type of the C<ResultSet> method C<sort()>, described above.
975
976 B<It ought to be possible to sort by CQL query, too, but at present
977 limitations in the underlying ZOOM-C library make this impossible.>
978
979 =head4 destroy()
980
981  $p->destroy()
982
983 Destroys a Query object, freeing its resources.  It is an error to
984 reuse a Query that has been C<destroy()>ed.
985
986 =head2 ZOOM::Options
987
988  $o1 = new ZOOM::Options();
989  $o1->option(user => "alf");
990  $o2 = new ZOOM::Options();
991  $o2->option(password => "fruit");
992  $opts = new ZOOM::Options($o1, $o2);
993  $conn = create ZOOM::Connection($opts);
994  $conn->connect($host); # Uses the specified username and password
995
996 Several classes of ZOOM objects carry their own sets of options, which
997 can be manipulated using their C<option()> method.  Sometimes,
998 however, it's useful to deal with the option sets directly, and the
999 C<ZOOM::Options> class exists to enable this approach.
1000
1001 Option sets are B<not currently described> in the ZOOM
1002 Abstract API at
1003 http://zoom.z3950.org/api/zoom-current.html
1004 They are an extension to that specification.
1005
1006 =head3 Methods
1007
1008 =head4 new()
1009
1010  $o1 = new ZOOM::Options();
1011  $o1and2 = new ZOOM::Options($o1);
1012  $o3 = new ZOOM::Options();
1013  $o1and3and4 = new ZOOM::Options($o1, $o3);
1014
1015 Creates and returns a new option set.  One or two (but no more)
1016 existing option sets may be passed as arguments, in which case they
1017 become ``parents'' of the new set, which thereby ``inherits'' their
1018 options, the values of the first parent overriding those of the second
1019 when both have a value for the same key.  An option set that inherits
1020 from a parent that has its own parents also inherits the grandparent's
1021 options, and so on.
1022
1023 =head4 option() / option_binary()
1024
1025  $o->option(preferredRecordSyntax => "usmarc");
1026  $o->option_binary(iconBlob => "foo\0bar");
1027  die if length($o->option_binary("iconBlob") != 7);
1028
1029 These methods are used to get and set options within a set, and behave
1030 the same way as the same-named C<Connection> methods - see above.  As
1031 with the C<Connection> methods, values passed to and retrieved using
1032 C<option()> are interpreted as NUL-terminated, while those passed to
1033 and retrieved from C<option_binary()> are binary-clean.
1034
1035 =head4 bool()
1036
1037  $o->option(x => "T");
1038  $o->option(y => "F");
1039  assert($o->bool("x", 1));
1040  assert(!$o->bool("y", 1));
1041  assert($o->bool("z", 1));
1042
1043 The first argument is a key, and the second is a default value.
1044 Returns the value associated with the specified key as a boolean, or
1045 the default value if the key has not been set.  The values C<T> (upper
1046 case) and C<1> are considered true; all other values (including C<t>
1047 (lower case) and non-zero integers other than one) are considered
1048 false.
1049
1050 This method is provided in ZOOM-C because in a statically typed
1051 language it's convenient to have the result returned as an
1052 easy-to-test type.  In a dynamically typed language such as Perl, this
1053 problem doesn't arise, so C<bool()> is nearly useless; but it is made
1054 available in case applications need to duplicate the idiosyncratic
1055 interpretation of truth and falsehood and ZOOM-C uses.
1056
1057 =head4 int()
1058
1059  $o->option(x => "012");
1060  assert($o->int("x", 20) == 12);
1061  assert($o->int("y", 20) == 20);
1062
1063 Returns the value associated with the specified key as an integer, or
1064 the default value if the key has not been set.  See the description of
1065 C<bool()> for why you almost certainly don't want to use this.
1066
1067 =head4 set_int()
1068
1069  $o->set_int(x => "29");
1070
1071 Sets the value of the specified option as an integer.  Of course, Perl
1072 happily converts strings to integers on its own, so you can just use
1073 C<option()> for this, but C<set_int()> is guaranteed to use the same
1074 string-to-integer conversion as ZOOM-C does, which might occasionally
1075 be useful.  Though I can't imagine how.
1076
1077 =head4 set_callback()
1078
1079 I<###>
1080
1081 =head4 destroy()
1082
1083  $o->destroy()
1084
1085 Destroys an Options object, freeing its resources.  It is an error to
1086 reuse an Options object that has been C<destroy()>ed.
1087
1088 =head1 ENUMERATIONS
1089
1090 The ZOOM module provides two enumerations that list possible return
1091 values from particular functions.  They are described in the following
1092 sections.
1093
1094 =head2 ZOOM::Error
1095
1096  if ($@->code() == ZOOM::Error::QUERY_PQF) {
1097      return "your query was not accepted";
1098  }
1099
1100 This class provides a set of manifest constants representing some of
1101 the possible error codes that can be raised by the ZOOM module.  The
1102 methods that return error-codes are
1103 C<ZOOM::Exception::code()>,
1104 C<ZOOM::Connection::error_x()>
1105 and
1106 C<ZOOM::Connection::errcode()>.
1107
1108 The C<ZOOM::Error> class provides the constants
1109 C<NONE>,
1110 C<CONNECT>,
1111 C<MEMORY>,
1112 C<ENCODE>,
1113 C<DECODE>,
1114 C<CONNECTION_LOST>,
1115 C<INIT>,
1116 C<INTERNAL>,
1117 C<TIMEOUT>,
1118 C<UNSUPPORTED_PROTOCOL>,
1119 C<UNSUPPORTED_QUERY>,
1120 C<INVALID_QUERY>,
1121 C<CREATE_QUERY>,
1122 C<QUERY_CQL>,
1123 C<QUERY_PQF>,
1124 C<SORTBY>,
1125 C<CLONE>,
1126 C<PACKAGE>
1127 and
1128 C<SCANTERM>,
1129 each of which specifies a client-side error.  These codes constitute
1130 the C<ZOOM> diagnostic set.
1131
1132 Since errors may also be diagnosed by the server, and returned to the
1133 client, error codes may also take values from the BIB-1 diagnostic set
1134 of Z39.50, listed at the Z39.50 Maintenance Agency's web-site at
1135 http://www.loc.gov/z3950/agency/defns/bib1diag.html
1136
1137 All error-codes, whether client-side from the C<ZOOM::Error>
1138 enumeration or server-side from the BIB-1 diagnostic set, can be
1139 translated into human-readable messages by passing them to the
1140 C<ZOOM::diag_str()> utility function.
1141
1142 =head2 ZOOM::Event
1143
1144  if ($conn->last_event() == ZOOM::Event::CONNECT) {
1145      print "Connected!\n";
1146  }
1147
1148 In applications that need it - mostly complex multiplexing
1149 applications - The C<ZOOM::Connection::last_event()> method is used to
1150 return an indication of the last event that occurred on a particular
1151 connection.  It always returns a value drawn from this enumeration,
1152 that is, one of C<NONE>, C<CONNECT>, C<SEND_DATA>, C<RECV_DATA>,
1153 C<TIMEOUT>, C<UNKNOWN>, C<SEND_APDU>, C<RECV_APDU>, C<RECV_RECORD> or
1154 C<RECV_SEARCH>.
1155
1156 You almost certainly don't need to know about this.  Frankly, I'm not
1157 sure how to use it myself.
1158
1159 =head1 SEE ALSO
1160
1161 The ZOOM abstract API,
1162 http://zoom.z3950.org/api/zoom-current.html
1163
1164 The C<Net::Z3950::ZOOM> module, included in the same distribution as this one.
1165
1166 The C<Net::Z3950> module, which this one supersedes.
1167 http://perl.z3950.org/
1168
1169 The documentation for the ZOOM-C module of the YAZ Toolkit, which this
1170 module is built on.  Specifically, its lists of options are useful.
1171 http://indexdata.com/yaz/doc/zoom.tkl
1172
1173 The BIB-1 diagnostic set of Z39.50,
1174 http://www.loc.gov/z3950/agency/defns/bib1diag.html
1175
1176 =head1 AUTHOR
1177
1178 Mike Taylor, E<lt>mike@indexdata.comE<gt>
1179
1180 =head1 COPYRIGHT AND LICENCE
1181
1182 Copyright (C) 2005 by Index Data.
1183
1184 This library is free software; you can redistribute it and/or modify
1185 it under the same terms as Perl itself, either Perl version 5.8.4 or,
1186 at your option, any later version of Perl 5 you may have available.
1187
1188 =cut
1189
1190 1;