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