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