Script configure uses yaz pthreads options. Added locking for
[idzebra-moved-to-github.git] / index / zserver.h
1 /*
2  * Copyright (C) 1994-2000, Index Data 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: zserver.h,v $
7  * Revision 1.50  2000-11-29 14:24:01  adam
8  * Script configure uses yaz pthreads options. Added locking for
9  * zebra_register_{lock,unlock}.
10  *
11  * Revision 1.49  2000/05/18 12:01:36  adam
12  * System call times(2) used again. More 64-bit fixes.
13  *
14  * Revision 1.48  2000/04/05 09:49:35  adam
15  * On Unix, zebra/z'mbol uses automake.
16  *
17  * Revision 1.47  2000/03/20 19:08:36  adam
18  * Added remote record import using Z39.50 extended services and Segment
19  * Requests.
20  *
21  * Revision 1.46  2000/03/15 15:00:31  adam
22  * First work on threaded version.
23  *
24  * Revision 1.45  1999/11/30 13:48:04  adam
25  * Improved installation. Updated for inclusion of YAZ header files.
26  *
27  * Revision 1.44  1999/11/04 15:00:45  adam
28  * Implemented delete result set(s).
29  *
30  * Revision 1.43  1999/07/14 10:59:27  adam
31  * Changed functions isc_getmethod, isams_getmethod.
32  * Improved fatal error handling (such as missing EXPLAIN schema).
33  *
34  * Revision 1.42  1999/05/26 07:49:13  adam
35  * C++ compilation.
36  *
37  * Revision 1.41  1999/05/12 13:08:06  adam
38  * First version of ISAMS.
39  *
40  * Revision 1.40  1998/11/16 16:03:45  adam
41  * Moved loggin utilities to Yaz. Was implemented in file zlogs.c.
42  *
43  * Revision 1.39  1998/10/16 08:14:35  adam
44  * Updated record control system.
45  *
46  * Revision 1.38  1998/09/22 10:03:44  adam
47  * Changed result sets to be persistent in the sense that they can
48  * be re-searched if needed.
49  * Fixed memory leak in rsm_or.
50  *
51  * Revision 1.37  1998/09/02 13:53:22  adam
52  * Extra parameter decode added to search routines to implement
53  * persistent queries.
54  *
55  * Revision 1.36  1998/06/24 12:16:16  adam
56  * Support for relations on text operands. Open range support in
57  * DFA module (i.e. [-j], [g-]).
58  *
59  * Revision 1.35  1998/06/23 15:33:35  adam
60  * Added feature to specify sort criteria in query (type 7 specifies
61  * sort flags).
62  *
63  * Revision 1.34  1998/06/22 11:36:50  adam
64  * Added authentication check facility to zebra.
65  *
66  * Revision 1.33  1998/06/12 12:22:14  adam
67  * Work on Zebra API.
68  *
69  * Revision 1.32  1998/05/27 16:57:47  adam
70  * Zebra returns surrogate diagnostic for single records when
71  * appropriate.
72  *
73  * Revision 1.31  1998/05/20 10:12:23  adam
74  * Implemented automatic EXPLAIN database maintenance.
75  * Modified Zebra to work with ASN.1 compiled version of YAZ.
76  *
77  * Revision 1.30  1998/03/05 08:45:13  adam
78  * New result set model and modular ranking system. Moved towards
79  * descent server API. System information stored as "SGML" records.
80  *
81  * Revision 1.29  1998/02/10 12:03:06  adam
82  * Implemented Sort.
83  *
84  * Revision 1.28  1998/01/29 13:40:11  adam
85  * Better logging for scan service.
86  *
87  * Revision 1.27  1997/10/27 14:33:06  adam
88  * Moved towards generic character mapping depending on "structure"
89  * field in abstract syntax file. Fixed a few memory leaks. Fixed
90  * bug with negative integers when doing searches with relational
91  * operators.
92  *
93  * Revision 1.26  1997/09/29 12:41:35  adam
94  * Fixed bug regarding USE_TIMES var.
95  *
96  * Revision 1.25  1997/09/29 09:08:36  adam
97  * Revised locking system to be thread safe for the server.
98  *
99  * Revision 1.24  1997/09/17 12:19:19  adam
100  * Zebra version corresponds to YAZ version 1.4.
101  * Changed Zebra server so that it doesn't depend on global common_resource.
102  *
103  * Revision 1.23  1996/12/23 15:30:46  adam
104  * Work on truncation.
105  * Bug fix: result sets weren't deleted after server shut down.
106  *
107  * Revision 1.22  1996/11/04 14:07:49  adam
108  * Moved truncation code to trunc.c.
109  *
110  * Revision 1.21  1996/10/29 14:09:58  adam
111  * Use of cisam system - enabled if setting isamc is 1.
112  *
113  * Revision 1.20  1996/06/04 10:19:02  adam
114  * Minor changes - removed include of ctype.h.
115  *
116  * Revision 1.19  1996/05/14  11:34:01  adam
117  * Scan support in multiple registers/databases.
118  *
119  * Revision 1.18  1996/05/14  06:16:50  adam
120  * Compact use/set bytes used in search service.
121  *
122  * Revision 1.17  1995/12/08 16:22:57  adam
123  * Work on update while servers are running. Three lock files introduced.
124  * The servers reload their registers when necessary, but they don't
125  * reestablish result sets yet.
126  *
127  * Revision 1.16  1995/12/07  17:38:48  adam
128  * Work locking mechanisms for concurrent updates/commit.
129  *
130  * Revision 1.15  1995/11/21  15:29:13  adam
131  * Config file 'base' read by default by both indexer and server.
132  *
133  * Revision 1.14  1995/11/16  17:00:57  adam
134  * Better logging of rpn query.
135  *
136  * Revision 1.13  1995/11/16  15:34:56  adam
137  * Uses new record management system in both indexer and server.
138  *
139  * Revision 1.12  1995/10/27  14:00:12  adam
140  * Implemented detection of database availability.
141  *
142  * Revision 1.11  1995/10/17  18:02:12  adam
143  * New feature: databases. Implemented as prefix to words in dictionary.
144  *
145  * Revision 1.10  1995/10/09  16:18:38  adam
146  * Function dict_lookup_grep got extra client data parameter.
147  *
148  * Revision 1.9  1995/10/06  14:38:01  adam
149  * New result set method: r_score.
150  * Local no (sysno) and score is transferred to retrieveCtrl.
151  *
152  * Revision 1.8  1995/10/06  13:52:06  adam
153  * Bug fixes. Handler may abort further scanning.
154  *
155  * Revision 1.7  1995/10/06  10:43:57  adam
156  * Scan added. 'occurrences' in scan entries not set yet.
157  *
158  * Revision 1.6  1995/09/28  09:19:48  adam
159  * xfree/xmalloc used everywhere.
160  * Extract/retrieve method seems to work for text records.
161  *
162  * Revision 1.5  1995/09/27  16:17:32  adam
163  * More work on retrieve.
164  *
165  * Revision 1.4  1995/09/14  11:53:28  adam
166  * First work on regular expressions/truncations.
167  *
168  * Revision 1.3  1995/09/08  08:53:23  adam
169  * Record buffer maintained in server_info.
170  *
171  * Revision 1.2  1995/09/06  16:11:19  adam
172  * Option: only one word key per file.
173  *
174  * Revision 1.1  1995/09/05  15:28:40  adam
175  * More work on search engine.
176  *
177  */
178
179 #if HAVE_SYS_TIMES_H
180 #include <sys/times.h>
181 #endif
182
183 #if HAVE_PTHREADS_H
184 #include <pthreads.h>
185 #endif
186
187 #include <yaz/backend.h>
188 #include <rset.h>
189
190 #include <sortidx.h>
191 #include <passwddb.h>
192 #include "index.h"
193 #include "zebraapi.h"
194 #include "zinfo.h"
195
196 YAZ_BEGIN_CDECL
197
198 typedef struct {
199     int sysno;
200     int score;
201 } *ZebraPosSet;
202
203 typedef struct zebra_set *ZebraSet;
204
205 typedef struct zebra_rank_class {
206     struct rank_control *control;
207     int init_flag;
208     void *class_handle;
209     struct zebra_rank_class *next;
210 } *ZebraRankClass;
211
212 struct zebra_service {
213     char *configName;
214     struct zebra_session *sessions;
215     ISAMS isams;
216 #if ZMBOL
217     ISAM isam;
218     ISAMC isamc;
219     ISAMD isamd;
220 #endif
221     Dict dict;
222     SortIdx sortIdx;
223     int registerState; /* 0 (no commit pages), 1 (use commit pages) */
224     time_t registerChange;
225     BFiles bfs;
226     Records records;
227     ZebraExplainInfo zei;
228     Res res;
229     ZebraLockHandle server_lock_cmt;
230     ZebraLockHandle server_lock_org;
231     char *server_path_prefix;
232     data1_handle dh;
233     ZebraMaps zebra_maps;
234     ZebraRankClass rank_classes;
235     RecTypes recTypes;
236     Passwd_db passwd_db;
237     Zebra_mutex_cond session_lock;
238     int stop_flag;
239     int active; /* 0=shutdown, 1=enabled and inactive, 2=activated */
240 };
241
242 struct recKeys {
243     int buf_used;
244     int buf_max;
245     char *buf;
246     char prevAttrSet;
247     short prevAttrUse;
248     int prevSeqNo;
249 };
250
251 struct sortKey {
252     char *string;
253     int length;
254     int attrSet;
255     int attrUse;
256     struct sortKey *next;
257 };
258
259 struct zebra_session {
260     struct zebra_session *next;
261     struct zebra_service *service;
262
263     struct recKeys keys;
264     struct sortKey *sortKeys;
265
266     char **key_buf;
267     size_t ptr_top;
268     size_t ptr_i;
269     size_t key_buf_used;
270     int key_file_no;
271     char *admin_databaseName;
272
273     int destroyed;
274     ZebraSet sets;
275     int errCode;
276     int hits;
277     char *errString;
278 #if HAVE_SYS_TIMES_H
279     struct tms tms1;
280     struct tms tms2;    
281 #endif
282 };
283
284 struct rank_control {
285     char *name;
286     void *(*create)(ZebraService zh);
287     void (*destroy)(ZebraService zh, void *class_handle);
288     void *(*begin)(ZebraHandle zh, void *class_handle, RSET rset);
289     void (*end)(ZebraHandle zh, void *set_handle);
290     int (*calc)(void *set_handle, int sysno);
291     void (*add)(void *set_handle, int seqno, int term_index);
292 };
293
294 RSET rpn_search (ZebraHandle zh, NMEM mem,
295                  Z_RPNQuery *rpn, int num_bases, char **basenames, 
296                  const char *setname, ZebraSet sset);
297
298
299 void rpn_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
300                oid_value attributeset,
301                int num_bases, char **basenames,
302                int *position, int *num_entries, ZebraScanEntry **list,
303                int *is_partial);
304
305 RSET rset_trunc (ZebraHandle zh, ISAMS_P *isam_p, int no,
306                  const char *term, int length_term, const char *flags);
307
308 ZebraSet resultSetAdd (ZebraHandle zh, const char *name, int ov);
309 ZebraSet resultSetGet (ZebraHandle zh, const char *name);
310 ZebraSet resultSetAddRPN (ZebraHandle zh, ODR stream, ODR decode,                                         Z_RPNQuery *rpn, int num_bases, char **basenames,
311                           const char *setname);
312 RSET resultSetRef (ZebraHandle zh, Z_ResultSetId *resultSetId);
313 void resultSetDestroy (ZebraHandle zh, int num_names, char **names,
314                        int *statuses);
315
316 ZebraPosSet zebraPosSetCreate (ZebraHandle zh, const char *name,
317                                int num, int *positions);
318 void zebraPosSetDestroy (ZebraHandle zh, ZebraPosSet records, int num);
319
320 void resultSetSort (ZebraHandle zh, NMEM nmem,
321                     int num_input_setnames, const char **input_setnames,
322                     const char *output_setname,
323                     Z_SortKeySpecList *sort_sequence, int *sort_status);
324 void resultSetSortSingle (ZebraHandle zh, NMEM nmem,
325                           ZebraSet sset, RSET rset,
326                           Z_SortKeySpecList *sort_sequence, int *sort_status);
327 void resultSetRank (ZebraHandle zh, ZebraSet zebraSet, RSET rset);
328
329 void zebra_sort (ZebraHandle zh, ODR stream,
330                  int num_input_setnames, const char **input_setnames,
331                  const char *output_setname, Z_SortKeySpecList *sort_sequence,
332                  int *sort_status);
333
334 int zebra_server_lock_init (ZebraService zh);
335 int zebra_server_lock_destroy (ZebraService zh);
336 int zebra_server_lock (ZebraService zh, int lockCommit);
337 void zebra_server_unlock (ZebraService zh, int commitPhase);
338 int zebra_server_lock_get_state (ZebraService zh, time_t *timep);
339
340 typedef struct attent
341 {
342     int attset_ordinal;
343     data1_local_attribute *local_attributes;
344 } attent;
345
346 void zebraRankInstall (ZebraService zh, struct rank_control *ctrl);
347 ZebraRankClass zebraRankLookup (ZebraHandle zh, const char *name);
348 void zebraRankDestroy (ZebraService zh);
349
350 int att_getentbyatt(ZebraHandle zh, attent *res, oid_value set, int att);
351
352 extern struct rank_control *rank1_class;
353
354 int zebra_record_fetch (ZebraHandle zh, int sysno, int score, ODR stream,
355                         oid_value input_format, Z_RecordComposition *comp,
356                         oid_value *output_format, char **rec_bufp,
357                         int *rec_lenp, char **basenamep);
358
359 void extract_get_fname_tmp (ZebraHandle zh, char *fname, int no);
360 void zebra_index_merge (ZebraHandle zh);
361
362
363 struct zebra_fetch_control {
364     off_t offset_end;
365     off_t record_offset;
366     off_t record_int_pos;
367     const char *record_int_buf;
368     int record_int_len;
369     int fd;
370 };
371
372 int zebra_record_ext_read (void *fh, char *buf, size_t count);
373 off_t zebra_record_ext_seek (void *fh, off_t offset);
374 off_t zebra_record_ext_tell (void *fh);
375 off_t zebra_record_int_seek (void *fh, off_t offset);
376 off_t zebra_record_int_tell (void *fh);
377 int zebra_record_int_read (void *fh, char *buf, size_t count);
378 void zebra_record_int_end (void *fh, off_t offset);
379
380 YAZ_END_CDECL