1 /* $Id: zebramap.c,v 1.75 2007-12-13 18:08:26 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"))
233 char full_path[1024];
234 if (!yaz_filepath_resolve(argv[1], zms->tabpath, zms->tabroot,
237 yaz_log(YLOG_WARN, "%s:%d: Could not locate icuchain config '%s'",
238 fname, lineno, argv[1]);
242 zm->doc = xmlParseFile(full_path);
245 yaz_log(YLOG_WARN, "%s:%d: Could not load icuchain config '%s'",
246 fname, lineno, argv[1]);
253 xmlNode *xml_node = xmlDocGetRootElement(zm->doc);
255 icu_chain_xml_config(xml_node,
256 /* not sure about sort for this function yet.. */
260 zm->type == ZEBRA_MAP_TYPE_SORT,
265 yaz_log(YLOG_WARN, "%s:%d: Failed to load ICU chain %s",
266 fname, lineno, argv[1]);
270 yaz_log(YLOG_WARN, "%s:%d: ICU support unavailable",
276 yaz_log(YLOG_WARN, "%s:%d: XML support unavailable",
281 else if (!yaz_matchstr(argv[0], "debug") && argc == 2)
283 zm->debug = atoi(argv[1]);
287 yaz_log(YLOG_WARN, "%s:%d: Unrecognized directive '%s'",
288 fname, lineno, argv[0]);
294 ZEBRA_RES zebra_maps_read_file(zebra_maps_t zms, const char *fname)
303 if (!(f = yaz_fopen(zms->tabpath, fname, "r", zms->tabroot)))
305 yaz_log(YLOG_ERRNO|YLOG_FATAL, "%s", fname);
308 while ((argc = readconf_line(f, &lineno, line, 512, argv, 10)))
310 int r = parse_command(zms, argc, argv, fname, lineno);
319 (zms->no_files_read)++;
323 zebra_maps_t zebra_maps_open(Res res, const char *base_path,
324 const char *profile_path)
326 zebra_maps_t zms = (zebra_maps_t) xmalloc(sizeof(*zms));
328 zms->nmem = nmem_create();
329 zms->tabpath = profile_path ? nmem_strdup(zms->nmem, profile_path) : 0;
332 zms->tabroot = nmem_strdup(zms->nmem, base_path);
336 zms->temp_map_str[0] = '\0';
337 zms->temp_map_str[1] = '\0';
339 zms->temp_map_ptr[0] = zms->temp_map_str;
340 zms->temp_map_ptr[1] = NULL;
342 zms->wrbuf_1 = wrbuf_alloc();
344 zms->no_files_read = 0;
348 zebra_map_t zebra_map_get(zebra_maps_t zms, const char *id)
351 for (zm = zms->map_list; zm; zm = zm->next)
352 if (!strcmp(zm->id, id))
357 zebra_map_t zebra_map_get_or_add(zebra_maps_t zms, const char *id)
359 struct zebra_map *zm = zebra_map_get(zms, id);
362 zm = zebra_add_map(zms, id, ZEBRA_MAP_TYPE_INDEX);
364 /* no reason to warn if no maps are read from file */
365 if (zms->no_files_read)
366 yaz_log(YLOG_WARN, "Unknown register type: %s", id);
368 zm->maptab_name = nmem_strdup(zms->nmem, "@");
369 zm->completeness = 0;
375 chrmaptab zebra_charmap_get(zebra_map_t zm)
379 if (!zm->maptab_name || !yaz_matchstr(zm->maptab_name, "@"))
381 if (!(zm->maptab = chrmaptab_create(zm->zebra_maps->tabpath,
383 zm->zebra_maps->tabroot)))
384 yaz_log(YLOG_WARN, "Failed to read character table %s",
387 yaz_log(YLOG_DEBUG, "Read character table %s", zm->maptab_name);
392 const char **zebra_maps_input(zebra_map_t zm,
393 const char **from, int len, int first)
395 chrmaptab maptab = zebra_charmap_get(zm);
397 return chr_map_input(maptab, from, len, first);
399 zm->zebra_maps->temp_map_str[0] = **from;
402 return zm->zebra_maps->temp_map_ptr;
405 const char **zebra_maps_search(zebra_map_t zm,
406 const char **from, int len, int *q_map_match)
411 maptab = zebra_charmap_get(zm);
415 map = chr_map_q_input(maptab, from, len, 0);
421 map = chr_map_input(maptab, from, len, 0);
425 zm->zebra_maps->temp_map_str[0] = **from;
428 return zm->zebra_maps->temp_map_ptr;
431 const char *zebra_maps_output(zebra_map_t zm,
434 chrmaptab maptab = zebra_charmap_get(zm);
437 return chr_map_output(maptab, from, 1);
441 /* ------------------------------------ */
443 int zebra_maps_is_complete(zebra_map_t zm)
446 return zm->completeness;
450 int zebra_maps_is_positioned(zebra_map_t zm)
453 return zm->positioned;
457 int zebra_maps_is_index(zebra_map_t zm)
460 return zm->type == ZEBRA_MAP_TYPE_INDEX;
464 int zebra_maps_is_staticrank(zebra_map_t zm)
467 return zm->type == ZEBRA_MAP_TYPE_STATICRANK;
471 int zebra_maps_is_sort(zebra_map_t zm)
474 return zm->type == ZEBRA_MAP_TYPE_SORT;
478 int zebra_maps_is_alwaysmatches(zebra_map_t zm)
481 return zm->alwaysmatches;
485 int zebra_maps_is_first_in_field(zebra_map_t zm)
488 return zm->first_in_field;
492 int zebra_maps_sort(zebra_maps_t zms, Z_SortAttributes *sortAttributes,
498 attr_init_AttrList(&use, sortAttributes->list, 1);
499 attr_init_AttrList(&structure, sortAttributes->list, 4);
502 structure_value = attr_find(&structure, 0);
503 if (structure_value == 109)
505 return attr_find(&use, NULL);
508 int zebra_maps_attr(zebra_maps_t zms, Z_AttributesPlusTerm *zapt,
509 const char **index_type, char **search_type, char *rank_type,
510 int *complete_flag, int *sort_flag)
512 AttrType completeness;
515 AttrType sort_relation;
518 int completeness_value;
520 const char *structure_str = 0;
522 int sort_relation_value;
526 attr_init_APT(&structure, zapt, 4);
527 attr_init_APT(&completeness, zapt, 6);
528 attr_init_APT(&relation, zapt, 2);
529 attr_init_APT(&sort_relation, zapt, 7);
530 attr_init_APT(&weight, zapt, 9);
531 attr_init_APT(&use, zapt, 1);
533 completeness_value = attr_find(&completeness, NULL);
534 structure_value = attr_find_ex(&structure, NULL, &structure_str);
535 relation_value = attr_find(&relation, NULL);
536 sort_relation_value = attr_find(&sort_relation, NULL);
537 weight_value = attr_find(&weight, NULL);
538 use_value = attr_find(&use, NULL);
540 if (completeness_value == 2 || completeness_value == 3)
546 *sort_flag =(sort_relation_value > 0) ? 1 : 0;
547 *search_type = "phrase";
548 strcpy(rank_type, "void");
549 if (relation_value == 102)
551 if (weight_value == -1)
553 sprintf(rank_type, "rank,w=%d,u=%d", weight_value, use_value);
559 switch (structure_value)
561 case 6: /* word list */
562 *search_type = "and-list";
564 case 105: /* free-form-text */
565 *search_type = "or-list";
567 case 106: /* document-text */
568 *search_type = "or-list";
573 case 108: /* string */
574 *search_type = "phrase";
576 case 107: /* local-number */
577 *search_type = "local";
580 case 109: /* numeric string */
582 *search_type = "numeric";
586 *search_type = "phrase";
590 *search_type = "phrase";
594 *search_type = "phrase";
598 *search_type = "phrase";
601 if (structure_str && *structure_str)
602 *index_type = structure_str;
612 WRBUF zebra_replace(zebra_map_t zm, const char *ex_list,
613 const char *input_str, int input_len)
615 wrbuf_rewind(zm->zebra_maps->wrbuf_1);
616 wrbuf_write(zm->zebra_maps->wrbuf_1, input_str, input_len);
617 return zm->zebra_maps->wrbuf_1;
620 #define SE_CHARS ";,.()-/?<> \r\n\t"
622 static int tokenize_simple(zebra_map_t zm,
623 const char **result_buf, size_t *result_len)
625 char *buf = wrbuf_buf(zm->input_str);
626 size_t len = wrbuf_len(zm->input_str);
627 size_t i = zm->simple_off;
630 while (i < len && strchr(SE_CHARS, buf[i]))
633 while (i < len && !strchr(SE_CHARS, buf[i]))
635 if (buf[i] > 32 && buf[i] < 127)
636 buf[i] = tolower(buf[i]);
643 *result_buf = buf + start;
644 *result_len = i - start;
651 int zebra_map_tokenize_next(zebra_map_t zm,
652 const char **result_buf, size_t *result_len,
653 const char **display_buf, size_t *display_len)
655 assert(zm->use_chain);
659 return tokenize_simple(zm, result_buf, result_len);
663 while (icu_chain_next_token(zm->icu_chain, &status))
665 assert(U_SUCCESS(status));
666 *result_buf = icu_chain_token_sortkey(zm->icu_chain);
669 *result_len = strlen(*result_buf);
673 *display_buf = icu_chain_token_display(zm->icu_chain);
675 *display_len = strlen(*display_buf);
679 wrbuf_rewind(zm->print_str);
680 wrbuf_write_escaped(zm->print_str, *result_buf, *result_len);
681 yaz_log(YLOG_LOG, "output %s", wrbuf_cstr(zm->print_str));
684 if (**result_buf != '\0')
687 assert(U_SUCCESS(status));
691 return tokenize_simple(zm, result_buf, result_len);
695 int zebra_map_tokenize_start(zebra_map_t zm,
696 const char *buf, size_t len)
698 assert(zm->use_chain);
700 wrbuf_rewind(zm->input_str);
701 wrbuf_write(zm->input_str, buf, len);
709 wrbuf_rewind(zm->print_str);
710 wrbuf_write_escaped(zm->print_str, wrbuf_buf(zm->input_str),
711 wrbuf_len(zm->input_str));
713 yaz_log(YLOG_LOG, "input %s",
714 wrbuf_cstr(zm->print_str));
716 icu_chain_assign_cstr(zm->icu_chain,
717 wrbuf_cstr(zm->input_str),
719 assert(U_SUCCESS(status));
726 int zebra_map_tokenize(zebra_map_t zm,
727 const char *buf, size_t len,
728 const char **result_buf, size_t *result_len)
730 assert(zm->use_chain);
734 wrbuf_rewind(zm->input_str);
735 wrbuf_write(zm->input_str, buf, len);
741 return tokenize_simple(zm, result_buf, result_len);
749 wrbuf_rewind(zm->print_str);
750 wrbuf_write_escaped(zm->print_str, wrbuf_buf(zm->input_str),
751 wrbuf_len(zm->input_str));
753 yaz_log(YLOG_LOG, "input %s",
754 wrbuf_cstr(zm->print_str));
756 icu_chain_assign_cstr(zm->icu_chain,
757 wrbuf_cstr(zm->input_str),
759 assert(U_SUCCESS(status));
761 while (icu_chain_next_token(zm->icu_chain, &status))
763 assert(U_SUCCESS(status));
764 *result_buf = icu_chain_token_sortkey(zm->icu_chain);
767 *result_len = strlen(*result_buf);
771 wrbuf_rewind(zm->print_str);
772 wrbuf_write_escaped(zm->print_str, *result_buf, *result_len);
773 yaz_log(YLOG_LOG, "output %s", wrbuf_cstr(zm->print_str));
776 if (**result_buf != '\0')
779 assert(U_SUCCESS(status));
783 return tokenize_simple(zm, result_buf, result_len);
788 int zebra_maps_is_icu(zebra_map_t zm)
791 return zm->use_chain;
801 * indent-tabs-mode: nil
803 * vim: shiftwidth=4 tabstop=8 expandtab