ZOOM: record objects "owned" by result sets.
[yaz-moved-to-github.git] / doc / zoom.xml
1 <!-- $Id: zoom.xml,v 1.9 2001-11-15 08:58:28 adam Exp $ -->
2  <chapter id="zoom"><title>Building clients with ZOOM</title>
3   
4   <para>
5     &zoom; is an acronym for 'Z39.50 Object-Orientation Model' and is
6    an initiative started by Mike Taylor (Mike is from the UK, which
7    explains the peculiar name of the model). The goal of &zoom; is to
8    provide a common Z39.50 client API not bound to a particular
9    programming language or toolkit.
10   </para>
11   <para>
12    The lack of a simple Z39.50 client API for &yaz; has become more
13    and more apparent over time. So when the first &zoom; specification
14    became available,
15    an implementation for &yaz; was quickly developed. For the first time, it is
16    now as easy (or easier!) to develop clients than servers with &yaz;. This
17    chapter describes the &zoom; C binding. Before going futher, please
18    reconsider whether C is the right programming language for the job.
19    There are other language bindings available for &yaz;, and still
20    more
21    are in active development. See the
22    <ulink url="http://zoom.z3950.org/">ZOOM website</ulink> for
23    more information.
24   </para>
25
26   <para>
27    In order to fully understand this chapter you should read and
28    try the example programs <literal>zoomtst1.c</literal>,
29    <literal>zoomtst2.c</literal>, .. in the <literal>zoom</literal>
30    directory.
31   </para>
32
33   <para>
34    The C language misses many features found in object oriented languages
35    such as C++, Java, etc. For example, you'll have to manually,
36    destroy all objects you create, even though you may think of them as
37    temporary. Most objects has a <literal>_create</literal> - and a
38    <literal>_destroy</literal> variant.
39    All objects are in fact pointers to internal stuff, but you don't see
40    that because of typedefs. All destroy methods should gracefully ignore a
41    <literal>NULL</literal> pointer.
42   </para>
43   <para>
44    In each of the sections below you'll find a sub section called
45    protocol behavior, that descries how the API maps to the Z39.50
46    protocol.
47   </para>
48   <sect1 id="zoom.connections"><title>Connections</title>
49    
50    <para>The Connection object is a session with a target.
51    </para>
52    <synopsis>
53    #include &lt;yaz/zoom.h>
54     
55    Z3950_connection Z3950_connection_new (const char *host, int portnum);
56     
57    Z3950_connection Z3950_connection_create (Z3950_options options);
58
59    void Z3950_connection_connect(Z3950_connection c, const char *host,
60                                  int portnum);
61    void Z3950_connection_destroy (Z3950_connection c);
62    </synopsis>
63    <para>
64     Connection objects are created with either function
65     <function>Z3950_connection_new</function> or 
66     <function>Z3950_connection_create</function>.
67     The former creates and automatically attempts to establish a network
68     connection with the target. The latter doesn't establish
69     a connection immediately, thus allowing you to specify options
70     before establishing network connection using the function
71     <function>Z3950_connection_connect</function>. 
72     If the portnumber, <literal>portnum</literal>, is zero, the
73     <literal>host</literal> is consulted for a port specification.
74     If no port is given, 210 is used. A colon denotes the beginning of
75     a port number in the host string. If the host string includes a
76     slash, the following part specifies a database for the connection.
77    </para>
78    <para>
79     Connection objects should be destroyed using the function
80     <function>Z3950_connection_destroy</function>.
81    </para>
82    <synopsis>
83     const char *Z3950_connection_option (Z3950_connection c,
84                                          const char *key,
85                                          const char *val);
86     const char *Z3950_connection_host (Z3950_connection c);
87    </synopsis>
88    <para>
89     The <function>Z3950_connection_option</function> allows you to
90     inspect or set an option given by <parameter>key</parameter>
91     for the connection.
92     If <parameter>val</parameter> is non-<literal>NULL</literal> that
93     holds the new value for option.
94     Otherwise, if <parameter>val</parameter> is
95     <literal>NULL</literal>,
96     the option is unchanged.
97     The function returns the (new) value of the option.
98    </para>
99    <table frame="top"><title>ZOOM Connection Options</title>
100     <tgroup cols="3">
101      <colspec colwidth="4*" colname="name"></colspec>
102      <colspec colwidth="7*" colname="description"></colspec>
103      <colspec colwidth="3*" colname="default"></colspec>
104      <thead>
105       <row>
106        <entry>Option</entry>
107        <entry>Description</entry>
108        <entry>Default</entry>
109       </row>
110      </thead>
111      <tbody>
112       <row><entry>
113         implementationName</entry><entry>Name of Your client
114        </entry><entry>none</entry></row>
115       <row><entry>
116         user</entry><entry>Authentication user name
117        </entry><entry>none</entry></row>
118       <row><entry>
119         group</entry><entry>Authentication group name
120        </entry><entry>none</entry></row>
121       <row><entry>
122         pass</entry><entry>Authentication password
123       </entry><entry>none</entry></row>
124       <row><entry>
125         proxy</entry><entry>Proxy host
126        </entry><entry>none</entry></row>
127       <row><entry>
128         async</entry><entry>If true (1) the connection operates in 
129         asynchronous operation which means that all calls are non-blocking
130         except
131         <link linkend="zoom.events"><function>Z3950_event</function></link>.
132        </entry><entry>0</entry></row>
133       <row><entry>
134         maximumRecordSize</entry><entry> Maximum size of single record.
135        </entry><entry>1 MB</entry></row>
136       <row><entry>
137         preferredMessageSize</entry><entry> Maximum size of multiple records.
138        </entry><entry>1 MB</entry></row>
139      </tbody>
140     </tgroup>
141    </table>
142    <para>
143     Function <function>Z3950_connection_host</function> returns
144      the host for the connection as specified in a call to
145     <function>Z3950_connection_new</function> or 
146     <function>Z3950_connection_connect</function>.
147     This function returns <literal>NULL</literal> if host isn't
148     set for the connection.
149    </para>
150    <synopsis>
151      int Z3950_connection_error (Z3950_connection c, const char **cp,
152                                  const char **addinfo);
153    </synopsis>
154    <para>
155     Use <function>Z3950_connection_error</function> to check for
156     errors for the last operation(s) performed. The function returns
157     zero if no errors occurred; non-zero otherwise indicating the error.
158     Pointers <parameter>cp</parameter> and <parameter>addinfo</parameter>
159     holds messages for the error and additional-info if passed as
160     non-<literal>NULL</literal>.
161    </para>
162    <sect2><title>Protocol behavior</title>
163     <para>
164      The calls <function>Z3950_connection_new</function> and
165      <function>Z3950_connection_connect</function> establises a TCP/IP
166       connection and sends an Initialize Request to the target if
167       possible. In addition, the calls waits for an Initialize Response
168       from the target and the result is inspected (OK or rejected).
169     </para>
170     <para>
171      If <literal>proxy</literal> is set then the client will establish
172      a TCP/IP connection with the peer as specified by the
173      <literal>proxy</literal> host and the hostname as part of the
174      connect calls will be set as part of the Initialize Request.
175      The proxy server will then "forward" the PDU's transparently
176      to the target behind the proxy.
177     </para>
178     <para>
179      For the authentication parameters, if option <literal>user</literal>
180      is set and both options <literal>group</literal> and
181      <literal>pass</literal> are unset, then Open style
182      authentication is used (Version 2/3) in which case the username
183      is usually followed by a slash, then by a password.
184      If either <literal>group</literal>
185      or <literal>pass</literal> is set then idPass authentication
186      (Version 3 only) is used. If none of the options are set, no
187      authentication parameters are set as part of the Initialize Request
188      (obviously).
189     </para>
190     <para>
191      When option <literal>async</literal> is 1, it really means that
192      all network operations are postponed (and queued) until the
193      function <literal>Z3950_event</literal> is invoked. When doing so
194      it doesn't make sense to check for errors after
195      <literal>Z3950_connection_new</literal> is called since that
196      operation "connecting - and init" is still incomplete and the
197      API cannot tell the outcome (yet).
198     </para>
199     </sect2>
200   </sect1>
201   <sect1 id="zoom.query"><title>Queries</title>
202    <para>
203     Query objects represents queries.
204    </para>
205    <synopsis>
206      Z3950_query Z3950_query_create(void);
207
208      void Z3950_query_destroy(Z3950_query q);
209
210      int Z3950_query_prefix(Z3950_query q, const char *str);
211
212      int Z3950_query_sortby(Z3950_query q, const char *criteria);
213    </synopsis>
214    <para>
215     Create query objects using <function>Z3950_query_create</function>
216     and destroy them by calling <function>Z3950_query_destroy</function>.
217     RPN-queries can be specified in <link linkend="PQF">PQF</link>
218     notation by using the
219     function <function>Z3950_query_prefix</function>. More
220     query types will be added later, such as
221     <link linkend="CCL">CCL</link> to RPN-mapping, native CCL query,
222     etc. In addition to a search, a sort criteria may be set. Function
223     <function>Z3950_query_sortby</function> specifies a 
224     sort criteria using the same string notation for sort as offered by
225     the <link linkend="sortspec">YAZ client</link>.
226    </para>
227    <sect2><title>Protocol behavior</title>
228     <para>
229      The query object is just an interface for the member Query
230      in the SearchRequest. The sortby-function is an interface to the
231      sortSequence member of the SortRequest.
232     </para>
233    </sect2>
234   </sect1>
235   <sect1 id="zoom.resultsets"><title>Result sets</title>
236    <para>
237     The result set object is a container for records returned from
238     a target.
239    </para>
240    <synopsis>
241      Z3950_resultset Z3950_connection_search(Z3950_connection,
242                                              Z3950_query q);
243
244      Z3950_resultset Z3950_connection_search_pqf(Z3950_connection c,
245                                                  const char *q);
246
247      void Z3950_resultset_destroy(Z3950_resultset r);
248    </synopsis>
249    <para>
250     Function <function>Z3950_connection_search</function> creates
251      a result set given a connection and query.
252     Destroy a result set by calling
253     <function>Z3950_resultset_destroy</function>.
254     Simple clients may using PQF only may use function
255     <function>Z3950_connection_search_pqf</function> in which case
256     creating query objects is not necessary.
257    </para>
258    <synopsis>
259      const char *Z3950_resultset_option (Z3950_resultset r,
260                                          const char *key,
261                                          const char *val);
262
263      size_t Z3950_resultset_size (Z3950_resultset r);
264    </synopsis>
265    <para>
266     Function <function>Z3950_resultset_options</function> sets or
267     modifies an option for a result set similar to 
268     <function>Z3950_connection_option</function>.
269    </para>
270    <para>
271     The number of hits also called result-count is returned by
272     function <function>Z3950_resultset_size</function>.
273    </para>
274    <table frame="top"><title>ZOOM Result set Options</title>
275     <tgroup cols="3">
276      <colspec colwidth="4*" colname="name"></colspec>
277      <colspec colwidth="7*" colname="description"></colspec>
278      <colspec colwidth="2*" colname="default"></colspec>
279      <thead>
280       <row>
281        <entry>Option</entry>
282        <entry>Description</entry>
283        <entry>Default</entry>
284       </row>
285      </thead>
286      <tbody>
287       <row><entry>
288         piggyback</entry><entry>True (1) if piggyback should be
289         used in searches; false (0) if not.
290        </entry><entry>1</entry></row>
291       <row><entry>
292         start</entry><entry>Offset of first record to be 
293         retrieved from target. First record has offset 0 unlike the
294         protocol specifications where first record has position 1.
295        </entry><entry>0</entry></row>
296       <row><entry>
297         count</entry><entry>Number of records to be retrieved.
298        </entry><entry>0</entry></row>
299       <row><entry>
300         elementSetName</entry><entry>Element-Set name of records. 
301         Most targets should honor element set name <literal>B</literal>
302         and <literal>F</literal> for brief and full respectively.
303        </entry><entry>none</entry></row>
304       <row><entry>
305         preferredRecordSyntax</entry><entry>Preferred Syntax, such as
306         <literal>USMARC</literal>, <literal>SUTRS</literal>, etc.
307        </entry><entry>none</entry></row>
308       <row><entry>
309         smallSetUpperBound</entry><entry>If hits is less than or equal to this
310         value, then target will return all records using small element set name
311        </entry><entry>0</entry></row>
312       <row><entry>
313         largeSetLowerBound</entry><entry>If hits is greator than this
314         value, the target will return no records.
315        </entry><entry>1</entry></row>
316       <row><entry>
317         mediumSetPresentNumber</entry><entry>This value represents
318         the number of records to be returned as part of a search when when
319         hits is less than or equal to large set lower bound and if hits
320         is greator than small set upper bound.
321        </entry><entry>0</entry></row>
322       <row><entry>
323         smallSetElementSetName</entry><entry>
324         The element set name to be used for small result sets.
325        </entry><entry>none</entry></row>
326       <row><entry>
327         mediumSetElementSetName</entry><entry>
328         The element set name to be for medium-sized result sets.
329        </entry><entry>none</entry></row>
330       <row><entry>
331         databaseName</entry><entry>One or more database names
332         separated by character plus (<literal>+</literal>).
333        </entry><entry>Default</entry></row>
334      </tbody>
335     </tgroup>
336    </table>
337    <sect2>
338     <title>Protocol behavior</title>
339     <para>
340      The creation of a result set involves at least a SearchRequest
341      - SearchResponse protocol handshake. Following that, if a sort
342      critieria was specified as part of the query, a sortRequest -
343      SortResponse handshake takes place. Note that it is necessary to
344      perform sorting before any retrieval takes place, so no records will
345      be returned from the target as part of the SearchResponse because these
346      would be unsorted. Hence, piggyback is disabled when sort critieria
347      is set. Following Search - and a Possible sort, Retrieval takes
348      place - as one or more Present Requests - Present Response being
349      transferred.
350      </para>
351     <para>
352      The API allows for two different modes for retrieval. A high level
353      mode which is somewhat more powerful and a low level one.
354      The low level is "enabled" when the settings
355      <literal>smallSetUpperBound</literal>,
356      <literal>mediumSetPresentNumber</literal> and
357      <literal>largeSetLowerBound</literal> are set. The low level mode
358      thus allows you to precisely set how records are returned as part
359      of a search response as offered by the Z39.50 protocol.
360      Since the client may be retrieving records as part of the
361      search response, this mode doesn't work well if sorting is used.
362      </para>
363     <para>
364      The high-level mode allows you to fetch a range of records from
365      the result set with a given start offset. When you use this mode
366      the client will automatically use piggyback if that is possible
367      with the target and perform one or more present requests as needed.
368      Even if the target returns fewer records as part of a present response
369      because of a record size limit, etc. the client will repeat sending
370      present requests. As an example, if option <literal>start</literal>
371      is 0 (default) and <literal>count</literal> is 4, and
372      <literal>piggyback</literal> is 1 (default) and no sorting critieria
373      is specified, then the client will attempt to retrieve the 4
374      records as part the search response (using piggyback). On the other
375      hand, if either <literal>start</literal> is positive or if
376      a sorting criteria is set, or if <literal>piggyback</literal>
377      is 0, then the client will not perform piggyback but send Present
378      Requests instead.
379     </para>
380     <para>
381      If either of the options <literal>mediumSetElementSetName</literal> and
382      <literal>smallSetElementSetName</literal> are unset, the value
383      of option <literal>elementSetName</literal> is used for piggyback
384      searches. This means that for the high-level mode you only have
385      to specify one elementSetName option rather than three.
386      </para>
387    </sect2>
388   </sect1>
389   <sect1 id="zoom.records"><title>Records</title>
390    <para>
391     A record object is a retrival record on the client side -
392     created from result sets.
393    </para>
394    <synopsis>
395      void Z3950_resultset_records (Z3950_resultset r,
396                                    Z3950_record *recs,
397                                    size_t start, size_t count);
398      Z3950_record Z3950_resultset_record (Z3950_resultset s, size_t pos);
399
400      void *Z3950_record_get (Z3950_record rec, const char *type,
401                              size_t *len);
402
403      Z3950_record Z3950_record_dup (Z3950_record rec);
404
405      void Z3950_record_destroy (Z3950_record rec);
406    </synopsis>
407    <para>
408     References to temporary records are returned by functions 
409     <function>Z3950_resultset_records</function> or
410     <function>Z3950_resultset_record</function>.
411     </para>
412    <para>
413     If a persistent pointer to a record is desired
414     <function>Z3950_record_dup</function> should be used.
415     It returns a record reference that at any
416     later stage should be destroyed by
417     <function>Z3950_record_destroy</function>.
418    </para>
419    <para>
420     A single record is returned by function
421     <function>Z3950_resultset_record</function> that takes a 
422     position as argument. First record has position zero.
423     If no record could be obtained <literal>NULL</literal> is returned.
424    </para>
425    <para>
426     Function <function>Z3950_resultset_records</function> retrieves
427     a number of records from a result set. Parameter <literal>start</literal>
428     and <literal>count</literal> specifies the range of records to
429     be returned. Upon completion array
430     <literal>recs[0], ..recs[count-1]</literal>
431     holds record objects for the records. The array of records
432      <literal>recs</literal> should be allocate prior to calling 
433     <function>Z3950_resultset_records</function>. Note that for those
434     records that couldn't be retrieved from the target
435     <literal>recs[ ..]</literal> is set to <literal>NULL</literal>.
436    </para>
437    <para id="zoom.record.get">
438     In order to extract information about a single record,
439     <function>Z3950_record_get</function> is provided. The
440     function returns a pointer to certain record information. The
441     nature (type) of the pointer depends on the <function>type</function>
442     given. In addition for certain types, the length
443     <literal>len</literal> passed will be set to the size in bytes of
444     the returned information.
445     <variablelist>
446      <varlistentry><term><literal>database</literal></term>
447       <listitem><para>Database of record is returned
448         as a C null-terminated string. Return type <literal>char *</literal>. 
449        </para></listitem>
450       </varlistentry>
451      <varlistentry><term><literal>syntax</literal></term>
452       <listitem><para>The transfer syntax (OID) of the record is returned
453         as a C null-terminated string. Return type <literal>char *</literal>. 
454        </para></listitem>
455       </varlistentry>
456      <varlistentry><term><literal>render</literal></term>
457       <listitem><para>The record is returned in a display friendly
458         format. Upon completion buffer is returned
459         (type <literal>char *</literal>) and length is stored in
460         <literal>*len</literal>.
461        </para></listitem>
462       </varlistentry>
463      <varlistentry><term><literal>raw</literal></term>
464       <listitem><para>The record is returned in the internal
465         YAZ specific format. The raw data is returned as type 
466         <literal>Z_External *</literal> is just the type for
467         the member <literal>retrievalRecord</literal> in
468         type <literal>NamePlusRecord</literal>.
469        </para></listitem>
470       </varlistentry>
471     </variablelist>
472    </para>
473    <sect2><title>Protocol behavior</title>
474     <para>
475      The functions <function>Z3950_resultset_record</function> and
476      <function>Z3950_resultset_records</function> inspects the client-side
477      record cache. If the records(s) were not found, i.e. not yet retrieved
478      from, they are fetched using Present Requests.
479     </para>
480    </sect2>
481   </sect1>
482   <sect1 id="zoom.options"><title>Options</title>
483    <para>
484     Most &zoom; objects provide a way to specify options to default behavior.
485     From an implementation point of view a set of options is just like
486     an associate array / hash array, etc.
487    </para>
488    <synopsis>
489      Z3950_options Z3950_options_create (void);
490
491      Z3950_options Z3950_options_create_with_parent (Z3950_options parent);
492
493      void Z3950_options_destroy (Z3950_options opt);
494    </synopsis>
495    <synopsis>
496      const char *Z3950_options_get (Z3950_options opt, const char *name);
497
498      void Z3950_options_set (Z3950_options opt, const char *name,
499                              const char *v);
500    </synopsis>
501    <synopsis>
502      typedef const char *(*Z3950_options_callback)
503                                      (void *handle, const char *name);
504
505      Z3950_options_callback
506              Z3950_options_set_callback (Z3950_options opt,
507                                          Z3950_options_callback c,
508                                          void *handle);
509    </synopsis>
510   </sect1>
511   <sect1 id="zoom.events"><title>Events</title>
512    <para>
513     If you're developing non-blocking applications, you have to deal 
514     with events.
515    </para>
516    <synopsis>
517     int Z3950_event (int no, Z3950_connection *cs);
518    </synopsis>
519    <para>
520     The <function>Z3950_event</function> executes pending events for
521     a number of connections. Supply the number of connections in
522     <literal>no</literal> and an array of connections in
523     <literal>cs</literal> (<literal>cs[0] ... cs[no-1]</literal>).
524     A pending event could be a sending a search, receiving a response,
525     etc.
526     When an event has a occured for one of the connections, this function
527     returns a positive integer <literal>n</literal> denoting that an event
528     occurred for connection <literal>cs[n-1]</literal>.
529     When no events are pending for the connections, a value of zero is
530     returned.
531     To ensure that all outstanding requests are performed call this function
532     repeatedly until zero is returned.
533    </para>
534   </sect1>
535  </chapter>
536  
537  <!-- Keep this comment at the end of the file
538  Local variables:
539  mode: sgml
540  sgml-omittag:t
541  sgml-shorttag:t
542  sgml-minimize-attributes:nil
543  sgml-always-quote-attributes:t
544  sgml-indent-step:1
545  sgml-indent-data:t
546  sgml-parent-document: "yaz.xml"
547  sgml-local-catalogs: nil
548  sgml-namecase-general:t
549  End:
550  -->
551