Multi register works with record paths and data1 profile path
[idzebra-moved-to-github.git] / util / zebramap.c
1 /*
2  * Copyright (C) 1994-1999, Index Data 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: zebramap.c,v $
7  * Revision 1.24  2002-04-04 20:50:37  adam
8  * Multi register works with record paths and data1 profile path
9  *
10  * Revision 1.23  2001/11/15 08:41:24  adam
11  * Fix for weight (bug introduced by previous commit).
12  *
13  * Revision 1.22  2001/11/14 22:06:27  adam
14  * Rank-weight may be controlled via query.
15  *
16  * Revision 1.21  2001/01/22 10:42:56  adam
17  * Added numerical sort.
18  *
19  * Revision 1.20  2000/03/02 14:35:19  adam
20  * Added structure year and date.
21  *
22  * Revision 1.19  1999/11/30 13:48:04  adam
23  * Improved installation. Updated for inclusion of YAZ header files.
24  *
25  * Revision 1.18  1999/10/15 08:27:46  adam
26  * Fixed replace handler. 8-bit fix.
27  *
28  * Revision 1.17  1999/09/08 12:13:21  adam
29  * Fixed minor bug "replace"-mappings. Removed some logging messages.
30  *
31  * Revision 1.16  1999/09/07 07:19:21  adam
32  * Work on character mapping. Implemented replace rules.
33  *
34  * Revision 1.15  1999/05/26 07:49:14  adam
35  * C++ compilation.
36  *
37  * Revision 1.14  1999/02/19 10:37:40  adam
38  * Minor fix.
39  *
40  * Revision 1.13  1999/02/18 15:01:04  adam
41  * Structure=key uses register type 0.
42  *
43  * Revision 1.12  1999/02/12 13:29:25  adam
44  * Implemented position-flag for registers.
45  *
46  * Revision 1.11  1998/10/13 20:09:19  adam
47  * Changed call to readconf_line.
48  *
49  * Revision 1.10  1998/06/23 15:33:37  adam
50  * Added feature to specify sort criteria in query (type 7 specifies
51  * sort flags).
52  *
53  * Revision 1.9  1998/04/02 14:35:30  adam
54  * First version of Zebra that works with compiled ASN.1.
55  *
56  * Revision 1.8  1998/03/05 08:42:44  adam
57  * Minor changes to zebramap data structures. Query mapping rules changed.
58  *
59  * Revision 1.7  1998/02/10 12:03:07  adam
60  * Implemented Sort.
61  *
62  * Revision 1.6  1998/01/29 13:36:01  adam
63  * Structure word-list, free-form-text and document-text all
64  * trigger ranked search.
65  *
66  * Revision 1.5  1997/11/19 10:22:14  adam
67  * Bug fix (introduced by previous commit).
68  *
69  * Revision 1.4  1997/11/18 10:05:08  adam
70  * Changed character map facility so that admin can specify character
71  * mapping files for each register type, w, p, etc.
72  *
73  * Revision 1.3  1997/11/17 15:35:26  adam
74  * Bug fix. Relation=relevance wasn't observed.
75  *
76  * Revision 1.2  1997/10/31 12:39:30  adam
77  * Changed log message.
78  *
79  * Revision 1.1  1997/10/27 14:33:06  adam
80  * Moved towards generic character mapping depending on "structure"
81  * field in abstract syntax file. Fixed a few memory leaks. Fixed
82  * bug with negative integers when doing searches with relational
83  * operators.
84  *
85  */
86
87 #include <assert.h>
88 #include <ctype.h>
89
90 #include <yaz/yaz-util.h>
91 #include <charmap.h>
92 #include <zebramap.h>
93
94 #define ZEBRA_MAP_TYPE_SORT  1
95 #define ZEBRA_MAP_TYPE_INDEX 2
96
97 #define ZEBRA_REPLACE_ANY  300
98
99 struct zm_token {
100     int *token_from;
101     char *token_to;
102     int token_min;
103     struct zm_token *next;
104 };
105
106 struct zebra_map {
107     unsigned reg_id;
108     int completeness;
109     int positioned;
110     int type;
111     union {
112         struct {
113             int dummy;
114         } index;
115         struct {
116             int entry_size;
117         } sort;
118     } u;
119     chrmaptab maptab;
120     const char *maptab_name;
121     struct zebra_map *next;
122     struct zm_token *replace_tokens;
123 };
124
125 struct zebra_maps {
126     char *tabpath;
127     char *tabroot;
128     NMEM nmem;
129     struct zebra_map *map_list;
130     char temp_map_str[2];
131     const char *temp_map_ptr[2];
132     struct zebra_map **lookup_array;
133     WRBUF wrbuf_1, wrbuf_2;
134 };
135
136 void zebra_maps_close (ZebraMaps zms)
137 {
138     struct zebra_map *zm = zms->map_list;
139     while (zm)
140     {
141         if (zm->maptab)
142             chrmaptab_destroy (zm->maptab);
143         zm = zm->next;
144     }
145     wrbuf_free (zms->wrbuf_1, 1);
146     wrbuf_free (zms->wrbuf_2, 1);
147     nmem_destroy (zms->nmem);
148     xfree (zms);
149 }
150
151 static void zebra_map_read (ZebraMaps zms, const char *name)
152 {
153     FILE *f;
154     char line[512];
155     char *argv[10];
156     int argc;
157     int lineno = 0;
158     struct zebra_map **zm = 0, *zp;
159
160     if (!(f = yaz_path_fopen_base(zms->tabpath, name, "r", zms->tabroot)))
161     {
162         logf(LOG_WARN|LOG_ERRNO, "%s", name);
163         return ;
164     }
165     while ((argc = readconf_line(f, &lineno, line, 512, argv, 10)))
166     {
167         if (!yaz_matchstr (argv[0], "index") && argc == 2)
168         {
169             if (!zm)
170                 zm = &zms->map_list;
171             else
172                 zm = &(*zm)->next;
173             *zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(**zm));
174             (*zm)->reg_id = argv[1][0];
175             (*zm)->maptab_name = NULL;
176             (*zm)->maptab = NULL;
177             (*zm)->type = ZEBRA_MAP_TYPE_INDEX;
178             (*zm)->completeness = 0;
179             (*zm)->positioned = 1;
180             (*zm)->replace_tokens = 0;
181         }
182         else if (!yaz_matchstr (argv[0], "sort") && argc == 2)
183         {
184             if (!zm)
185                 zm = &zms->map_list;
186             else
187                 zm = &(*zm)->next;
188             *zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(**zm));
189             (*zm)->reg_id = argv[1][0];
190             (*zm)->maptab_name = NULL;
191             (*zm)->type = ZEBRA_MAP_TYPE_SORT;
192             (*zm)->u.sort.entry_size = 80;
193             (*zm)->maptab = NULL;
194             (*zm)->completeness = 0;
195             (*zm)->positioned = 0;
196             (*zm)->replace_tokens = 0;
197         }
198         else if (zm && !yaz_matchstr (argv[0], "charmap") && argc == 2)
199         {
200             (*zm)->maptab_name = nmem_strdup (zms->nmem, argv[1]);
201         }
202         else if (zm && !yaz_matchstr (argv[0], "completeness") && argc == 2)
203         {
204             (*zm)->completeness = atoi (argv[1]);
205         }
206         else if (zm && !yaz_matchstr (argv[0], "position") && argc == 2)
207         {
208             (*zm)->positioned = atoi (argv[1]);
209         }
210         else if (zm && !yaz_matchstr (argv[0], "entrysize") && argc == 2)
211         {
212             if ((*zm)->type == ZEBRA_MAP_TYPE_SORT)
213                 (*zm)->u.sort.entry_size = atoi (argv[1]);
214         }
215         else if (zm && !yaz_matchstr (argv[0], "replace") && argc >= 2)
216         {
217             struct zm_token *token = nmem_malloc (zms->nmem, sizeof(*token));
218             token->next = (*zm)->replace_tokens;
219             (*zm)->replace_tokens = token;
220 #if 0
221             logf (LOG_LOG, "replace %s", argv[1]);
222 #endif
223             token->token_from = 0;
224             if (argc >= 2)
225             {
226                 char *cp = argv[1];
227                 int *dp = token->token_from = (int *)
228                     nmem_malloc (zms->nmem, (1+strlen(cp))*sizeof(int));
229                 while (*cp)
230                     if (*cp == '$')
231                     {
232                         *dp++ = ' ';
233                         cp++;
234                     }
235                     else if (*cp == '.')
236                     {
237                         *dp++ = ZEBRA_REPLACE_ANY;
238                         cp++;
239                     }
240                     else
241                     {
242                         *dp++ = zebra_prim(&cp);
243 #if 0
244                         logf (LOG_LOG, "  char %2X %c", dp[-1], dp[-1]);
245 #endif
246                     }
247                 *dp = '\0';
248             }
249             if (argc >= 3)
250             {
251                 char *cp = argv[2];
252                 char *dp = token->token_to =
253                     nmem_malloc (zms->nmem, strlen(cp)+1);
254                 while (*cp)
255                     if (*cp == '$')
256                     {
257                         *dp++ = ' ';
258                         cp++;
259                     }
260                     else
261                         *dp++ = zebra_prim(&cp);
262                 *dp = '\0';
263             }
264             else
265                 token->token_to = 0;
266         }
267     }
268     if (zm)
269         (*zm)->next = NULL;
270     fclose (f);
271
272     for (zp = zms->map_list; zp; zp = zp->next)
273         zms->lookup_array[zp->reg_id] = zp;
274 }
275
276 static void zms_map_handle (void *p, const char *name, const char *value)
277 {
278     ZebraMaps zms = (ZebraMaps) p;
279     
280     zebra_map_read (zms, value);
281 }
282
283 ZebraMaps zebra_maps_open (Res res, const char *base)
284 {
285     ZebraMaps zms = (ZebraMaps) xmalloc (sizeof(*zms));
286     int i;
287
288     zms->nmem = nmem_create ();
289     zms->tabpath = nmem_strdup (zms->nmem,
290                                 res_get_def (res, "profilePath", "."));
291     zms->tabroot = 0;
292     if (base)
293         zms->tabroot = nmem_strdup (zms->nmem, base);
294     zms->map_list = NULL;
295
296     zms->temp_map_str[0] = '\0';
297     zms->temp_map_str[1] = '\0';
298
299     zms->temp_map_ptr[0] = zms->temp_map_str;
300     zms->temp_map_ptr[1] = NULL;
301
302     zms->lookup_array = (struct zebra_map**)
303         nmem_malloc (zms->nmem, sizeof(*zms->lookup_array)*256);
304     for (i = 0; i<256; i++)
305         zms->lookup_array[i] = 0;
306     if (!res || !res_trav (res, "index", zms, zms_map_handle))
307         zebra_map_read (zms, "default.idx");
308
309     zms->wrbuf_1 = wrbuf_alloc();
310     zms->wrbuf_2 = wrbuf_alloc();
311
312     return zms;
313 }
314
315 struct zebra_map *zebra_map_get (ZebraMaps zms, unsigned reg_id)
316 {
317     return zms->lookup_array[reg_id];
318 }
319
320 chrmaptab zebra_charmap_get (ZebraMaps zms, unsigned reg_id)
321 {
322     struct zebra_map *zm = zebra_map_get (zms, reg_id);
323     if (!zm)
324     {
325         zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(*zm));
326         logf (LOG_WARN, "Unknown register type: %c", reg_id);
327
328         zm->reg_id = reg_id;
329         zm->maptab_name = nmem_strdup (zms->nmem, "@");
330         zm->maptab = NULL;
331         zm->type = ZEBRA_MAP_TYPE_INDEX;
332         zm->completeness = 0;
333         zm->next = zms->map_list;
334         zms->map_list = zm->next;
335
336         zms->lookup_array[zm->reg_id & 255] = zm;
337     }
338     if (!zm->maptab)
339     {
340         if (!zm->maptab_name || !yaz_matchstr (zm->maptab_name, "@"))
341             return NULL;
342         if (!(zm->maptab = chrmaptab_create (zms->tabpath,
343                                              zm->maptab_name, 0,
344                                              zms->tabroot)))
345             logf(LOG_WARN, "Failed to read character table %s",
346                  zm->maptab_name);
347         else
348             logf(LOG_DEBUG, "Read character table %s", zm->maptab_name);
349     }
350     return zm->maptab;
351 }
352
353 const char **zebra_maps_input (ZebraMaps zms, unsigned reg_id,
354                                const char **from, int len)
355 {
356     chrmaptab maptab;
357
358     maptab = zebra_charmap_get (zms, reg_id);
359     if (maptab)
360         return chr_map_input(maptab, from, len);
361     
362     zms->temp_map_str[0] = **from;
363
364     (*from)++;
365     return zms->temp_map_ptr;
366 }
367
368 #if 0
369 int zebra_maps_input_tokens (ZebraMaps zms, unsigned reg_id,
370                              const char *input_str, int input_len,
371                              WRBUF wrbuf)
372 {
373     chrmaptab maptab = zebra_charmap_get (zms, reg_id);
374     int len[4];
375     char *str[3];
376     int input_i = 0;
377     int first = 1;
378     const char **out;
379         
380     if (!maptab)
381     {
382         wrbuf_write (wrbuf, input_str, input_len);
383         return -1;
384     }
385     str[0] = " ";
386     len[0] = 1;
387     str[1] = input_str;
388     len[1] = input_len;
389     str[2] = " ";
390     len[2] = 1;
391     len[3] = -1;
392     
393     out = chr_map_input (maptab, str, len);
394     while (len[1] > 0)
395     {
396         while (out && *out && **out == *CHR_SPACE)
397             out = chr_map_input (maptab, str, len);
398     }
399 }
400 #endif
401
402 const char *zebra_maps_output(ZebraMaps zms, unsigned reg_id,
403                               const char **from)
404 {
405     chrmaptab maptab = zebra_charmap_get (zms, reg_id);
406     if (!maptab)
407         return 0;
408     return chr_map_output (maptab, from, 1);
409 }
410
411
412 /* ------------------------------------ */
413
414 typedef struct {
415     int type;
416     int major;
417     int minor;
418     Z_AttributeElement **attributeList;
419     int num_attributes;
420 } AttrType;
421
422 static int attr_find (AttrType *src, oid_value *attributeSetP)
423 {
424     while (src->major < src->num_attributes)
425     {
426         Z_AttributeElement *element;
427
428         element = src->attributeList[src->major];
429         if (src->type == *element->attributeType)
430         {
431             switch (element->which) 
432             {
433             case Z_AttributeValue_numeric:
434                 ++(src->major);
435                 if (element->attributeSet && attributeSetP)
436                 {
437                     oident *attrset;
438
439                     attrset = oid_getentbyoid (element->attributeSet);
440                     *attributeSetP = attrset->value;
441                 }
442                 return *element->value.numeric;
443                 break;
444             case Z_AttributeValue_complex:
445                 if (src->minor >= element->value.complex->num_list ||
446                     element->value.complex->list[src->minor]->which !=  
447                     Z_StringOrNumeric_numeric)
448                     break;
449                 ++(src->minor);
450                 if (element->attributeSet && attributeSetP)
451                 {
452                     oident *attrset;
453
454                     attrset = oid_getentbyoid (element->attributeSet);
455                     *attributeSetP = attrset->value;
456                 }
457                 return *element->value.complex->list[src->minor-1]->u.numeric;
458             default:
459                 assert (0);
460             }
461         }
462         ++(src->major);
463     }
464     return -1;
465 }
466
467 static void attr_init_APT (AttrType *src, Z_AttributesPlusTerm *zapt, int type)
468 {
469 #ifdef ASN_COMPILED
470     src->attributeList = zapt->attributes->attributes;
471     src->num_attributes = zapt->attributes->num_attributes;
472 #else
473     src->attributeList = zapt->attributeList;
474     src->num_attributes = zapt->num_attributes;
475 #endif
476     src->type = type;
477     src->major = 0;
478     src->minor = 0;
479 }
480
481 static void attr_init_AttrList (AttrType *src, Z_AttributeList *list, int type)
482 {
483     src->attributeList = list->attributes;
484     src->num_attributes = list->num_attributes;
485     src->type = type;
486     src->major = 0;
487     src->minor = 0;
488 }
489
490 /* ------------------------------------ */
491
492 int zebra_maps_is_complete (ZebraMaps zms, unsigned reg_id)
493
494     struct zebra_map *zm = zebra_map_get (zms, reg_id);
495     if (zm)
496         return zm->completeness;
497     return 0;
498 }
499
500 int zebra_maps_is_positioned (ZebraMaps zms, unsigned reg_id)
501 {
502     struct zebra_map *zm = zebra_map_get (zms, reg_id);
503     if (zm)
504         return zm->positioned;
505     return 0;
506 }
507     
508 int zebra_maps_is_sort (ZebraMaps zms, unsigned reg_id)
509 {
510     struct zebra_map *zm = zebra_map_get (zms, reg_id);
511     if (zm)
512         return zm->type == ZEBRA_MAP_TYPE_SORT;
513     return 0;
514 }
515
516 int zebra_maps_sort (ZebraMaps zms, Z_SortAttributes *sortAttributes,
517                      int *numerical)
518 {
519     AttrType use;
520     AttrType structure;
521     int structure_value;
522     attr_init_AttrList (&use, sortAttributes->list, 1);
523     attr_init_AttrList (&structure, sortAttributes->list, 4);
524
525     *numerical = 0;
526     structure_value = attr_find (&structure, 0);
527     if (structure_value == 109)
528         *numerical = 1;
529     return attr_find (&use, NULL);
530 }
531
532 int zebra_maps_attr (ZebraMaps zms, Z_AttributesPlusTerm *zapt,
533                      unsigned *reg_id, char **search_type, char *rank_type,
534                      int *complete_flag, int *sort_flag)
535 {
536     AttrType completeness;
537     AttrType structure;
538     AttrType relation;
539     AttrType sort_relation;
540     AttrType weight;
541     int completeness_value;
542     int structure_value;
543     int relation_value;
544     int sort_relation_value;
545     int weight_value;
546
547     attr_init_APT (&structure, zapt, 4);
548     attr_init_APT (&completeness, zapt, 6);
549     attr_init_APT (&relation, zapt, 2);
550     attr_init_APT (&sort_relation, zapt, 7);
551     attr_init_APT (&weight, zapt, 9);
552
553     completeness_value = attr_find (&completeness, NULL);
554     structure_value = attr_find (&structure, NULL);
555     relation_value = attr_find (&relation, NULL);
556     sort_relation_value = attr_find (&sort_relation, NULL);
557     weight_value = attr_find (&weight, NULL);
558
559     if (completeness_value == 2 || completeness_value == 3)
560         *complete_flag = 1;
561     else
562         *complete_flag = 0;
563     *reg_id = 0;
564
565     *sort_flag = (sort_relation_value > 0) ? 1 : 0;
566     *search_type = "phrase";
567     strcpy (rank_type, "void");
568     if (relation_value == 102)
569     {
570         if (weight_value == -1)
571             weight_value = 34;
572         sprintf (rank_type, "rank,%d", weight_value);
573     }    
574     if (*complete_flag)
575         *reg_id = 'p';
576     else
577         *reg_id = 'w';
578     switch (structure_value)
579     {
580     case 6:   /* word list */
581         *search_type = "and-list";
582         break;
583     case 105: /* free-form-text */
584         *search_type = "or-list";
585         break;
586     case 106: /* document-text */
587         *search_type = "or-list";
588         break;  
589     case -1:
590     case 1:   /* phrase */
591     case 2:   /* word */
592     case 108: /* string */ 
593         *search_type = "phrase";
594         break;
595     case 107: /* local-number */
596         *search_type = "local";
597         *reg_id = 0;
598         break;
599     case 109: /* numeric string */
600         *reg_id = 'n';
601         *search_type = "numeric";
602         break;
603     case 104: /* urx */
604         *reg_id = 'u';
605         *search_type = "phrase";
606         break;
607     case 3:   /* key */
608         *reg_id = '0';
609         *search_type = "phrase";
610         break;
611     case 4:  /* year */
612         *reg_id = 'y';
613         *search_type = "phrase";
614         break;
615     case 5:  /* date */
616         *reg_id = 'd';
617         *search_type = "phrase";
618         break;
619     default:
620         return -1;
621     }
622     return 0;
623 }
624
625 int zebra_replace_sub(ZebraMaps zms, unsigned reg_id, const char *ex_list,
626                       const char *input_str, int input_len, WRBUF wrbuf);
627
628 WRBUF zebra_replace(ZebraMaps zms, unsigned reg_id, const char *ex_list,
629                     const char *input_str, int input_len)
630 {
631     struct zebra_map *zm = zebra_map_get (zms, reg_id);
632
633     wrbuf_rewind(zms->wrbuf_1);
634     wrbuf_write(zms->wrbuf_1, input_str, input_len);
635     if (!zm || !zm->replace_tokens)
636         return zms->wrbuf_1;
637   
638 #if 0
639     logf (LOG_LOG, "in:%.*s:", wrbuf_len(zms->wrbuf_1),
640           wrbuf_buf(zms->wrbuf_1));
641 #endif
642     for (;;)
643     {
644         if (!zebra_replace_sub(zms, reg_id, ex_list, wrbuf_buf(zms->wrbuf_1),
645                                wrbuf_len(zms->wrbuf_1), zms->wrbuf_2))
646             return zms->wrbuf_2;
647         if (!zebra_replace_sub(zms, reg_id, ex_list, wrbuf_buf(zms->wrbuf_2),
648                                wrbuf_len(zms->wrbuf_2), zms->wrbuf_1))
649             return zms->wrbuf_1;
650     }
651     return 0;
652 }
653
654 int zebra_replace_sub(ZebraMaps zms, unsigned reg_id, const char *ex_list,
655                       const char *input_str, int input_len, WRBUF wrbuf)
656 {
657     int i = -1;
658     int no_replaces = 0;
659     struct zebra_map *zm = zebra_map_get (zms, reg_id);
660
661     wrbuf_rewind(wrbuf);
662     for (i = -1; i <= input_len; )
663     {
664         struct zm_token *token;
665         char replace_string[128];
666         int replace_out;
667         int replace_in = 0;
668
669         for (token = zm->replace_tokens; !replace_in && token;
670              token = token->next)
671         {
672             int j = 0;
673             int replace_done = 0;
674             replace_out = 0;
675             for (;; j++)
676             {
677                 int c;
678                 if (!token->token_from[j])
679                 {
680                     replace_in = j;
681                     break;
682                 }
683                 if (ex_list && strchr (ex_list, token->token_from[j]))
684                     break;
685                 if (i+j < 0 || j+i >= input_len)
686                     c = ' ';
687                 else
688                     c = input_str[j+i] & 255;
689                 if (token->token_from[j] == ZEBRA_REPLACE_ANY)
690                 {
691                     if (c == ' ')
692                         break;
693                     replace_string[replace_out++] = c;
694                 }
695                 else
696                 {
697                     if (c != token->token_from[j])
698                     {
699                         break;
700                     }
701                     if (!replace_done)
702                     {
703                         const char *cp = token->token_to;
704                         replace_done = 1;
705                         for (; cp && *cp; cp++)
706                             replace_string[replace_out++] = *cp;
707                     }
708                 }
709             }
710         }
711         if (!replace_in)
712         {
713             if (i >= 0 && i < input_len)
714                 wrbuf_putc(wrbuf, input_str[i]);
715             i++;
716         }
717         else
718         {
719             no_replaces++;
720             if (replace_out)
721                 wrbuf_write(wrbuf, replace_string, replace_out);
722             i += replace_in;
723         }
724     }
725 #if 0
726     logf (LOG_LOG, "out:%.*s:", wrbuf_len(wrbuf), wrbuf_buf(wrbuf));
727 #endif
728     return no_replaces;
729 }