1 /* $Id: zebramap.c,v 1.73 2007-12-07 14:09:10 adam Exp $
2 Copyright (C) 1995-2007
5 This file is part of the Zebra server.
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
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
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
29 #include <yaz/yaz-util.h>
36 #define ZEBRA_MAP_TYPE_SORT 1
37 #define ZEBRA_MAP_TYPE_INDEX 2
38 #define ZEBRA_MAP_TYPE_STATICRANK 3
40 #define ZEBRA_REPLACE_ANY 300
57 const char *maptab_name;
58 zebra_maps_t zebra_maps;
63 struct icu_chain *icu_chain;
68 struct zebra_map *next;
76 const char *temp_map_ptr[2];
83 void zebra_maps_close(zebra_maps_t zms)
85 struct zebra_map *zm = zms->map_list;
89 chrmaptab_destroy(zm->maptab);
92 icu_chain_destroy(zm->icu_chain);
97 wrbuf_destroy(zm->input_str);
98 wrbuf_destroy(zm->print_str);
101 wrbuf_destroy(zms->wrbuf_1);
102 nmem_destroy(zms->nmem);
106 zebra_map_t zebra_add_map(zebra_maps_t zms, const char *index_type,
109 zebra_map_t zm = (zebra_map_t) nmem_malloc(zms->nmem, sizeof(*zm));
111 zm->zebra_maps = zms;
112 zm->id = nmem_strdup(zms->nmem, index_type);
118 zm->completeness = 0;
120 zm->alwaysmatches = 0;
121 zm->first_in_field = 0;
124 zms->last_map->next = zm;
135 zm->input_str = wrbuf_alloc();
136 zm->print_str = wrbuf_alloc();
140 static int parse_command(zebra_maps_t zms, int argc, char **argv,
141 const char *fname, int lineno)
143 zebra_map_t zm = zms->last_map;
146 yaz_log(YLOG_WARN, "%s:%d: Missing arguments for '%s'",
147 fname, lineno, argv[0]);
152 yaz_log(YLOG_WARN, "%s:%d: Too many arguments for '%s'",
153 fname, lineno, argv[0]);
156 if (!yaz_matchstr(argv[0], "index"))
158 zm = zebra_add_map(zms, argv[1], ZEBRA_MAP_TYPE_INDEX);
161 else if (!yaz_matchstr(argv[0], "sort"))
163 zm = zebra_add_map(zms, argv[1], ZEBRA_MAP_TYPE_SORT);
164 zm->u.sort.entry_size = 80;
166 else if (!yaz_matchstr(argv[0], "staticrank"))
168 zm = zebra_add_map(zms, argv[1], ZEBRA_MAP_TYPE_STATICRANK);
169 zm->completeness = 1;
173 yaz_log(YLOG_WARN, "%s:%d: Missing sort/index before '%s'",
174 fname, lineno, argv[0]);
177 else if (!yaz_matchstr(argv[0], "charmap") && argc == 2)
179 if (zm->type != ZEBRA_MAP_TYPE_STATICRANK)
180 zm->maptab_name = nmem_strdup(zms->nmem, argv[1]);
183 yaz_log(YLOG_WARN|YLOG_FATAL, "%s:%d: charmap for "
184 "staticrank is invalid", fname, lineno);
185 yaz_log(YLOG_LOG, "Type is %d", zm->type);
189 else if (!yaz_matchstr(argv[0], "completeness") && argc == 2)
191 zm->completeness = atoi(argv[1]);
193 else if (!yaz_matchstr(argv[0], "position") && argc == 2)
195 zm->positioned = atoi(argv[1]);
197 else if (!yaz_matchstr(argv[0], "alwaysmatches") && argc == 2)
199 if (zm->type != ZEBRA_MAP_TYPE_STATICRANK)
200 zm->alwaysmatches = atoi(argv[1]);
203 yaz_log(YLOG_WARN|YLOG_FATAL, "%s:%d: alwaysmatches for "
204 "staticrank is invalid", fname, lineno);
208 else if (!yaz_matchstr(argv[0], "firstinfield") && argc == 2)
210 zm->first_in_field = atoi(argv[1]);
212 else if (!yaz_matchstr(argv[0], "entrysize") && argc == 2)
214 if (zm->type == ZEBRA_MAP_TYPE_SORT)
215 zm->u.sort.entry_size = atoi(argv[1]);
219 "%s:%d: entrysize only valid in sort section",
224 else if (!yaz_matchstr(argv[0], "simplechain"))
231 else if (!yaz_matchstr(argv[0], "icuchain"))
234 zm->doc = xmlParseFile(argv[1]);
237 yaz_log(YLOG_WARN, "%s:%d: Could not load icuchain config '%s'",
238 fname, lineno, argv[1]);
245 xmlNode *xml_node = xmlDocGetRootElement(zm->doc);
247 icu_chain_xml_config(xml_node,
248 /* not sure about sort for this function yet.. */
252 zm->type == ZEBRA_MAP_TYPE_SORT,
257 yaz_log(YLOG_WARN, "%s:%d: Failed to load ICU chain %s",
258 fname, lineno, argv[1]);
262 yaz_log(YLOG_WARN, "%s:%d: ICU support unavailable",
268 yaz_log(YLOG_WARN, "%s:%d: XML support unavailable",
273 else if (!yaz_matchstr(argv[0], "debug") && argc == 2)
275 zm->debug = atoi(argv[1]);
279 yaz_log(YLOG_WARN, "%s:%d: Unrecognized directive '%s'",
280 fname, lineno, argv[0]);
286 ZEBRA_RES zebra_maps_read_file(zebra_maps_t zms, const char *fname)
295 if (!(f = yaz_fopen(zms->tabpath, fname, "r", zms->tabroot)))
297 yaz_log(YLOG_ERRNO|YLOG_FATAL, "%s", fname);
300 while ((argc = readconf_line(f, &lineno, line, 512, argv, 10)))
302 int r = parse_command(zms, argc, argv, fname, lineno);
311 (zms->no_files_read)++;
315 zebra_maps_t zebra_maps_open(Res res, const char *base_path,
316 const char *profile_path)
318 zebra_maps_t zms = (zebra_maps_t) xmalloc(sizeof(*zms));
320 zms->nmem = nmem_create();
321 zms->tabpath = profile_path ? nmem_strdup(zms->nmem, profile_path) : 0;
324 zms->tabroot = nmem_strdup(zms->nmem, base_path);
328 zms->temp_map_str[0] = '\0';
329 zms->temp_map_str[1] = '\0';
331 zms->temp_map_ptr[0] = zms->temp_map_str;
332 zms->temp_map_ptr[1] = NULL;
334 zms->wrbuf_1 = wrbuf_alloc();
336 zms->no_files_read = 0;
340 zebra_map_t zebra_map_get(zebra_maps_t zms, const char *id)
343 for (zm = zms->map_list; zm; zm = zm->next)
344 if (!strcmp(zm->id, id))
349 zebra_map_t zebra_map_get_or_add(zebra_maps_t zms, const char *id)
351 struct zebra_map *zm = zebra_map_get(zms, id);
354 zm = zebra_add_map(zms, id, ZEBRA_MAP_TYPE_INDEX);
356 /* no reason to warn if no maps are read from file */
357 if (zms->no_files_read)
358 yaz_log(YLOG_WARN, "Unknown register type: %s", id);
360 zm->maptab_name = nmem_strdup(zms->nmem, "@");
361 zm->completeness = 0;
367 chrmaptab zebra_charmap_get(zebra_map_t zm)
371 if (!zm->maptab_name || !yaz_matchstr(zm->maptab_name, "@"))
373 if (!(zm->maptab = chrmaptab_create(zm->zebra_maps->tabpath,
375 zm->zebra_maps->tabroot)))
376 yaz_log(YLOG_WARN, "Failed to read character table %s",
379 yaz_log(YLOG_DEBUG, "Read character table %s", zm->maptab_name);
384 const char **zebra_maps_input(zebra_map_t zm,
385 const char **from, int len, int first)
387 chrmaptab maptab = zebra_charmap_get(zm);
389 return chr_map_input(maptab, from, len, first);
391 zm->zebra_maps->temp_map_str[0] = **from;
394 return zm->zebra_maps->temp_map_ptr;
397 const char **zebra_maps_search(zebra_map_t zm,
398 const char **from, int len, int *q_map_match)
403 maptab = zebra_charmap_get(zm);
407 map = chr_map_q_input(maptab, from, len, 0);
413 map = chr_map_input(maptab, from, len, 0);
417 zm->zebra_maps->temp_map_str[0] = **from;
420 return zm->zebra_maps->temp_map_ptr;
423 const char *zebra_maps_output(zebra_map_t zm,
426 chrmaptab maptab = zebra_charmap_get(zm);
429 return chr_map_output(maptab, from, 1);
433 /* ------------------------------------ */
435 int zebra_maps_is_complete(zebra_map_t zm)
438 return zm->completeness;
442 int zebra_maps_is_positioned(zebra_map_t zm)
445 return zm->positioned;
449 int zebra_maps_is_index(zebra_map_t zm)
452 return zm->type == ZEBRA_MAP_TYPE_INDEX;
456 int zebra_maps_is_staticrank(zebra_map_t zm)
459 return zm->type == ZEBRA_MAP_TYPE_STATICRANK;
463 int zebra_maps_is_sort(zebra_map_t zm)
466 return zm->type == ZEBRA_MAP_TYPE_SORT;
470 int zebra_maps_is_alwaysmatches(zebra_map_t zm)
473 return zm->alwaysmatches;
477 int zebra_maps_is_first_in_field(zebra_map_t zm)
480 return zm->first_in_field;
484 int zebra_maps_sort(zebra_maps_t zms, Z_SortAttributes *sortAttributes,
490 attr_init_AttrList(&use, sortAttributes->list, 1);
491 attr_init_AttrList(&structure, sortAttributes->list, 4);
494 structure_value = attr_find(&structure, 0);
495 if (structure_value == 109)
497 return attr_find(&use, NULL);
500 int zebra_maps_attr(zebra_maps_t zms, Z_AttributesPlusTerm *zapt,
501 const char **index_type, char **search_type, char *rank_type,
502 int *complete_flag, int *sort_flag)
504 AttrType completeness;
507 AttrType sort_relation;
510 int completeness_value;
512 const char *structure_str = 0;
514 int sort_relation_value;
518 attr_init_APT(&structure, zapt, 4);
519 attr_init_APT(&completeness, zapt, 6);
520 attr_init_APT(&relation, zapt, 2);
521 attr_init_APT(&sort_relation, zapt, 7);
522 attr_init_APT(&weight, zapt, 9);
523 attr_init_APT(&use, zapt, 1);
525 completeness_value = attr_find(&completeness, NULL);
526 structure_value = attr_find_ex(&structure, NULL, &structure_str);
527 relation_value = attr_find(&relation, NULL);
528 sort_relation_value = attr_find(&sort_relation, NULL);
529 weight_value = attr_find(&weight, NULL);
530 use_value = attr_find(&use, NULL);
532 if (completeness_value == 2 || completeness_value == 3)
538 *sort_flag =(sort_relation_value > 0) ? 1 : 0;
539 *search_type = "phrase";
540 strcpy(rank_type, "void");
541 if (relation_value == 102)
543 if (weight_value == -1)
545 sprintf(rank_type, "rank,w=%d,u=%d", weight_value, use_value);
551 switch (structure_value)
553 case 6: /* word list */
554 *search_type = "and-list";
556 case 105: /* free-form-text */
557 *search_type = "or-list";
559 case 106: /* document-text */
560 *search_type = "or-list";
565 case 108: /* string */
566 *search_type = "phrase";
568 case 107: /* local-number */
569 *search_type = "local";
572 case 109: /* numeric string */
574 *search_type = "numeric";
578 *search_type = "phrase";
582 *search_type = "phrase";
586 *search_type = "phrase";
590 *search_type = "phrase";
593 if (structure_str && *structure_str)
594 *index_type = structure_str;
604 WRBUF zebra_replace(zebra_map_t zm, const char *ex_list,
605 const char *input_str, int input_len)
607 wrbuf_rewind(zm->zebra_maps->wrbuf_1);
608 wrbuf_write(zm->zebra_maps->wrbuf_1, input_str, input_len);
609 return zm->zebra_maps->wrbuf_1;
612 #define SE_CHARS ";,.()-/?<> \r\n\t"
614 static int tokenize_simple(zebra_map_t zm,
615 const char **result_buf, size_t *result_len)
617 char *buf = wrbuf_buf(zm->input_str);
618 size_t len = wrbuf_len(zm->input_str);
619 size_t i = zm->simple_off;
622 while (i < len && strchr(SE_CHARS, buf[i]))
625 while (i < len && !strchr(SE_CHARS, buf[i]))
627 if (buf[i] > 32 && buf[i] < 127)
628 buf[i] = tolower(buf[i]);
635 *result_buf = buf + start;
636 *result_len = i - start;
643 int zebra_map_tokenize_next(zebra_map_t zm,
644 const char **result_buf, size_t *result_len)
646 assert(zm->use_chain);
650 return tokenize_simple(zm, result_buf, result_len);
654 while (icu_chain_next_token(zm->icu_chain, &status))
656 assert(U_SUCCESS(status));
657 *result_buf = icu_chain_token_sortkey(zm->icu_chain);
660 *result_len = strlen(*result_buf);
664 wrbuf_rewind(zm->print_str);
665 wrbuf_write_escaped(zm->print_str, *result_buf, *result_len);
666 yaz_log(YLOG_LOG, "output %s", wrbuf_cstr(zm->print_str));
669 if (**result_buf != '\0')
672 assert(U_SUCCESS(status));
676 return tokenize_simple(zm, result_buf, result_len);
680 int zebra_map_tokenize_start(zebra_map_t zm,
681 const char *buf, size_t len)
683 assert(zm->use_chain);
685 wrbuf_rewind(zm->input_str);
686 wrbuf_write(zm->input_str, buf, len);
694 wrbuf_rewind(zm->print_str);
695 wrbuf_write_escaped(zm->print_str, wrbuf_buf(zm->input_str),
696 wrbuf_len(zm->input_str));
698 yaz_log(YLOG_LOG, "input %s",
699 wrbuf_cstr(zm->print_str));
701 icu_chain_assign_cstr(zm->icu_chain,
702 wrbuf_cstr(zm->input_str),
704 assert(U_SUCCESS(status));
711 int zebra_map_tokenize(zebra_map_t zm,
712 const char *buf, size_t len,
713 const char **result_buf, size_t *result_len)
715 assert(zm->use_chain);
719 wrbuf_rewind(zm->input_str);
720 wrbuf_write(zm->input_str, buf, len);
726 return tokenize_simple(zm, result_buf, result_len);
734 wrbuf_rewind(zm->print_str);
735 wrbuf_write_escaped(zm->print_str, wrbuf_buf(zm->input_str),
736 wrbuf_len(zm->input_str));
738 yaz_log(YLOG_LOG, "input %s",
739 wrbuf_cstr(zm->print_str));
741 icu_chain_assign_cstr(zm->icu_chain,
742 wrbuf_cstr(zm->input_str),
744 assert(U_SUCCESS(status));
746 while (icu_chain_next_token(zm->icu_chain, &status))
748 assert(U_SUCCESS(status));
749 *result_buf = icu_chain_token_sortkey(zm->icu_chain);
752 *result_len = strlen(*result_buf);
756 wrbuf_rewind(zm->print_str);
757 wrbuf_write_escaped(zm->print_str, *result_buf, *result_len);
758 yaz_log(YLOG_LOG, "output %s", wrbuf_cstr(zm->print_str));
761 if (**result_buf != '\0')
764 assert(U_SUCCESS(status));
768 return tokenize_simple(zm, result_buf, result_len);
773 int zebra_maps_is_icu(zebra_map_t zm)
776 return zm->use_chain;
786 * indent-tabs-mode: nil
788 * vim: shiftwidth=4 tabstop=8 expandtab