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