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