Use attribute actually used in search requests.
[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.16  1995-10-02 16:24:40  adam
8  * Use attribute actually used in search requests.
9  *
10  * Revision 1.15  1995/10/02  15:18:52  adam
11  * New member in recRetrieveCtrl: diagnostic.
12  *
13  * Revision 1.14  1995/09/28  12:10:32  adam
14  * Bug fixes. Field prefix used in queries.
15  *
16  * Revision 1.13  1995/09/18  14:17:50  adam
17  * Minor changes.
18  *
19  * Revision 1.12  1995/09/15  14:45:21  adam
20  * Retrieve control.
21  * Work on truncation.
22  *
23  * Revision 1.11  1995/09/14  11:53:27  adam
24  * First work on regular expressions/truncations.
25  *
26  * Revision 1.10  1995/09/11  15:23:26  adam
27  * More work on relevance search.
28  *
29  * Revision 1.9  1995/09/11  13:09:35  adam
30  * More work on relevance feedback.
31  *
32  * Revision 1.8  1995/09/08  14:52:27  adam
33  * Minor changes. Dictionary is lower case now.
34  *
35  * Revision 1.7  1995/09/07  13:58:36  adam
36  * New parameter: result-set file descriptor (RSFD) to support multiple
37  * positions within the same result-set.
38  * Boolean operators: and, or, not implemented.
39  * Result-set references.
40  *
41  * Revision 1.6  1995/09/06  16:11:18  adam
42  * Option: only one word key per file.
43  *
44  * Revision 1.5  1995/09/06  10:33:04  adam
45  * More work on present. Some log messages removed.
46  *
47  * Revision 1.4  1995/09/05  15:28:40  adam
48  * More work on search engine.
49  *
50  * Revision 1.3  1995/09/04  15:20:22  adam
51  * Minor changes.
52  *
53  * Revision 1.2  1995/09/04  12:33:43  adam
54  * Various cleanup. YAZ util used instead.
55  *
56  * Revision 1.1  1995/09/04  09:10:40  adam
57  * More work on index add/del/update.
58  * Merge sort implemented.
59  * Initial work on z39 server.
60  *
61  */
62 #include <stdio.h>
63 #include <assert.h>
64 #include <unistd.h>
65
66 #include "zserver.h"
67
68 #include <rsisam.h>
69 #include <rstemp.h>
70 #include <rsnull.h>
71 #include <rsbool.h>
72 #include <rsrel.h>
73
74 /*
75  * attr_print: log attributes
76  */
77 static void attr_print (Z_AttributesPlusTerm *t)
78 {
79     int of, i;
80     for (of = 0; of < t->num_attributes; of++)
81     {
82         Z_AttributeElement *element;
83         element = t->attributeList[of];
84
85         switch (element->which) 
86         {
87         case Z_AttributeValue_numeric:
88             logf (LOG_DEBUG, "attributeType=%d value=%d", 
89                   *element->attributeType,
90                   *element->value.numeric);
91             break;
92         case Z_AttributeValue_complex:
93             logf (LOG_DEBUG, "attributeType=%d complex", 
94                   *element->attributeType);
95             for (i = 0; i<element->value.complex->num_list; i++)
96             {
97                 if (element->value.complex->list[i]->which ==
98                     Z_StringOrNumeric_string)
99                     logf (LOG_DEBUG, "   string: '%s'",
100                           element->value.complex->list[i]->u.string);
101                 else if (element->value.complex->list[i]->which ==
102                          Z_StringOrNumeric_numeric)
103                     logf (LOG_DEBUG, "   numeric: '%d'",
104                           *element->value.complex->list[i]->u.numeric);
105             }
106             break;
107         default:
108             assert (0);
109         }
110     }
111 }
112
113 typedef struct {
114     int type;
115     int major;
116     int minor;
117     Z_AttributesPlusTerm *zapt;
118 } AttrType;
119
120 static int attr_find (AttrType *src)
121 {
122     while (src->major < src->zapt->num_attributes)
123     {
124         Z_AttributeElement *element;
125         element = src->zapt->attributeList[src->major];
126
127         if (src->type == *element->attributeType)
128         {
129             switch (element->which) 
130             {
131             case Z_AttributeValue_numeric:
132                 ++(src->major);
133                 return *element->value.numeric;
134                 break;
135             case Z_AttributeValue_complex:
136                 if (src->minor >= element->value.complex->num_list ||
137                     element->value.complex->list[src->minor]->which !=  
138                     Z_StringOrNumeric_numeric)
139                     break;
140                 ++(src->minor);
141                 return *element->value.complex->list[src->minor-1]->u.numeric;
142             default:
143                 assert (0);
144             }
145         }
146         ++(src->major);
147     }
148     return -1;
149 }
150
151 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
152                        int type)
153 {
154     src->zapt = zapt;
155     src->type = type;
156     src->major = 0;
157     src->minor = 0;
158 }
159
160 struct trunc_info {
161     int  *indx;
162     char **heap;
163     int  heapnum;
164     int  (*cmp)(const void *p1, const void *p2);
165     int  keysize;
166     char *swapbuf;
167     char *tmpbuf;
168     char *buf;
169 };
170
171 static void heap_swap (struct trunc_info *ti, int i1, int i2)
172 {
173     int swap;
174
175     memcpy (ti->swapbuf, ti->heap[i1], ti->keysize);
176     memcpy (ti->heap[i1], ti->heap[i2], ti->keysize);
177     memcpy (ti->heap[i2], ti->swapbuf, ti->keysize);
178
179     swap = ti->indx[i1];
180     ti->indx[i1] = ti->indx[i2];
181     ti->indx[i2] = swap;
182 }
183
184 static void heap_delete (struct trunc_info *ti)
185 {
186     int cur = 1, child = 2;
187
188     assert (ti->heapnum > 0);
189     memcpy (ti->heap[1], ti->heap[ti->heapnum], ti->keysize);
190     ti->indx[1] = ti->indx[ti->heapnum--];
191     while (child <= ti->heapnum) {
192         if (child < ti->heapnum &&
193             (*ti->cmp)(ti->heap[child], ti->heap[1+child]) > 0)
194             child++;
195         if ((*ti->cmp)(ti->heap[cur], ti->heap[child]) > 0)
196         {
197             heap_swap (ti, cur, child);
198             cur = child;
199             child = 2*cur;
200         }
201         else
202             break;
203     }
204 }
205
206 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
207 {
208     int cur, parent;
209
210     cur = ++(ti->heapnum);
211     memcpy (ti->heap[cur], buf, ti->keysize);
212     ti->indx[cur] = indx;
213     parent = cur/2;
214     while (parent && (*ti->cmp)(ti->heap[parent], ti->heap[cur]) > 0)
215     {
216         heap_swap (ti, cur, parent);
217         cur = parent;
218         parent = cur/2;
219     }
220 }
221
222 static
223 struct trunc_info *heap_init (int size, int key_size,
224                               int (*cmp)(const void *p1, const void *p2))
225 {
226     struct trunc_info *ti = xmalloc (sizeof(*ti));
227     int i;
228
229     ++size;
230     ti->heapnum = 0;
231     ti->keysize = key_size;
232     ti->cmp = cmp;
233     ti->indx = xmalloc (size * sizeof(*ti->indx));
234     ti->heap = xmalloc (size * sizeof(*ti->heap));
235     ti->swapbuf = xmalloc (ti->keysize);
236     ti->tmpbuf = xmalloc (ti->keysize);
237     ti->buf = xmalloc (size * ti->keysize);
238     for (i = size; --i >= 0; )
239         ti->heap[i] = ti->buf + ti->keysize * i;
240     return ti;
241 }
242
243 static void heap_close (struct trunc_info *ti)
244 {
245     xfree (ti->indx);
246     xfree (ti->heap);
247     xfree (ti->swapbuf);
248     xfree (ti->tmpbuf);
249     xfree (ti);
250 }
251
252 static RSET rset_trunc (ISAM isam, ISAM_P *isam_p, int from, int to,
253                         int merge_chunk)
254 {
255     logf (LOG_DEBUG, "rset_trunc, range=%d-%d", from, to-1);
256     if (to - from > merge_chunk)
257     {
258         return NULL;
259     }
260     else
261     {
262         ISPT *ispt;
263         int i;
264         struct trunc_info *ti;
265         RSET result;
266         RSFD rsfd;
267         rset_temp_parms parms;
268
269         ispt = xmalloc (sizeof(*ispt) * (to-from));
270         parms.key_size = sizeof (struct it_key);
271         result = rset_create (rset_kind_temp, &parms);
272         rsfd = rset_open (result, 1);
273
274         ti = heap_init (to-from, sizeof(struct it_key),
275                         key_compare);
276         for (i = to-from; --i >= 0; )
277         {
278             ispt[i] = is_position (isam, isam_p[from+i]);
279             if (is_readkey (ispt[i], ti->tmpbuf))
280                 heap_insert (ti, ti->tmpbuf, i);
281         }
282         while (ti->heapnum)
283         {
284             int n = ti->indx[1];
285
286             rset_write (result, rsfd, ti->heap[1]);
287             heap_delete (ti);
288             if (is_readkey (ispt[n], ti->tmpbuf))
289                 heap_insert (ti, ti->tmpbuf, n);
290         }
291         for (i = to-from; --i >= 0; )
292             is_pt_free (ispt[i]);
293         rset_close (result, rsfd);
294         heap_close (ti);
295         xfree (ispt);
296         return result;
297     }
298 }
299
300 static ISAM_P *isam_p_buf = NULL;
301 static int isam_p_size = 0;
302 static int isam_p_indx;
303
304 static void add_isam_p (const char *info)
305 {
306     if (isam_p_indx == isam_p_size)
307     {
308         ISAM_P *new_isam_p_buf;
309         
310         isam_p_size = 2*isam_p_size + 100;
311         new_isam_p_buf = xmalloc (sizeof(*new_isam_p_buf) *
312                                   isam_p_size);
313         if (isam_p_buf)
314         {
315             memcpy (new_isam_p_buf, isam_p_buf,
316                     isam_p_indx * sizeof(*isam_p_buf));
317             xfree (isam_p_buf);
318         }
319         isam_p_buf = new_isam_p_buf;
320     }
321     assert (*info == sizeof(*isam_p_buf));
322     memcpy (isam_p_buf + isam_p_indx, info+1, sizeof(*isam_p_buf));
323     isam_p_indx++;
324 }
325
326 static int grep_handle (Dict_char *name, const char *info)
327 {
328     logf (LOG_DEBUG, "dict name: %s", name);
329     add_isam_p (info);
330     return 0;
331 }
332
333 static int trunc_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
334                        const char *term_sub, ISAM_P **isam_ps)
335 {
336     char term_dict[2*IT_MAX_WORD+2];
337     int i, j;
338     const char *info;    
339     AttrType truncation;
340     int truncation_value;
341
342     attr_init (&truncation, zapt, 5);
343     truncation_value = attr_find (&truncation);
344     logf (LOG_DEBUG, "truncation value %d", truncation_value);
345     switch (truncation_value)
346     {
347     case -1:         /* not specified */
348     case 100:        /* do not truncate */
349         strcpy (term_dict, term_sub);
350         logf (LOG_DEBUG, "dict_lookup: %s", term_dict);
351         if ((info = dict_lookup (zi->wordDict, term_dict)))
352             add_isam_p (info);
353         break;
354     case 1:          /* right truncation */
355         strcpy (term_dict, term_sub);
356         strcat (term_dict, ".*");
357         dict_lookup_grep (zi->wordDict, term_dict, 0, grep_handle);
358         break;
359     case 2:          /* left truncation */
360     case 3:          /* left&right truncation */
361         zi->errCode = 120;
362         return -1;
363     case 101:        /* process # in term */
364         for (j = 0, i = 0; term_sub[i] && i < 2; i++)
365             term_dict[j++] = term_sub[i];
366         for (; term_sub[i]; i++)
367             if (term_sub[i] == '#')
368             {
369                 term_dict[j++] = '.';
370                 term_dict[j++] = '*';
371             }
372             else
373                 term_dict[j++] = term_sub[i];
374         term_dict[j] = '\0';
375         dict_lookup_grep (zi->wordDict, term_dict, 0, grep_handle);
376         break;
377     case 102:        /* regular expression */
378         strcpy (term_dict, term_sub);
379         dict_lookup_grep (zi->wordDict, term_dict, 0, grep_handle);
380         break;
381     }
382     *isam_ps = isam_p_buf;
383     logf (LOG_DEBUG, "%d positions", isam_p_indx);
384     return 0;
385 }
386
387 static void field_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
388                         char *termz)
389 {
390     size_t i, j, sizez;
391     AttrType use;
392     int use_value;
393     Z_Term *term = zapt->term;
394
395     attr_init (&use, zapt, 1);
396     use_value = attr_find (&use);
397     if (use_value == -1)
398         use_value = 1016;
399
400     i = index_word_prefix (termz, 1, use_value);
401     sizez = i + term->u.general->len;
402     if (sizez > IT_MAX_WORD)
403         sizez = IT_MAX_WORD;
404     for (j = 0; i < sizez; i++, j++)
405         termz[i] = index_char_cvt (term->u.general->buf[j]);
406     termz[i] = '\0';
407 }
408
409
410 static RSET rpn_search_APT_relevance (ZServerInfo *zi, 
411                                       Z_AttributesPlusTerm *zapt)
412 {
413     rset_relevance_parms parms;
414     char termz[IT_MAX_WORD+1];
415     char term_sub[IT_MAX_WORD+1];
416     char *p0 = termz, *p1 = NULL;
417     Z_Term *term = zapt->term;
418
419     parms.key_size = sizeof(struct it_key);
420     parms.max_rec = 100;
421     parms.cmp = key_compare;
422     parms.is = zi->wordIsam;
423
424     if (term->which != Z_Term_general)
425     {
426         zi->errCode = 124;
427         return NULL;
428     }
429     field_term (zi, zapt, termz);
430     isam_p_indx = 0;  /* global, set by trunc_term - see below */
431     while (1)
432     {
433         if ((p1 = strchr (p0, ' ')))
434         {
435             memcpy (term_sub, p0, p1-p0);
436             term_sub[p1-p0] = '\0';
437         }
438         else
439             strcpy (term_sub, p0);
440         if (trunc_term (zi, zapt, term_sub, &parms.isam_positions))
441             return NULL;
442         if (!p1)
443             break;
444         p0 = p1+1;
445     }
446     parms.no_isam_positions = isam_p_indx;
447     if (isam_p_indx > 0)
448         return rset_create (rset_kind_relevance, &parms);
449     else
450         return rset_create (rset_kind_null, NULL);
451 }
452
453 static RSET rpn_search_APT_word (ZServerInfo *zi,
454                                  Z_AttributesPlusTerm *zapt)
455 {
456     ISAM_P *isam_positions;
457     rset_isam_parms parms;
458
459     char termz[IT_MAX_WORD+1];
460     Z_Term *term = zapt->term;
461
462     if (term->which != Z_Term_general)
463     {
464         zi->errCode = 124;
465         return NULL;
466     }
467     field_term (zi, zapt, termz);
468     isam_p_indx = 0;  /* global, set by trunc_term - see below */
469     if (trunc_term (zi, zapt, termz, &isam_positions))
470         return NULL;
471     if (isam_p_indx < 1)
472         return rset_create (rset_kind_null, NULL);
473     else if (isam_p_indx == 1)
474     {
475         parms.is = zi->wordIsam;
476         parms.pos = *isam_positions;
477         return rset_create (rset_kind_isam, &parms);
478     }
479     else
480         return rset_trunc (zi->wordIsam, isam_positions, 0, isam_p_indx, 400);
481 }
482
483 static RSET rpn_search_APT_phrase (ZServerInfo *zi,
484                                    Z_AttributesPlusTerm *zapt)
485 {
486     ISAM_P *isam_positions;
487     rset_isam_parms parms;
488
489     char termz[IT_MAX_WORD+1];
490     Z_Term *term = zapt->term;
491
492     if (term->which != Z_Term_general)
493     {
494         zi->errCode = 124;
495         return NULL;
496     }
497     field_term (zi, zapt, termz);
498     isam_p_indx = 0;  /* global, set by trunc_term - see below */
499     if (trunc_term (zi, zapt, termz, &isam_positions))
500         return NULL;
501     if (isam_p_indx != 1)
502         return rset_create (rset_kind_null, NULL);
503     parms.is = zi->wordIsam;
504     parms.pos = *isam_positions;
505     return rset_create (rset_kind_isam, &parms);
506 }
507
508 static RSET rpn_search_APT (ZServerInfo *zi, Z_AttributesPlusTerm *zapt)
509 {
510     AttrType relation;
511     AttrType structure;
512     int relation_value, structure_value;
513
514     attr_init (&relation, zapt, 2);
515     attr_init (&structure, zapt, 4);
516     
517     relation_value = attr_find (&relation);
518     structure_value = attr_find (&structure);
519     switch (structure_value)
520     {
521     case -1:
522         if (relation_value == 102) /* relevance relation */
523             return rpn_search_APT_relevance (zi, zapt);
524         return rpn_search_APT_word (zi, zapt);
525     case 1: /* phrase */
526         if (relation_value == 102) /* relevance relation */
527             return rpn_search_APT_relevance (zi, zapt);
528         return rpn_search_APT_phrase (zi, zapt);
529         break;
530     case 2: /* word */
531         if (relation_value == 102) /* relevance relation */
532             return rpn_search_APT_relevance (zi, zapt);
533         return rpn_search_APT_word (zi, zapt);
534     case 3: /* key */
535         break;
536     case 4: /* year */
537         break;
538     case 5: /* date - normalized */
539         break;
540     case 6: /* word list */
541         return rpn_search_APT_relevance (zi, zapt);
542     case 100: /* date - un-normalized */
543         break;
544     case 101: /* name - normalized */
545         break;
546     case 102: /* date - un-normalized */
547         break;
548     case 103: /* structure */
549         break;
550     case 104: /* urx */
551         break;
552     case 105: /* free-form-text */
553         return rpn_search_APT_relevance (zi, zapt);
554     case 106: /* document-text */
555         return rpn_search_APT_relevance (zi, zapt);
556     case 107: /* local-number */
557         break;
558     case 108: /* string */ 
559         return rpn_search_APT_word (zi, zapt);
560     case 109: /* numeric string */
561         break;
562     }
563     zi->errCode = 118;
564     return NULL;
565 }
566
567 static RSET rpn_search_ref (ZServerInfo *zi, Z_ResultSetId *resultSetId)
568 {
569     ZServerSet *s;
570
571     if (!(s = resultSetGet (zi, resultSetId)))
572         return rset_create (rset_kind_null, NULL);
573     return s->rset;
574 }
575
576 static RSET rpn_search_structure (ZServerInfo *zi, Z_RPNStructure *zs)
577 {
578     RSET r = NULL;
579     if (zs->which == Z_RPNStructure_complex)
580     {
581         rset_bool_parms bool_parms;
582
583         bool_parms.rset_l = rpn_search_structure (zi, zs->u.complex->s1);
584         if (bool_parms.rset_l == NULL)
585             return NULL;
586         bool_parms.rset_r = rpn_search_structure (zi, zs->u.complex->s2);
587         if (bool_parms.rset_r == NULL)
588         {
589             rset_delete (bool_parms.rset_l);
590             return NULL;
591         }
592         bool_parms.key_size = sizeof(struct it_key);
593         bool_parms.cmp = key_compare;
594
595         switch (zs->u.complex->operator->which)
596         {
597         case Z_Operator_and:
598             r = rset_create (rset_kind_and, &bool_parms);
599             break;
600         case Z_Operator_or:
601             r = rset_create (rset_kind_or, &bool_parms);
602             break;
603         case Z_Operator_and_not:
604             r = rset_create (rset_kind_not, &bool_parms);
605             break;
606         default:
607             assert (0);
608         }
609     }
610     else if (zs->which == Z_RPNStructure_simple)
611     {
612         if (zs->u.simple->which == Z_Operand_APT)
613         {
614             logf (LOG_DEBUG, "rpn_search_APT");
615             r = rpn_search_APT (zi, zs->u.simple->u.attributesPlusTerm);
616         }
617         else if (zs->u.simple->which == Z_Operand_resultSetId)
618         {
619             logf (LOG_DEBUG, "rpn_search_ref");
620             r = rpn_search_ref (zi, zs->u.simple->u.resultSetId);
621         }
622         else
623         {
624             assert (0);
625         }
626     }
627     else
628     {
629         assert (0);
630     }
631     return r;
632 }
633
634 static void count_set (RSET r, int *count)
635 {
636     int psysno = 0;
637     struct it_key key;
638     RSFD rfd;
639
640     logf (LOG_DEBUG, "rpn_save_set");
641     *count = 0;
642     rfd = rset_open (r, 0);
643     while (rset_read (r, rfd, &key))
644     {
645         if (key.sysno != psysno)
646         {
647             psysno = key.sysno;
648             (*count)++;
649         }
650     }
651     rset_close (r, rfd);
652     logf (LOG_DEBUG, "%d distinct sysnos", *count);
653 }
654
655 int rpn_search (ZServerInfo *zi,
656                 Z_RPNQuery *rpn, int num_bases, char **basenames, 
657                 const char *setname, int *hits)
658 {
659     RSET rset;
660
661     zi->errCode = 0;
662     zi->errString = NULL;
663     rset = rpn_search_structure (zi, rpn->RPNStructure);
664     if (!rset)
665         return zi->errCode;
666     count_set (rset, hits);
667     resultSetAdd (zi, setname, 1, rset);
668     return zi->errCode;
669 }
670