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