Changed record update API . It is now handled by function
[idzebra-moved-to-github.git] / include / idzebra / api.h
1 /* $Id: api.h,v 1.47 2007-03-14 11:48:31 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/oid.h>
36 #include <yaz/proto.h>
37 #include <idzebra/res.h>
38 #include <idzebra/version.h>
39 #include <idzebra/recctrl.h>
40
41 YAZ_BEGIN_CDECL
42
43 typedef struct {
44     zint processed;
45     zint inserted;
46     zint updated;
47     zint deleted;
48     long utime;
49     long stime;
50 } ZebraTransactionStatus;
51
52 /** Retrieval Record Descriptor */
53 typedef struct {
54     int errCode;         /* non-zero if error when fetching this */
55     char *errString;     /* error string */
56     int position;        /* position of record in result set (1,2,..) */
57     char *buf;           /* record buffer (void pointer really) */
58     int len;             /* length */
59     oid_value format;    /* record syntax */
60     char *base; 
61     zint sysno;
62     int  score;
63 } ZebraRetrievalRecord;
64
65 /** Scan Term Descriptor */
66 typedef struct {
67     zint occurrences;    /* scan term occurrences */
68     char *term;          /* scan term string */
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 /** \brief Set limit before Zebra does approx hit count
166     \param zh session handle
167     \param approx_limit the limit
168     
169     Results will be approximiate if hit count is greater than the
170     limit specified. By default there is a high-limit (no limit).
171 */
172 ZEBRA_RES zebra_set_approx_limit(ZebraHandle zh, zint approx_limit);
173
174 /** \brief Search using PQF Query String
175     \param zh session handle
176     \param pqf_query query
177     \param setname name of resultset
178     \param hits of hits is returned
179 */
180 YAZ_EXPORT
181 ZEBRA_RES zebra_search_PQF(ZebraHandle zh, const char *pqf_query,
182                            const char *setname, zint *hits);
183
184 /** \brief Search using RPN Query structure (from ASN.1)
185     \param zh session handle
186     \param o ODR handle
187     \param query RPN query using YAZ structure
188     \param setname name of resultset
189     \param hits number of hits is returned
190     \param estimated_hit_count whether hit count is an estimate
191     \param partial_resultset whether result is only partially evaluated
192 */
193 YAZ_EXPORT
194 ZEBRA_RES zebra_search_RPN_x(ZebraHandle zh, ODR o, Z_RPNQuery *query,
195                            const char *setname, zint *hits,
196                            int *estimated_hit_count,
197                            int *partial_resultset);
198
199
200 /** \brief Search using RPN Query structure (from ASN.1)
201     \param zh session handle
202     \param o ODR handle
203     \param query RPN query using YAZ structure
204     \param setname name of resultset
205     \param hits number of hits is returned
206 */
207 YAZ_EXPORT
208 ZEBRA_RES zebra_search_RPN(ZebraHandle zh, ODR o, Z_RPNQuery *query,
209                               const char *setname, zint *hits);
210
211 /** \brief Retrieve records from result set (after search)
212     \param zh session handle
213     \param stream allocate records returned using this ODR
214     \param setname name of result set to retrieve records from
215     \param comp Z39.50 record composition
216     \param input_format transfer syntax (OID)
217     \param num_recs number of records to retrieve
218     \param recs store records in this structure (size is num_recs)
219 */
220 YAZ_EXPORT
221 ZEBRA_RES zebra_records_retrieve(ZebraHandle zh, ODR stream,
222                                  const char *setname,
223                                  Z_RecordComposition *comp,
224                                  oid_value input_format,
225                                  int num_recs,
226                                  ZebraRetrievalRecord *recs);
227 /** \brief Deletes one or more resultsets 
228     \param zh session handle
229     \param function Z_DeleteResultSetRequest_{list,all}
230     \param num_setnames number of result sets
231     \param setnames result set names
232     \param statuses status result
233 */
234 YAZ_EXPORT
235 int zebra_deleteResultSet(ZebraHandle zh, int function,
236                           int num_setnames, char **setnames,
237                           int *statuses);
238
239
240 /** \brief returns number of term info terms assocaited with result set
241     \param zh session handle
242     \param setname result set name
243     \param num_terms number of terms returned in this integer
244     
245     This function is used in conjunction with zebra_result_set_term_info.
246     If operation was successful, ZEBRA_OK is returned; otherwise
247     ZEBRA_FAIL is returned (typically non-existing setname)
248 */
249 YAZ_EXPORT
250 ZEBRA_RES zebra_result_set_term_no(ZebraHandle zh, const char *setname,
251                                    int *num_terms);
252
253 /** \brief returns information about a term assocated with a result set
254     \param zh session handle
255     \param setname result set name
256     \param no the term we want to know about (0=first, 1=second,..)
257     \param count the number of occurrences of this term, aka hits (output) 
258     \param approx about hits: 0=exact,1=approx (output)
259     \param termbuf buffer for term string (intput, output)
260     \param termlen size of termbuf (input=max, output=actual length)
261     \param term_ref_id if non-NULL *term_ref_id holds term reference
262     
263     Returns information about one search term associated with result set.
264     Use zebra_result_set_term_no to read total number of terms associated
265     with result set. If this function can not return information,
266     due to no out of range or bad result set name, ZEBRA_FAIL is
267     returned.
268     The passed termbuf must be able to hold at least *termlen characters.
269     Upon completion, *termlen holds actual length of search term.
270 */
271 YAZ_EXPORT
272 ZEBRA_RES zebra_result_set_term_info(ZebraHandle zh, const char *setname,
273                                      int no, zint *count, int *approx,
274                                      char *termbuf, size_t *termlen,
275                                      const char **term_ref_id);
276
277
278 /** \brief performs Scan (Z39.50 style)
279     \param zh session handle
280     \param stream ODR handle for result
281     \param zapt Attribute plus Term (start term)
282     \param attributeset Attributeset for Attribute plus Term
283     \param position input/output position
284     \param num_entries number of terms requested / returned 
285     \param entries list of resulting terms (ODR allocated)
286     \param is_partial upon return 1=partial, 0=complete
287     \param setname limit scan by this set (NULL means no limit)
288 */
289 YAZ_EXPORT ZEBRA_RES zebra_scan(ZebraHandle zh, ODR stream,
290                                 Z_AttributesPlusTerm *zapt,
291                                 oid_value attributeset,
292                                 int *position, int *num_entries,
293                                 ZebraScanEntry **entries,
294                                 int *is_partial,
295                                 const char *setname);
296
297 /** \brief performs Scan (taking PQF string)
298     \param zh session handle
299     \param stream ODR handle for result
300     \param query PQF scan query
301     \param position input/output position
302     \param num_entries number of terms requested / returned 
303     \param entries list of resulting terms (ODR allocated)
304     \param is_partial upon return 1=partial, 0=complete
305     \param setname limit scan by this set (NULL means no limit)
306 */
307 YAZ_EXPORT
308 ZEBRA_RES zebra_scan_PQF(ZebraHandle zh, ODR stream, const char *query,
309                          int *position, int *num_entries,
310                          ZebraScanEntry **entries,
311                          int *is_partial, const char *setname);
312
313 /** \brief authenticate user. Returns 0 if OK, != 0 on failure
314     \param zh session handle
315     \param user user name
316     \param pass password
317 */
318 YAZ_EXPORT
319 ZEBRA_RES zebra_auth(ZebraHandle zh, const char *user, const char *pass);
320
321 /** \brief Normalize zebra term for register (subject to change!)
322     \param zh session handle
323     \param reg_id register ID, 'w', 'p',..
324     \param input_str input string buffer
325     \param input_len input string length
326     \param output_str output string buffer
327     \param output_len output string length
328 */
329 YAZ_EXPORT
330 int zebra_string_norm(ZebraHandle zh, unsigned reg_id, const char *input_str, 
331                       int input_len, char *output_str, int output_len);
332
333 /** \brief Creates a database
334     \param zh session handle
335     \param db database to be created
336 */
337 YAZ_EXPORT
338 ZEBRA_RES zebra_create_database(ZebraHandle zh, const char *db);
339
340 /** \brief Deletes a database (drop)
341     \param zh session handle
342     \param db database to be deleted
343 */
344 YAZ_EXPORT
345 ZEBRA_RES zebra_drop_database(ZebraHandle zh, const char *db);
346
347 YAZ_EXPORT
348 ZEBRA_RES zebra_admin_shutdown(ZebraHandle zh);
349
350 YAZ_EXPORT
351 ZEBRA_RES zebra_admin_start(ZebraHandle zh);
352
353 YAZ_EXPORT
354 ZEBRA_RES zebra_shutdown(ZebraService zs);
355
356 YAZ_EXPORT
357 ZEBRA_RES zebra_admin_import_begin(ZebraHandle zh, const char *database,
358                                    const char *record_type);
359
360 YAZ_EXPORT 
361 ZEBRA_RES zebra_admin_import_segment(ZebraHandle zh,
362                                      Z_Segment *segment);
363
364 YAZ_EXPORT 
365 ZEBRA_RES zebra_admin_import_end(ZebraHandle zh);
366
367 YAZ_EXPORT 
368 ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw
369     ) ZEBRA_GCC_ATTR((warn_unused_result));
370
371 YAZ_EXPORT
372 ZEBRA_RES zebra_end_trans(ZebraHandle zh
373     ) ZEBRA_GCC_ATTR((warn_unused_result));
374
375 YAZ_EXPORT
376 ZEBRA_RES zebra_end_transaction(ZebraHandle zh,
377                                 ZebraTransactionStatus *stat);
378
379 YAZ_EXPORT
380 ZEBRA_RES zebra_commit(ZebraHandle zh);
381
382 YAZ_EXPORT
383 ZEBRA_RES zebra_clean(ZebraHandle zh);
384
385 YAZ_EXPORT
386 ZEBRA_RES zebra_init(ZebraHandle zh);
387
388 YAZ_EXPORT
389 ZEBRA_RES zebra_compact(ZebraHandle zh);
390
391 YAZ_EXPORT 
392 ZEBRA_RES zebra_repository_update(ZebraHandle zh, const char *path);
393
394 YAZ_EXPORT 
395 ZEBRA_RES zebra_repository_delete(ZebraHandle zh, const char *path);
396
397 YAZ_EXPORT 
398 ZEBRA_RES zebra_repository_show(ZebraHandle zh, const char *path);
399
400 /** \brief Simple update record
401     \param zh session handle
402     \param buf record buffer
403     \param buf_size record buffer size
404
405     This function is a simple wrapper or zebra_update_record with
406     action=action_update (insert or replace) .
407 */
408 YAZ_EXPORT
409 YAZ_EXPORT 
410 ZEBRA_RES zebra_add_record(ZebraHandle zh, const char *buf, int buf_size);
411                                
412 /** \brief Updates record
413     \param zh session handle
414     \param action (insert,replace,delete or update (replace/insert)
415     \param recordType filter type (0 indicates default)
416     \param sysno system id (0 may be passed for no known id)
417     \param match match criteria (0 may be passed for no known criteria)
418     \param fname filename to be printed for logging (0 may be passed)
419     \param buf record buffer
420     \param buf_size record buffer size
421 */
422 YAZ_EXPORT
423 ZEBRA_RES zebra_update_record(ZebraHandle zh, 
424                               enum zebra_recctrl_action_t action,
425                               const char *recordType,
426                               zint *sysno, const char *match,
427                               const char *fname,
428                               const char *buf, int buf_size);
429
430 YAZ_EXPORT 
431 ZEBRA_RES zebra_sort(ZebraHandle zh, ODR stream,
432                      int num_input_setnames,
433                      const char **input_setnames,
434                      const char *output_setname,
435                      Z_SortKeySpecList *sort_sequence,
436                      int *sort_status
437     ) ZEBRA_GCC_ATTR((warn_unused_result));    
438
439 YAZ_EXPORT
440 ZEBRA_RES zebra_select_databases(ZebraHandle zh, int num_bases, 
441                                  const char **basenames
442     ) ZEBRA_GCC_ATTR((warn_unused_result));
443
444 YAZ_EXPORT
445 ZEBRA_RES zebra_select_database(ZebraHandle zh, const char *basename
446     ) ZEBRA_GCC_ATTR((warn_unused_result));
447
448 YAZ_EXPORT
449 void zebra_shadow_enable(ZebraHandle zh, int value);
450
451 YAZ_EXPORT
452 int zebra_register_statistics(ZebraHandle zh, int dumpdict);
453
454 YAZ_EXPORT
455 ZEBRA_RES zebra_record_encoding(ZebraHandle zh, const char *encoding);
456
457 YAZ_EXPORT
458 ZEBRA_RES zebra_octet_term_encoding(ZebraHandle zh, const char *encoding);
459
460 /* Resources */
461 YAZ_EXPORT
462 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value);
463 YAZ_EXPORT
464 const char *zebra_get_resource(ZebraHandle zh, 
465                                const char *name, const char *defaultvalue);
466
467
468 YAZ_EXPORT
469 void zebra_pidfname(ZebraService zs, char *path);
470
471 typedef struct {
472     char *term;
473     char *db;
474     zint sysno;
475     int score;
476 } ZebraMetaRecord;
477
478 YAZ_EXPORT
479 ZebraMetaRecord *zebra_meta_records_create(ZebraHandle zh,
480                                            const char *name,
481                                            int num, zint *positions);
482
483
484 YAZ_EXPORT
485 ZebraMetaRecord *zebra_meta_records_create_range(ZebraHandle zh,
486                                                  const char *name, 
487                                                  zint start, int num);
488
489 YAZ_EXPORT
490 void zebra_meta_records_destroy(ZebraHandle zh, ZebraMetaRecord *records,
491                                 int num);
492
493 YAZ_EXPORT 
494 struct BFiles_struct *zebra_get_bfs(ZebraHandle zh);
495
496 YAZ_EXPORT
497 ZEBRA_RES zebra_set_limit(ZebraHandle zh, int complement_flag, zint *ids);
498
499 YAZ_EXPORT
500 ZEBRA_RES zebra_set_break_handler(ZebraHandle zh, 
501                                   int (*f)(void *client_data),
502                                   void *client_data);
503
504 YAZ_END_CDECL                                 
505
506 /** \mainpage Zebra
507  *
508  * \section intro_sec Introduction
509  *
510  * Zebra is a search engine for structure data, such as XML, MARC
511  * and others.
512  *
513  * API users should read the api.h for all the public definitions.
514  *
515  * The remaining sections briefly describe each of
516  * Zebra major modules/components.
517  *
518  * \section util Base Utilities
519  *
520  * The Zebra utilities (util.h) defines fundamental types and a few
521  * utilites for Zebra.
522  *
523  * \section res Resources
524  *
525  * The resources system (res.h) is a manager of configuration 
526  * resources. The resources can be viewed as a simple database.
527  * Resources can be read from a configurtion file, they can be
528  * read or written by an application. Resources can also be written,
529  * but that facility is not currently in use.
530  *
531  * \section bfile Bfiles
532  *
533  * The Bfiles (bfile.h) provides a portable interface to the
534  * local file system. It also provides a facility for safe updates
535  * (shadow updates). All file system access is handle by this module
536  * (except for trival reads of configuration files).
537  *
538  * \section dict Dictionary
539  *
540  * The Zebra dictionary (dict.h) maps a search term (key) to a value. The
541  * value is a reference to the list of records identifers in which
542  * the term occurs. Zebra uses an ISAM data structure for the list
543  * of term occurrences. The Dictionary uses \ref bfile.
544  *
545  * \section isam ISAM
546  *
547  * Zebra maintains an ISAM for each term where each ISAM is a list
548  * of record identifiers corresponding to the records in which the
549  * term occur. Unlike traditional ISAM systems, the Zebra ISAM
550  * is compressed. The ISAM system uses \ref bfile.
551  *
552  * Zebra has more than one ISAM system. The old and stable ISAM system
553  * is named isamc (see isamc.h). Another version isams is a write-once
554  * isam system that is quite compact - suitable for CD-ROMs (isams.h). 
555  * The newest ISAM system, isamb, is implemented as a B-Tree (see isamb.h).
556  *
557  * \section data1 Data-1
558  *
559  * The data1 (data1.h) module deals with structured documents. The module can
560  * can read, modify and write documents. The document structure was
561  * originally based on GRS-1 - a Z39.50 v3 structure that predates
562  * DOM. These days the data1 structure may describe XML/SGML as well.
563  * The data1, like DOM, is a tree structure. Each node in the tree
564  * can be of type element, text (cdata), preprocessing instruction,
565  * comment. Element nodes can point to attribute nodes.
566  *
567  * \section recctrl Record Control
568  *
569  * The record control module (recctrl.h) is responsible for
570  * managing the various record types ("classes" or filters).
571  *
572  * \section rset Result-Set
573  *
574  * The Result-Set module (rset.h) defines an interface that all
575  * Zebra Search Results must implement. Each operation (AND, OR, ..)
576  * correspond to an implementation of that interface.
577  *
578  * \section dfa DFA
579  *
580  * DFA (dfa.h) Deterministic Finite Automa is a regular expression engine.
581  * The module compiles a regular expression to a DFA. The DFA can then
582  * be used in various application to perform fast match against the
583  * origianl expression. The \ref Dict uses DFA to perform lookup
584  * using regular expressions.
585  */
586
587 #endif
588 /*
589  * Local variables:
590  * c-basic-offset: 4
591  * indent-tabs-mode: nil
592  * End:
593  * vim: shiftwidth=4 tabstop=8 expandtab
594  */
595