New result set model and modular ranking system. Moved towards
[idzebra-moved-to-github.git] / index / zebraapi.c
1 /*
2  * Copyright (C) 1995-1998, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: zebraapi.c,v $
7  * Revision 1.1  1998-03-05 08:45:13  adam
8  * New result set model and modular ranking system. Moved towards
9  * descent server API. System information stored as "SGML" records.
10  *
11  */
12
13 #include <stdio.h>
14 #ifdef WINDOWS
15 #include <io.h>
16 #include <process.h>
17 #else
18 #include <unistd.h>
19 #endif
20
21 #include "zserver.h"
22
23 static int zebra_register_lock (ZebraHandle zh)
24 {
25     time_t lastChange;
26     int state = zebra_server_lock_get_state(zh, &lastChange);
27
28     switch (state)
29     {
30     case 'c':
31         state = 1;
32         break;
33     default:
34         state = 0;
35     }
36     zebra_server_lock (zh, state);
37 #if USE_TIMES
38     times (&zh->tms1);
39 #endif
40     if (zh->registerState == state)
41     {
42         if (zh->registerChange >= lastChange)
43             return 0;
44         logf (LOG_LOG, "Register completely updated since last access");
45     }
46     else if (zh->registerState == -1)
47         logf (LOG_LOG, "Reading register using state %d pid=%ld", state,
48               (long) getpid());
49     else
50         logf (LOG_LOG, "Register has changed state from %d to %d",
51               zh->registerState, state);
52     zh->registerChange = lastChange;
53     if (zh->records)
54     {
55         zebraExplain_close (zh->zei, 0);
56         dict_close (zh->dict);
57         sortIdx_close (zh->sortIdx);
58         if (zh->isam)
59             is_close (zh->isam);
60         if (zh->isamc)
61             isc_close (zh->isamc);
62         rec_close (&zh->records);
63     }
64     bf_cache (zh->bfs, state ? res_get (zh->res, "shadow") : NULL);
65     zh->registerState = state;
66     zh->records = rec_open (zh->bfs, 0);
67     if (!(zh->dict = dict_open (zh->bfs, FNAME_DICT, 40, 0)))
68         return -1;
69     if (!(zh->sortIdx = sortIdx_open (zh->bfs, 0)))
70         return -1;
71     zh->isam = NULL;
72     zh->isamc = NULL;
73     if (!res_get_match (zh->res, "isam", "i", NULL))
74     {
75         if (!(zh->isamc = isc_open (zh->bfs, FNAME_ISAMC,
76                                     0, key_isamc_m(zh->res))))
77             return -1;
78
79     }
80     else
81     {
82         if (!(zh->isam = is_open (zh->bfs, FNAME_ISAM, key_compare, 0,
83                                   sizeof (struct it_key), zh->res)))
84             return -1;
85     }
86     zh->zei = zebraExplain_open (zh->records, zh->dh, 0);
87
88     return 0;
89 }
90
91 static void zebra_register_unlock (ZebraHandle zh)
92 {
93     static int waitSec = -1;
94
95 #if USE_TIMES
96     times (&zh->tms2);
97     logf (LOG_LOG, "user/system: %ld/%ld",
98                         (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
99                         (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
100 #endif
101     if (waitSec == -1)
102     {
103         char *s = res_get (zh->res, "debugRequestWait");
104         if (s)
105             waitSec = atoi (s);
106         else
107             waitSec = 0;
108     }
109 #ifdef WINDOWS
110 #else
111     if (waitSec > 0)
112         sleep (waitSec);
113 #endif
114     if (zh->registerState != -1)
115         zebra_server_unlock (zh, zh->registerState);
116 }
117
118 ZebraHandle zebra_open (const char *host, const char *configName)
119 {
120     ZebraHandle zh = xmalloc (sizeof(*zh));
121
122     if (!(zh->res = res_open (configName)))
123     {
124         logf (LOG_WARN, "Failed to read resources `%s'", configName);
125         return NULL;
126     }
127     zebra_server_lock_init (zh);
128     zh->dh = data1_create ();
129     zh->bfs = bfs_create (res_get (zh->res, "register"));
130     bf_lockDir (zh->bfs, res_get (zh->res, "lockDir"));
131     data1_set_tabpath (zh->dh, res_get(zh->res, "profilePath"));
132     zh->sets = NULL;
133     zh->registerState = -1;  /* trigger open of registers! */
134     zh->registerChange = 0;
135     
136     zh->records = NULL;
137     zh->registered_sets = NULL;
138     zh->zebra_maps = zebra_maps_open (zh->res);
139     zh->rank_classes = NULL;
140     
141     zebraRankInstall (zh, rank1_class);
142     return zh;
143 }
144
145 void zebra_close (ZebraHandle zh)
146 {
147     if (zh->records)
148     {
149         resultSetDestroy (zh);
150         zebraExplain_close (zh->zei, 0);
151         dict_close (zh->dict);
152         sortIdx_close (zh->sortIdx);
153         if (zh->isam)
154             is_close (zh->isam);
155         if (zh->isamc)
156             isc_close (zh->isamc);
157         rec_close (&zh->records);
158         zebra_register_unlock (zh);
159     }
160     zebra_maps_close (zh->zebra_maps);
161     zebraRankDestroy (zh);
162     bfs_destroy (zh->bfs);
163     data1_destroy (zh->dh);
164     zebra_server_lock_destroy (zh);
165
166     res_close (zh->res);
167     xfree (zh);
168 }
169
170 void zebra_search_rpn (ZebraHandle zh, ODR stream,
171                        Z_RPNQuery *query, int num_bases, char **basenames, 
172                        const char *setname)
173 {
174     zebra_register_lock (zh);
175     zh->errCode = 0;
176     zh->errString = NULL;
177     zh->hits = 0;
178     rpn_search (zh, stream, query, num_bases, basenames, setname);
179     zebra_register_unlock (zh);
180 }
181
182 void zebra_records_retrieve (ZebraHandle zh, ODR stream,
183                              const char *setname, Z_RecordComposition *comp,
184                              oid_value input_format, int num_recs,
185                              ZebraRetrievalRecord *recs)
186 {
187     ZebraPosSet poset;
188     int i, *pos_array;
189
190     pos_array = xmalloc (sizeof(*pos_array));
191     for (i = 0; i<num_recs; i++)
192         pos_array[i] = recs[i].position;
193
194     zebra_register_lock (zh);
195
196     poset = zebraPosSetCreate (zh, setname, num_recs, pos_array);
197     if (!poset)
198     {
199         logf (LOG_DEBUG, "zebraPosSetCreate error");
200         zh->errCode = 13;
201     }
202     else
203     {
204         for (i = 0; i<num_recs; i++)
205         {
206             if (!poset[i].sysno)
207             {
208                 zh->errCode = 13;
209                 logf (LOG_DEBUG, "Out of range. pos=%d", pos_array[i]);
210             }
211             else
212             {
213                 zh->errCode =
214                     zebra_record_fetch (zh, poset[i].sysno, poset[i].score,
215                                         stream, input_format, comp,
216                                         &recs[i].format, &recs[i].buf,
217                                         &recs[i].len,
218                                         &recs[i].base);
219             }
220         }
221         zebraPosSetDestroy (zh, poset, num_recs);
222     }
223     zebra_register_unlock (zh);
224     xfree (pos_array);
225 }
226
227 void zebra_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
228                  oid_value attributeset,
229                  int num_bases, char **basenames,
230                  int *position, int *num_entries, ZebraScanEntry **entries,
231                  int *is_partial)
232 {
233     zebra_register_lock (zh);
234     rpn_scan (zh, stream, zapt, attributeset,
235               num_bases, basenames, position,
236               num_entries, entries, is_partial);
237     zebra_register_unlock (zh);
238 }
239
240 void zebra_sort (ZebraHandle zh, ODR stream,
241                  int num_input_setnames, char **input_setnames,
242                  char *output_setname, Z_SortKeySpecList *sort_sequence,
243                  int *sort_status)
244 {
245     zebra_register_lock (zh);
246     resultSetSort (zh, stream, num_input_setnames, input_setnames,
247                    output_setname, sort_sequence, sort_status);
248     zebra_register_unlock (zh);
249 }
250
251 void zebra_setDB (ZebraHandle zh, int num_bases, char **basenames)
252 {
253
254 }
255
256 void zebra_setRecordType (ZebraHandle zh, const char *type)
257 {
258
259 }
260
261 void zebra_setGroup (ZebraHandle zh, const char *group)
262 {
263
264 }
265
266 void zebra_admin (ZebraHandle zh, const char *command)
267 {
268
269 }