Fixed bug #685: Optimize xelm/melm matching. Indexing the Koha collection
[idzebra-moved-to-github.git] / util / zebramap.c
1 /* $Id: zebramap.c,v 1.32.2.6 2006-08-14 10:39:25 adam Exp $
2    Copyright (C) 1995-2005
3    Index Data ApS
4
5 This file is part of the Zebra server.
6
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18     along with this program; if not, write to the Free Software
19     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
21
22 */
23
24 #include <assert.h>
25 #include <stdlib.h>
26 #include <ctype.h>
27
28 #include <charmap.h>
29 #include <yaz/yaz-util.h>
30
31 #include <zebramap.h>
32
33 #define ZEBRA_MAP_TYPE_SORT  1
34 #define ZEBRA_MAP_TYPE_INDEX 2
35
36 #define ZEBRA_REPLACE_ANY  300
37
38 struct zm_token {
39     int *token_from;
40     char *token_to;
41     int token_min;
42     struct zm_token *next;
43 };
44
45 struct zebra_map {
46     unsigned reg_id;
47     int completeness;
48     int positioned;
49     int type;
50     union {
51         struct {
52             int dummy;
53         } index;
54         struct {
55             int entry_size;
56         } sort;
57     } u;
58     chrmaptab maptab;
59     const char *maptab_name;
60     struct zebra_map *next;
61     struct zm_token *replace_tokens;
62 };
63
64 struct zebra_maps {
65     char *tabpath;
66     char *tabroot;
67     NMEM nmem;
68     struct zebra_map *map_list;
69     char temp_map_str[2];
70     const char *temp_map_ptr[2];
71     struct zebra_map **lookup_array;
72     WRBUF wrbuf_1, wrbuf_2;
73 };
74
75 void zebra_maps_close (ZebraMaps zms)
76 {
77     struct zebra_map *zm = zms->map_list;
78     while (zm)
79     {
80         if (zm->maptab)
81             chrmaptab_destroy (zm->maptab);
82         zm = zm->next;
83     }
84     wrbuf_free (zms->wrbuf_1, 1);
85     wrbuf_free (zms->wrbuf_2, 1);
86     nmem_destroy (zms->nmem);
87     xfree (zms);
88 }
89
90 static void zebra_map_read (ZebraMaps zms, const char *name)
91 {
92     FILE *f;
93     char line[512];
94     char *argv[10];
95     int argc;
96     int lineno = 0;
97     struct zebra_map **zm = 0, *zp;
98
99     if (!(f = yaz_fopen(zms->tabpath, name, "r", zms->tabroot)))
100     {
101         yaz_log(LOG_WARN|LOG_ERRNO, "%s", name);
102         return ;
103     }
104     while ((argc = readconf_line(f, &lineno, line, 512, argv, 10)))
105     {
106         if (!yaz_matchstr (argv[0], "index") && argc == 2)
107         {
108             if (!zm)
109                 zm = &zms->map_list;
110             else
111                 zm = &(*zm)->next;
112             *zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(**zm));
113             (*zm)->reg_id = argv[1][0];
114             (*zm)->maptab_name = NULL;
115             (*zm)->maptab = NULL;
116             (*zm)->type = ZEBRA_MAP_TYPE_INDEX;
117             (*zm)->completeness = 0;
118             (*zm)->positioned = 1;
119             (*zm)->replace_tokens = 0;
120         }
121         else if (!yaz_matchstr (argv[0], "sort") && argc == 2)
122         {
123             if (!zm)
124                 zm = &zms->map_list;
125             else
126                 zm = &(*zm)->next;
127             *zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(**zm));
128             (*zm)->reg_id = argv[1][0];
129             (*zm)->maptab_name = NULL;
130             (*zm)->type = ZEBRA_MAP_TYPE_SORT;
131             (*zm)->u.sort.entry_size = 80;
132             (*zm)->maptab = NULL;
133             (*zm)->completeness = 0;
134             (*zm)->positioned = 0;
135             (*zm)->replace_tokens = 0;
136         }
137         else if (zm && !yaz_matchstr (argv[0], "charmap") && argc == 2)
138         {
139             (*zm)->maptab_name = nmem_strdup (zms->nmem, argv[1]);
140         }
141         else if (zm && !yaz_matchstr (argv[0], "completeness") && argc == 2)
142         {
143             (*zm)->completeness = atoi (argv[1]);
144         }
145         else if (zm && !yaz_matchstr (argv[0], "position") && argc == 2)
146         {
147             (*zm)->positioned = atoi (argv[1]);
148         }
149         else if (zm && !yaz_matchstr (argv[0], "entrysize") && argc == 2)
150         {
151             if ((*zm)->type == ZEBRA_MAP_TYPE_SORT)
152                 (*zm)->u.sort.entry_size = atoi (argv[1]);
153         }
154         else if (zm && !yaz_matchstr (argv[0], "replace") && argc >= 2)
155         {
156             struct zm_token *token = nmem_malloc (zms->nmem, sizeof(*token));
157             token->next = (*zm)->replace_tokens;
158             (*zm)->replace_tokens = token;
159 #if 0
160             yaz_log (LOG_LOG, "replace %s", argv[1]);
161 #endif
162             token->token_from = 0;
163             if (argc >= 2)
164             {
165                 char *cp = argv[1];
166                 int *dp = token->token_from = (int *)
167                     nmem_malloc (zms->nmem, (1+strlen(cp))*sizeof(int));
168                 while (*cp)
169                     if (*cp == '$')
170                     {
171                         *dp++ = ' ';
172                         cp++;
173                     }
174                     else if (*cp == '.')
175                     {
176                         *dp++ = ZEBRA_REPLACE_ANY;
177                         cp++;
178                     }
179                     else
180                     {
181                         *dp++ = zebra_prim(&cp);
182 #if 0
183                         yaz_log (LOG_LOG, "  char %2X %c", dp[-1], dp[-1]);
184 #endif
185                     }
186                 *dp = '\0';
187             }
188             if (argc >= 3)
189             {
190                 char *cp = argv[2];
191                 char *dp = token->token_to =
192                     nmem_malloc (zms->nmem, strlen(cp)+1);
193                 while (*cp)
194                     if (*cp == '$')
195                     {
196                         *dp++ = ' ';
197                         cp++;
198                     }
199                     else
200                         *dp++ = zebra_prim(&cp);
201                 *dp = '\0';
202             }
203             else
204                 token->token_to = 0;
205         }
206     }
207     if (zm)
208         (*zm)->next = NULL;
209     yaz_fclose (f);
210
211     for (zp = zms->map_list; zp; zp = zp->next)
212         zms->lookup_array[zp->reg_id] = zp;
213 }
214
215 static void zms_map_handle (void *p, const char *name, const char *value)
216 {
217     ZebraMaps zms = (ZebraMaps) p;
218     
219     zebra_map_read (zms, value);
220 }
221
222 ZebraMaps zebra_maps_open (Res res, const char *base)
223 {
224     ZebraMaps zms = (ZebraMaps) xmalloc (sizeof(*zms));
225     int i;
226
227     zms->nmem = nmem_create ();
228     zms->tabpath = nmem_strdup (zms->nmem,
229                                 res_get_def (res, "profilePath",
230                                              DEFAULT_PROFILE_PATH));
231     zms->tabroot = 0;
232     if (base)
233         zms->tabroot = nmem_strdup (zms->nmem, base);
234     zms->map_list = NULL;
235
236     zms->temp_map_str[0] = '\0';
237     zms->temp_map_str[1] = '\0';
238
239     zms->temp_map_ptr[0] = zms->temp_map_str;
240     zms->temp_map_ptr[1] = NULL;
241
242     zms->lookup_array = (struct zebra_map**)
243         nmem_malloc (zms->nmem, sizeof(*zms->lookup_array)*256);
244     for (i = 0; i<256; i++)
245         zms->lookup_array[i] = 0;
246     if (!res || !res_trav (res, "index", zms, zms_map_handle))
247         zebra_map_read (zms, "default.idx");
248
249     zms->wrbuf_1 = wrbuf_alloc();
250     zms->wrbuf_2 = wrbuf_alloc();
251
252     return zms;
253 }
254
255 struct zebra_map *zebra_map_get (ZebraMaps zms, unsigned reg_id)
256 {
257     return zms->lookup_array[reg_id];
258 }
259
260 chrmaptab zebra_charmap_get (ZebraMaps zms, unsigned reg_id)
261 {
262     struct zebra_map *zm = zebra_map_get (zms, reg_id);
263     if (!zm)
264     {
265         zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(*zm));
266         yaz_log (LOG_WARN, "Unknown register type: %c", reg_id);
267
268         zm->reg_id = reg_id;
269         zm->maptab_name = nmem_strdup (zms->nmem, "@");
270         zm->maptab = NULL;
271         zm->type = ZEBRA_MAP_TYPE_INDEX;
272         zm->completeness = 0;
273         zm->next = zms->map_list;
274         zm->replace_tokens = 0;
275         zms->map_list = zm->next;
276
277         zms->lookup_array[zm->reg_id & 255] = zm;
278     }
279     if (!zm->maptab)
280     {
281         if (!zm->maptab_name || !strcmp (zm->maptab_name, "@"))
282             return NULL;
283         if (!(zm->maptab = chrmaptab_create (zms->tabpath,
284                                              zm->maptab_name, 0,
285                                              zms->tabroot)))
286             yaz_log(LOG_WARN, "Failed to read character table %s",
287                  zm->maptab_name);
288         else
289             yaz_log(LOG_DEBUG, "Read character table %s", zm->maptab_name);
290     }
291     return zm->maptab;
292 }
293
294 const char **zebra_maps_input (ZebraMaps zms, unsigned reg_id,
295                                const char **from, int len, int first)
296 {
297     chrmaptab maptab;
298
299     maptab = zebra_charmap_get (zms, reg_id);
300     if (maptab)
301         return chr_map_input(maptab, from, len, first);
302     
303     zms->temp_map_str[0] = **from;
304
305     (*from)++;
306     return zms->temp_map_ptr;
307 }
308
309 const char **zebra_maps_search(ZebraMaps zms, unsigned reg_id,
310                                const char **from, int len,  int *q_map_match)
311 {
312     chrmaptab maptab;
313     
314     *q_map_match = 0;
315     maptab = zebra_charmap_get (zms, reg_id);
316     if (maptab)
317     {
318         const char **map;
319         map = chr_map_q_input(maptab, from, len, 0);
320         if (map && map[0])
321         {
322             *q_map_match = 1;
323             return map;
324         }
325         map = chr_map_input(maptab, from, len, 0);
326         if (map)
327             return map;
328     }
329     zms->temp_map_str[0] = **from;
330
331     (*from)++;
332     return zms->temp_map_ptr;
333 }
334
335 const char *zebra_maps_output(ZebraMaps zms, unsigned reg_id,
336                               const char **from)
337 {
338     chrmaptab maptab = zebra_charmap_get (zms, reg_id);
339     if (!maptab)
340         return 0;
341     return chr_map_output (maptab, from, 1);
342 }
343
344
345 /* ------------------------------------ */
346
347 typedef struct {
348     int type;
349     int major;
350     int minor;
351     Z_AttributeElement **attributeList;
352     int num_attributes;
353 } AttrType;
354
355 static int attr_find (AttrType *src, oid_value *attributeSetP)
356 {
357     while (src->major < src->num_attributes)
358     {
359         Z_AttributeElement *element;
360
361         element = src->attributeList[src->major];
362         if (src->type == *element->attributeType)
363         {
364             switch (element->which) 
365             {
366             case Z_AttributeValue_numeric:
367                 ++(src->major);
368                 if (element->attributeSet && attributeSetP)
369                 {
370                     oident *attrset;
371
372                     attrset = oid_getentbyoid (element->attributeSet);
373                     *attributeSetP = attrset->value;
374                 }
375                 return *element->value.numeric;
376                 break;
377             case Z_AttributeValue_complex:
378                 if (src->minor >= element->value.complex->num_list ||
379                     element->value.complex->list[src->minor]->which !=  
380                     Z_StringOrNumeric_numeric)
381                     break;
382                 ++(src->minor);
383                 if (element->attributeSet && attributeSetP)
384                 {
385                     oident *attrset;
386
387                     attrset = oid_getentbyoid (element->attributeSet);
388                     *attributeSetP = attrset->value;
389                 }
390                 return *element->value.complex->list[src->minor-1]->u.numeric;
391             default:
392                 assert (0);
393             }
394         }
395         ++(src->major);
396     }
397     return -1;
398 }
399
400 static void attr_init_APT (AttrType *src, Z_AttributesPlusTerm *zapt, int type)
401 {
402     src->attributeList = zapt->attributes->attributes;
403     src->num_attributes = zapt->attributes->num_attributes;
404     src->type = type;
405     src->major = 0;
406     src->minor = 0;
407 }
408
409 static void attr_init_AttrList (AttrType *src, Z_AttributeList *list, int type)
410 {
411     src->attributeList = list->attributes;
412     src->num_attributes = list->num_attributes;
413     src->type = type;
414     src->major = 0;
415     src->minor = 0;
416 }
417
418 /* ------------------------------------ */
419
420 int zebra_maps_is_complete (ZebraMaps zms, unsigned reg_id)
421
422     struct zebra_map *zm = zebra_map_get (zms, reg_id);
423     if (zm)
424         return zm->completeness;
425     return 0;
426 }
427
428 int zebra_maps_is_positioned (ZebraMaps zms, unsigned reg_id)
429 {
430     struct zebra_map *zm = zebra_map_get (zms, reg_id);
431     if (zm)
432         return zm->positioned;
433     return 0;
434 }
435     
436 int zebra_maps_is_sort (ZebraMaps zms, unsigned reg_id)
437 {
438     struct zebra_map *zm = zebra_map_get (zms, reg_id);
439     if (zm)
440         return zm->type == ZEBRA_MAP_TYPE_SORT;
441     return 0;
442 }
443
444 int zebra_maps_sort (ZebraMaps zms, Z_SortAttributes *sortAttributes,
445                      int *numerical)
446 {
447     AttrType use;
448     AttrType structure;
449     int structure_value;
450     attr_init_AttrList (&use, sortAttributes->list, 1);
451     attr_init_AttrList (&structure, sortAttributes->list, 4);
452
453     *numerical = 0;
454     structure_value = attr_find (&structure, 0);
455     if (structure_value == 109)
456         *numerical = 1;
457     return attr_find (&use, NULL);
458 }
459
460 int zebra_maps_attr (ZebraMaps zms, Z_AttributesPlusTerm *zapt,
461                      unsigned *reg_id, char **search_type, char *rank_type,
462                      int *complete_flag, int *sort_flag)
463 {
464     AttrType completeness;
465     AttrType structure;
466     AttrType relation;
467     AttrType sort_relation;
468     AttrType weight;
469     AttrType use;
470     int completeness_value;
471     int structure_value;
472     int relation_value;
473     int sort_relation_value;
474     int weight_value;
475     int use_value;
476
477     attr_init_APT (&structure, zapt, 4);
478     attr_init_APT (&completeness, zapt, 6);
479     attr_init_APT (&relation, zapt, 2);
480     attr_init_APT (&sort_relation, zapt, 7);
481     attr_init_APT (&weight, zapt, 9);
482     attr_init_APT (&use, zapt, 1);
483
484     completeness_value = attr_find (&completeness, NULL);
485     structure_value = attr_find (&structure, NULL);
486     relation_value = attr_find (&relation, NULL);
487     sort_relation_value = attr_find (&sort_relation, NULL);
488     weight_value = attr_find (&weight, NULL);
489     use_value = attr_find(&use, NULL);
490
491     if (completeness_value == 2 || completeness_value == 3)
492         *complete_flag = 1;
493     else
494         *complete_flag = 0;
495     *reg_id = 0;
496
497     *sort_flag = (sort_relation_value > 0) ? 1 : 0;
498     *search_type = "phrase";
499     strcpy (rank_type, "void");
500     if (relation_value == 102)
501     {
502         if (weight_value == -1)
503             weight_value = 34;
504         sprintf (rank_type, "rank,w=%d,u=%d", weight_value, use_value);
505     }
506     if (relation_value == 103)
507     {
508         *search_type = "always";
509         return 0;
510     }
511     if (*complete_flag)
512         *reg_id = 'p';
513     else
514         *reg_id = 'w';
515     switch (structure_value)
516     {
517     case 6:   /* word list */
518         *search_type = "and-list";
519         break;
520     case 105: /* free-form-text */
521         *search_type = "or-list";
522         break;
523     case 106: /* document-text */
524         *search_type = "or-list";
525         break;  
526     case -1:
527     case 1:   /* phrase */
528     case 2:   /* word */
529     case 108: /* string */ 
530         *search_type = "phrase";
531         break;
532     case 107: /* local-number */
533         *search_type = "local";
534         *reg_id = 0;
535         break;
536     case 109: /* numeric string */
537         *reg_id = 'n';
538         *search_type = "numeric";
539         break;
540     case 104: /* urx */
541         *reg_id = 'u';
542         *search_type = "phrase";
543         break;
544     case 3:   /* key */
545         *reg_id = '0';
546         *search_type = "phrase";
547         break;
548     case 4:  /* year */
549         *reg_id = 'y';
550         *search_type = "phrase";
551         break;
552     case 5:  /* date */
553         *reg_id = 'd';
554         *search_type = "phrase";
555         break;
556     default:
557         return -1;
558     }
559     return 0;
560 }
561
562 int zebra_replace_sub(ZebraMaps zms, unsigned reg_id, const char *ex_list,
563                       const char *input_str, int input_len, WRBUF wrbuf);
564
565 WRBUF zebra_replace(ZebraMaps zms, unsigned reg_id, const char *ex_list,
566                     const char *input_str, int input_len)
567 {
568     struct zebra_map *zm = zebra_map_get (zms, reg_id);
569
570     wrbuf_rewind(zms->wrbuf_1);
571     wrbuf_write(zms->wrbuf_1, input_str, input_len);
572     if (!zm || !zm->replace_tokens)
573         return zms->wrbuf_1;
574   
575 #if 0
576     yaz_log (LOG_LOG, "in:%.*s:", wrbuf_len(zms->wrbuf_1),
577           wrbuf_buf(zms->wrbuf_1));
578 #endif
579     for (;;)
580     {
581         if (!zebra_replace_sub(zms, reg_id, ex_list, wrbuf_buf(zms->wrbuf_1),
582                                wrbuf_len(zms->wrbuf_1), zms->wrbuf_2))
583             return zms->wrbuf_2;
584         if (!zebra_replace_sub(zms, reg_id, ex_list, wrbuf_buf(zms->wrbuf_2),
585                                wrbuf_len(zms->wrbuf_2), zms->wrbuf_1))
586             return zms->wrbuf_1;
587     }
588     return 0;
589 }
590
591 int zebra_replace_sub(ZebraMaps zms, unsigned reg_id, const char *ex_list,
592                       const char *input_str, int input_len, WRBUF wrbuf)
593 {
594     int i = -1;
595     int no_replaces = 0;
596     struct zebra_map *zm = zebra_map_get (zms, reg_id);
597
598     wrbuf_rewind(wrbuf);
599     for (i = -1; i <= input_len; )
600     {
601         struct zm_token *token;
602         char replace_string[128];
603         int replace_out = 0;
604         int replace_in = 0;
605
606         for (token = zm->replace_tokens; !replace_in && token;
607              token = token->next)
608         {
609             int j = 0;
610             int replace_done = 0;
611             replace_out = 0;
612             for (;; j++)
613             {
614                 int c;
615                 if (!token->token_from[j])
616                 {
617                     replace_in = j;
618                     break;
619                 }
620                 if (ex_list && strchr (ex_list, token->token_from[j]))
621                     break;
622                 if (i+j < 0 || j+i >= input_len)
623                     c = ' ';
624                 else
625                     c = input_str[j+i] & 255;
626                 if (token->token_from[j] == ZEBRA_REPLACE_ANY)
627                 {
628                     if (c == ' ')
629                         break;
630                     replace_string[replace_out++] = c;
631                 }
632                 else
633                 {
634                     if (c != token->token_from[j])
635                     {
636                         break;
637                     }
638                     if (!replace_done)
639                     {
640                         const char *cp = token->token_to;
641                         replace_done = 1;
642                         for (; cp && *cp; cp++)
643                             replace_string[replace_out++] = *cp;
644                     }
645                 }
646             }
647         }
648         if (!replace_in)
649         {
650             if (i >= 0 && i < input_len)
651                 wrbuf_putc(wrbuf, input_str[i]);
652             i++;
653         }
654         else
655         {
656             no_replaces++;
657             if (replace_out)
658                 wrbuf_write(wrbuf, replace_string, replace_out);
659             i += replace_in;
660         }
661     }
662 #if 0
663     yaz_log (LOG_LOG, "out:%.*s:", wrbuf_len(wrbuf), wrbuf_buf(wrbuf));
664 #endif
665     return no_replaces;
666 }