Use GCC attribute warn_unsed_result for a few of the most important
[idzebra-moved-to-github.git] / include / idzebra / api.h
1 /* $Id: api.h,v 1.32 2006-03-31 15:58:03 adam Exp $
2    Copyright (C) 1995-2005
3    Index Data ApS
4
5 This file is part of the Zebra server.
6
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra.  If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
21 */
22
23 /** \file api.h
24     \brief Zebra API
25     
26     Return codes:
27     Most functions return ZEBRA_RES, where ZEBRA_FAIL indicates
28     failure; ZEBRA_OK indicates success.
29 */
30
31 #ifndef IDZEBRA_API_H
32 #define IDZEBRA_API_H
33
34 #include <yaz/odr.h>
35 #include <yaz/oid.h>
36 #include <yaz/proto.h>
37 #include <idzebra/res.h>
38 #include <idzebra/version.h>
39
40 YAZ_BEGIN_CDECL
41
42 /*
43   expand GCC_ATTRIBUTE if GCC is in use. See :
44   http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
45 */
46
47 #ifndef GCC_ATTRIBUTE
48 #ifdef __GNUC__
49 #define GCC_ATTRIBUTE(x) __attribute__ (x)
50 #else
51 #define GCC_ATTRIBUTE(x)
52 #endif
53 #endif
54
55 typedef struct {
56     int processed;
57     int inserted;
58     int updated;
59     int deleted;
60     long utime;
61     long stime;
62 } ZebraTransactionStatus;
63
64 /** Retrieval Record Descriptor */
65 typedef struct {
66     int errCode;         /* non-zero if error when fetching this */
67     char *errString;     /* error string */
68     int position;        /* position of record in result set (1,2,..) */
69     char *buf;           /* record buffer (void pointer really) */
70     int len;             /* length */
71     oid_value format;    /* record syntax */
72     char *base; 
73     SYSNO sysno;
74     int  score;
75 } ZebraRetrievalRecord;
76
77 /** Scan Term Descriptor */
78 typedef struct {
79     int occurrences;     /* scan term occurrences */
80     char *term;          /* scan term string */
81 } ZebraScanEntry;
82
83 /** \var ZebraHandle
84     \brief a Zebra Handle - (session)
85 */
86 typedef struct zebra_session *ZebraHandle;
87
88 /** \var ZebraService
89     \brief a Zebra Service handle
90 */
91 typedef struct zebra_service *ZebraService;
92
93 /** \fn ZebraService zebra_start(const char *configName)
94     \brief starts a Zebra service. 
95     \param configName name of configuration file
96     
97     This function is a simplified version of zebra_start_res.
98 */
99 YAZ_EXPORT
100 ZebraService zebra_start(const char *configName
101     ) GCC_ATTRIBUTE((warn_unused_result));
102
103 /** \fn ZebraService zebra_start_res(const char *configName, \
104                                      Res def_res, Res over_res)
105     \brief starts a Zebra service with resources.
106     \param configName name of configuration file
107     \param def_res default resources
108     \param over_res overriding resources
109     
110     This function typically called once in a program. A Zebra Service
111     acts as a factory for Zebra session handles.
112 */
113 YAZ_EXPORT
114 ZebraService zebra_start_res(const char *configName,
115                              Res def_res, Res over_res
116     ) GCC_ATTRIBUTE((warn_unused_result));
117
118 /**
119    \brief stops a Zebra service.
120    \param zs service handle
121    
122    Frees resources used by the service.
123 */
124 YAZ_EXPORT
125 ZEBRA_RES zebra_stop(ZebraService zs);
126
127 /**
128    \brief Lists enabled Zebra filters
129    \param zs service handle
130    \param cd callback parameter (opaque)
131    \param cb callback function
132  */
133 YAZ_EXPORT
134 void zebra_filter_info(ZebraService zs, void *cd,
135                        void (*cb)(void *cd, const char *name));
136
137
138 /**
139    \brief Creates a Zebra session handle within service.
140    \param zs service handle.
141    
142    There should be one handle for each thread doing something
143    with zebra, be that searching or indexing. In simple apps 
144    one handle is sufficient 
145 */
146 YAZ_EXPORT
147 ZebraHandle zebra_open(ZebraService zs, Res res
148     ) GCC_ATTRIBUTE((warn_unused_result));
149
150 /**
151    \brief Destroys Zebra session handle.
152    \param zh zebra session handle.
153  */
154 YAZ_EXPORT
155 ZEBRA_RES zebra_close(ZebraHandle zh);
156
157 /**
158    \brief Returns error code for last error
159    \param zh zebra session handle.
160 */
161 YAZ_EXPORT
162 int zebra_errCode(ZebraHandle zh);
163
164 /**
165    \brief Returns error string for last error
166    \param zh zebra session handle.
167 */
168 YAZ_EXPORT
169 const char *zebra_errString(ZebraHandle zh);
170
171 /**
172    \brief Returns additional info for last error
173    \param zh zebra session handle.
174 */
175 YAZ_EXPORT
176 char *zebra_errAdd(ZebraHandle zh);
177
178 /**
179    \brief Returns error code and additional info for last error
180    \param zh zebra session handle.
181    \param code pointer to returned error code
182    \param addinfo pointer to returned additional info
183 */
184 YAZ_EXPORT
185 void zebra_result(ZebraHandle zh, int *code, char **addinfo);
186
187 /**
188    \brief Set limit before Zebra does approx hit count
189    \param zh session handle
190    \param approx_limit the limit
191    
192    Results will be approximiate if hit count is greater than the
193    limit specified. By default there is a high-limit (no limit).
194 */
195 ZEBRA_RES zebra_set_approx_limit(ZebraHandle zh, zint approx_limit);
196
197 /**
198    \brief Search using PQF Query 
199    \param zh session handle
200    \param pqf_query query
201    \param setname name of resultset
202    \param hits of hits is returned
203  */
204 YAZ_EXPORT
205 ZEBRA_RES zebra_search_PQF(ZebraHandle zh, const char *pqf_query,
206                            const char *setname, zint *hits);
207
208 /** \fn ZEBRA_RES zebra_search_RPN(ZebraHandle zh, ODR o, Z_RPNQuery *query, \
209                 const char *setname, zint *hits)
210     \brief Search using RPN Query 
211     \param zh session handle
212     \param o ODR handle
213     \param query RPN query using YAZ structure
214     \param setname name of resultset
215     \param hits number of hits is returned
216  */
217 YAZ_EXPORT
218 ZEBRA_RES zebra_search_RPN(ZebraHandle zh, ODR o, Z_RPNQuery *query,
219                               const char *setname, zint *hits);
220
221 /** 
222     \fn ZEBRA_RES zebra_records_retrieve(ZebraHandle zh, ODR stream, \
223                 const char *setname, Z_RecordComposition *comp, \
224                 oid_value input_format, int num_recs, \
225                 ZebraRetrievalRecord *recs)
226     \brief Retrieve records from result set (after search)
227     \param zh session handle
228     \param stream allocate records returned using this ODR
229     \param setname name of result set to retrieve records from
230     \param comp Z39.50 record composition
231     \param input_format transfer syntax (OID)
232     \param num_recs number of records to retrieve
233     \param recs store records in this structure (size is num_recs)
234 */
235 YAZ_EXPORT
236 ZEBRA_RES zebra_records_retrieve(ZebraHandle zh, ODR stream,
237                                  const char *setname,
238                                  Z_RecordComposition *comp,
239                                  oid_value input_format,
240                                  int num_recs,
241                                  ZebraRetrievalRecord *recs);
242 /**
243    \brief Deletes one or more resultsets 
244    \param zh session handle
245    \param function Z_DeleteResultSetRequest_{list,all}
246    \param num_setnames number of result sets
247    \param setnames result set names
248    \param statuses status result
249 */
250 YAZ_EXPORT
251 int zebra_deleteResultSet(ZebraHandle zh, int function,
252                           int num_setnames, char **setnames,
253                           int *statuses);
254
255
256 /**
257    \brief returns number of term info terms assocaited with result set
258    \param zh session handle
259    \param setname result set name
260    \param num_terms number of terms returned in this integer
261
262    This function is used in conjunction with zebra_result_set_term_info.
263    If operation was successful, ZEBRA_OK is returned; otherwise
264    ZEBRA_FAIL is returned (typically non-existing setname)
265 */
266 YAZ_EXPORT
267 ZEBRA_RES zebra_result_set_term_no(ZebraHandle zh, const char *setname,
268                                    int *num_terms);
269
270 /**
271    \brief returns information about a term assocated with a result set
272    \param zh session handle
273    \param setname result set name
274    \param no the term we want to know about (0=first, 1=second,..)
275    \param count the number of occurrences of this term, aka hits (output) 
276    \param approx about hits: 0=exact,1=approx (output)
277    \param termbuf buffer for term string (intput, output)
278    \param termlen size of termbuf (input=max, output=actual length)
279    \param term_ref_id if non-NULL *term_ref_id holds term reference
280
281    Returns information about one search term associated with result set.
282    Use zebra_result_set_term_no to read total number of terms associated
283    with result set. If this function can not return information,
284    due to no out of range or bad result set name, ZEBRA_FAIL is
285    returned.
286    The passed termbuf must be able to hold at least *termlen characters.
287    Upon completion, *termlen holds actual length of search term.
288 */
289 YAZ_EXPORT
290 ZEBRA_RES zebra_result_set_term_info(ZebraHandle zh, const char *setname,
291                                      int no, zint *count, int *approx,
292                                      char *termbuf, size_t *termlen,
293                                      const char **term_ref_id);
294
295
296 /**
297    \brief performs Scan (Z39.50 style)
298    \param zh session handle
299    \param stream ODR handle for result
300    \param zapt Attribute plus Term (start term)
301    \param attributeset Attributeset for Attribute plus Term
302    \param position input/output position
303    \param num_entries number of terms requested / returned 
304    \param entries list of resulting terms (ODR allocated)
305    \param is_partial upon return 1=partial, 0=complete
306    \param setname limit scan by this set (NULL means no limit)
307 */
308 YAZ_EXPORT ZEBRA_RES zebra_scan(ZebraHandle zh, ODR stream,
309                                 Z_AttributesPlusTerm *zapt,
310                                 oid_value attributeset,
311                                 int *position, int *num_entries,
312                                 ZebraScanEntry **entries,
313                                 int *is_partial,
314                                 const char *setname);
315
316 /**
317    \brief performs Scan (taking PQF string)
318    \param zh session handle
319    \param stream ODR handle for result
320    \param query PQF scan query
321    \param position input/output position
322    \param num_entries number of terms requested / returned 
323    \param entries list of resulting terms (ODR allocated)
324    \param is_partial upon return 1=partial, 0=complete
325    \param setname limit scan by this set (NULL means no limit)
326 */
327 YAZ_EXPORT
328 ZEBRA_RES zebra_scan_PQF(ZebraHandle zh, ODR stream, const char *query,
329                     int *position, int *num_entries, ZebraScanEntry **entries,
330                     int *is_partial, const char *setname);
331
332 /**
333    \brief authenticate user. Returns 0 if OK, != 0 on failure
334    \param zh session handle
335    \param user user name
336    \param pass password
337  */
338 YAZ_EXPORT
339 ZEBRA_RES zebra_auth(ZebraHandle zh, const char *user, const char *pass);
340
341 /**
342    \brief Normalize zebra term for register (subject to change!)
343    \param zh session handle
344    \param reg_id register ID, 'w', 'p',..
345    \param input_str input string buffer
346    \param input_len input string length
347    \param output_str output string buffer
348    \param output_len output string length
349  */
350 YAZ_EXPORT
351 int zebra_string_norm(ZebraHandle zh, unsigned reg_id, const char *input_str, 
352                 int input_len, char *output_str, int output_len);
353
354 /**
355    \brief Creates a database
356    \param zh session handle
357    \param db database to be created
358 */
359 YAZ_EXPORT
360 ZEBRA_RES zebra_create_database(ZebraHandle zh, const char *db);
361
362 /**
363    \brief Deletes a database (drop)
364    \param zh session handle
365    \param db database to be deleted
366 */
367 YAZ_EXPORT
368 ZEBRA_RES zebra_drop_database(ZebraHandle zh, const char *db);
369
370 YAZ_EXPORT
371 ZEBRA_RES zebra_admin_shutdown(ZebraHandle zh);
372
373 YAZ_EXPORT
374 ZEBRA_RES zebra_admin_start(ZebraHandle zh);
375
376 YAZ_EXPORT
377 ZEBRA_RES zebra_shutdown(ZebraService zs);
378
379 YAZ_EXPORT
380 ZEBRA_RES zebra_admin_import_begin(ZebraHandle zh, const char *database,
381                                    const char *record_type);
382
383 YAZ_EXPORT 
384 ZEBRA_RES zebra_admin_import_segment(ZebraHandle zh,
385                                      Z_Segment *segment);
386
387 YAZ_EXPORT 
388 ZEBRA_RES zebra_admin_import_end(ZebraHandle zh);
389
390 YAZ_EXPORT 
391 ZEBRA_RES zebra_admin_exchange_record(ZebraHandle zh,
392                                       const char *rec_buf,
393                                       size_t rec_len,
394                                       const char *recid_buf, size_t recid_len,
395                                       int action);
396
397 YAZ_EXPORT 
398 ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw
399     ) GCC_ATTRIBUTE((warn_unused_result));
400
401 YAZ_EXPORT
402 ZEBRA_RES zebra_end_trans(ZebraHandle zh
403     ) GCC_ATTRIBUTE((warn_unused_result));
404
405 YAZ_EXPORT
406 ZEBRA_RES zebra_end_transaction(ZebraHandle zh,
407                                 ZebraTransactionStatus *stat);
408
409 YAZ_EXPORT
410 ZEBRA_RES zebra_commit(ZebraHandle zh);
411
412 YAZ_EXPORT
413 ZEBRA_RES zebra_clean(ZebraHandle zh);
414
415 YAZ_EXPORT
416 ZEBRA_RES zebra_init(ZebraHandle zh);
417
418 YAZ_EXPORT
419 ZEBRA_RES zebra_compact(ZebraHandle zh);
420
421 YAZ_EXPORT int zebra_repository_update(ZebraHandle zh, const char *path);
422 YAZ_EXPORT int zebra_repository_delete(ZebraHandle zh, const char *path);
423 YAZ_EXPORT int zebra_repository_show(ZebraHandle zh, const char *path);
424
425 YAZ_EXPORT int zebra_add_record(ZebraHandle zh, const char *buf, int buf_size);
426                                
427 YAZ_EXPORT 
428 ZEBRA_RES zebra_insert_record(ZebraHandle zh, 
429                               const char *recordType,
430                               SYSNO *sysno, const char *match,
431                               const char *fname,
432                               const char *buf, int buf_size,
433                               int force_update);
434 YAZ_EXPORT
435 ZEBRA_RES zebra_update_record(ZebraHandle zh, 
436                               const char *recordType,
437                               SYSNO *sysno, const char *match,
438                               const char *fname,
439                               const char *buf, int buf_size,
440                               int force_update);
441 YAZ_EXPORT 
442 ZEBRA_RES zebra_delete_record(ZebraHandle zh, 
443                               const char *recordType,
444                               SYSNO *sysno, const char *match, const char *fname,
445                               const char *buf, int buf_size,
446                               int force_update);
447
448 YAZ_EXPORT 
449 ZEBRA_RES zebra_sort(ZebraHandle zh, ODR stream,
450                      int num_input_setnames,
451                      const char **input_setnames,
452                      const char *output_setname,
453                      Z_SortKeySpecList *sort_sequence,
454                      int *sort_status
455     ) GCC_ATTRIBUTE((warn_unused_result));    
456
457 YAZ_EXPORT
458 ZEBRA_RES zebra_select_databases(ZebraHandle zh, int num_bases, 
459                                  const char **basenames
460     ) GCC_ATTRIBUTE((warn_unused_result));
461
462 YAZ_EXPORT
463 ZEBRA_RES zebra_select_database(ZebraHandle zh, const char *basename
464     ) GCC_ATTRIBUTE((warn_unused_result));
465
466 YAZ_EXPORT
467 void zebra_shadow_enable(ZebraHandle zh, int value);
468
469 YAZ_EXPORT
470 int zebra_register_statistics(ZebraHandle zh, int dumpdict);
471
472 YAZ_EXPORT
473 ZEBRA_RES zebra_record_encoding(ZebraHandle zh, const char *encoding);
474
475 YAZ_EXPORT
476 ZEBRA_RES zebra_octet_term_encoding(ZebraHandle zh, const char *encoding);
477
478 /* Resources */
479 YAZ_EXPORT
480 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value);
481 YAZ_EXPORT
482 const char *zebra_get_resource(ZebraHandle zh, 
483                                const char *name, const char *defaultvalue);
484
485
486 YAZ_EXPORT
487 void zebra_pidfname(ZebraService zs, char *path);
488
489 typedef struct {
490     char *term;
491     char *db;
492     zint sysno;
493     int score;
494 } ZebraMetaRecord;
495
496 YAZ_EXPORT
497 ZebraMetaRecord *zebra_meta_records_create(ZebraHandle zh,
498                                            const char *name,
499                                            int num, zint *positions);
500
501
502 YAZ_EXPORT
503 ZebraMetaRecord *zebra_meta_records_create_range(ZebraHandle zh,
504                                                  const char *name, 
505                                                  zint start, int num);
506
507 YAZ_EXPORT
508 void zebra_meta_records_destroy(ZebraHandle zh, ZebraMetaRecord *records,
509                                 int num);
510
511 YAZ_EXPORT 
512 struct BFiles_struct *zebra_get_bfs(ZebraHandle zh);
513
514 YAZ_EXPORT
515 ZEBRA_RES zebra_set_limit(ZebraHandle zh, int complement_flag, zint *ids);
516
517 YAZ_END_CDECL                                 
518
519 /** \mainpage Zebra
520  *
521  * \section intro_sec Introduction
522  *
523  * Zebra is a search engine for structure data, such as XML, MARC
524  * and others.
525  *
526  * API users should read the api.h for all the public definitions.
527  *
528  * The remaining sections briefly describe each of
529  * Zebra major modules/components.
530  *
531  * \section util Base Utilities
532  *
533  * The Zebra utilities (util.h) defines fundamental types and a few
534  * utilites for Zebra.
535  *
536  * \section res Resources
537  *
538  * The resources system (res.h) is a manager of configuration 
539  * resources. The resources can be viewed as a simple database.
540  * Resources can be read from a configurtion file, they can be
541  * read or written by an application. Resources can also be written,
542  * but that facility is not currently in use.
543  *
544  * \section bfile Bfiles
545  *
546  * The Bfiles (bfile.h) provides a portable interface to the
547  * local file system. It also provides a facility for safe updates
548  * (shadow updates). All file system access is handle by this module
549  * (except for trival reads of configuration files).
550  *
551  * \section dict Dictionary
552  *
553  * The Zebra dictionary (dict.h) maps a search term (key) to a value. The
554  * value is a reference to the list of records identifers in which
555  * the term occurs. Zebra uses an ISAM data structure for the list
556  * of term occurrences. The Dictionary uses \ref bfile.
557  *
558  * \section isam ISAM
559  *
560  * Zebra maintains an ISAM for each term where each ISAM is a list
561  * of record identifiers corresponding to the records in which the
562  * term occur. Unlike traditional ISAM systems, the Zebra ISAM
563  * is compressed. The ISAM system uses \ref bfile.
564  *
565  * Zebra has more than one ISAM system. The old and stable ISAM system
566  * is named isamc (see isamc.h). Another version isams is a write-once
567  * isam system that is quite compact - suitable for CD-ROMs (isams.h). 
568  * The newest ISAM system, isamb, is implemented as a B-Tree (see isamb.h).
569  *
570  * \section data1 Data-1
571  *
572  * The data1 (data1.h) module deals with structured documents. The module can
573  * can read, modify and write documents. The document structure was
574  * originally based on GRS-1 - a Z39.50 v3 structure that predates
575  * DOM. These days the data1 structure may describe XML/SGML as well.
576  * The data1, like DOM, is a tree structure. Each node in the tree
577  * can be of type element, text (cdata), preprocessing instruction,
578  * comment. Element nodes can point to attribute nodes.
579  *
580  * \section recctrl Record Control
581  *
582  * The record control module (recctrl.h) is responsible for
583  * managing the various record types ("classes" or filters).
584  *
585  * \section rset Result-Set
586  *
587  * The Result-Set module (rset.h) defines an interface that all
588  * Zebra Search Results must implement. Each operation (AND, OR, ..)
589  * correspond to an implementation of that interface.
590  *
591  * \section dfa DFA
592  *
593  * DFA (dfa.h) Deterministic Finite Automa is a regular expression engine.
594  * The module compiles a regular expression to a DFA. The DFA can then
595  * be used in various application to perform fast match against the
596  * origianl expression. The \ref Dict uses DFA to perform lookup
597  * using regular expressions.
598  */
599
600 #endif