1 /* $Id: zebramap.c,v 1.62 2007-11-05 11:27:24 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>
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
52 const char *maptab_name;
53 zebra_maps_t zebra_maps;
54 struct zebra_map *next;
62 const char *temp_map_ptr[2];
69 void zebra_maps_close(zebra_maps_t zms)
71 struct zebra_map *zm = zms->map_list;
75 chrmaptab_destroy(zm->maptab);
78 wrbuf_destroy(zms->wrbuf_1);
79 nmem_destroy(zms->nmem);
83 zebra_map_t zebra_add_map(zebra_maps_t zms, const char *index_type,
86 zebra_map_t zm = (zebra_map_t) nmem_malloc(zms->nmem, sizeof(*zm));
89 zm->id = nmem_strdup(zms->nmem, index_type);
95 zm->alwaysmatches = 0;
96 zm->first_in_field = 0;
99 zms->last_map->next = zm;
110 static int parse_command(zebra_maps_t zms, int argc, char **argv,
111 const char *fname, int lineno)
113 zebra_map_t zm = zms->last_map;
116 yaz_log(YLOG_WARN, "%s:%d: Missing arguments for '%s'",
117 fname, lineno, argv[0]);
122 yaz_log(YLOG_WARN, "%s:%d: Too many arguments for '%s'",
123 fname, lineno, argv[0]);
126 if (!yaz_matchstr(argv[0], "index"))
128 zm = zebra_add_map(zms, argv[1], ZEBRA_MAP_TYPE_INDEX);
131 else if (!yaz_matchstr(argv[0], "sort"))
133 zm = zebra_add_map(zms, argv[1], ZEBRA_MAP_TYPE_SORT);
134 zm->u.sort.entry_size = 80;
136 else if (!yaz_matchstr(argv[0], "staticrank"))
138 zm = zebra_add_map(zms, argv[1], ZEBRA_MAP_TYPE_STATICRANK);
139 zm->completeness = 1;
143 yaz_log(YLOG_WARN, "%s:%d: Missing sort/index before '%s'",
144 fname, lineno, argv[0]);
147 else if (!yaz_matchstr(argv[0], "charmap") && argc == 2)
149 if (zm->type != ZEBRA_MAP_TYPE_STATICRANK)
150 zm->maptab_name = nmem_strdup(zms->nmem, argv[1]);
153 yaz_log(YLOG_WARN|YLOG_FATAL, "%s:%d: charmap for "
154 "staticrank is invalid", fname, lineno);
155 yaz_log(YLOG_LOG, "Type is %d", zm->type);
159 else if (!yaz_matchstr(argv[0], "completeness") && argc == 2)
161 zm->completeness = atoi(argv[1]);
163 else if (!yaz_matchstr(argv[0], "position") && argc == 2)
165 zm->positioned = atoi(argv[1]);
167 else if (!yaz_matchstr(argv[0], "alwaysmatches") && argc == 2)
169 if (zm->type != ZEBRA_MAP_TYPE_STATICRANK)
170 zm->alwaysmatches = atoi(argv[1]);
173 yaz_log(YLOG_WARN|YLOG_FATAL, "%s:%d: alwaysmatches for "
174 "staticrank is invalid", fname, lineno);
178 else if (!yaz_matchstr(argv[0], "firstinfield") && argc == 2)
180 zm->first_in_field = atoi(argv[1]);
182 else if (!yaz_matchstr(argv[0], "entrysize") && argc == 2)
184 if (zm->type == ZEBRA_MAP_TYPE_SORT)
185 zm->u.sort.entry_size = atoi(argv[1]);
189 "%s:%d: entrysize only valid in sort section",
196 yaz_log(YLOG_WARN, "%s:%d: Unrecognized directive '%s'",
197 fname, lineno, argv[0]);
203 ZEBRA_RES zebra_maps_read_file(zebra_maps_t zms, const char *fname)
212 if (!(f = yaz_fopen(zms->tabpath, fname, "r", zms->tabroot)))
214 yaz_log(YLOG_ERRNO|YLOG_FATAL, "%s", fname);
217 while ((argc = readconf_line(f, &lineno, line, 512, argv, 10)))
219 int r = parse_command(zms, argc, argv, fname, lineno);
230 zebra_maps_t zebra_maps_open(Res res, const char *base_path,
231 const char *profile_path)
233 zebra_maps_t zms = (zebra_maps_t) xmalloc(sizeof(*zms));
235 zms->nmem = nmem_create();
237 zms->tabpath = profile_path ? nmem_strdup(zms->nmem, profile_path) : 0;
240 zms->tabroot = nmem_strdup(zms->nmem, base_path);
244 zms->temp_map_str[0] = '\0';
245 zms->temp_map_str[1] = '\0';
247 zms->temp_map_ptr[0] = zms->temp_map_str;
248 zms->temp_map_ptr[1] = NULL;
250 zms->wrbuf_1 = wrbuf_alloc();
255 zebra_map_t zebra_map_get(zebra_maps_t zms, const char *id)
258 for (zm = zms->map_list; zm; zm = zm->next)
259 if (!strcmp(zm->id, id))
264 zebra_map_t zebra_map_get_or_add(zebra_maps_t zms, const char *id)
266 struct zebra_map *zm = zebra_map_get(zms, id);
269 zm = zebra_add_map(zms, id, ZEBRA_MAP_TYPE_INDEX);
271 /* no reason to warn if no maps are installed at ALL
272 Note that zebra_add_maps increments no_maps ..
274 if (zms->no_maps > 1)
275 yaz_log(YLOG_WARN, "Unknown register type: %s", id);
279 zm->maptab_name = nmem_strdup(zms->nmem, "@");
280 zm->completeness = 0;
286 chrmaptab zebra_charmap_get(zebra_map_t zm)
290 if (!zm->maptab_name || !yaz_matchstr(zm->maptab_name, "@"))
292 if (!(zm->maptab = chrmaptab_create(zm->zebra_maps->tabpath,
294 zm->zebra_maps->tabroot)))
295 yaz_log(YLOG_WARN, "Failed to read character table %s",
298 yaz_log(YLOG_DEBUG, "Read character table %s", zm->maptab_name);
303 const char **zebra_maps_input(zebra_map_t zm,
304 const char **from, int len, int first)
306 chrmaptab maptab = zebra_charmap_get(zm);
308 return chr_map_input(maptab, from, len, first);
310 zm->zebra_maps->temp_map_str[0] = **from;
313 return zm->zebra_maps->temp_map_ptr;
316 const char **zebra_maps_search(zebra_map_t zm,
317 const char **from, int len, int *q_map_match)
322 maptab = zebra_charmap_get(zm);
326 map = chr_map_q_input(maptab, from, len, 0);
332 map = chr_map_input(maptab, from, len, 0);
336 zm->zebra_maps->temp_map_str[0] = **from;
339 return zm->zebra_maps->temp_map_ptr;
342 const char *zebra_maps_output(zebra_map_t zm,
345 chrmaptab maptab = zebra_charmap_get(zm);
348 return chr_map_output(maptab, from, 1);
352 /* ------------------------------------ */
354 int zebra_maps_is_complete(zebra_map_t zm)
357 return zm->completeness;
361 int zebra_maps_is_positioned(zebra_map_t zm)
364 return zm->positioned;
368 int zebra_maps_is_index(zebra_map_t zm)
371 return zm->type == ZEBRA_MAP_TYPE_INDEX;
375 int zebra_maps_is_staticrank(zebra_map_t zm)
378 return zm->type == ZEBRA_MAP_TYPE_STATICRANK;
382 int zebra_maps_is_sort(zebra_map_t zm)
385 return zm->type == ZEBRA_MAP_TYPE_SORT;
389 int zebra_maps_is_alwaysmatches(zebra_map_t zm)
392 return zm->alwaysmatches;
396 int zebra_maps_is_first_in_field(zebra_map_t zm)
399 return zm->first_in_field;
403 int zebra_maps_sort(zebra_maps_t zms, Z_SortAttributes *sortAttributes,
409 attr_init_AttrList(&use, sortAttributes->list, 1);
410 attr_init_AttrList(&structure, sortAttributes->list, 4);
413 structure_value = attr_find(&structure, 0);
414 if (structure_value == 109)
416 return attr_find(&use, NULL);
419 int zebra_maps_attr(zebra_maps_t zms, Z_AttributesPlusTerm *zapt,
420 const char **index_type, char **search_type, char *rank_type,
421 int *complete_flag, int *sort_flag)
423 AttrType completeness;
426 AttrType sort_relation;
429 int completeness_value;
431 const char *structure_str = 0;
433 int sort_relation_value;
437 attr_init_APT(&structure, zapt, 4);
438 attr_init_APT(&completeness, zapt, 6);
439 attr_init_APT(&relation, zapt, 2);
440 attr_init_APT(&sort_relation, zapt, 7);
441 attr_init_APT(&weight, zapt, 9);
442 attr_init_APT(&use, zapt, 1);
444 completeness_value = attr_find(&completeness, NULL);
445 structure_value = attr_find_ex(&structure, NULL, &structure_str);
446 relation_value = attr_find(&relation, NULL);
447 sort_relation_value = attr_find(&sort_relation, NULL);
448 weight_value = attr_find(&weight, NULL);
449 use_value = attr_find(&use, NULL);
451 if (completeness_value == 2 || completeness_value == 3)
457 *sort_flag =(sort_relation_value > 0) ? 1 : 0;
458 *search_type = "phrase";
459 strcpy(rank_type, "void");
460 if (relation_value == 102)
462 if (weight_value == -1)
464 sprintf(rank_type, "rank,w=%d,u=%d", weight_value, use_value);
470 switch (structure_value)
472 case 6: /* word list */
473 *search_type = "and-list";
475 case 105: /* free-form-text */
476 *search_type = "or-list";
478 case 106: /* document-text */
479 *search_type = "or-list";
484 case 108: /* string */
485 *search_type = "phrase";
487 case 107: /* local-number */
488 *search_type = "local";
491 case 109: /* numeric string */
493 *search_type = "numeric";
497 *search_type = "phrase";
501 *search_type = "phrase";
505 *search_type = "phrase";
509 *search_type = "phrase";
512 if (structure_str && *structure_str)
513 *index_type = structure_str;
523 WRBUF zebra_replace(zebra_map_t zm, const char *ex_list,
524 const char *input_str, int input_len)
526 wrbuf_rewind(zm->zebra_maps->wrbuf_1);
527 wrbuf_write(zm->zebra_maps->wrbuf_1, input_str, input_len);
528 return zm->zebra_maps->wrbuf_1;
534 * indent-tabs-mode: nil
536 * vim: shiftwidth=4 tabstop=8 expandtab