bd6516aafc24128f82620f3800781ed9b5e98afa
[idzebra-moved-to-github.git] / include / idzebra / api.h
1 /* $Id: api.h,v 1.25 2005-06-02 11:59:53 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);
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 Clears last error.
173     \param zh zebra session handle.
174 */
175 YAZ_EXPORT
176 void zebra_clearError(ZebraHandle zh);
177
178 /**
179    \brief Search using PQF Query 
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 /** \fn ZEBRA_RES zebra_search_RPN(ZebraHandle zh, ODR o, Z_RPNQuery *query, \
190                 const char *setname, zint *hits)
191     \brief Search using RPN Query 
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  */
198 YAZ_EXPORT
199 ZEBRA_RES zebra_search_RPN(ZebraHandle zh, ODR o, Z_RPNQuery *query,
200                               const char *setname, zint *hits);
201
202 /** 
203     \fn ZEBRA_RES zebra_records_retrieve(ZebraHandle zh, ODR stream, \
204                 const char *setname, Z_RecordComposition *comp, \
205                 oid_value input_format, int num_recs, \
206                 ZebraRetrievalRecord *recs)
207     \brief Retrieve records from result set (after search)
208     \param zh session handle
209     \param stream allocate records returned using this ODR
210     \param setname name of result set to retrieve records from
211     \param comp Z39.50 record composition
212     \param input_format transfer syntax (OID)
213     \param num_recs number of records to retrieve
214     \param recs store records in this structure (size is num_recs)
215 */
216 YAZ_EXPORT
217 ZEBRA_RES zebra_records_retrieve(ZebraHandle zh, ODR stream,
218                                  const char *setname,
219                                  Z_RecordComposition *comp,
220                                  oid_value input_format,
221                                  int num_recs,
222                                  ZebraRetrievalRecord *recs);
223 /**
224    \brief Deletes one or more resultsets 
225    \param zh session handle
226    \param function Z_DeleteResultSetRequest_{list,all}
227    \param num_setnames number of result sets
228    \param setnames result set names
229    \param statuses status result
230 */
231 YAZ_EXPORT
232 int zebra_deleteResultSet(ZebraHandle zh, int function,
233                           int num_setnames, char **setnames,
234                           int *statuses);
235
236
237
238 YAZ_EXPORT
239 ZEBRA_RES zebra_result_set_term_no(ZebraHandle zh, const char *setname,
240                                    int *num_terms);
241
242 YAZ_EXPORT
243 ZEBRA_RES zebra_result_set_term_info(ZebraHandle zh, const char *setname,
244                                      int no, zint *count, int *approx,
245                                      char *termbuf, size_t *termlen);
246
247
248 /**
249    \brief performs Scan (Z39.50 style)
250    \param zh session handle
251    \param stream ODR handle for result
252    \param zapt Attribute plus Term (start term)
253    \param attributeset Attributeset for Attribute plus Term
254    \param position input/output position
255    \param num_entries number of terms requested / returned 
256    \param entries list of resulting terms (ODR allocated)
257    \param is_partial upon return 1=partial, 0=complete
258 */
259 YAZ_EXPORT ZEBRA_RES zebra_scan(ZebraHandle zh, ODR stream,
260                                 Z_AttributesPlusTerm *zapt,
261                                 oid_value attributeset,
262                                 int *position, int *num_entries,
263                                 ZebraScanEntry **entries,
264                                 int *is_partial);
265
266 /**
267    \brief performs Scan (taking PQF string)
268    \param zh session handle
269    \param stream ODR handle for result
270    \param query PQF scan query
271    \param position input/output position
272    \param num_entries number of terms requested / returned 
273    \param entries list of resulting terms (ODR allocated)
274    \param is_partial upon return 1=partial, 0=complete
275 */
276 YAZ_EXPORT
277 ZEBRA_RES zebra_scan_PQF(ZebraHandle zh, ODR stream, const char *query,
278                     int *position, int *num_entries, ZebraScanEntry **entries,
279                     int *is_partial);
280
281 /**
282    \brief authenticate user. Returns 0 if OK, != 0 on failure
283    \param zh session handle
284    \param user user name
285    \param pass password
286  */
287 YAZ_EXPORT
288 ZEBRA_RES zebra_auth(ZebraHandle zh, const char *user, const char *pass);
289
290 /**
291    \brief Normalize zebra term for register (subject to change!)
292    \param zh session handle
293    \param reg_id register ID, 'w', 'p',..
294    \param input_str input string buffer
295    \param input_len input string length
296    \param output_str output string buffer
297    \param output_len output string length
298  */
299 YAZ_EXPORT
300 int zebra_string_norm(ZebraHandle zh, unsigned reg_id, const char *input_str, 
301                 int input_len, char *output_str, int output_len);
302
303 /**
304    \brief Creates a database
305    \param zh session handle
306    \param db database to be created
307 */
308 YAZ_EXPORT
309 ZEBRA_RES zebra_create_database(ZebraHandle zh, const char *db);
310
311 /**
312    \brief Deletes a database (drop)
313    \param zh session handle
314    \param db database to be deleted
315 */
316 YAZ_EXPORT
317 ZEBRA_RES zebra_drop_database(ZebraHandle zh, const char *db);
318
319 YAZ_EXPORT
320 ZEBRA_RES zebra_admin_shutdown(ZebraHandle zh);
321
322 YAZ_EXPORT
323 ZEBRA_RES zebra_admin_start(ZebraHandle zh);
324
325 YAZ_EXPORT
326 ZEBRA_RES zebra_shutdown(ZebraService zs);
327
328 YAZ_EXPORT
329 ZEBRA_RES zebra_admin_import_begin(ZebraHandle zh, const char *database,
330                                    const char *record_type);
331
332 YAZ_EXPORT 
333 ZEBRA_RES zebra_admin_import_segment(ZebraHandle zh,
334                                      Z_Segment *segment);
335
336 YAZ_EXPORT 
337 ZEBRA_RES zebra_admin_import_end(ZebraHandle zh);
338
339 YAZ_EXPORT 
340 ZEBRA_RES zebra_admin_exchange_record(ZebraHandle zh,
341                                       const char *rec_buf,
342                                       size_t rec_len,
343                                       const char *recid_buf, size_t recid_len,
344                                       int action);
345
346 YAZ_EXPORT 
347 ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw);
348
349 YAZ_EXPORT
350 ZEBRA_RES zebra_end_trans(ZebraHandle zh);
351
352 YAZ_EXPORT
353 ZEBRA_RES zebra_end_transaction(ZebraHandle zh,
354                                 ZebraTransactionStatus *stat);
355
356 YAZ_EXPORT
357 ZEBRA_RES zebra_commit(ZebraHandle zh);
358
359 YAZ_EXPORT
360 ZEBRA_RES zebra_clean(ZebraHandle zh);
361
362 YAZ_EXPORT
363 ZEBRA_RES zebra_init(ZebraHandle zh);
364
365 YAZ_EXPORT
366 ZEBRA_RES zebra_compact(ZebraHandle zh);
367
368 YAZ_EXPORT int zebra_repository_update(ZebraHandle zh, const char *path);
369 YAZ_EXPORT int zebra_repository_delete(ZebraHandle zh, const char *path);
370 YAZ_EXPORT int zebra_repository_show(ZebraHandle zh, const char *path);
371
372 YAZ_EXPORT int zebra_add_record(ZebraHandle zh, const char *buf, int buf_size);
373                                
374 YAZ_EXPORT 
375 ZEBRA_RES zebra_insert_record(ZebraHandle zh, 
376                               const char *recordType,
377                               SYSNO *sysno, const char *match,
378                               const char *fname,
379                               const char *buf, int buf_size,
380                               int force_update);
381 YAZ_EXPORT
382 ZEBRA_RES zebra_update_record(ZebraHandle zh, 
383                               const char *recordType,
384                               SYSNO *sysno, const char *match,
385                               const char *fname,
386                               const char *buf, int buf_size,
387                               int force_update);
388 YAZ_EXPORT 
389 ZEBRA_RES zebra_delete_record(ZebraHandle zh, 
390                               const char *recordType,
391                               SYSNO *sysno, const char *match, const char *fname,
392                               const char *buf, int buf_size,
393                               int force_update);
394
395 YAZ_EXPORT 
396 int zebra_resultSetTerms(ZebraHandle zh, const char *setname, 
397                          int no, zint *count, 
398                          int *type, char *out, size_t *len);
399
400 YAZ_EXPORT 
401 ZEBRA_RES zebra_sort(ZebraHandle zh, ODR stream,
402                      int num_input_setnames,
403                      const char **input_setnames,
404                      const char *output_setname,
405                      Z_SortKeySpecList *sort_sequence,
406                      int *sort_status);
407
408 YAZ_EXPORT
409 ZEBRA_RES zebra_select_databases(ZebraHandle zh, int num_bases, 
410                                  const char **basenames);
411
412 YAZ_EXPORT
413 ZEBRA_RES zebra_select_database(ZebraHandle zh, const char *basename);
414
415 YAZ_EXPORT
416 void zebra_shadow_enable(ZebraHandle zh, int value);
417
418 YAZ_EXPORT
419 int zebra_register_statistics(ZebraHandle zh, int dumpdict);
420
421 YAZ_EXPORT
422 ZEBRA_RES zebra_record_encoding(ZebraHandle zh, const char *encoding);
423
424 YAZ_EXPORT
425 ZEBRA_RES zebra_octet_term_encoding(ZebraHandle zh, const char *encoding);
426
427 /* Resources */
428 YAZ_EXPORT
429 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value);
430 YAZ_EXPORT
431 const char *zebra_get_resource(ZebraHandle zh, 
432                                const char *name, const char *defaultvalue);
433
434
435 YAZ_EXPORT
436 void zebra_pidfname(ZebraService zs, char *path);
437
438 typedef struct {
439     char *term;
440     char *db;
441     zint sysno;
442     int score;
443 } ZebraMetaRecord;
444
445 YAZ_EXPORT
446 ZebraMetaRecord *zebra_meta_records_create(ZebraHandle zh,
447                                            const char *name,
448                                            int num, zint *positions);
449
450
451 YAZ_EXPORT
452 ZebraMetaRecord *zebra_meta_records_create_range(ZebraHandle zh,
453                                                  const char *name, 
454                                                  zint start, int num);
455
456 YAZ_EXPORT
457 void zebra_meta_records_destroy(ZebraHandle zh, ZebraMetaRecord *records,
458                                 int num);
459
460 YAZ_EXPORT 
461 struct BFiles_struct *zebra_get_bfs(ZebraHandle zh);
462
463 YAZ_EXPORT
464 ZEBRA_RES zebra_set_limit(ZebraHandle zh, int complement_flag, zint *ids);
465
466 YAZ_END_CDECL                                 
467
468 /** \mainpage Zebra
469  *
470  * \section intro_sec Introduction
471  *
472  * Zebra is a search engine for structure data, such as XML, MARC
473  * and others. The following chapters briefly describe each of
474  * Zebra major modules/components.
475  *
476  * \section util Base Utilities
477  *
478  * The Zebra utilities (util.h) defines fundamental types and a few
479  * utilites for Zebra.
480  *
481  * \section res Resources
482  *
483  * The resources system (res.h) is a manager of configuration 
484  * resources. The resources can be viewed as a simple database.
485  * Resources can be read from a configurtion file, they can be
486  * read or written by an application. Resources can also be written,
487  * but that facility is not currently in use.
488  *
489  * \section bfile Bfiles
490  *
491  * The Bfiles (bfile.h) provides a portable interface to the
492  * local file system. It also provides a facility for safe updates
493  * (shadow updates). All file system access is handle by this module
494  * (except for trival reads of configuration files).
495  *
496  * \section dict Dictionary
497  *
498  * The Zebra dictionary (dict.h) maps a search term (key) to a value. The
499  * value is a reference to the list of records identifers in which
500  * the term occurs. Zebra uses an ISAM data structure for the list
501  * of term occurrences. The Dictionary uses \ref bfile.
502  *
503  * \section isam ISAM
504  *
505  * Zebra maintains an ISAM for each term where each ISAM is a list
506  * of record identifiers corresponding to the records in which the
507  * term occur. Unlike traditional ISAM systems, the Zebra ISAM
508  * is compressed. The ISAM system uses \ref bfile.
509  *
510  * Zebra has more than one ISAM system. The old and stable ISAM system
511  * is named isamc (see isamc.h). Another version isams is a write-once
512  * isam system that is quite compact - suitable for CD-ROMs (isams.h). 
513  * The newest ISAM system, isamb, is implemented as a B-Tree (see isamb.h).
514  *
515  * \section data1 Data-1
516  *
517  * The data1 (data1.h) module deals with structured documents. The module can
518  * can read, modify and write documents. The document structure was
519  * originally based on GRS-1 - a Z39.50 v3 structure that predates
520  * DOM. These days the data1 structure may describe XML/SGML as well.
521  * The data1, like DOM, is a tree structure. Each node in the tree
522  * can be of type element, text (cdata), preprocessing instruction,
523  * comment. Element nodes can point to attribute nodes.
524  *
525  * \section recctrl Record Control
526  *
527  * The record control module (recctrl.h) is responsible for
528  * managing the various record types ("classes" or filters).
529  *
530  * \section rset Result-Set
531  *
532  * The Result-Set module (rset.h) defines an interface that all
533  * Zebra Search Results must implement. Each operation (AND, OR, ..)
534  * correspond to an implementation of that interface.
535  *
536  * \section dfa DFA
537  *
538  * DFA (dfa.h) Deterministic Finite Automa is a regular expression engine.
539  * The module compiles a regular expression to a DFA. The DFA can then
540  * be used in various application to perform fast match against the
541  * origianl expression. The \ref Dict uses DFA to perform lookup
542  * using regular expressions.
543  */
544
545 #endif