Many public functions returns ZEBRA_RES rather than int to avoid
[idzebra-moved-to-github.git] / include / idzebra / api.h
1 /* $Id: api.h,v 1.18 2005-04-15 10:47:48 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 an int. Unix-like, 0 means OK, 
28     non-zero means an error. The error info should be available
29     via zebra_errCode and friends. 
30 */
31
32 #ifndef IDZEBRA_API_H
33 #define IDZEBRA_API_H
34
35 #include <yaz/odr.h>
36 #include <yaz/oid.h>
37 #include <yaz/proto.h>
38 #include <idzebra/res.h>
39 #include <idzebra/version.h>
40
41 YAZ_BEGIN_CDECL
42
43 typedef struct {
44     int processed;
45     int inserted;
46     int updated;
47     int 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     SYSNO sysno;
62     int  score;
63 } ZebraRetrievalRecord;
64
65 /* Scan Term Descriptor */
66 typedef struct {
67     int 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 /** \fn ZebraService zebra_start(const char *configName)
82     \brief starts a Zebra service. 
83     \param configName name of configuration file
84     
85     This function is a simplified version of zebra_start_res.
86 */
87 YAZ_EXPORT 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 /** \fn ZEBRA_RES zebra_stop(ZebraService zs)
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 /** \fn void zebra_filter_info(ZebraService zs, void *cd,
113     void(*cb)(void *cd, const char *name))
114     \brief lists enabled Zebra filters
115     \param zs service handle
116     \param cd callback parameter (opaque)
117     \param cb callback function
118  */
119 YAZ_EXPORT
120 void zebra_filter_info(ZebraService zs, void *cd,
121                        void (*cb)(void *cd, const char *name));
122
123
124 /** \fn ZebraHandle zebra_open(ZebraService zs)
125     \brief creates a Zebra session handle within service.
126     \param zs service handle.
127     
128     There should be one handle for each thread doing something
129     with zebra, be that searching or indexing. In simple apps 
130     one handle is sufficient 
131 */
132 YAZ_EXPORT ZebraHandle zebra_open(ZebraService zs);
133
134 /** \fn int zebra_close(ZebraHandle zh)
135     \brief destroys Zebra session handle.
136     \param zh zebra session handle.
137  */
138 YAZ_EXPORT ZEBRA_RES zebra_close(ZebraHandle zh);
139
140 /** \fn int zebra_errCode(ZebraHandle zh)
141     \brief returns error code for last error
142     \param zh zebra session handle.
143 */
144 YAZ_EXPORT int zebra_errCode(ZebraHandle zh);
145
146 /** \fn const char *zebra_errString(ZebraHandle zh)
147     \brief returns error string for last error
148     \param zh zebra session handle.
149 */
150 YAZ_EXPORT const char *zebra_errString(ZebraHandle zh);
151
152 /** \fn char *zebra_errAdd(ZebraHandle zh)
153     \brief returns additional info for last error
154     \param zh zebra session handle.
155 */
156 YAZ_EXPORT char *zebra_errAdd(ZebraHandle zh);
157
158 /** \fn int zebra_result(ZebraHandle zh, int *code, char **addinfo)
159     \brief returns error code and additional info for last error
160     \param zh zebra session handle.
161     \param code pointer to returned error code
162     \param addinfo pointer to returned additional info
163 */
164 YAZ_EXPORT int zebra_result(ZebraHandle zh, int *code, char **addinfo);
165
166 /** \fn void zebra_clearError(ZebraHandle zh)
167     \brief clears last error.
168     \param zh zebra session handle.
169  */
170 YAZ_EXPORT void zebra_clearError(ZebraHandle zh);
171
172 /** \fn ZEBRA_RES zebra_search_PQF(ZebraHandle zh, const char *pqf_query,
173     const char *setname, int *hits)
174     \brief Search using PQF Query 
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 ZEBRA_RES zebra_search_PQF(ZebraHandle zh, const char *pqf_query,
181                                       const char *setname, zint *hits);
182
183 /** \fn ZEBRA_RES zebra_search_RPN(ZebraHandle zh, ODR o, Z_RPNQuery *query,
184     const char *setname, zint *hits)
185     \brief Search using RPN Query 
186     \param zh session handle
187     \param o ODR handle
188     \param query RPN query using YAZ structure
189     \param setname name of resultset
190     \param hits number of hits is returned
191  */
192 YAZ_EXPORT ZEBRA_RES zebra_search_RPN(ZebraHandle zh, ODR o, Z_RPNQuery *query,
193                                       const char *setname, zint *hits);
194
195 /** 
196     \fn ZEBRA_RES zebra_records_retrieve(ZebraHandle zh, ODR stream,
197     const char *setname, Z_RecordComposition *comp, oid_value input_format,
198     int num_recs, ZebraRetrievalRecord *recs)
199     \brief retrieve records from result set (after search)
200     \param zh session handle
201     \param stream allocate records returned using this ODR
202     \param setname name of result set to retrieve records from
203     \param comp Z39.50 record composition
204     \param input_format transfer syntax (OID)
205     \param num_recs number of records to retrieve
206     \param recs store records in this structure (size is num_recs)
207 */
208 YAZ_EXPORT
209 ZEBRA_RES zebra_records_retrieve(ZebraHandle zh, ODR stream,
210                                  const char *setname,
211                                  Z_RecordComposition *comp,
212                                  oid_value input_format,
213                                  int num_recs,
214                                  ZebraRetrievalRecord *recs);
215 /**
216    \fn int zebra_deleteResultSet(ZebraHandle zh, int function,
217    int num_setnames, char **setnames, int *statuses)
218    \brief delete one or more resultsets 
219    \param zh session handle
220    \param function Z_DeleteResultSetRequest_{list,all}
221    \param num_setnames number of result sets
222    \param setnames result set names
223    \param statuses status result
224 */
225 YAZ_EXPORT
226 int zebra_deleteResultSet(ZebraHandle zh, int function,
227                           int num_setnames, char **setnames,
228                           int *statuses);
229
230 /**
231    \fn ZEBRA_RES zebra_scan(ZebraHandle zh, ODR stream,
232    Z_AttributesPlusTerm *zapt, oid_value attributeset,
233    int *position, int *num_entries, ZebraScanEntry **list, int *is_partial)
234    \brief performs Scan (Z39.50 style)
235    \param zh session handle
236    \param stream ODR handle for result
237    \param zapt Attribute plus Term (start term)
238    \param attributeset Attributeset for Attribute plus Term
239    \param position input/output position
240    \param num_entries number of terms requested / returned 
241    \param list list of resulting terms (ODR allocated)
242    \param is_partial upon return 1=partial, 0=complete
243 */
244 YAZ_EXPORT ZEBRA_RES zebra_scan(ZebraHandle zh, ODR stream,
245                                 Z_AttributesPlusTerm *zapt,
246                                 oid_value attributeset,
247                                 int *position, int *num_entries,
248                                 ZebraScanEntry **list,
249                                 int *is_partial);
250
251 /**
252    \fn ZEBRA_RES zebra_scan_PQF(ZebraHandle zh, ODR stream,
253    const char *query, int *position, int *num_entries, ZebraScanEntry **list, int *is_partial)
254    \brief performs Scan (taking PQF string)
255    \param zh session handle
256    \param stream ODR handle for result
257    \param query PQF scan query
258    \param position input/output position
259    \param num_entries number of terms requested / returned 
260    \param list list of resulting terms (ODR allocated)
261    \param is_partial upon return 1=partial, 0=complete
262 */
263 YAZ_EXPORT ZEBRA_RES zebra_scan_PQF(ZebraHandle zh, ODR stream,
264                                     const char *query,
265                                     int *position, int *num_entries,
266                                     ZebraScanEntry **entries,
267                                     int *is_partial);
268 /**
269    \fn ZEBRA_RES zebra_auth(ZebraHandle zh, const char *user, const char *pass)
270    \brief authenticate user. Returns 0 if OK, != 0 on failure
271    \param zh session handle
272    \param user user name
273    \param pass password
274  */
275 YAZ_EXPORT ZEBRA_RES zebra_auth(ZebraHandle zh,
276                                 const char *user, const char *pass);
277
278 /**
279    \fn int zebra_string_norm(ZebraHandle zh, unsigned reg_id,
280    const char *input_str, int input_len,
281    char *output_str, int output_len)
282    \brief normalize zebra term for register (subject to change!)
283    \param zh session handle
284    \param reg_id register ID, 'w', 'p',..
285    \param input_str input string buffer
286    \param input_len input string length
287    \param output_str output string buffer
288    \param output_len output string length
289  */
290 YAZ_EXPORT int zebra_string_norm(ZebraHandle zh, unsigned reg_id,
291                                   const char *input_str, int input_len,
292                                   char *output_str, int output_len);
293
294 /**
295    \fn ZEBRA_RES zebra_create_database(ZebraHandle zh, const char *db)
296    \brief creates a database
297    \param zh session handle
298    \param db database to be created
299 */
300 YAZ_EXPORT ZEBRA_RES zebra_create_database(ZebraHandle zh, const char *db);
301
302 /**
303    \fn int zebra_drop_database(ZebraHandle zh, const char *db)
304    \brief deletes a database (drop)
305    \param zh session handle
306    \param db database to be deleted
307 */
308 YAZ_EXPORT
309 ZEBRA_RES zebra_drop_database(ZebraHandle zh, const char *db);
310
311 YAZ_EXPORT
312 ZEBRA_RES zebra_admin_shutdown(ZebraHandle zh);
313
314 YAZ_EXPORT
315 ZEBRA_RES zebra_admin_start(ZebraHandle zh);
316
317 YAZ_EXPORT
318 ZEBRA_RES zebra_shutdown(ZebraService zs);
319
320 YAZ_EXPORT
321 ZEBRA_RES zebra_admin_import_begin(ZebraHandle zh, const char *database,
322                                    const char *record_type);
323
324 YAZ_EXPORT 
325 ZEBRA_RES zebra_admin_import_segment(ZebraHandle zh,
326                                      Z_Segment *segment);
327
328 YAZ_EXPORT 
329 ZEBRA_RES zebra_admin_import_end(ZebraHandle zh);
330
331 YAZ_EXPORT 
332 ZEBRA_RES zebra_admin_exchange_record(ZebraHandle zh,
333                                       const char *rec_buf,
334                                       size_t rec_len,
335                                       const char *recid_buf, size_t recid_len,
336                                       int action);
337
338 YAZ_EXPORT 
339 ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw);
340
341 YAZ_EXPORT
342 ZEBRA_RES zebra_end_trans(ZebraHandle zh);
343
344 YAZ_EXPORT
345 ZEBRA_RES zebra_end_transaction(ZebraHandle zh,
346                                 ZebraTransactionStatus *stat);
347
348 YAZ_EXPORT
349 ZEBRA_RES zebra_commit(ZebraHandle zh);
350
351 YAZ_EXPORT
352 ZEBRA_RES zebra_clean(ZebraHandle zh);
353
354 YAZ_EXPORT
355 ZEBRA_RES zebra_init(ZebraHandle zh);
356
357 YAZ_EXPORT int zebra_compact(ZebraHandle zh);
358 YAZ_EXPORT int zebra_repository_update(ZebraHandle zh, const char *path);
359 YAZ_EXPORT int zebra_repository_delete(ZebraHandle zh, const char *path);
360 YAZ_EXPORT int zebra_repository_show(ZebraHandle zh, const char *path);
361
362 YAZ_EXPORT int zebra_add_record(ZebraHandle zh, const char *buf, int buf_size);
363                                
364 YAZ_EXPORT 
365 ZEBRA_RES zebra_insert_record(ZebraHandle zh, 
366                               const char *recordType,
367                               SYSNO *sysno, const char *match,
368                               const char *fname,
369                               const char *buf, int buf_size,
370                               int force_update);
371 YAZ_EXPORT
372 ZEBRA_RES zebra_update_record(ZebraHandle zh, 
373                               const char *recordType,
374                               SYSNO *sysno, const char *match,
375                               const char *fname,
376                               const char *buf, int buf_size,
377                               int force_update);
378 YAZ_EXPORT 
379 ZEBRA_RES zebra_delete_record(ZebraHandle zh, 
380                               const char *recordType,
381                               SYSNO *sysno, const char *match, const char *fname,
382                               const char *buf, int buf_size,
383                               int force_update);
384
385 YAZ_EXPORT 
386 int zebra_resultSetTerms(ZebraHandle zh, const char *setname, 
387                          int no, zint *count, 
388                          int *type, char *out, size_t *len);
389
390 YAZ_EXPORT 
391 ZEBRA_RES zebra_sort(ZebraHandle zh, ODR stream,
392                      int num_input_setnames,
393                      const char **input_setnames,
394                      const char *output_setname,
395                      Z_SortKeySpecList *sort_sequence,
396                      int *sort_status);
397
398 YAZ_EXPORT
399 ZEBRA_RES zebra_select_databases(ZebraHandle zh, int num_bases, 
400                                  const char **basenames);
401
402 YAZ_EXPORT
403 ZEBRA_RES zebra_select_database(ZebraHandle zh, const char *basename);
404
405 YAZ_EXPORT
406 void zebra_shadow_enable(ZebraHandle zh, int value);
407
408 YAZ_EXPORT
409 int zebra_register_statistics(ZebraHandle zh, int dumpdict);
410
411 YAZ_EXPORT
412 ZEBRA_RES zebra_record_encoding(ZebraHandle zh, const char *encoding);
413
414 YAZ_EXPORT
415 ZEBRA_RES zebra_octet_term_encoding(ZebraHandle zh, const char *encoding);
416
417 /* Resources */
418 YAZ_EXPORT
419 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value);
420 YAZ_EXPORT
421 const char *zebra_get_resource(ZebraHandle zh, 
422                                const char *name, const char *defaultvalue);
423
424
425 YAZ_EXPORT
426 void zebra_pidfname(ZebraService zs, char *path);
427
428 typedef struct {
429     char *term;
430     char *db;
431     zint sysno;
432     int score;
433 } ZebraMetaRecord;
434
435 YAZ_EXPORT
436 ZebraMetaRecord *zebra_meta_records_create(ZebraHandle zh,
437                                            const char *name,
438                                            int num, zint *positions);
439
440
441 YAZ_EXPORT
442 ZebraMetaRecord *zebra_meta_records_create_range(ZebraHandle zh,
443                                                  const char *name, 
444                                                  zint start, int num);
445
446 YAZ_EXPORT
447 void zebra_meta_records_destroy(ZebraHandle zh, ZebraMetaRecord *records,
448                                 int num);
449
450 YAZ_EXPORT 
451 struct BFiles_struct *zebra_get_bfs(ZebraHandle zh);
452 YAZ_END_CDECL                                 
453 #endif