Add ZOOM_resultset_release
[yaz-moved-to-github.git] / include / yaz / zoom.h
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2010 Index Data.
3  * All rights reserved.
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  *     * Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *     * Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *     * Neither the name of Index Data nor the names of its contributors
13  *       may be used to endorse or promote products derived from this
14  *       software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 /**
29  * \file zoom.h
30  * \brief Header for ZOOM
31  */
32
33 #include <stdlib.h>
34 #include <yaz/yconfig.h>
35
36 #define ZOOM_BEGIN_CDECL YAZ_BEGIN_CDECL
37 #define ZOOM_END_CDECL YAZ_END_CDECL
38
39 #ifdef WIN32
40 #  define ZOOM_API(x) __declspec(dllexport) x __stdcall
41 #else
42 #  define ZOOM_API(x) x
43 # endif
44
45 ZOOM_BEGIN_CDECL
46
47 /* ----------------------------------------------------------- */
48 /* the types we use */
49
50 typedef struct ZOOM_options_p *ZOOM_options;
51 typedef struct ZOOM_query_p *ZOOM_query;
52 typedef struct ZOOM_connection_p *ZOOM_connection;
53 typedef struct ZOOM_resultset_p *ZOOM_resultset;
54 typedef struct ZOOM_record_p *ZOOM_record;
55 typedef struct ZOOM_scanset_p *ZOOM_scanset;
56 typedef struct ZOOM_package_p *ZOOM_package;
57
58 typedef const char *(*ZOOM_options_callback)(void *handle, const char *name);
59
60 /* ----------------------------------------------------------- */
61 /* connections */
62
63 /* create connection, connect to host, if portnum is 0, then port is
64    read from host string (e.g. myhost:9821) */
65 ZOOM_API(ZOOM_connection)
66 ZOOM_connection_new(const char *host, int portnum);
67
68 /* create connection, don't connect, apply options */
69 ZOOM_API(ZOOM_connection)
70 ZOOM_connection_create(ZOOM_options options);
71
72 /* connect given existing connection */
73 ZOOM_API(void)
74 ZOOM_connection_connect(ZOOM_connection c, const char *host,
75                               int portnum);
76
77 ZOOM_API(void)
78 ZOOM_connection_close(ZOOM_connection c);
79
80 /* destroy connection (close connection also) */
81 ZOOM_API(void)
82 ZOOM_connection_destroy(ZOOM_connection c);
83
84 /* get/set option for connection */
85 ZOOM_API(const char *)
86 ZOOM_connection_option_get(ZOOM_connection c, const char *key);
87
88 ZOOM_API(const char *)
89 ZOOM_connection_option_getl(ZOOM_connection c, const char *key, int *lenp);
90
91 ZOOM_API(void)
92 ZOOM_connection_option_set(ZOOM_connection c, const char *key,
93                            const char *val);
94
95 ZOOM_API(void)
96 ZOOM_connection_option_setl(ZOOM_connection c, const char *key,
97                             const char *val, int len);
98
99 /* return error code (0 == success, failure otherwise). cp
100    holds error string on failure, addinfo holds addititional info (if any)
101 */
102 ZOOM_API(int)
103 ZOOM_connection_error(ZOOM_connection c, const char **cp,
104                       const char **addinfo);
105
106 ZOOM_API(int)
107 ZOOM_connection_error_x(ZOOM_connection c, const char **cp,
108                         const char **addinfo, const char **diagset);
109
110 /* returns error code */
111 ZOOM_API(int)
112 ZOOM_connection_errcode(ZOOM_connection c);
113 /* returns error message */
114 ZOOM_API(const char *)
115 ZOOM_connection_errmsg(ZOOM_connection c);
116 /* returns additional info */
117 ZOOM_API(const char *)
118 ZOOM_connection_addinfo(ZOOM_connection c);
119 /* returns diagnostic set */
120 ZOOM_API(const char *)
121 ZOOM_connection_diagset(ZOOM_connection c);
122 /* translates error code into human-readable string */
123 ZOOM_API(const char *)
124 ZOOM_diag_str (int error);
125
126 #define ZOOM_ERROR_NONE 0
127 #define ZOOM_ERROR_CONNECT 10000
128 #define ZOOM_ERROR_MEMORY  10001
129 #define ZOOM_ERROR_ENCODE  10002
130 #define ZOOM_ERROR_DECODE  10003
131 #define ZOOM_ERROR_CONNECTION_LOST 10004
132 #define ZOOM_ERROR_INIT 10005
133 #define ZOOM_ERROR_INTERNAL 10006
134 #define ZOOM_ERROR_TIMEOUT 10007
135 #define ZOOM_ERROR_UNSUPPORTED_PROTOCOL 10008
136 #define ZOOM_ERROR_UNSUPPORTED_QUERY 10009
137 #define ZOOM_ERROR_INVALID_QUERY 10010
138 #define ZOOM_ERROR_CQL_PARSE 10011
139 #define ZOOM_ERROR_CQL_TRANSFORM 10012
140 #define ZOOM_ERROR_CCL_CONFIG 10013
141 #define ZOOM_ERROR_CCL_PARSE 10014
142
143 ZOOM_API(int)
144 ZOOM_connection_last_event(ZOOM_connection cs);
145
146 #define ZOOM_EVENT_NONE 0
147 #define ZOOM_EVENT_CONNECT 1
148 #define ZOOM_EVENT_SEND_DATA  2
149 #define ZOOM_EVENT_RECV_DATA 3
150 #define ZOOM_EVENT_TIMEOUT 4
151 #define ZOOM_EVENT_UNKNOWN 5
152 #define ZOOM_EVENT_SEND_APDU 6
153 #define ZOOM_EVENT_RECV_APDU 7
154 #define ZOOM_EVENT_RECV_RECORD 8
155 #define ZOOM_EVENT_RECV_SEARCH 9
156 #define ZOOM_EVENT_END 10
157 #define ZOOM_EVENT_MAX 10
158
159 /* ----------------------------------------------------------- */
160 /* result sets */
161
162 /* create result set given a search */
163 ZOOM_API(ZOOM_resultset)
164 ZOOM_connection_search(ZOOM_connection, ZOOM_query q);
165 /* create result set given PQF query */
166 ZOOM_API(ZOOM_resultset)
167 ZOOM_connection_search_pqf(ZOOM_connection c, const char *q);
168
169 /* destroy result set */
170 ZOOM_API(void)
171 ZOOM_resultset_destroy(ZOOM_resultset r);
172
173 /** release result set from connection.
174
175     The result will will no longer be able to perform retrievals
176     from the connection from which it was created.
177 */
178 ZOOM_API(void)
179 ZOOM_resultset_release(ZOOM_resultset r);
180
181 /* result set option */
182 ZOOM_API(const char *)
183 ZOOM_resultset_option_get(ZOOM_resultset r, const char *key);
184 ZOOM_API(void)
185 ZOOM_resultset_option_set(ZOOM_resultset r, const char *key, const char *val);
186
187 /* return size of result set (alias hit count AKA result count) */
188 ZOOM_API(size_t)
189 ZOOM_resultset_size(ZOOM_resultset r);
190
191 /* retrieve records */
192 ZOOM_API(void)
193 ZOOM_resultset_records(ZOOM_resultset r, ZOOM_record *recs,
194                        size_t start, size_t count);
195
196 /* return record object at pos. Returns 0 if unavailable */
197 ZOOM_API(ZOOM_record)
198 ZOOM_resultset_record(ZOOM_resultset s, size_t pos);
199
200 /* like ZOOM_resultset_record - but never blocks .. */
201 ZOOM_API(ZOOM_record)
202 ZOOM_resultset_record_immediate(ZOOM_resultset s, size_t pos);
203
204 /* reset record cache for result set */
205 ZOOM_API(void)
206 ZOOM_resultset_cache_reset(ZOOM_resultset r);
207
208 /* ----------------------------------------------------------- */
209 /* records */
210
211 /* get record information, in a form given by type */
212 ZOOM_API(const char *)
213 ZOOM_record_get(ZOOM_record rec, const char *type, int *len);
214
215 /* destroy record */
216 ZOOM_API(void)
217 ZOOM_record_destroy(ZOOM_record rec);
218
219 /* return copy of record */
220 ZOOM_API(ZOOM_record)
221 ZOOM_record_clone(ZOOM_record srec);
222
223 /* return error info (surrogate diagnostic) for record */
224 ZOOM_API(int)
225     ZOOM_record_error(ZOOM_record rec, const char **msg,
226                       const char **addinfo, const char **diagset);
227     
228 /* ----------------------------------------------------------- */
229 /* queries */
230
231 /* create search object */
232 ZOOM_API(ZOOM_query)
233 ZOOM_query_create(void);
234 /* destroy it */
235 ZOOM_API(void)
236 ZOOM_query_destroy(ZOOM_query s);
237 /* CQL */
238 ZOOM_API(int)
239 ZOOM_query_cql(ZOOM_query s, const char *str);
240 /* CQL translated client-side into RPN: `conn' is optional for diagnostics */
241 ZOOM_API(int)
242 ZOOM_query_cql2rpn(ZOOM_query s, const char *str, ZOOM_connection conn);
243 /* CCL translated client-side into RPN: `conn' is optional for diagnostics */
244 ZOOM_API(int)
245 ZOOM_query_ccl2rpn(ZOOM_query s, const char *query_str,
246                    const char *config, 
247                    int *ccl_error, const char **error_string, int *error_pos);
248 /* PQF */
249 ZOOM_API(int)
250 ZOOM_query_prefix(ZOOM_query s, const char *str);
251 /* specify sort criteria for search */
252 ZOOM_API(int)
253 ZOOM_query_sortby(ZOOM_query s, const char *criteria);
254
255 /* ----------------------------------------------------------- */
256 /* scan */
257 ZOOM_API(ZOOM_scanset)
258 ZOOM_connection_scan(ZOOM_connection c, const char *startterm);
259
260 ZOOM_API(ZOOM_scanset)
261 ZOOM_connection_scan1(ZOOM_connection c, ZOOM_query startterm);
262
263 ZOOM_API(const char *)
264 ZOOM_scanset_term(ZOOM_scanset scan, size_t pos,
265                   size_t *occ, size_t *len);
266
267 ZOOM_API(const char *)
268 ZOOM_scanset_display_term(ZOOM_scanset scan, size_t pos,
269                           size_t *occ, size_t *len);
270
271 ZOOM_API(size_t)
272 ZOOM_scanset_size(ZOOM_scanset scan);
273
274 ZOOM_API(void)
275 ZOOM_scanset_destroy(ZOOM_scanset scan);
276
277 ZOOM_API(const char *)
278 ZOOM_scanset_option_get(ZOOM_scanset scan, const char *key);
279
280 ZOOM_API(void)
281 ZOOM_scanset_option_set(ZOOM_scanset scan, const char *key,
282                             const char *val);
283 /* ----------------------------------------------------------- */
284 /* Extended Services Packages */
285
286 ZOOM_API(ZOOM_package)
287 ZOOM_connection_package(ZOOM_connection c, ZOOM_options options);
288
289 ZOOM_API(void)
290 ZOOM_package_destroy(ZOOM_package p);
291
292 ZOOM_API(void)
293 ZOOM_package_send(ZOOM_package p, const char *type);
294
295 ZOOM_API(const char *)
296 ZOOM_package_option_get(ZOOM_package p, const char *key);
297
298 ZOOM_API(const char *)
299 ZOOM_package_option_getl(ZOOM_package p, const char *key, int *lenp);
300
301 ZOOM_API(void)
302 ZOOM_package_option_set(ZOOM_package p, const char *key,
303                         const char *val);
304
305 ZOOM_API(void)
306 ZOOM_package_option_setl(ZOOM_package p, const char *key,
307                          const char *val, int len);
308
309 /* ----------------------------------------------------------- */
310 /* Sort.  First function is deprecated, use second instead */
311 ZOOM_API(void)
312 ZOOM_resultset_sort(ZOOM_resultset r,
313                     const char *sort_type, const char *sort_spec);
314 ZOOM_API(int)
315 ZOOM_resultset_sort1(ZOOM_resultset r,
316                      const char *sort_type, const char *sort_spec);
317
318 /* ----------------------------------------------------------- */
319 /* options */
320
321 ZOOM_API(ZOOM_options_callback)
322 ZOOM_options_set_callback(ZOOM_options opt,
323                           ZOOM_options_callback c, void *handle);
324 ZOOM_API(ZOOM_options)
325 ZOOM_options_create(void);
326
327 ZOOM_API(ZOOM_options)
328 ZOOM_options_create_with_parent(ZOOM_options parent);
329
330 ZOOM_API(ZOOM_options)
331 ZOOM_options_create_with_parent2(ZOOM_options parent1,
332                                  ZOOM_options parent2);
333
334 ZOOM_API(ZOOM_options)
335 ZOOM_options_dup(ZOOM_options src);
336
337 ZOOM_API(const char *)
338 ZOOM_options_get(ZOOM_options opt, const char *name);
339
340 ZOOM_API(const char *)
341 ZOOM_options_getl(ZOOM_options opt, const char *name, int *lenp);
342
343 ZOOM_API(void)
344 ZOOM_options_set(ZOOM_options opt, const char *name, const char *v);
345     
346 ZOOM_API(void)
347 ZOOM_options_setl(ZOOM_options opt, const char *name, const char *value,
348                   int len);
349
350 ZOOM_API(void)
351 ZOOM_options_destroy(ZOOM_options opt);
352
353 ZOOM_API(int)
354 ZOOM_options_get_bool(ZOOM_options opt, const char *name, int defa);
355
356 ZOOM_API(int)
357 ZOOM_options_get_int(ZOOM_options opt, const char *name, int defa);
358
359 ZOOM_API(void)
360 ZOOM_options_set_int(ZOOM_options opt, const char *name, int value);
361
362 /** \brief select/poll socket mask: read */
363 #define ZOOM_SELECT_READ 1
364 /** \brief select/poll socket mask: write */
365 #define ZOOM_SELECT_WRITE 2
366 /** \brief select/poll socket mask: except */
367 #define ZOOM_SELECT_EXCEPT 4
368
369 /** \brief wait for events on connection(s) (BLOCKING)
370     \param no number of connections (size of cs)
371     \param cs connection array
372     \retval 0 no event was fired
373     \retval >0 event was fired for connection at (retval-1)
374     
375     blocking poll for events on a number of connections. Returns positive
376     integer if event occurred ; zero if none occurred and no more
377     events are pending. The positive integer specifies the
378     connection for which the event occurred.
379 */
380 ZOOM_API(int)
381 ZOOM_event(int no, ZOOM_connection *cs);
382
383
384 /** \brief determines if connection is idle (no active or pending work)
385     \param c connection
386     \retval 1 is idle
387     \retval 0 is non-idle (active)
388 */
389 ZOOM_API(int)
390 ZOOM_connection_is_idle(ZOOM_connection c);
391
392
393 /** \brief process one event for one of connections given
394     \param no number of connections (size of cs)
395     \param cs connection array
396     \retval 0 no event was processed
397     \retval >0 event was processed for connection at (retval-1)
398
399     This function attemps to deal with outstandings events in a non-blocking
400     mode. If no events was processed (return value of 0),  then the system
401     should attempt to deal with sockets in blocking mode using socket
402     select/poll which means calling the following functions:
403     ZOOM_connection_get_socket, ZOOM_connection_get_mask,
404     ZOOM_connection_get_timeout.
405 */
406 ZOOM_API(int)
407 ZOOM_event_nonblock(int no, ZOOM_connection *cs);
408
409
410 /** \brief process one event for connection
411     \param c connection
412     \retval 0 no event was processed
413     \retval 1 event was processed for connection
414
415     This function attemps to deal with outstandings events in 
416     a non-blocking fashion. If no event was processed (return value of 0),
417     then the system should attempt to deal with sockets in blocking mode
418     using socket select/poll which means calling the following functions:
419     ZOOM_connection_get_socket, ZOOM_connection_get_mask,
420     ZOOM_connection_get_timeout. If an event was processed call this
421     function again.
422 */
423 ZOOM_API(int)
424 ZOOM_connection_process(ZOOM_connection c);
425
426
427 /** \brief executes non-blocking tasks for connection
428     \param c connection
429     \retval 0 no task was executed
430     \retval 1 task was executed (but probably not completed)
431
432     This function, unlike, ZOOM_connection_process, does not try to
433     return any events (and remove them). But events may be generated
434     from it. These are saved and may later be retrieved with
435     ZOOM_connection_process and ZOOM_connection_last_event .
436 */
437 ZOOM_API(int)
438 ZOOM_connection_exec_task(ZOOM_connection c);
439
440
441 /** \brief get socket fd for ZOOM connection
442     \param c connection
443     \retval -1 no socket assigned for connection
444     \retval >=0 socket for connection
445
446     Use this function when preparing for socket/poll and
447     in conjunction with ZOOM_connection_get_mask.
448 */
449 ZOOM_API(int)
450 ZOOM_connection_get_socket(ZOOM_connection c);
451
452
453 /** \brief get socket mask for connection 
454     \param c connection
455     \returns mask for connection (possibly 0)
456
457     Use this function when preparing for socket select/poll and
458     in conjunction with ZOOM_connection_get_socket.
459 */
460 ZOOM_API(int)
461 ZOOM_connection_get_mask(ZOOM_connection c);
462
463
464 /** \brief set socket mask for connection (DO NOT call outside zoom) */
465 ZOOM_API(int)
466 ZOOM_connection_set_mask(ZOOM_connection c, int mask);
467
468
469 /** \brief get timeout in seconds for ZOOM connection
470     \param c connection
471     \returns timeout value in seconds
472
473     Use this function when preparing for socket/poll and
474     in conjunction with ZOOM_connection_get_socket.
475 */
476 ZOOM_API(int)
477 ZOOM_connection_get_timeout(ZOOM_connection c);
478
479
480 /** \brief fire socket event timeout
481     \param c connection
482     \retval 0 event was fired OK
483     \retval -1 event was not fired
484
485     Call this function when a timeout occurs - for example in the
486     case of select(2) returning 0.
487 */
488 ZOOM_API(int)
489 ZOOM_connection_fire_event_timeout(ZOOM_connection c);
490
491
492 /** \brief fire socket event activity (read,write,except)
493     \param c connection
494     \param mask or'ed mask of ZOOM_SELECT_.. values
495     \retval 0 event was fired OK
496     \retval -1 event was not fired
497 */
498 ZOOM_API(int)
499 ZOOM_connection_fire_event_socket(ZOOM_connection c, int mask);
500
501
502 /** \brief peek at next event
503     \param c connection
504     \returns ZOOM_EVENT_NONE (for no events in queue), ZOOM_EVENT_CONNECT, ..
505
506     Does not actually remove the event from the event queue. ZOOM_event and
507     ZOOM_process_event removes one event.
508 */
509
510 ZOOM_API(int)
511 ZOOM_connection_peek_event(ZOOM_connection c);
512
513 ZOOM_API(const char *)
514 ZOOM_get_event_str(int event);
515
516 ZOOM_END_CDECL
517
518 /*
519  * Local variables:
520  * c-basic-offset: 4
521  * c-file-style: "Stroustrup"
522  * indent-tabs-mode: nil
523  * End:
524  * vim: shiftwidth=4 tabstop=8 expandtab
525  */
526