The zebramaps implements index_types functionality.
[idzebra-moved-to-github.git] / util / zebramap.c
1 /* $Id: zebramap.c,v 1.65 2007-11-06 10:30:02 adam Exp $
2    Copyright (C) 1995-2007
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 Zebra; see the file LICENSE.zebra.  If not, write to the
19    Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20    02111-1307, USA.
21 */
22
23 #include <assert.h>
24 #include <stdlib.h>
25 #include <ctype.h>
26
27 #include <charmap.h>
28 #include <attrfind.h>
29 #include <yaz/yaz-util.h>
30
31 #if HAVE_ICU
32 #include <yaz/icu_I18N.h>
33 #endif
34 #include <zebramap.h>
35
36 #define ZEBRA_MAP_TYPE_SORT  1
37 #define ZEBRA_MAP_TYPE_INDEX 2
38 #define ZEBRA_MAP_TYPE_STATICRANK 3
39
40 #define ZEBRA_REPLACE_ANY  300
41
42 struct zebra_map {
43     const char *id;
44     int completeness;
45     int positioned;
46     int alwaysmatches;
47     int first_in_field;
48     int type;
49     int use_chain;
50     union {
51         struct {
52             int entry_size;
53         } sort;
54     } u;
55     chrmaptab maptab;
56     const char *maptab_name;
57     const char *locale;
58     zebra_maps_t zebra_maps;
59 #if YAZ_HAVE_XML2
60     xmlDocPtr doc;
61 #endif
62 #if HAVE_ICU
63     struct icu_chain *icu_chain;
64 #endif
65     WRBUF simple_buf;
66     size_t simple_off;
67     struct zebra_map *next;
68 };
69
70 struct zebra_maps_s {
71     char *tabpath;
72     char *tabroot;
73     NMEM nmem;
74     char temp_map_str[2];
75     const char *temp_map_ptr[2];
76     WRBUF wrbuf_1;
77     int no_files_read;
78     zebra_map_t map_list;
79     zebra_map_t last_map;
80 };
81
82 void zebra_maps_close(zebra_maps_t zms)
83 {
84     struct zebra_map *zm = zms->map_list;
85     while (zm)
86     {
87         if (zm->maptab)
88             chrmaptab_destroy(zm->maptab);
89 #if HAVE_ICU
90         if (zm->icu_chain)
91             icu_chain_destroy(zm->icu_chain);
92 #endif
93 #if YAZ_HAVE_XML2
94         xmlFreeDoc(zm->doc);
95 #endif
96         wrbuf_destroy(zm->simple_buf);
97         zm = zm->next;
98     }
99     wrbuf_destroy(zms->wrbuf_1);
100     nmem_destroy(zms->nmem);
101     xfree(zms);
102 }
103
104 zebra_map_t zebra_add_map(zebra_maps_t zms, const char *index_type,
105                           int map_type)
106 {
107     zebra_map_t zm = (zebra_map_t) nmem_malloc(zms->nmem, sizeof(*zm));
108
109     zm->zebra_maps = zms;
110     zm->id = nmem_strdup(zms->nmem, index_type);
111     zm->maptab_name = 0;
112     zm->use_chain = 0;
113     zm->locale = 0;
114     zm->maptab = 0;
115     zm->type = map_type;
116     zm->completeness = 0;
117     zm->positioned = 0;
118     zm->alwaysmatches = 0;
119     zm->first_in_field = 0;
120
121     if (zms->last_map)
122         zms->last_map->next = zm;
123     else
124         zms->map_list = zm;
125     zms->last_map = zm;
126     zm->next = 0;
127 #if HAVE_ICU
128     zm->icu_chain = 0;
129 #endif
130 #if YAZ_HAVE_XML2
131     zm->doc = 0;
132 #endif
133     zm->simple_buf = wrbuf_alloc();
134     return zm;
135 }
136
137 static int parse_command(zebra_maps_t zms, int argc, char **argv,
138                          const char *fname, int lineno)
139 {
140     zebra_map_t zm = zms->last_map;
141     if (argc == 1)
142     {
143         yaz_log(YLOG_WARN, "%s:%d: Missing arguments for '%s'",
144                 fname, lineno, argv[0]);
145         return -1;
146     }
147     if (argc > 2)
148     {
149         yaz_log(YLOG_WARN, "%s:%d: Too many arguments for '%s'",
150                 fname, lineno, argv[0]);
151         return -1;
152     }
153     if (!yaz_matchstr(argv[0], "index"))
154     {
155         zm = zebra_add_map(zms, argv[1], ZEBRA_MAP_TYPE_INDEX);
156         zm->positioned = 1;
157     }
158     else if (!yaz_matchstr(argv[0], "sort"))
159     {
160         zm = zebra_add_map(zms, argv[1], ZEBRA_MAP_TYPE_SORT);
161         zm->u.sort.entry_size = 80;
162     }
163     else if (!yaz_matchstr(argv[0], "staticrank"))
164     {
165         zm = zebra_add_map(zms, argv[1], ZEBRA_MAP_TYPE_STATICRANK);
166         zm->completeness = 1;
167     }
168     else if (!zm)
169     {
170         yaz_log(YLOG_WARN, "%s:%d: Missing sort/index before '%s'",  
171                 fname, lineno, argv[0]);
172         return -1;
173     }
174     else if (!yaz_matchstr(argv[0], "charmap") && argc == 2)
175     {
176         if (zm->type != ZEBRA_MAP_TYPE_STATICRANK)
177             zm->maptab_name = nmem_strdup(zms->nmem, argv[1]);
178         else
179         {
180             yaz_log(YLOG_WARN|YLOG_FATAL, "%s:%d: charmap for "
181                     "staticrank is invalid", fname, lineno);
182             yaz_log(YLOG_LOG, "Type is %d", zm->type);
183             return -1;
184         }
185     }
186     else if (!yaz_matchstr(argv[0], "completeness") && argc == 2)
187     {
188         zm->completeness = atoi(argv[1]);
189     }
190     else if (!yaz_matchstr(argv[0], "position") && argc == 2)
191     {
192         zm->positioned = atoi(argv[1]);
193     }
194     else if (!yaz_matchstr(argv[0], "alwaysmatches") && argc == 2)
195     {
196         if (zm->type != ZEBRA_MAP_TYPE_STATICRANK)
197             zm->alwaysmatches = atoi(argv[1]);
198         else
199         {
200             yaz_log(YLOG_WARN|YLOG_FATAL, "%s:%d: alwaysmatches for "
201                     "staticrank is invalid", fname, lineno);
202             return -1;
203         }
204     }
205     else if (!yaz_matchstr(argv[0], "firstinfield") && argc == 2)
206     {
207         zm->first_in_field = atoi(argv[1]);
208     }
209     else if (!yaz_matchstr(argv[0], "entrysize") && argc == 2)
210     {
211         if (zm->type == ZEBRA_MAP_TYPE_SORT)
212             zm->u.sort.entry_size = atoi(argv[1]);
213         else
214         {
215             yaz_log(YLOG_WARN, 
216                     "%s:%d: entrysize only valid in sort section",  
217                     fname, lineno);
218             return -1;
219         }
220     }
221     else if (!yaz_matchstr(argv[0], "locale"))
222     {
223         zm->locale = nmem_strdup(zms->nmem, argv[1]);
224     }
225     else if (!yaz_matchstr(argv[0], "simplechain"))
226     {
227         zm->use_chain = 1;
228         zm->icu_chain = 0;
229     }
230     else if (!yaz_matchstr(argv[0], "icuchain"))
231     {
232 #if YAZ_HAVE_XML2
233         if (!zm->locale)
234         {
235             yaz_log(YLOG_WARN, "%s:%d: locale required before icuchain", 
236                     fname, lineno);
237             return -1;
238         }
239         zm->doc = xmlParseFile(argv[1]);
240         if (!zm->doc)
241         {
242             yaz_log(YLOG_WARN, "%s:%d: Could not load icuchain config '%s'",
243                     fname, lineno, argv[1]);
244             return -1;
245         }
246         else
247         {
248 #if HAVE_ICU
249             UErrorCode status;
250             xmlNode *xml_node = xmlDocGetRootElement(zm->doc);
251             zm->icu_chain = 
252                 icu_chain_xml_config(xml_node, zm->locale, 
253                                      zm->type == ZEBRA_MAP_TYPE_SORT,
254                                      &status);
255             if (!zm->icu_chain)
256             {
257                 yaz_log(YLOG_WARN, "%s:%d: Failed to load ICU chain %s",
258                         fname, lineno, argv[1]);
259             }
260             zm->use_chain = 1;
261 #else
262             yaz_log(YLOG_WARN, "%s:%d: ICU support unavailable",
263                     fname, lineno);
264             return -1;
265 #endif
266         }
267 #else
268         yaz_log(YLOG_WARN, "%s:%d: XML support unavailable",
269                 fname, lineno);
270         return -1;
271 #endif
272     }
273     else
274     {
275         yaz_log(YLOG_WARN, "%s:%d: Unrecognized directive '%s'",  
276                 fname, lineno, argv[0]);
277         return -1;
278     }
279     return 0;
280 }
281
282 ZEBRA_RES zebra_maps_read_file(zebra_maps_t zms, const char *fname)
283 {
284     FILE *f;
285     char line[512];
286     char *argv[10];
287     int argc;
288     int lineno = 0;
289     int failures = 0;
290
291     if (!(f = yaz_fopen(zms->tabpath, fname, "r", zms->tabroot)))
292     {
293         yaz_log(YLOG_ERRNO|YLOG_FATAL, "%s", fname);
294         return ZEBRA_FAIL;
295     }
296     while ((argc = readconf_line(f, &lineno, line, 512, argv, 10)))
297     {
298         int r = parse_command(zms, argc, argv, fname, lineno);
299         if (r)
300             failures++;
301     }
302     yaz_fclose(f);
303
304     if (failures)
305         return ZEBRA_FAIL;
306
307     (zms->no_files_read)++;
308     return ZEBRA_OK;
309 }
310
311 zebra_maps_t zebra_maps_open(Res res, const char *base_path,
312                              const char *profile_path)
313 {
314     zebra_maps_t zms = (zebra_maps_t) xmalloc(sizeof(*zms));
315
316     zms->nmem = nmem_create();
317     zms->tabpath = profile_path ? nmem_strdup(zms->nmem, profile_path) : 0;
318     zms->tabroot = 0;
319     if (base_path)
320         zms->tabroot = nmem_strdup(zms->nmem, base_path);
321     zms->map_list = 0;
322     zms->last_map = 0;
323
324     zms->temp_map_str[0] = '\0';
325     zms->temp_map_str[1] = '\0';
326
327     zms->temp_map_ptr[0] = zms->temp_map_str;
328     zms->temp_map_ptr[1] = NULL;
329
330     zms->wrbuf_1 = wrbuf_alloc();
331
332     zms->no_files_read = 0;
333     return zms;
334 }
335
336 zebra_map_t zebra_map_get(zebra_maps_t zms, const char *id)
337 {
338     zebra_map_t zm;
339     for (zm = zms->map_list; zm; zm = zm->next)
340         if (!strcmp(zm->id, id))
341             break;
342     return zm;
343 }
344
345 zebra_map_t zebra_map_get_or_add(zebra_maps_t zms, const char *id)
346 {
347     struct zebra_map *zm = zebra_map_get(zms, id);
348     if (!zm)
349     {
350         zm = zebra_add_map(zms, id, ZEBRA_MAP_TYPE_INDEX);
351         
352         /* no reason to warn if no maps are read from file */
353         if (zms->no_files_read)
354             yaz_log(YLOG_WARN, "Unknown register type: %s", id);
355
356         zm->maptab_name = nmem_strdup(zms->nmem, "@");
357         zm->completeness = 0;
358         zm->positioned = 1;
359     }
360     return zm;
361 }
362
363 chrmaptab zebra_charmap_get(zebra_map_t zm)
364 {
365     if (!zm->maptab)
366     {
367         if (!zm->maptab_name || !yaz_matchstr(zm->maptab_name, "@"))
368             return NULL;
369         if (!(zm->maptab = chrmaptab_create(zm->zebra_maps->tabpath,
370                                             zm->maptab_name,
371                                             zm->zebra_maps->tabroot)))
372             yaz_log(YLOG_WARN, "Failed to read character table %s",
373                     zm->maptab_name);
374         else
375             yaz_log(YLOG_DEBUG, "Read character table %s", zm->maptab_name);
376     }
377     return zm->maptab;
378 }
379
380 const char **zebra_maps_input(zebra_map_t zm,
381                               const char **from, int len, int first)
382 {
383     chrmaptab maptab = zebra_charmap_get(zm);
384     if (maptab)
385         return chr_map_input(maptab, from, len, first);
386     
387     zm->zebra_maps->temp_map_str[0] = **from;
388
389     (*from)++;
390     return zm->zebra_maps->temp_map_ptr;
391 }
392
393 const char **zebra_maps_search(zebra_map_t zm,
394                                const char **from, int len,  int *q_map_match)
395 {
396     chrmaptab maptab;
397     
398     *q_map_match = 0;
399     maptab = zebra_charmap_get(zm);
400     if (maptab)
401     {
402         const char **map;
403         map = chr_map_q_input(maptab, from, len, 0);
404         if (map && map[0])
405         {
406             *q_map_match = 1;
407             return map;
408         }
409         map = chr_map_input(maptab, from, len, 0);
410         if (map)
411             return map;
412     }
413     zm->zebra_maps->temp_map_str[0] = **from;
414
415     (*from)++;
416     return zm->zebra_maps->temp_map_ptr;
417 }
418
419 const char *zebra_maps_output(zebra_map_t zm,
420                               const char **from)
421 {
422     chrmaptab maptab = zebra_charmap_get(zm);
423     if (!maptab)
424         return 0;
425     return chr_map_output(maptab, from, 1);
426 }
427
428
429 /* ------------------------------------ */
430
431 int zebra_maps_is_complete(zebra_map_t zm)
432
433     if (zm)
434         return zm->completeness;
435     return 0;
436 }
437
438 int zebra_maps_is_positioned(zebra_map_t zm)
439 {
440     if (zm)
441         return zm->positioned;
442     return 0;
443 }
444
445 int zebra_maps_is_index(zebra_map_t zm)
446 {
447     if (zm)
448         return zm->type == ZEBRA_MAP_TYPE_INDEX;
449     return 0;
450 }
451
452 int zebra_maps_is_staticrank(zebra_map_t zm)
453 {
454     if (zm)
455         return zm->type == ZEBRA_MAP_TYPE_STATICRANK;
456     return 0;
457 }
458     
459 int zebra_maps_is_sort(zebra_map_t zm)
460 {
461     if (zm)
462         return zm->type == ZEBRA_MAP_TYPE_SORT;
463     return 0;
464 }
465
466 int zebra_maps_is_alwaysmatches(zebra_map_t zm)
467 {
468     if (zm)
469         return zm->alwaysmatches;
470     return 0;
471 }
472
473 int zebra_maps_is_first_in_field(zebra_map_t zm)
474 {
475     if (zm)
476         return zm->first_in_field;
477     return 0;
478 }
479
480 int zebra_maps_sort(zebra_maps_t zms, Z_SortAttributes *sortAttributes,
481                     int *numerical)
482 {
483     AttrType use;
484     AttrType structure;
485     int structure_value;
486     attr_init_AttrList(&use, sortAttributes->list, 1);
487     attr_init_AttrList(&structure, sortAttributes->list, 4);
488
489     *numerical = 0;
490     structure_value = attr_find(&structure, 0);
491     if (structure_value == 109)
492         *numerical = 1;
493     return attr_find(&use, NULL);
494 }
495
496 int zebra_maps_attr(zebra_maps_t zms, Z_AttributesPlusTerm *zapt,
497                     const char **index_type, char **search_type, char *rank_type,
498                     int *complete_flag, int *sort_flag)
499 {
500     AttrType completeness;
501     AttrType structure;
502     AttrType relation;
503     AttrType sort_relation;
504     AttrType weight;
505     AttrType use;
506     int completeness_value;
507     int structure_value;
508     const char *structure_str = 0;
509     int relation_value;
510     int sort_relation_value;
511     int weight_value;
512     int use_value;
513
514     attr_init_APT(&structure, zapt, 4);
515     attr_init_APT(&completeness, zapt, 6);
516     attr_init_APT(&relation, zapt, 2);
517     attr_init_APT(&sort_relation, zapt, 7);
518     attr_init_APT(&weight, zapt, 9);
519     attr_init_APT(&use, zapt, 1);
520
521     completeness_value = attr_find(&completeness, NULL);
522     structure_value = attr_find_ex(&structure, NULL, &structure_str);
523     relation_value = attr_find(&relation, NULL);
524     sort_relation_value = attr_find(&sort_relation, NULL);
525     weight_value = attr_find(&weight, NULL);
526     use_value = attr_find(&use, NULL);
527
528     if (completeness_value == 2 || completeness_value == 3)
529         *complete_flag = 1;
530     else
531         *complete_flag = 0;
532     *index_type = 0;
533
534     *sort_flag =(sort_relation_value > 0) ? 1 : 0;
535     *search_type = "phrase";
536     strcpy(rank_type, "void");
537     if (relation_value == 102)
538     {
539         if (weight_value == -1)
540             weight_value = 34;
541         sprintf(rank_type, "rank,w=%d,u=%d", weight_value, use_value);
542     }
543     if (*complete_flag)
544         *index_type = "p";
545     else
546         *index_type = "w";
547     switch (structure_value)
548     {
549     case 6:   /* word list */
550         *search_type = "and-list";
551         break;
552     case 105: /* free-form-text */
553         *search_type = "or-list";
554         break;
555     case 106: /* document-text */
556         *search_type = "or-list";
557         break;  
558     case -1:
559     case 1:   /* phrase */
560     case 2:   /* word */
561     case 108: /* string */ 
562         *search_type = "phrase";
563         break;
564     case 107: /* local-number */
565         *search_type = "local";
566         *index_type = 0;
567         break;
568     case 109: /* numeric string */
569         *index_type = "n";
570         *search_type = "numeric";
571         break;
572     case 104: /* urx */
573         *index_type = "u";
574         *search_type = "phrase";
575         break;
576     case 3:   /* key */
577         *index_type = "0";
578         *search_type = "phrase";
579         break;
580     case 4:  /* year */
581         *index_type = "y";
582         *search_type = "phrase";
583         break;
584     case 5:  /* date */
585         *index_type = "d";
586         *search_type = "phrase";
587         break;
588     case -2:
589         if (structure_str && *structure_str)
590             *index_type = structure_str;
591         else
592             return -1;
593         break;
594     default:
595         return -1;
596     }
597     return 0;
598 }
599
600 WRBUF zebra_replace(zebra_map_t zm, const char *ex_list,
601                     const char *input_str, int input_len)
602 {
603     wrbuf_rewind(zm->zebra_maps->wrbuf_1);
604     wrbuf_write(zm->zebra_maps->wrbuf_1, input_str, input_len);
605     return zm->zebra_maps->wrbuf_1;
606 }
607
608 #define SE_CHARS ";,.()-/?<> \r\n\t"
609
610 static int tokenize_simple(zebra_map_t zm,
611                            const char **result_buf, size_t *result_len)
612 {
613     char *buf = wrbuf_buf(zm->simple_buf);
614     size_t len = wrbuf_len(zm->simple_buf);
615     size_t i = zm->simple_off;
616     size_t start;
617
618     while (i < len && strchr(SE_CHARS, buf[i]))
619         i++;
620     start = i;
621     while (i < len && !strchr(SE_CHARS, buf[i]))
622     {
623         if (buf[i] > 32 && buf[i] < 127)
624             buf[i] = tolower(buf[i]);
625         i++;
626     }
627
628     zm->simple_off = i;
629     if (start != i)
630     {
631         *result_buf = buf + start;
632         *result_len = i - start;
633         return 1;
634     }
635     return 0;
636  }
637
638 int zebra_map_tokenize(zebra_map_t zm,
639                        const char *buf, size_t len,
640                        const char **result_buf, size_t *result_len)
641 {
642     assert(zm->use_chain);
643     if (!zm->icu_chain)
644     {
645         if (buf)
646         {
647             wrbuf_rewind(zm->simple_buf);
648             wrbuf_write(zm->simple_buf, buf, len);
649             zm->simple_off = 0;
650         }
651         return tokenize_simple(zm, result_buf, result_len);
652     }
653     return 0;
654 }
655
656 int zebra_maps_is_icu(zebra_map_t zm)
657 {
658 #if HAVE_ICU
659     return zm->use_chain;
660 #else
661     return 0;
662 #endif
663 }
664
665
666 /*
667  * Local variables:
668  * c-basic-offset: 4
669  * indent-tabs-mode: nil
670  * End:
671  * vim: shiftwidth=4 tabstop=8 expandtab
672  */
673