1 /* This file is part of the Zebra server.
2 Copyright (C) 1995-2008 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 zebra_map_t zebra_map_get(zebra_maps_t zms, const char *id)
348 for (zm = zms->map_list; zm; zm = zm->next)
349 if (!strcmp(zm->id, id))
354 zebra_map_t zebra_map_get_or_add(zebra_maps_t zms, const char *id)
356 struct zebra_map *zm = zebra_map_get(zms, id);
359 zm = zebra_add_map(zms, id, ZEBRA_MAP_TYPE_INDEX);
361 /* no reason to warn if no maps are read from file */
362 if (zms->no_files_read)
363 yaz_log(YLOG_WARN, "Unknown register type: %s", id);
365 zm->maptab_name = nmem_strdup(zms->nmem, "@");
366 zm->completeness = 0;
372 chrmaptab zebra_charmap_get(zebra_map_t zm)
376 if (!zm->maptab_name || !yaz_matchstr(zm->maptab_name, "@"))
378 if (!(zm->maptab = chrmaptab_create(zm->zebra_maps->tabpath,
380 zm->zebra_maps->tabroot)))
381 yaz_log(YLOG_WARN, "Failed to read character table %s",
384 yaz_log(YLOG_DEBUG, "Read character table %s", zm->maptab_name);
389 const char **zebra_maps_input(zebra_map_t zm,
390 const char **from, int len, int first)
392 chrmaptab maptab = zebra_charmap_get(zm);
394 return chr_map_input(maptab, from, len, first);
396 zm->zebra_maps->temp_map_str[0] = **from;
399 return zm->zebra_maps->temp_map_ptr;
402 const char **zebra_maps_search(zebra_map_t zm,
403 const char **from, int len, int *q_map_match)
408 maptab = zebra_charmap_get(zm);
412 map = chr_map_q_input(maptab, from, len, 0);
418 map = chr_map_input(maptab, from, len, 0);
422 zm->zebra_maps->temp_map_str[0] = **from;
425 return zm->zebra_maps->temp_map_ptr;
428 const char *zebra_maps_output(zebra_map_t zm,
431 chrmaptab maptab = zebra_charmap_get(zm);
434 return chr_map_output(maptab, from, 1);
438 /* ------------------------------------ */
440 int zebra_maps_is_complete(zebra_map_t zm)
443 return zm->completeness;
447 int zebra_maps_is_positioned(zebra_map_t zm)
450 return zm->positioned;
454 int zebra_maps_is_index(zebra_map_t zm)
457 return zm->type == ZEBRA_MAP_TYPE_INDEX;
461 int zebra_maps_is_staticrank(zebra_map_t zm)
464 return zm->type == ZEBRA_MAP_TYPE_STATICRANK;
468 int zebra_maps_is_sort(zebra_map_t zm)
471 return zm->type == ZEBRA_MAP_TYPE_SORT;
475 int zebra_maps_is_alwaysmatches(zebra_map_t zm)
478 return zm->alwaysmatches;
482 int zebra_maps_is_first_in_field(zebra_map_t zm)
485 return zm->first_in_field;
489 int zebra_maps_sort(zebra_maps_t zms, Z_SortAttributes *sortAttributes,
495 attr_init_AttrList(&use, sortAttributes->list, 1);
496 attr_init_AttrList(&structure, sortAttributes->list, 4);
499 structure_value = attr_find(&structure, 0);
500 if (structure_value == 109)
502 return attr_find(&use, NULL);
505 int zebra_maps_attr(zebra_maps_t zms, Z_AttributesPlusTerm *zapt,
506 const char **index_type, char **search_type, char *rank_type,
507 int *complete_flag, int *sort_flag)
509 AttrType completeness;
512 AttrType sort_relation;
515 int completeness_value;
517 const char *structure_str = 0;
519 int sort_relation_value;
523 attr_init_APT(&structure, zapt, 4);
524 attr_init_APT(&completeness, zapt, 6);
525 attr_init_APT(&relation, zapt, 2);
526 attr_init_APT(&sort_relation, zapt, 7);
527 attr_init_APT(&weight, zapt, 9);
528 attr_init_APT(&use, zapt, 1);
530 completeness_value = attr_find(&completeness, NULL);
531 structure_value = attr_find_ex(&structure, NULL, &structure_str);
532 relation_value = attr_find(&relation, NULL);
533 sort_relation_value = attr_find(&sort_relation, NULL);
534 weight_value = attr_find(&weight, NULL);
535 use_value = attr_find(&use, NULL);
537 if (completeness_value == 2 || completeness_value == 3)
543 *sort_flag =(sort_relation_value > 0) ? 1 : 0;
544 *search_type = "phrase";
545 strcpy(rank_type, "void");
546 if (relation_value == 102)
548 if (weight_value == -1)
550 sprintf(rank_type, "rank,w=%d,u=%d", weight_value, use_value);
556 switch (structure_value)
558 case 6: /* word list */
559 *search_type = "and-list";
561 case 105: /* free-form-text */
562 *search_type = "or-list";
564 case 106: /* document-text */
565 *search_type = "or-list";
570 case 108: /* string */
571 *search_type = "phrase";
573 case 107: /* local-number */
574 *search_type = "local";
577 case 109: /* numeric string */
579 *search_type = "numeric";
583 *search_type = "phrase";
587 *search_type = "phrase";
591 *search_type = "phrase";
595 *search_type = "phrase";
598 if (structure_str && *structure_str)
599 *index_type = structure_str;
609 WRBUF zebra_replace(zebra_map_t zm, const char *ex_list,
610 const char *input_str, int input_len)
612 wrbuf_rewind(zm->zebra_maps->wrbuf_1);
613 wrbuf_write(zm->zebra_maps->wrbuf_1, input_str, input_len);
614 return zm->zebra_maps->wrbuf_1;
617 #define SE_CHARS ";,.()-/?<> \r\n\t"
619 static int tokenize_simple(zebra_map_t zm,
620 const char **result_buf, size_t *result_len)
622 char *buf = wrbuf_buf(zm->input_str);
623 size_t len = wrbuf_len(zm->input_str);
624 size_t i = zm->simple_off;
627 while (i < len && strchr(SE_CHARS, buf[i]))
630 while (i < len && !strchr(SE_CHARS, buf[i]))
632 if (buf[i] > 32 && buf[i] < 127)
633 buf[i] = tolower(buf[i]);
640 *result_buf = buf + start;
641 *result_len = i - start;
648 int zebra_map_tokenize_next(zebra_map_t zm,
649 const char **result_buf, size_t *result_len,
650 const char **display_buf, size_t *display_len)
652 assert(zm->use_chain);
656 return tokenize_simple(zm, result_buf, result_len);
660 while (icu_chain_next_token(zm->icu_chain, &status))
662 if (!U_SUCCESS(status))
664 *result_buf = icu_chain_token_sortkey(zm->icu_chain);
667 *result_len = strlen(*result_buf);
671 *display_buf = icu_chain_token_display(zm->icu_chain);
673 *display_len = strlen(*display_buf);
677 wrbuf_rewind(zm->print_str);
678 wrbuf_write_escaped(zm->print_str, *result_buf, *result_len);
679 yaz_log(YLOG_LOG, "output %s", wrbuf_cstr(zm->print_str));
682 if (**result_buf != '\0')
688 return tokenize_simple(zm, result_buf, result_len);
692 int zebra_map_tokenize_start(zebra_map_t zm,
693 const char *buf, size_t len)
695 assert(zm->use_chain);
697 wrbuf_rewind(zm->input_str);
698 wrbuf_write(zm->input_str, buf, len);
706 wrbuf_rewind(zm->print_str);
707 wrbuf_write_escaped(zm->print_str, wrbuf_buf(zm->input_str),
708 wrbuf_len(zm->input_str));
710 yaz_log(YLOG_LOG, "input %s",
711 wrbuf_cstr(zm->print_str));
713 icu_chain_assign_cstr(zm->icu_chain,
714 wrbuf_cstr(zm->input_str),
716 if (!U_SUCCESS(status))
720 yaz_log(YLOG_WARN, "bad encoding for input");
729 int zebra_maps_is_icu(zebra_map_t zm)
733 return zm->use_chain;
743 * indent-tabs-mode: nil
745 * vim: shiftwidth=4 tabstop=8 expandtab