1 /* $Id: zebramap.c,v 1.65 2007-11-06 10:30:02 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>
32 #include <yaz/icu_I18N.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
56 const char *maptab_name;
58 zebra_maps_t zebra_maps;
63 struct icu_chain *icu_chain;
67 struct zebra_map *next;
75 const char *temp_map_ptr[2];
82 void zebra_maps_close(zebra_maps_t zms)
84 struct zebra_map *zm = zms->map_list;
88 chrmaptab_destroy(zm->maptab);
91 icu_chain_destroy(zm->icu_chain);
96 wrbuf_destroy(zm->simple_buf);
99 wrbuf_destroy(zms->wrbuf_1);
100 nmem_destroy(zms->nmem);
104 zebra_map_t zebra_add_map(zebra_maps_t zms, const char *index_type,
107 zebra_map_t zm = (zebra_map_t) nmem_malloc(zms->nmem, sizeof(*zm));
109 zm->zebra_maps = zms;
110 zm->id = nmem_strdup(zms->nmem, index_type);
116 zm->completeness = 0;
118 zm->alwaysmatches = 0;
119 zm->first_in_field = 0;
122 zms->last_map->next = zm;
133 zm->simple_buf = 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], "locale"))
223 zm->locale = nmem_strdup(zms->nmem, argv[1]);
225 else if (!yaz_matchstr(argv[0], "simplechain"))
230 else if (!yaz_matchstr(argv[0], "icuchain"))
235 yaz_log(YLOG_WARN, "%s:%d: locale required before icuchain",
239 zm->doc = xmlParseFile(argv[1]);
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, zm->locale,
253 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",
275 yaz_log(YLOG_WARN, "%s:%d: Unrecognized directive '%s'",
276 fname, lineno, argv[0]);
282 ZEBRA_RES zebra_maps_read_file(zebra_maps_t zms, const char *fname)
291 if (!(f = yaz_fopen(zms->tabpath, fname, "r", zms->tabroot)))
293 yaz_log(YLOG_ERRNO|YLOG_FATAL, "%s", fname);
296 while ((argc = readconf_line(f, &lineno, line, 512, argv, 10)))
298 int r = parse_command(zms, argc, argv, fname, lineno);
307 (zms->no_files_read)++;
311 zebra_maps_t zebra_maps_open(Res res, const char *base_path,
312 const char *profile_path)
314 zebra_maps_t zms = (zebra_maps_t) xmalloc(sizeof(*zms));
316 zms->nmem = nmem_create();
317 zms->tabpath = profile_path ? nmem_strdup(zms->nmem, profile_path) : 0;
320 zms->tabroot = nmem_strdup(zms->nmem, base_path);
324 zms->temp_map_str[0] = '\0';
325 zms->temp_map_str[1] = '\0';
327 zms->temp_map_ptr[0] = zms->temp_map_str;
328 zms->temp_map_ptr[1] = NULL;
330 zms->wrbuf_1 = wrbuf_alloc();
332 zms->no_files_read = 0;
336 zebra_map_t zebra_map_get(zebra_maps_t zms, const char *id)
339 for (zm = zms->map_list; zm; zm = zm->next)
340 if (!strcmp(zm->id, id))
345 zebra_map_t zebra_map_get_or_add(zebra_maps_t zms, const char *id)
347 struct zebra_map *zm = zebra_map_get(zms, id);
350 zm = zebra_add_map(zms, id, ZEBRA_MAP_TYPE_INDEX);
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);
356 zm->maptab_name = nmem_strdup(zms->nmem, "@");
357 zm->completeness = 0;
363 chrmaptab zebra_charmap_get(zebra_map_t zm)
367 if (!zm->maptab_name || !yaz_matchstr(zm->maptab_name, "@"))
369 if (!(zm->maptab = chrmaptab_create(zm->zebra_maps->tabpath,
371 zm->zebra_maps->tabroot)))
372 yaz_log(YLOG_WARN, "Failed to read character table %s",
375 yaz_log(YLOG_DEBUG, "Read character table %s", zm->maptab_name);
380 const char **zebra_maps_input(zebra_map_t zm,
381 const char **from, int len, int first)
383 chrmaptab maptab = zebra_charmap_get(zm);
385 return chr_map_input(maptab, from, len, first);
387 zm->zebra_maps->temp_map_str[0] = **from;
390 return zm->zebra_maps->temp_map_ptr;
393 const char **zebra_maps_search(zebra_map_t zm,
394 const char **from, int len, int *q_map_match)
399 maptab = zebra_charmap_get(zm);
403 map = chr_map_q_input(maptab, from, len, 0);
409 map = chr_map_input(maptab, from, len, 0);
413 zm->zebra_maps->temp_map_str[0] = **from;
416 return zm->zebra_maps->temp_map_ptr;
419 const char *zebra_maps_output(zebra_map_t zm,
422 chrmaptab maptab = zebra_charmap_get(zm);
425 return chr_map_output(maptab, from, 1);
429 /* ------------------------------------ */
431 int zebra_maps_is_complete(zebra_map_t zm)
434 return zm->completeness;
438 int zebra_maps_is_positioned(zebra_map_t zm)
441 return zm->positioned;
445 int zebra_maps_is_index(zebra_map_t zm)
448 return zm->type == ZEBRA_MAP_TYPE_INDEX;
452 int zebra_maps_is_staticrank(zebra_map_t zm)
455 return zm->type == ZEBRA_MAP_TYPE_STATICRANK;
459 int zebra_maps_is_sort(zebra_map_t zm)
462 return zm->type == ZEBRA_MAP_TYPE_SORT;
466 int zebra_maps_is_alwaysmatches(zebra_map_t zm)
469 return zm->alwaysmatches;
473 int zebra_maps_is_first_in_field(zebra_map_t zm)
476 return zm->first_in_field;
480 int zebra_maps_sort(zebra_maps_t zms, Z_SortAttributes *sortAttributes,
486 attr_init_AttrList(&use, sortAttributes->list, 1);
487 attr_init_AttrList(&structure, sortAttributes->list, 4);
490 structure_value = attr_find(&structure, 0);
491 if (structure_value == 109)
493 return attr_find(&use, NULL);
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)
500 AttrType completeness;
503 AttrType sort_relation;
506 int completeness_value;
508 const char *structure_str = 0;
510 int sort_relation_value;
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);
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);
528 if (completeness_value == 2 || completeness_value == 3)
534 *sort_flag =(sort_relation_value > 0) ? 1 : 0;
535 *search_type = "phrase";
536 strcpy(rank_type, "void");
537 if (relation_value == 102)
539 if (weight_value == -1)
541 sprintf(rank_type, "rank,w=%d,u=%d", weight_value, use_value);
547 switch (structure_value)
549 case 6: /* word list */
550 *search_type = "and-list";
552 case 105: /* free-form-text */
553 *search_type = "or-list";
555 case 106: /* document-text */
556 *search_type = "or-list";
561 case 108: /* string */
562 *search_type = "phrase";
564 case 107: /* local-number */
565 *search_type = "local";
568 case 109: /* numeric string */
570 *search_type = "numeric";
574 *search_type = "phrase";
578 *search_type = "phrase";
582 *search_type = "phrase";
586 *search_type = "phrase";
589 if (structure_str && *structure_str)
590 *index_type = structure_str;
600 WRBUF zebra_replace(zebra_map_t zm, const char *ex_list,
601 const char *input_str, int input_len)
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;
608 #define SE_CHARS ";,.()-/?<> \r\n\t"
610 static int tokenize_simple(zebra_map_t zm,
611 const char **result_buf, size_t *result_len)
613 char *buf = wrbuf_buf(zm->simple_buf);
614 size_t len = wrbuf_len(zm->simple_buf);
615 size_t i = zm->simple_off;
618 while (i < len && strchr(SE_CHARS, buf[i]))
621 while (i < len && !strchr(SE_CHARS, buf[i]))
623 if (buf[i] > 32 && buf[i] < 127)
624 buf[i] = tolower(buf[i]);
631 *result_buf = buf + start;
632 *result_len = i - start;
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)
642 assert(zm->use_chain);
647 wrbuf_rewind(zm->simple_buf);
648 wrbuf_write(zm->simple_buf, buf, len);
651 return tokenize_simple(zm, result_buf, result_len);
656 int zebra_maps_is_icu(zebra_map_t zm)
659 return zm->use_chain;
669 * indent-tabs-mode: nil
671 * vim: shiftwidth=4 tabstop=8 expandtab