First work on regular expressions/truncations.
[idzebra-moved-to-github.git] / index / zrpn.c
1 /*
2  * Copyright (C) 1994-1995, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: zrpn.c,v $
7  * Revision 1.11  1995-09-14 11:53:27  adam
8  * First work on regular expressions/truncations.
9  *
10  * Revision 1.10  1995/09/11  15:23:26  adam
11  * More work on relevance search.
12  *
13  * Revision 1.9  1995/09/11  13:09:35  adam
14  * More work on relevance feedback.
15  *
16  * Revision 1.8  1995/09/08  14:52:27  adam
17  * Minor changes. Dictionary is lower case now.
18  *
19  * Revision 1.7  1995/09/07  13:58:36  adam
20  * New parameter: result-set file descriptor (RSFD) to support multiple
21  * positions within the same result-set.
22  * Boolean operators: and, or, not implemented.
23  * Result-set references.
24  *
25  * Revision 1.6  1995/09/06  16:11:18  adam
26  * Option: only one word key per file.
27  *
28  * Revision 1.5  1995/09/06  10:33:04  adam
29  * More work on present. Some log messages removed.
30  *
31  * Revision 1.4  1995/09/05  15:28:40  adam
32  * More work on search engine.
33  *
34  * Revision 1.3  1995/09/04  15:20:22  adam
35  * Minor changes.
36  *
37  * Revision 1.2  1995/09/04  12:33:43  adam
38  * Various cleanup. YAZ util used instead.
39  *
40  * Revision 1.1  1995/09/04  09:10:40  adam
41  * More work on index add/del/update.
42  * Merge sort implemented.
43  * Initial work on z39 server.
44  *
45  */
46 #include <stdio.h>
47 #include <assert.h>
48 #include <unistd.h>
49
50 #include "zserver.h"
51
52 #include <rsisam.h>
53 #include <rstemp.h>
54 #include <rsnull.h>
55 #include <rsbool.h>
56 #include <rsrel.h>
57
58 /*
59  * attr_print: log attributes
60  */
61 static void attr_print (Z_AttributesPlusTerm *t)
62 {
63     int of, i;
64     for (of = 0; of < t->num_attributes; of++)
65     {
66         Z_AttributeElement *element;
67         element = t->attributeList[of];
68
69         switch (element->which) 
70         {
71         case Z_AttributeValue_numeric:
72             logf (LOG_DEBUG, "attributeType=%d value=%d", 
73                   *element->attributeType,
74                   *element->value.numeric);
75             break;
76         case Z_AttributeValue_complex:
77             logf (LOG_DEBUG, "attributeType=%d complex", 
78                   *element->attributeType);
79             for (i = 0; i<element->value.complex->num_list; i++)
80             {
81                 if (element->value.complex->list[i]->which ==
82                     Z_StringOrNumeric_string)
83                     logf (LOG_DEBUG, "   string: '%s'",
84                           element->value.complex->list[i]->u.string);
85                 else if (element->value.complex->list[i]->which ==
86                          Z_StringOrNumeric_numeric)
87                     logf (LOG_DEBUG, "   numeric: '%d'",
88                           *element->value.complex->list[i]->u.numeric);
89             }
90             break;
91         default:
92             assert (0);
93         }
94     }
95 }
96
97 typedef struct {
98     int type;
99     int major;
100     int minor;
101     Z_AttributesPlusTerm *zapt;
102 } AttrType;
103
104 static int attr_find (AttrType *src)
105 {
106     while (src->major < src->zapt->num_attributes)
107     {
108         Z_AttributeElement *element;
109         element = src->zapt->attributeList[src->major];
110
111         if (src->type == *element->attributeType)
112         {
113             switch (element->which) 
114             {
115             case Z_AttributeValue_numeric:
116                 ++(src->major);
117                 return *element->value.numeric;
118                 break;
119             case Z_AttributeValue_complex:
120                 if (src->minor >= element->value.complex->num_list ||
121                     element->value.complex->list[src->minor]->which !=  
122                     Z_StringOrNumeric_numeric)
123                     break;
124                 ++(src->minor);
125                 return *element->value.complex->list[src->minor-1]->u.numeric;
126             default:
127                 assert (0);
128             }
129         }
130         ++(src->major);
131     }
132     return -1;
133 }
134
135 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
136                        int type)
137 {
138     src->zapt = zapt;
139     src->type = type;
140     src->major = 0;
141     src->minor = 0;
142 }
143
144 static ISAM_P *isam_p_buf = NULL;
145 static int isam_p_size = 0;
146 static int isam_p_indx;
147
148 static void add_isam_p (const char *info)
149 {
150     if (isam_p_indx == isam_p_size)
151     {
152         ISAM_P *new_isam_p_buf;
153         
154         isam_p_size = 2*isam_p_size + 100;
155         new_isam_p_buf = xmalloc (sizeof(*new_isam_p_buf) *
156                                   isam_p_size);
157         if (isam_p_buf)
158         {
159             memcpy (new_isam_p_buf, isam_p_buf,
160                     isam_p_indx * sizeof(*isam_p_buf));
161             xfree (isam_p_buf);
162         }
163         isam_p_buf = new_isam_p_buf;
164     }
165     assert (*info == sizeof(*isam_p_buf));
166     memcpy (isam_p_buf + isam_p_indx, info+1, sizeof(*isam_p_buf));
167     isam_p_indx++;
168 }
169
170 static int grep_handle (Dict_char *name, const char *info)
171 {
172     logf (LOG_DEBUG, "dict name: %s", name);
173     add_isam_p (info);
174     return 0;
175 }
176
177 static int trunc_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
178                        ISAM_P **isam_ps,
179                        int *no, int split_flag)
180 {
181     char termz[IT_MAX_WORD+1];
182     char term_sub[IT_MAX_WORD+1];
183     char term_dict[2*IT_MAX_WORD+2];
184     int sizez, i, j;
185     char *p0 = termz, *p1 = NULL;
186     const char *info;    
187     AttrType truncation;
188     int truncation_value;
189     Z_Term *term = zapt->term;
190
191     isam_p_indx = 0;
192     attr_init (&truncation, zapt, 5);
193     truncation_value = attr_find (&truncation);
194     logf (LOG_DEBUG, "truncation value %d", truncation_value);
195     *no = 0;
196     if (term->which != Z_Term_general)
197     {
198         zi->errCode = 124;
199         return -1;
200     }
201     sizez = term->u.general->len;
202     if (sizez > IT_MAX_WORD)
203         sizez = IT_MAX_WORD;
204     for (i = 0; i<sizez; i++)
205         termz[i] = index_char_cvt (term->u.general->buf[i]);
206     termz[i] = '\0';
207
208     while (1)
209     {
210         if (split_flag && (p1 = strchr (p0, ' ')))
211         {
212             memcpy (term_sub, p0, p1-p0);
213             term_sub[p1-p0] = '\0';
214         }
215         else
216             strcpy (term_sub, p0);
217         switch (truncation_value)
218         {
219         case -1:         /* not specified */
220         case 100:        /* do not truncate */
221             strcpy (term_dict, term_sub);
222             logf (LOG_DEBUG, "dict_lookup: %s", term_dict);
223             if ((info = dict_lookup (zi->wordDict, term_dict)))
224                 add_isam_p (info);
225             break;
226         case 1:          /* right truncation */
227             strcpy (term_dict, term_sub);
228             strcat (term_dict, ".*");
229             dict_lookup_grep (zi->wordDict, term_dict, 0, grep_handle);
230             break;
231         case 2:          /* left truncation */
232         case 3:          /* left&right truncation */
233             zi->errCode = 120;
234             return -1;
235         case 101:        /* process # in term */
236             for (j = 0, i = 0; term_sub[i] && i < 3; i++)
237                 term_dict[j++] = term_sub[i];
238             for (; term_sub[i]; i++)
239                 if (term_sub[i] == '#')
240                 {
241                     term_dict[j++] = '.';
242                     term_dict[j++] = '*';
243                 }
244                 else
245                     term_dict[j++] = term_sub[i];
246             term_dict[j] = '\0';
247             dict_lookup_grep (zi->wordDict, term_dict, 0, grep_handle);
248             break;
249         case 102:        /* regular expression */
250             strcpy (term_dict, term_sub);
251             dict_lookup_grep (zi->wordDict, term_dict, 0, grep_handle);
252             break;
253         }
254         if (!p1)
255             break;
256         p0 = p1+1;
257     }       
258     *isam_ps = isam_p_buf;
259     *no = isam_p_indx; 
260     logf (LOG_DEBUG, "%d positions", *no);
261     return 0;
262 }
263
264 static RSET rpn_search_APT_relevance (ZServerInfo *zi, 
265                                       Z_AttributesPlusTerm *zapt)
266 {
267     rset_relevance_parms parms;
268
269     parms.key_size = sizeof(struct it_key);
270     parms.max_rec = 100;
271     parms.cmp = key_compare;
272     parms.is = zi->wordIsam;
273     if (trunc_term (zi, zapt, &parms.isam_positions, 
274                 &parms.no_isam_positions, 1))
275         return NULL;
276     if (parms.no_isam_positions > 0)
277         return rset_create (rset_kind_relevance, &parms);
278     else
279         return rset_create (rset_kind_null, NULL);
280 }
281
282 static RSET rpn_search_APT_word (ZServerInfo *zi,
283                                  Z_AttributesPlusTerm *zapt)
284 {
285     ISAM_P *isam_positions;
286     int no_isam_positions;
287     rset_isam_parms parms;
288
289     if (trunc_term (zi, zapt, &isam_positions,
290                     &no_isam_positions, 0))
291         return NULL;
292     if (no_isam_positions != 1)
293         return rset_create (rset_kind_null, NULL);
294     parms.is = zi->wordIsam;
295     parms.pos = *isam_positions;
296     return rset_create (rset_kind_isam, &parms);
297 }
298
299 static RSET rpn_search_APT_phrase (ZServerInfo *zi,
300                                    Z_AttributesPlusTerm *zapt)
301 {
302     ISAM_P *isam_positions;
303     int no_isam_positions;
304     rset_isam_parms parms;
305
306     if (trunc_term (zi, zapt, &isam_positions,
307                     &no_isam_positions, 1))
308         return NULL;
309     if (no_isam_positions != 1)
310         return rset_create (rset_kind_null, NULL);
311     parms.is = zi->wordIsam;
312     parms.pos = *isam_positions;
313     return rset_create (rset_kind_isam, &parms);
314 }
315
316 static RSET rpn_search_APT (ZServerInfo *zi, Z_AttributesPlusTerm *zapt)
317 {
318     AttrType relation;
319     AttrType structure;
320     int relation_value, structure_value;
321
322     attr_init (&relation, zapt, 2);
323     attr_init (&structure, zapt, 4);
324     
325     relation_value = attr_find (&relation);
326     structure_value = attr_find (&structure);
327     switch (structure_value)
328     {
329     case -1:
330         if (relation_value == 102) /* relevance relation */
331             return rpn_search_APT_relevance (zi, zapt);
332         return rpn_search_APT_word (zi, zapt);
333     case 1: /* phrase */
334         if (relation_value == 102) /* relevance relation */
335             return rpn_search_APT_relevance (zi, zapt);
336         return rpn_search_APT_phrase (zi, zapt);
337         break;
338     case 2: /* word */
339         if (relation_value == 102) /* relevance relation */
340             return rpn_search_APT_relevance (zi, zapt);
341         return rpn_search_APT_word (zi, zapt);
342     case 3: /* key */
343         break;
344     case 4: /* year */
345         break;
346     case 5: /* date - normalized */
347         break;
348     case 6: /* word list */
349         return rpn_search_APT_relevance (zi, zapt);
350     case 100: /* date - un-normalized */
351         break;
352     case 101: /* name - normalized */
353         break;
354     case 102: /* date - un-normalized */
355         break;
356     case 103: /* structure */
357         break;
358     case 104: /* urx */
359         break;
360     case 105: /* free-form-text */
361         return rpn_search_APT_relevance (zi, zapt);
362     case 106: /* document-text */
363         return rpn_search_APT_relevance (zi, zapt);
364     case 107: /* local-number */
365         break;
366     case 108: /* string */ 
367         return rpn_search_APT_word (zi, zapt);
368     case 109: /* numeric string */
369         break;
370     }
371     zi->errCode = 118;
372     return NULL;
373 }
374
375 static RSET rpn_search_ref (ZServerInfo *zi, Z_ResultSetId *resultSetId)
376 {
377     ZServerSet *s;
378
379     if (!(s = resultSetGet (zi, resultSetId)))
380         return rset_create (rset_kind_null, NULL);
381     return s->rset;
382 }
383
384 static RSET rpn_search_structure (ZServerInfo *zi, Z_RPNStructure *zs)
385 {
386     RSET r = NULL;
387     if (zs->which == Z_RPNStructure_complex)
388     {
389         rset_bool_parms bool_parms;
390
391         bool_parms.rset_l = rpn_search_structure (zi, zs->u.complex->s1);
392         if (bool_parms.rset_l == NULL)
393             return NULL;
394         bool_parms.rset_r = rpn_search_structure (zi, zs->u.complex->s2);
395         if (bool_parms.rset_r == NULL)
396         {
397             rset_delete (bool_parms.rset_l);
398             return NULL;
399         }
400         bool_parms.key_size = sizeof(struct it_key);
401         bool_parms.cmp = key_compare;
402
403         switch (zs->u.complex->operator->which)
404         {
405         case Z_Operator_and:
406             r = rset_create (rset_kind_and, &bool_parms);
407             break;
408         case Z_Operator_or:
409             r = rset_create (rset_kind_or, &bool_parms);
410             break;
411         case Z_Operator_and_not:
412             r = rset_create (rset_kind_not, &bool_parms);
413             break;
414         default:
415             assert (0);
416         }
417     }
418     else if (zs->which == Z_RPNStructure_simple)
419     {
420         if (zs->u.simple->which == Z_Operand_APT)
421         {
422             logf (LOG_DEBUG, "rpn_search_APT");
423             r = rpn_search_APT (zi, zs->u.simple->u.attributesPlusTerm);
424         }
425         else if (zs->u.simple->which == Z_Operand_resultSetId)
426         {
427             logf (LOG_DEBUG, "rpn_search_ref");
428             r = rpn_search_ref (zi, zs->u.simple->u.resultSetId);
429         }
430         else
431         {
432             assert (0);
433         }
434     }
435     else
436     {
437         assert (0);
438     }
439     return r;
440 }
441
442 static void count_set (RSET r, int *count)
443 {
444     int psysno = 0;
445     struct it_key key;
446     RSFD rfd;
447
448     logf (LOG_DEBUG, "rpn_save_set");
449     *count = 0;
450     rfd = rset_open (r, 0);
451     while (rset_read (r, rfd, &key))
452     {
453         if (key.sysno != psysno)
454         {
455             psysno = key.sysno;
456             (*count)++;
457         }
458     }
459     rset_close (r, rfd);
460     logf (LOG_DEBUG, "%d distinct sysnos", *count);
461 }
462
463 int rpn_search (ZServerInfo *zi,
464                 Z_RPNQuery *rpn, int num_bases, char **basenames, 
465                 const char *setname, int *hits)
466 {
467     RSET rset;
468
469     zi->errCode = 0;
470     zi->errString = NULL;
471     rset = rpn_search_structure (zi, rpn->RPNStructure);
472     if (!rset)
473         return zi->errCode;
474     count_set (rset, hits);
475     resultSetAdd (zi, setname, 1, rset);
476     return zi->errCode;
477 }
478