1 /* This file is part of the Zebra server.
2 Copyright (C) 1994-2009 Index Data
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 #include <yaz/yaz-util.h>
33 #define ZEBRA_MAP_TYPE_SORT 1
34 #define ZEBRA_MAP_TYPE_INDEX 2
35 #define ZEBRA_MAP_TYPE_STATICRANK 3
37 #define ZEBRA_REPLACE_ANY 300
54 const char *maptab_name;
55 zebra_maps_t zebra_maps;
60 struct icu_chain *icu_chain;
65 struct zebra_map *next;
73 const char *temp_map_ptr[2];
80 void zebra_maps_close(zebra_maps_t zms)
82 struct zebra_map *zm = zms->map_list;
86 chrmaptab_destroy(zm->maptab);
89 icu_chain_destroy(zm->icu_chain);
94 wrbuf_destroy(zm->input_str);
95 wrbuf_destroy(zm->print_str);
98 wrbuf_destroy(zms->wrbuf_1);
99 nmem_destroy(zms->nmem);
103 zebra_map_t zebra_add_map(zebra_maps_t zms, const char *index_type,
106 zebra_map_t zm = (zebra_map_t) nmem_malloc(zms->nmem, sizeof(*zm));
108 zm->zebra_maps = zms;
109 zm->id = nmem_strdup(zms->nmem, index_type);
115 zm->completeness = 0;
117 zm->alwaysmatches = 0;
118 zm->first_in_field = 0;
121 zms->last_map->next = zm;
132 zm->input_str = wrbuf_alloc();
133 zm->print_str = wrbuf_alloc();
137 static int parse_command(zebra_maps_t zms, int argc, char **argv,
138 const char *fname, int lineno)
140 zebra_map_t zm = zms->last_map;
143 yaz_log(YLOG_WARN, "%s:%d: Missing arguments for '%s'",
144 fname, lineno, argv[0]);
149 yaz_log(YLOG_WARN, "%s:%d: Too many arguments for '%s'",
150 fname, lineno, argv[0]);
153 if (!yaz_matchstr(argv[0], "index"))
155 zm = zebra_add_map(zms, argv[1], ZEBRA_MAP_TYPE_INDEX);
158 else if (!yaz_matchstr(argv[0], "sort"))
160 zm = zebra_add_map(zms, argv[1], ZEBRA_MAP_TYPE_SORT);
161 zm->u.sort.entry_size = 80;
163 else if (!yaz_matchstr(argv[0], "staticrank"))
165 zm = zebra_add_map(zms, argv[1], ZEBRA_MAP_TYPE_STATICRANK);
166 zm->completeness = 1;
170 yaz_log(YLOG_WARN, "%s:%d: Missing sort/index before '%s'",
171 fname, lineno, argv[0]);
174 else if (!yaz_matchstr(argv[0], "charmap") && argc == 2)
176 if (zm->type != ZEBRA_MAP_TYPE_STATICRANK)
177 zm->maptab_name = nmem_strdup(zms->nmem, argv[1]);
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);
186 else if (!yaz_matchstr(argv[0], "completeness") && argc == 2)
188 zm->completeness = atoi(argv[1]);
190 else if (!yaz_matchstr(argv[0], "position") && argc == 2)
192 zm->positioned = atoi(argv[1]);
194 else if (!yaz_matchstr(argv[0], "alwaysmatches") && argc == 2)
196 if (zm->type != ZEBRA_MAP_TYPE_STATICRANK)
197 zm->alwaysmatches = atoi(argv[1]);
200 yaz_log(YLOG_WARN|YLOG_FATAL, "%s:%d: alwaysmatches for "
201 "staticrank is invalid", fname, lineno);
205 else if (!yaz_matchstr(argv[0], "firstinfield") && argc == 2)
207 zm->first_in_field = atoi(argv[1]);
209 else if (!yaz_matchstr(argv[0], "entrysize") && argc == 2)
211 if (zm->type == ZEBRA_MAP_TYPE_SORT)
212 zm->u.sort.entry_size = atoi(argv[1]);
216 "%s:%d: entrysize only valid in sort section",
221 else if (!yaz_matchstr(argv[0], "simplechain"))
228 else if (!yaz_matchstr(argv[0], "icuchain"))
230 char full_path[1024];
231 if (!yaz_filepath_resolve(argv[1], zms->tabpath, zms->tabroot,
234 yaz_log(YLOG_WARN, "%s:%d: Could not locate icuchain config '%s'",
235 fname, lineno, argv[1]);
239 zm->doc = xmlParseFile(full_path);
242 yaz_log(YLOG_WARN, "%s:%d: Could not load icuchain config '%s'",
243 fname, lineno, argv[1]);
250 xmlNode *xml_node = xmlDocGetRootElement(zm->doc);
252 icu_chain_xml_config(xml_node,
253 /* not sure about sort for this function yet.. */
257 zm->type == ZEBRA_MAP_TYPE_SORT,
262 yaz_log(YLOG_WARN, "%s:%d: Failed to load ICU chain %s",
263 fname, lineno, argv[1]);
267 yaz_log(YLOG_WARN, "%s:%d: ICU support unavailable",
273 yaz_log(YLOG_WARN, "%s:%d: XML support unavailable",
278 else if (!yaz_matchstr(argv[0], "debug") && argc == 2)
280 zm->debug = atoi(argv[1]);
284 yaz_log(YLOG_WARN, "%s:%d: Unrecognized directive '%s'",
285 fname, lineno, argv[0]);
291 ZEBRA_RES zebra_maps_read_file(zebra_maps_t zms, const char *fname)
300 if (!(f = yaz_fopen(zms->tabpath, fname, "r", zms->tabroot)))
302 yaz_log(YLOG_ERRNO|YLOG_FATAL, "%s", fname);
305 while ((argc = readconf_line(f, &lineno, line, 512, argv, 10)))
307 int r = parse_command(zms, argc, argv, fname, lineno);
316 (zms->no_files_read)++;
320 zebra_maps_t zebra_maps_open(Res res, const char *base_path,
321 const char *profile_path)
323 zebra_maps_t zms = (zebra_maps_t) xmalloc(sizeof(*zms));
325 zms->nmem = nmem_create();
326 zms->tabpath = profile_path ? nmem_strdup(zms->nmem, profile_path) : 0;
329 zms->tabroot = nmem_strdup(zms->nmem, base_path);
333 zms->temp_map_str[0] = '\0';
334 zms->temp_map_str[1] = '\0';
336 zms->temp_map_ptr[0] = zms->temp_map_str;
337 zms->temp_map_ptr[1] = NULL;
339 zms->wrbuf_1 = wrbuf_alloc();
341 zms->no_files_read = 0;
345 void zebra_maps_define_default_sort(zebra_maps_t zms)
347 zebra_map_t zm = zebra_add_map(zms, "s", ZEBRA_MAP_TYPE_SORT);
348 zm->u.sort.entry_size = 80;
351 zebra_map_t zebra_map_get(zebra_maps_t zms, const char *id)
354 for (zm = zms->map_list; zm; zm = zm->next)
355 if (!strcmp(zm->id, id))
360 zebra_map_t zebra_map_get_or_add(zebra_maps_t zms, const char *id)
362 struct zebra_map *zm = zebra_map_get(zms, id);
365 zm = zebra_add_map(zms, id, ZEBRA_MAP_TYPE_INDEX);
367 /* no reason to warn if no maps are read from file */
368 if (zms->no_files_read)
369 yaz_log(YLOG_WARN, "Unknown register type: %s", id);
371 zm->maptab_name = nmem_strdup(zms->nmem, "@");
372 zm->completeness = 0;
378 chrmaptab zebra_charmap_get(zebra_map_t zm)
382 if (!zm->maptab_name || !yaz_matchstr(zm->maptab_name, "@"))
384 if (!(zm->maptab = chrmaptab_create(zm->zebra_maps->tabpath,
386 zm->zebra_maps->tabroot)))
387 yaz_log(YLOG_WARN, "Failed to read character table %s",
390 yaz_log(YLOG_DEBUG, "Read character table %s", zm->maptab_name);
395 const char **zebra_maps_input(zebra_map_t zm,
396 const char **from, int len, int first)
398 chrmaptab maptab = zebra_charmap_get(zm);
400 return chr_map_input(maptab, from, len, first);
402 zm->zebra_maps->temp_map_str[0] = **from;
405 return zm->zebra_maps->temp_map_ptr;
408 const char **zebra_maps_search(zebra_map_t zm,
409 const char **from, int len, int *q_map_match)
414 maptab = zebra_charmap_get(zm);
418 map = chr_map_q_input(maptab, from, len, 0);
424 map = chr_map_input(maptab, from, len, 0);
428 zm->zebra_maps->temp_map_str[0] = **from;
431 return zm->zebra_maps->temp_map_ptr;
434 const char *zebra_maps_output(zebra_map_t zm,
437 chrmaptab maptab = zebra_charmap_get(zm);
440 return chr_map_output(maptab, from, 1);
444 /* ------------------------------------ */
446 int zebra_maps_is_complete(zebra_map_t zm)
449 return zm->completeness;
453 int zebra_maps_is_positioned(zebra_map_t zm)
456 return zm->positioned;
460 int zebra_maps_is_index(zebra_map_t zm)
463 return zm->type == ZEBRA_MAP_TYPE_INDEX;
467 int zebra_maps_is_staticrank(zebra_map_t zm)
470 return zm->type == ZEBRA_MAP_TYPE_STATICRANK;
474 int zebra_maps_is_sort(zebra_map_t zm)
477 return zm->type == ZEBRA_MAP_TYPE_SORT;
481 int zebra_maps_is_alwaysmatches(zebra_map_t zm)
484 return zm->alwaysmatches;
488 int zebra_maps_is_first_in_field(zebra_map_t zm)
491 return zm->first_in_field;
495 int zebra_maps_sort(zebra_maps_t zms, Z_SortAttributes *sortAttributes,
501 attr_init_AttrList(&use, sortAttributes->list, 1);
502 attr_init_AttrList(&structure, sortAttributes->list, 4);
505 structure_value = attr_find(&structure, 0);
506 if (structure_value == 109)
508 return attr_find(&use, NULL);
511 int zebra_maps_attr(zebra_maps_t zms, Z_AttributesPlusTerm *zapt,
512 const char **index_type, char **search_type, char *rank_type,
513 int *complete_flag, int *sort_flag)
515 AttrType completeness;
518 AttrType sort_relation;
521 int completeness_value;
523 const char *structure_str = 0;
525 int sort_relation_value;
529 attr_init_APT(&structure, zapt, 4);
530 attr_init_APT(&completeness, zapt, 6);
531 attr_init_APT(&relation, zapt, 2);
532 attr_init_APT(&sort_relation, zapt, 7);
533 attr_init_APT(&weight, zapt, 9);
534 attr_init_APT(&use, zapt, 1);
536 completeness_value = attr_find(&completeness, NULL);
537 structure_value = attr_find_ex(&structure, NULL, &structure_str);
538 relation_value = attr_find(&relation, NULL);
539 sort_relation_value = attr_find(&sort_relation, NULL);
540 weight_value = attr_find(&weight, NULL);
541 use_value = attr_find(&use, NULL);
543 if (completeness_value == 2 || completeness_value == 3)
549 *sort_flag =(sort_relation_value > 0) ? 1 : 0;
550 *search_type = "phrase";
551 strcpy(rank_type, "void");
552 if (relation_value == 102)
554 if (weight_value == -1)
556 sprintf(rank_type, "rank,w=%d,u=%d", weight_value, use_value);
562 switch (structure_value)
564 case 6: /* word list */
565 *search_type = "and-list";
567 case 105: /* free-form-text */
568 *search_type = "or-list";
570 case 106: /* document-text */
571 *search_type = "or-list";
576 case 108: /* string */
577 *search_type = "phrase";
579 case 107: /* local-number */
580 *search_type = "local";
583 case 109: /* numeric string */
585 *search_type = "numeric";
589 *search_type = "phrase";
593 *search_type = "phrase";
597 *search_type = "phrase";
601 *search_type = "phrase";
604 if (structure_str && *structure_str)
605 *index_type = structure_str;
615 WRBUF zebra_replace(zebra_map_t zm, const char *ex_list,
616 const char *input_str, int input_len)
618 wrbuf_rewind(zm->zebra_maps->wrbuf_1);
619 wrbuf_write(zm->zebra_maps->wrbuf_1, input_str, input_len);
620 return zm->zebra_maps->wrbuf_1;
623 #define SE_CHARS ";,.()-/?<> \r\n\t"
625 static int tokenize_simple(zebra_map_t zm,
626 const char **result_buf, size_t *result_len)
628 char *buf = wrbuf_buf(zm->input_str);
629 size_t len = wrbuf_len(zm->input_str);
630 size_t i = zm->simple_off;
633 while (i < len && strchr(SE_CHARS, buf[i]))
636 while (i < len && !strchr(SE_CHARS, buf[i]))
638 if (buf[i] > 32 && buf[i] < 127)
639 buf[i] = tolower(buf[i]);
646 *result_buf = buf + start;
647 *result_len = i - start;
654 int zebra_map_tokenize_next(zebra_map_t zm,
655 const char **result_buf, size_t *result_len,
656 const char **display_buf, size_t *display_len)
658 assert(zm->use_chain);
662 return tokenize_simple(zm, result_buf, result_len);
666 while (icu_chain_next_token(zm->icu_chain, &status))
668 if (!U_SUCCESS(status))
670 *result_buf = icu_chain_token_sortkey(zm->icu_chain);
673 *result_len = strlen(*result_buf);
677 *display_buf = icu_chain_token_display(zm->icu_chain);
679 *display_len = strlen(*display_buf);
683 wrbuf_rewind(zm->print_str);
684 wrbuf_write_escaped(zm->print_str, *result_buf, *result_len);
685 yaz_log(YLOG_LOG, "output %s", wrbuf_cstr(zm->print_str));
688 if (**result_buf != '\0')
694 return tokenize_simple(zm, result_buf, result_len);
698 int zebra_map_tokenize_start(zebra_map_t zm,
699 const char *buf, size_t len)
701 assert(zm->use_chain);
703 wrbuf_rewind(zm->input_str);
704 wrbuf_write(zm->input_str, buf, len);
712 wrbuf_rewind(zm->print_str);
713 wrbuf_write_escaped(zm->print_str, wrbuf_buf(zm->input_str),
714 wrbuf_len(zm->input_str));
716 yaz_log(YLOG_LOG, "input %s",
717 wrbuf_cstr(zm->print_str));
719 icu_chain_assign_cstr(zm->icu_chain,
720 wrbuf_cstr(zm->input_str),
722 if (!U_SUCCESS(status))
726 yaz_log(YLOG_WARN, "bad encoding for input");
735 int zebra_maps_is_icu(zebra_map_t zm)
739 return zm->use_chain;
749 * c-file-style: "Stroustrup"
750 * indent-tabs-mode: nil
752 * vim: shiftwidth=4 tabstop=8 expandtab