ZOOM: initial experiments with memcached
[yaz-moved-to-github.git] / include / yaz / zoom.h
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 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_facet_field_p *ZOOM_facet_field;
56 typedef struct ZOOM_scanset_p *ZOOM_scanset;
57 typedef struct ZOOM_package_p *ZOOM_package;
58
59 typedef const char *(*ZOOM_options_callback)(void *handle, const char *name);
60
61 /* ----------------------------------------------------------- */
62 /* connections */
63
64 /* create connection, connect to host, if portnum is 0, then port is
65    read from host string (e.g. myhost:9821) */
66 ZOOM_API(ZOOM_connection)
67 ZOOM_connection_new(const char *host, int portnum);
68
69 /* create connection, don't connect, apply options */
70 ZOOM_API(ZOOM_connection)
71 ZOOM_connection_create(ZOOM_options options);
72
73 /* connect given existing connection */
74 ZOOM_API(void)
75 ZOOM_connection_connect(ZOOM_connection c, const char *host,
76                               int portnum);
77
78 ZOOM_API(void)
79 ZOOM_connection_close(ZOOM_connection c);
80
81 /* destroy connection (close connection also) */
82 ZOOM_API(void)
83 ZOOM_connection_destroy(ZOOM_connection c);
84
85 /* get/set option for connection */
86 ZOOM_API(const char *)
87 ZOOM_connection_option_get(ZOOM_connection c, const char *key);
88
89 ZOOM_API(const char *)
90 ZOOM_connection_option_getl(ZOOM_connection c, const char *key, int *lenp);
91
92 ZOOM_API(void)
93 ZOOM_connection_option_set(ZOOM_connection c, const char *key,
94                            const char *val);
95
96 ZOOM_API(void)
97 ZOOM_connection_option_setl(ZOOM_connection c, const char *key,
98                             const char *val, int len);
99
100 /* return error code (0 == success, failure otherwise). cp
101    holds error string on failure, addinfo holds addititional info (if any)
102 */
103 ZOOM_API(int)
104 ZOOM_connection_error(ZOOM_connection c, const char **cp,
105                       const char **addinfo);
106
107 ZOOM_API(int)
108 ZOOM_connection_error_x(ZOOM_connection c, const char **cp,
109                         const char **addinfo, const char **diagset);
110
111 /* returns error code */
112 ZOOM_API(int)
113 ZOOM_connection_errcode(ZOOM_connection c);
114 /* returns error message */
115 ZOOM_API(const char *)
116 ZOOM_connection_errmsg(ZOOM_connection c);
117 /* returns additional info */
118 ZOOM_API(const char *)
119 ZOOM_connection_addinfo(ZOOM_connection c);
120 /* returns diagnostic set */
121 ZOOM_API(const char *)
122 ZOOM_connection_diagset(ZOOM_connection c);
123 /* translates error code into human-readable string */
124 ZOOM_API(const char *)
125 ZOOM_diag_str (int error);
126
127 #define ZOOM_ERROR_NONE 0
128 #define ZOOM_ERROR_CONNECT 10000
129 #define ZOOM_ERROR_MEMORY  10001
130 #define ZOOM_ERROR_ENCODE  10002
131 #define ZOOM_ERROR_DECODE  10003
132 #define ZOOM_ERROR_CONNECTION_LOST 10004
133 #define ZOOM_ERROR_INIT 10005
134 #define ZOOM_ERROR_INTERNAL 10006
135 #define ZOOM_ERROR_TIMEOUT 10007
136 #define ZOOM_ERROR_UNSUPPORTED_PROTOCOL 10008
137 #define ZOOM_ERROR_UNSUPPORTED_QUERY 10009
138 #define ZOOM_ERROR_INVALID_QUERY 10010
139 #define ZOOM_ERROR_CQL_PARSE 10011
140 #define ZOOM_ERROR_CQL_TRANSFORM 10012
141 #define ZOOM_ERROR_CCL_CONFIG 10013
142 #define ZOOM_ERROR_CCL_PARSE 10014
143 #define ZOOM_ERROR_ES_INVALID_ACTION 10015
144 #define ZOOM_ERROR_ES_INVALID_VERSION 10016
145 #define ZOOM_ERROR_ES_INVALID_SYNTAX 10017
146 #define ZOOM_ERROR_MEMCACHED 10018
147
148 ZOOM_API(int)
149 ZOOM_connection_last_event(ZOOM_connection cs);
150
151 #define ZOOM_EVENT_NONE 0
152 #define ZOOM_EVENT_CONNECT 1
153 #define ZOOM_EVENT_SEND_DATA  2
154 #define ZOOM_EVENT_RECV_DATA 3
155 #define ZOOM_EVENT_TIMEOUT 4
156 #define ZOOM_EVENT_UNKNOWN 5
157 #define ZOOM_EVENT_SEND_APDU 6
158 #define ZOOM_EVENT_RECV_APDU 7
159 #define ZOOM_EVENT_RECV_RECORD 8
160 #define ZOOM_EVENT_RECV_SEARCH 9
161 #define ZOOM_EVENT_END 10
162 #define ZOOM_EVENT_MAX 10
163
164 /* ----------------------------------------------------------- */
165 /* result sets */
166
167 /* create result set given a search */
168 ZOOM_API(ZOOM_resultset)
169 ZOOM_connection_search(ZOOM_connection, ZOOM_query q);
170 /* create result set given PQF query */
171 ZOOM_API(ZOOM_resultset)
172 ZOOM_connection_search_pqf(ZOOM_connection c, const char *q);
173
174 /* destroy result set */
175 ZOOM_API(void)
176 ZOOM_resultset_destroy(ZOOM_resultset r);
177
178 /** release result set from connection.
179
180     The result will will no longer be able to perform retrievals
181     from the connection from which it was created.
182 */
183 ZOOM_API(void)
184 ZOOM_resultset_release(ZOOM_resultset r);
185
186 /* result set option */
187 ZOOM_API(const char *)
188 ZOOM_resultset_option_get(ZOOM_resultset r, const char *key);
189 ZOOM_API(void)
190 ZOOM_resultset_option_set(ZOOM_resultset r, const char *key, const char *val);
191
192 /* return size of result set (alias hit count AKA result count) */
193 ZOOM_API(size_t)
194 ZOOM_resultset_size(ZOOM_resultset r);
195
196 /* retrieve records */
197 ZOOM_API(void)
198 ZOOM_resultset_records(ZOOM_resultset r, ZOOM_record *recs,
199                        size_t start, size_t count);
200
201 /* return record object at pos. Returns 0 if unavailable */
202 ZOOM_API(ZOOM_record)
203 ZOOM_resultset_record(ZOOM_resultset s, size_t pos);
204
205 /* like ZOOM_resultset_record - but never blocks .. */
206 ZOOM_API(ZOOM_record)
207 ZOOM_resultset_record_immediate(ZOOM_resultset s, size_t pos);
208
209 /* reset record cache for result set */
210 ZOOM_API(void)
211 ZOOM_resultset_cache_reset(ZOOM_resultset r);
212
213
214 /* retrieve facet field */
215 ZOOM_API(ZOOM_facet_field)
216 ZOOM_resultset_get_facet_field(ZOOM_resultset r, const char *facet_name);
217
218 /* retrieve facet field at position. Returns 0 if unavailable */
219 ZOOM_API(ZOOM_facet_field)
220 ZOOM_resultset_get_facet_field_by_index(ZOOM_resultset r, int pos);
221
222 /* return number of facets available */
223 ZOOM_API(size_t)
224 ZOOM_resultset_facets_size(ZOOM_resultset r);
225
226 /* retrieve (array of pointers to) facet fields */
227 ZOOM_API(ZOOM_facet_field *)
228 ZOOM_resultset_facets(ZOOM_resultset r);
229
230 /* retrieve (array of pointers to) facet fields */
231 ZOOM_API(const char **)
232 ZOOM_resultset_facets_names(ZOOM_resultset r);
233
234
235
236 /* ----------------------------------------------------------- */
237 /* records */
238
239 /* get record information, in a form given by type */
240 ZOOM_API(const char *)
241 ZOOM_record_get(ZOOM_record rec, const char *type, int *len);
242
243 /* destroy record */
244 ZOOM_API(void)
245 ZOOM_record_destroy(ZOOM_record rec);
246
247 /* return copy of record */
248 ZOOM_API(ZOOM_record)
249 ZOOM_record_clone(ZOOM_record srec);
250
251 /* return error info (surrogate diagnostic) for record */
252 ZOOM_API(int)
253     ZOOM_record_error(ZOOM_record rec, const char **msg,
254                       const char **addinfo, const char **diagset);
255
256 /* ----------------------------------------------------------- */
257 /* facets */
258
259 /* get facet name */
260 ZOOM_API(const char *)
261 ZOOM_facet_field_name(ZOOM_facet_field facet_field);
262
263 /* get terms count on facet */
264 ZOOM_API(size_t)
265 ZOOM_facet_field_term_count(ZOOM_facet_field facet_field);
266
267 /* get facet information, term and frequency, at a position. Returns 0 if out of bounds */
268 ZOOM_API(const char *)
269 ZOOM_facet_field_get_term(ZOOM_facet_field facet_field, size_t idx, int *freq);
270
271 /* ----------------------------------------------------------- */
272
273
274 /* queries */
275
276 /* create search object */
277 ZOOM_API(ZOOM_query)
278 ZOOM_query_create(void);
279 /* destroy it */
280 ZOOM_API(void)
281 ZOOM_query_destroy(ZOOM_query s);
282 /* CQL */
283 ZOOM_API(int)
284 ZOOM_query_cql(ZOOM_query s, const char *str);
285 /* CQL translated client-side into RPN: `conn' is optional for diagnostics */
286 ZOOM_API(int)
287 ZOOM_query_cql2rpn(ZOOM_query s, const char *str, ZOOM_connection conn);
288 /* CCL translated client-side into RPN: `conn' is optional for diagnostics */
289 ZOOM_API(int)
290 ZOOM_query_ccl2rpn(ZOOM_query s, const char *query_str,
291                    const char *config,
292                    int *ccl_error, const char **error_string, int *error_pos);
293 /* PQF */
294 ZOOM_API(int)
295 ZOOM_query_prefix(ZOOM_query s, const char *str);
296 /* specify sort criteria for search */
297 ZOOM_API(int)
298 ZOOM_query_sortby(ZOOM_query s, const char *criteria);
299 ZOOM_API(int)
300 ZOOM_query_sortby2(ZOOM_query s, const char *strategy, const char *criteria);
301
302 ZOOM_API(void)
303 ZOOM_query_addref(ZOOM_query s);
304
305 /* ----------------------------------------------------------- */
306 /* scan */
307 ZOOM_API(ZOOM_scanset)
308 ZOOM_connection_scan(ZOOM_connection c, const char *startterm);
309
310 ZOOM_API(ZOOM_scanset)
311 ZOOM_connection_scan1(ZOOM_connection c, ZOOM_query startterm);
312
313 ZOOM_API(const char *)
314 ZOOM_scanset_term(ZOOM_scanset scan, size_t pos,
315                   size_t *occ, size_t *len);
316
317 ZOOM_API(const char *)
318 ZOOM_scanset_display_term(ZOOM_scanset scan, size_t pos,
319                           size_t *occ, size_t *len);
320
321 ZOOM_API(size_t)
322 ZOOM_scanset_size(ZOOM_scanset scan);
323
324 ZOOM_API(void)
325 ZOOM_scanset_destroy(ZOOM_scanset scan);
326
327 ZOOM_API(const char *)
328 ZOOM_scanset_option_get(ZOOM_scanset scan, const char *key);
329
330 ZOOM_API(void)
331 ZOOM_scanset_option_set(ZOOM_scanset scan, const char *key,
332                             const char *val);
333 /* ----------------------------------------------------------- */
334 /* Extended Services Packages */
335
336 ZOOM_API(ZOOM_package)
337 ZOOM_connection_package(ZOOM_connection c, ZOOM_options options);
338
339 ZOOM_API(void)
340 ZOOM_package_destroy(ZOOM_package p);
341
342 ZOOM_API(void)
343 ZOOM_package_send(ZOOM_package p, const char *type);
344
345 ZOOM_API(const char *)
346 ZOOM_package_option_get(ZOOM_package p, const char *key);
347
348 ZOOM_API(const char *)
349 ZOOM_package_option_getl(ZOOM_package p, const char *key, int *lenp);
350
351 ZOOM_API(void)
352 ZOOM_package_option_set(ZOOM_package p, const char *key,
353                         const char *val);
354
355 ZOOM_API(void)
356 ZOOM_package_option_setl(ZOOM_package p, const char *key,
357                          const char *val, int len);
358
359 /* ----------------------------------------------------------- */
360 /* Sort.  First function is deprecated, use second instead */
361 ZOOM_API(void)
362 ZOOM_resultset_sort(ZOOM_resultset r,
363                     const char *sort_type, const char *sort_spec);
364 ZOOM_API(int)
365 ZOOM_resultset_sort1(ZOOM_resultset r,
366                      const char *sort_type, const char *sort_spec);
367
368 /* ----------------------------------------------------------- */
369 /* options */
370
371 ZOOM_API(ZOOM_options_callback)
372 ZOOM_options_set_callback(ZOOM_options opt,
373                           ZOOM_options_callback c, void *handle);
374 ZOOM_API(ZOOM_options)
375 ZOOM_options_create(void);
376
377 ZOOM_API(ZOOM_options)
378 ZOOM_options_create_with_parent(ZOOM_options parent);
379
380 ZOOM_API(ZOOM_options)
381 ZOOM_options_create_with_parent2(ZOOM_options parent1,
382                                  ZOOM_options parent2);
383
384 ZOOM_API(ZOOM_options)
385 ZOOM_options_dup(ZOOM_options src);
386
387 ZOOM_API(const char *)
388 ZOOM_options_get(ZOOM_options opt, const char *name);
389
390 ZOOM_API(const char *)
391 ZOOM_options_getl(ZOOM_options opt, const char *name, int *lenp);
392
393 ZOOM_API(void)
394 ZOOM_options_set(ZOOM_options opt, const char *name, const char *v);
395
396 ZOOM_API(void)
397 ZOOM_options_setl(ZOOM_options opt, const char *name, const char *value,
398                   int len);
399
400 ZOOM_API(void)
401 ZOOM_options_destroy(ZOOM_options opt);
402
403 ZOOM_API(int)
404 ZOOM_options_get_bool(ZOOM_options opt, const char *name, int defa);
405
406 ZOOM_API(int)
407 ZOOM_options_get_int(ZOOM_options opt, const char *name, int defa);
408
409 ZOOM_API(void)
410 ZOOM_options_set_int(ZOOM_options opt, const char *name, int value);
411
412 /** \brief select/poll socket mask: read */
413 #define ZOOM_SELECT_READ 1
414 /** \brief select/poll socket mask: write */
415 #define ZOOM_SELECT_WRITE 2
416 /** \brief select/poll socket mask: except */
417 #define ZOOM_SELECT_EXCEPT 4
418
419 /** \brief wait for events on connection(s) (BLOCKING)
420     \param no number of connections (size of cs)
421     \param cs connection array
422     \retval 0 no event was fired
423     \retval >0 event was fired for connection at (retval-1)
424
425     blocking poll for events on a number of connections. Returns positive
426     integer if event occurred ; zero if none occurred and no more
427     events are pending. The positive integer specifies the
428     connection for which the event occurred.
429 */
430 ZOOM_API(int)
431 ZOOM_event(int no, ZOOM_connection *cs);
432
433
434 /** \brief determines if connection is idle (no active or pending work)
435     \param c connection
436     \retval 1 is idle
437     \retval 0 is non-idle (active)
438 */
439 ZOOM_API(int)
440 ZOOM_connection_is_idle(ZOOM_connection c);
441
442
443 /** \brief process one event for one of connections given
444     \param no number of connections (size of cs)
445     \param cs connection array
446     \retval 0 no event was processed
447     \retval >0 event was processed for connection at (retval-1)
448
449     This function attemps to deal with outstandings events in a non-blocking
450     mode. If no events was processed (return value of 0),  then the system
451     should attempt to deal with sockets in blocking mode using socket
452     select/poll which means calling the following functions:
453     ZOOM_connection_get_socket, ZOOM_connection_get_mask,
454     ZOOM_connection_get_timeout.
455 */
456 ZOOM_API(int)
457 ZOOM_event_nonblock(int no, ZOOM_connection *cs);
458
459
460 /** \brief process one event for connection
461     \param c connection
462     \retval 0 no event was processed
463     \retval 1 event was processed for connection
464
465     This function attemps to deal with outstandings events in
466     a non-blocking fashion. If no event was processed (return value of 0),
467     then the system should attempt to deal with sockets in blocking mode
468     using socket select/poll which means calling the following functions:
469     ZOOM_connection_get_socket, ZOOM_connection_get_mask,
470     ZOOM_connection_get_timeout. If an event was processed call this
471     function again.
472 */
473 ZOOM_API(int)
474 ZOOM_connection_process(ZOOM_connection c);
475
476
477 /** \brief executes non-blocking tasks for connection
478     \param c connection
479     \retval 0 no task was executed
480     \retval 1 task was executed (but probably not completed)
481
482     This function, unlike, ZOOM_connection_process, does not try to
483     return any events (and remove them). But events may be generated
484     from it. These are saved and may later be retrieved with
485     ZOOM_connection_process and ZOOM_connection_last_event .
486 */
487 ZOOM_API(int)
488 ZOOM_connection_exec_task(ZOOM_connection c);
489
490
491 /** \brief get socket fd for ZOOM connection
492     \param c connection
493     \retval -1 no socket assigned for connection
494     \retval >=0 socket for connection
495
496     Use this function when preparing for socket/poll and
497     in conjunction with ZOOM_connection_get_mask.
498 */
499 ZOOM_API(int)
500 ZOOM_connection_get_socket(ZOOM_connection c);
501
502
503 /** \brief get socket mask for connection
504     \param c connection
505     \returns mask for connection (possibly 0)
506
507     Use this function when preparing for socket select/poll and
508     in conjunction with ZOOM_connection_get_socket.
509 */
510 ZOOM_API(int)
511 ZOOM_connection_get_mask(ZOOM_connection c);
512
513
514 /** \brief set socket mask for connection (DO NOT call outside zoom) */
515 ZOOM_API(int)
516 ZOOM_connection_set_mask(ZOOM_connection c, int mask);
517
518
519 /** \brief get timeout in seconds for ZOOM connection
520     \param c connection
521     \returns timeout value in seconds
522
523     Use this function when preparing for socket/poll and
524     in conjunction with ZOOM_connection_get_socket.
525 */
526 ZOOM_API(int)
527 ZOOM_connection_get_timeout(ZOOM_connection c);
528
529
530 /** \brief fire socket event timeout
531     \param c connection
532     \retval 0 event was fired OK
533     \retval -1 event was not fired
534
535     Call this function when a timeout occurs - for example in the
536     case of select(2) returning 0.
537 */
538 ZOOM_API(int)
539 ZOOM_connection_fire_event_timeout(ZOOM_connection c);
540
541
542 /** \brief fire socket event activity (read,write,except)
543     \param c connection
544     \param mask or'ed mask of ZOOM_SELECT_.. values
545     \retval 0 event was fired OK
546     \retval -1 event was not fired
547 */
548 ZOOM_API(int)
549 ZOOM_connection_fire_event_socket(ZOOM_connection c, int mask);
550
551
552 /** \brief peek at next event
553     \param c connection
554     \returns ZOOM_EVENT_NONE (for no events in queue), ZOOM_EVENT_CONNECT, ..
555
556     Does not actually remove the event from the event queue. ZOOM_event and
557     ZOOM_process_event removes one event.
558 */
559
560 ZOOM_API(int)
561 ZOOM_connection_peek_event(ZOOM_connection c);
562
563 ZOOM_API(const char *)
564 ZOOM_get_event_str(int event);
565
566 #ifdef WRBUF_H
567
568 /** \brief log APDUs to WRBUF
569     \param c connection
570     \param w WRBUF where APDUs are logged
571 */
572 ZOOM_API(void) ZOOM_connection_save_apdu_wrbuf(ZOOM_connection c, WRBUF w);
573 #endif
574
575 ZOOM_END_CDECL
576
577 /*
578  * Local variables:
579  * c-basic-offset: 4
580  * c-file-style: "Stroustrup"
581  * indent-tabs-mode: nil
582  * End:
583  * vim: shiftwidth=4 tabstop=8 expandtab
584  */
585