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