1 /* $Id: zebramap.c,v 1.57 2007-03-19 21:50:39 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
55 const char *maptab_name;
56 struct zebra_map *next;
63 struct zebra_map *map_list;
65 const char *temp_map_ptr[2];
66 struct zebra_map **lookup_array;
71 void zebra_maps_close(ZebraMaps zms)
73 struct zebra_map *zm = zms->map_list;
77 chrmaptab_destroy(zm->maptab);
80 wrbuf_destroy(zms->wrbuf_1);
81 nmem_destroy(zms->nmem);
85 ZEBRA_RES zebra_maps_read_file(ZebraMaps zms, const char *fname)
93 struct zebra_map **zm = 0, *zp;
95 if (!(f = yaz_fopen(zms->tabpath, fname, "r", zms->tabroot)))
97 yaz_log(YLOG_ERRNO|YLOG_FATAL, "%s", fname);
100 while ((argc = readconf_line(f, &lineno, line, 512, argv, 10)))
104 yaz_log(YLOG_WARN, "%s:%d: Missing arguments for '%s'",
105 fname, lineno, argv[0]);
111 yaz_log(YLOG_WARN, "%s:%d: Too many arguments for '%s'",
112 fname, lineno, argv[0]);
116 if (!yaz_matchstr(argv[0], "index"))
122 *zm = (struct zebra_map *) nmem_malloc(zms->nmem, sizeof(**zm));
123 (*zm)->reg_id = argv[1][0];
124 (*zm)->maptab_name = NULL;
125 (*zm)->maptab = NULL;
126 (*zm)->type = ZEBRA_MAP_TYPE_INDEX;
127 (*zm)->completeness = 0;
128 (*zm)->positioned = 1;
129 (*zm)->alwaysmatches = 0;
130 (*zm)->first_in_field = 0;
133 else if (!yaz_matchstr(argv[0], "sort"))
139 *zm = (struct zebra_map *) nmem_malloc(zms->nmem, sizeof(**zm));
140 (*zm)->reg_id = argv[1][0];
141 (*zm)->maptab_name = NULL;
142 (*zm)->type = ZEBRA_MAP_TYPE_SORT;
143 (*zm)->u.sort.entry_size = 80;
144 (*zm)->maptab = NULL;
145 (*zm)->completeness = 0;
146 (*zm)->positioned = 0;
147 (*zm)->alwaysmatches = 0;
148 (*zm)->first_in_field = 0;
151 else if (!yaz_matchstr(argv[0], "staticrank"))
157 *zm = (struct zebra_map *) nmem_malloc(zms->nmem, sizeof(**zm));
158 (*zm)->reg_id = argv[1][0];
159 (*zm)->maptab_name = NULL;
160 (*zm)->type = ZEBRA_MAP_TYPE_STATICRANK;
161 (*zm)->maptab = NULL;
162 (*zm)->completeness = 1;
163 (*zm)->positioned = 0;
164 (*zm)->alwaysmatches = 0;
165 (*zm)->first_in_field = 0;
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 yaz_log(YLOG_WARN, "%s:%d: Unrecognized directive '%s'",
217 fname, lineno, argv[0]);
225 for (zp = zms->map_list; zp; zp = zp->next)
226 zms->lookup_array[zp->reg_id] = zp;
233 ZebraMaps zebra_maps_open(Res res, const char *base_path,
234 const char *profile_path)
236 ZebraMaps zms = (ZebraMaps) xmalloc(sizeof(*zms));
239 zms->nmem = nmem_create();
241 zms->tabpath = profile_path ? nmem_strdup(zms->nmem, profile_path) : 0;
244 zms->tabroot = nmem_strdup(zms->nmem, base_path);
245 zms->map_list = NULL;
247 zms->temp_map_str[0] = '\0';
248 zms->temp_map_str[1] = '\0';
250 zms->temp_map_ptr[0] = zms->temp_map_str;
251 zms->temp_map_ptr[1] = NULL;
253 zms->lookup_array = (struct zebra_map**)
254 nmem_malloc(zms->nmem, sizeof(*zms->lookup_array)*256);
255 zms->wrbuf_1 = wrbuf_alloc();
257 for (i = 0; i<256; i++)
258 zms->lookup_array[i] = 0;
262 struct zebra_map *zebra_map_get(ZebraMaps zms, unsigned reg_id)
264 assert(reg_id >= 0 && reg_id <= 255);
265 return zms->lookup_array[reg_id];
268 chrmaptab zebra_charmap_get(ZebraMaps zms, unsigned reg_id)
270 struct zebra_map *zm = zebra_map_get(zms, reg_id);
273 zm = (struct zebra_map *) nmem_malloc(zms->nmem, sizeof(*zm));
275 /* no reason to warn if no maps are installed at ALL */
277 yaz_log(YLOG_WARN, "Unknown register type: %c", reg_id);
280 zm->maptab_name = nmem_strdup(zms->nmem, "@");
282 zm->type = ZEBRA_MAP_TYPE_INDEX;
283 zm->completeness = 0;
284 zm->next = zms->map_list;
285 zms->map_list = zm->next;
287 zms->lookup_array[zm->reg_id & 255] = zm;
291 if (!zm->maptab_name || !yaz_matchstr(zm->maptab_name, "@"))
293 if (!(zm->maptab = chrmaptab_create(zms->tabpath,
296 yaz_log(YLOG_WARN, "Failed to read character table %s",
299 yaz_log(YLOG_DEBUG, "Read character table %s", zm->maptab_name);
304 const char **zebra_maps_input(ZebraMaps zms, unsigned reg_id,
305 const char **from, int len, int first)
309 maptab = zebra_charmap_get(zms, reg_id);
311 return chr_map_input(maptab, from, len, first);
313 zms->temp_map_str[0] = **from;
316 return zms->temp_map_ptr;
319 const char **zebra_maps_search(ZebraMaps zms, unsigned reg_id,
320 const char **from, int len, int *q_map_match)
325 maptab = zebra_charmap_get(zms, reg_id);
329 map = chr_map_q_input(maptab, from, len, 0);
335 map = chr_map_input(maptab, from, len, 0);
339 zms->temp_map_str[0] = **from;
342 return zms->temp_map_ptr;
345 const char *zebra_maps_output(ZebraMaps zms, unsigned reg_id,
348 chrmaptab maptab = zebra_charmap_get(zms, reg_id);
351 return chr_map_output(maptab, from, 1);
355 /* ------------------------------------ */
357 int zebra_maps_is_complete(ZebraMaps zms, unsigned reg_id)
359 struct zebra_map *zm = zebra_map_get(zms, reg_id);
361 return zm->completeness;
365 int zebra_maps_is_positioned(ZebraMaps zms, unsigned reg_id)
367 struct zebra_map *zm = zebra_map_get(zms, reg_id);
369 return zm->positioned;
373 int zebra_maps_is_index(ZebraMaps zms, unsigned reg_id)
375 struct zebra_map *zm = zebra_map_get(zms, reg_id);
377 return zm->type == ZEBRA_MAP_TYPE_INDEX;
381 int zebra_maps_is_staticrank(ZebraMaps zms, unsigned reg_id)
383 struct zebra_map *zm = zebra_map_get(zms, reg_id);
385 return zm->type == ZEBRA_MAP_TYPE_STATICRANK;
389 int zebra_maps_is_sort(ZebraMaps zms, unsigned reg_id)
391 struct zebra_map *zm = zebra_map_get(zms, reg_id);
393 return zm->type == ZEBRA_MAP_TYPE_SORT;
397 int zebra_maps_is_alwaysmatches(ZebraMaps zms, unsigned reg_id)
399 struct zebra_map *zm = zebra_map_get(zms, reg_id);
401 return zm->alwaysmatches;
405 int zebra_maps_is_first_in_field(ZebraMaps zms, unsigned reg_id)
407 struct zebra_map *zm = zebra_map_get(zms, reg_id);
409 return zm->first_in_field;
413 int zebra_maps_sort(ZebraMaps zms, Z_SortAttributes *sortAttributes,
419 attr_init_AttrList(&use, sortAttributes->list, 1);
420 attr_init_AttrList(&structure, sortAttributes->list, 4);
423 structure_value = attr_find(&structure, 0);
424 if (structure_value == 109)
426 return attr_find(&use, NULL);
429 int zebra_maps_attr(ZebraMaps zms, Z_AttributesPlusTerm *zapt,
430 unsigned *reg_id, char **search_type, char *rank_type,
431 int *complete_flag, int *sort_flag)
433 AttrType completeness;
436 AttrType sort_relation;
439 int completeness_value;
442 int sort_relation_value;
446 attr_init_APT(&structure, zapt, 4);
447 attr_init_APT(&completeness, zapt, 6);
448 attr_init_APT(&relation, zapt, 2);
449 attr_init_APT(&sort_relation, zapt, 7);
450 attr_init_APT(&weight, zapt, 9);
451 attr_init_APT(&use, zapt, 1);
453 completeness_value = attr_find(&completeness, NULL);
454 structure_value = attr_find(&structure, NULL);
455 relation_value = attr_find(&relation, NULL);
456 sort_relation_value = attr_find(&sort_relation, NULL);
457 weight_value = attr_find(&weight, NULL);
458 use_value = attr_find(&use, NULL);
460 if (completeness_value == 2 || completeness_value == 3)
466 *sort_flag =(sort_relation_value > 0) ? 1 : 0;
467 *search_type = "phrase";
468 strcpy(rank_type, "void");
469 if (relation_value == 102)
471 if (weight_value == -1)
473 sprintf(rank_type, "rank,w=%d,u=%d", weight_value, use_value);
479 switch (structure_value)
481 case 6: /* word list */
482 *search_type = "and-list";
484 case 105: /* free-form-text */
485 *search_type = "or-list";
487 case 106: /* document-text */
488 *search_type = "or-list";
493 case 108: /* string */
494 *search_type = "phrase";
496 case 107: /* local-number */
497 *search_type = "local";
500 case 109: /* numeric string */
502 *search_type = "numeric";
506 *search_type = "phrase";
510 *search_type = "phrase";
514 *search_type = "phrase";
518 *search_type = "phrase";
526 WRBUF zebra_replace(ZebraMaps zms, unsigned reg_id, const char *ex_list,
527 const char *input_str, int input_len)
529 wrbuf_rewind(zms->wrbuf_1);
530 wrbuf_write(zms->wrbuf_1, input_str, input_len);
537 * indent-tabs-mode: nil
539 * vim: shiftwidth=4 tabstop=8 expandtab