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