1 /* $Id: zebramap.c,v 1.46 2006-03-28 12:39:08 adam Exp $
2 Copyright (C) 1995-2005
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
28 #include <yaz/yaz-util.h>
30 #include <idzebra/zebramap.h>
32 #define ZEBRA_MAP_TYPE_SORT 1
33 #define ZEBRA_MAP_TYPE_INDEX 2
35 #define ZEBRA_REPLACE_ANY 300
51 const char *maptab_name;
52 struct zebra_map *next;
59 struct zebra_map *map_list;
61 const char *temp_map_ptr[2];
62 struct zebra_map **lookup_array;
67 void zebra_maps_close(ZebraMaps zms)
69 struct zebra_map *zm = zms->map_list;
73 chrmaptab_destroy(zm->maptab);
76 wrbuf_free(zms->wrbuf_1, 1);
77 nmem_destroy(zms->nmem);
81 ZEBRA_RES zebra_maps_read_file(ZebraMaps zms, const char *fname)
88 struct zebra_map **zm = 0, *zp;
90 if (!(f = yaz_fopen(zms->tabpath, fname, "r", zms->tabroot)))
92 yaz_log(YLOG_ERRNO|YLOG_FATAL, "%s", fname);
95 while ((argc = readconf_line(f, &lineno, line, 512, argv, 10)))
97 if (!yaz_matchstr(argv[0], "index") && argc == 2)
103 *zm = (struct zebra_map *) nmem_malloc(zms->nmem, sizeof(**zm));
104 (*zm)->reg_id = argv[1][0];
105 (*zm)->maptab_name = NULL;
106 (*zm)->maptab = NULL;
107 (*zm)->type = ZEBRA_MAP_TYPE_INDEX;
108 (*zm)->completeness = 0;
109 (*zm)->positioned = 1;
112 else if (!yaz_matchstr(argv[0], "sort") && argc == 2)
118 *zm = (struct zebra_map *) nmem_malloc(zms->nmem, sizeof(**zm));
119 (*zm)->reg_id = argv[1][0];
120 (*zm)->maptab_name = NULL;
121 (*zm)->type = ZEBRA_MAP_TYPE_SORT;
122 (*zm)->u.sort.entry_size = 80;
123 (*zm)->maptab = NULL;
124 (*zm)->completeness = 0;
125 (*zm)->positioned = 0;
128 else if (zm && !yaz_matchstr(argv[0], "charmap") && argc == 2)
130 (*zm)->maptab_name = nmem_strdup(zms->nmem, argv[1]);
132 else if (zm && !yaz_matchstr(argv[0], "completeness") && argc == 2)
134 (*zm)->completeness = atoi(argv[1]);
136 else if (zm && !yaz_matchstr(argv[0], "position") && argc == 2)
138 (*zm)->positioned = atoi(argv[1]);
140 else if (zm && !yaz_matchstr(argv[0], "entrysize") && argc == 2)
142 if ((*zm)->type == ZEBRA_MAP_TYPE_SORT)
143 (*zm)->u.sort.entry_size = atoi(argv[1]);
150 for (zp = zms->map_list; zp; zp = zp->next)
151 zms->lookup_array[zp->reg_id] = zp;
156 ZebraMaps zebra_maps_open(Res res, const char *base_path,
157 const char *profile_path)
159 ZebraMaps zms = (ZebraMaps) xmalloc(sizeof(*zms));
162 zms->nmem = nmem_create();
164 zms->tabpath = nmem_strdup(zms->nmem, profile_path);
167 zms->tabroot = nmem_strdup(zms->nmem, base_path);
168 zms->map_list = NULL;
170 zms->temp_map_str[0] = '\0';
171 zms->temp_map_str[1] = '\0';
173 zms->temp_map_ptr[0] = zms->temp_map_str;
174 zms->temp_map_ptr[1] = NULL;
176 zms->lookup_array = (struct zebra_map**)
177 nmem_malloc(zms->nmem, sizeof(*zms->lookup_array)*256);
178 zms->wrbuf_1 = wrbuf_alloc();
180 for (i = 0; i<256; i++)
181 zms->lookup_array[i] = 0;
185 struct zebra_map *zebra_map_get(ZebraMaps zms, unsigned reg_id)
187 assert(reg_id >= 0 && reg_id <= 255);
188 return zms->lookup_array[reg_id];
191 chrmaptab zebra_charmap_get(ZebraMaps zms, unsigned reg_id)
193 struct zebra_map *zm = zebra_map_get(zms, reg_id);
196 zm = (struct zebra_map *) nmem_malloc(zms->nmem, sizeof(*zm));
198 /* no reason to warn if no maps are installed at ALL */
200 yaz_log(YLOG_WARN, "Unknown register type: %c", reg_id);
203 zm->maptab_name = nmem_strdup(zms->nmem, "@");
205 zm->type = ZEBRA_MAP_TYPE_INDEX;
206 zm->completeness = 0;
207 zm->next = zms->map_list;
208 zms->map_list = zm->next;
210 zms->lookup_array[zm->reg_id & 255] = zm;
214 if (!zm->maptab_name || !yaz_matchstr(zm->maptab_name, "@"))
216 if (!(zm->maptab = chrmaptab_create(zms->tabpath,
219 yaz_log(YLOG_WARN, "Failed to read character table %s",
222 yaz_log(YLOG_DEBUG, "Read character table %s", zm->maptab_name);
227 const char **zebra_maps_input(ZebraMaps zms, unsigned reg_id,
228 const char **from, int len, int first)
232 maptab = zebra_charmap_get(zms, reg_id);
234 return chr_map_input(maptab, from, len, first);
236 zms->temp_map_str[0] = **from;
239 return zms->temp_map_ptr;
242 const char **zebra_maps_search(ZebraMaps zms, unsigned reg_id,
243 const char **from, int len, int *q_map_match)
248 maptab = zebra_charmap_get(zms, reg_id);
252 map = chr_map_q_input(maptab, from, len, 0);
258 map = chr_map_input(maptab, from, len, 0);
262 zms->temp_map_str[0] = **from;
265 return zms->temp_map_ptr;
268 const char *zebra_maps_output(ZebraMaps zms, unsigned reg_id,
271 chrmaptab maptab = zebra_charmap_get(zms, reg_id);
274 return chr_map_output(maptab, from, 1);
278 /* ------------------------------------ */
284 Z_AttributeElement **attributeList;
288 static int attr_find(AttrType *src, oid_value *attributeSetP)
290 while (src->major < src->num_attributes)
292 Z_AttributeElement *element;
294 element = src->attributeList[src->major];
295 if (src->type == *element->attributeType)
297 switch (element->which)
299 case Z_AttributeValue_numeric:
301 if (element->attributeSet && attributeSetP)
305 attrset = oid_getentbyoid(element->attributeSet);
306 *attributeSetP = attrset->value;
308 return *element->value.numeric;
310 case Z_AttributeValue_complex:
311 if (src->minor >= element->value.complex->num_list ||
312 element->value.complex->list[src->minor]->which !=
313 Z_StringOrNumeric_numeric)
316 if (element->attributeSet && attributeSetP)
320 attrset = oid_getentbyoid(element->attributeSet);
321 *attributeSetP = attrset->value;
323 return *element->value.complex->list[src->minor-1]->u.numeric;
333 static void attr_init_APT(AttrType *src, Z_AttributesPlusTerm *zapt, int type)
335 src->attributeList = zapt->attributes->attributes;
336 src->num_attributes = zapt->attributes->num_attributes;
342 static void attr_init_AttrList(AttrType *src, Z_AttributeList *list, int type)
344 src->attributeList = list->attributes;
345 src->num_attributes = list->num_attributes;
351 /* ------------------------------------ */
353 int zebra_maps_is_complete(ZebraMaps zms, unsigned reg_id)
355 struct zebra_map *zm = zebra_map_get(zms, reg_id);
357 return zm->completeness;
361 int zebra_maps_is_positioned(ZebraMaps zms, unsigned reg_id)
363 struct zebra_map *zm = zebra_map_get(zms, reg_id);
365 return zm->positioned;
369 int zebra_maps_is_sort(ZebraMaps zms, unsigned reg_id)
371 struct zebra_map *zm = zebra_map_get(zms, reg_id);
373 return zm->type == ZEBRA_MAP_TYPE_SORT;
377 int zebra_maps_sort(ZebraMaps zms, Z_SortAttributes *sortAttributes,
383 attr_init_AttrList(&use, sortAttributes->list, 1);
384 attr_init_AttrList(&structure, sortAttributes->list, 4);
387 structure_value = attr_find(&structure, 0);
388 if (structure_value == 109)
390 return attr_find(&use, NULL);
393 int zebra_maps_attr(ZebraMaps zms, Z_AttributesPlusTerm *zapt,
394 unsigned *reg_id, char **search_type, char *rank_type,
395 int *complete_flag, int *sort_flag)
397 AttrType completeness;
400 AttrType sort_relation;
403 int completeness_value;
406 int sort_relation_value;
410 attr_init_APT(&structure, zapt, 4);
411 attr_init_APT(&completeness, zapt, 6);
412 attr_init_APT(&relation, zapt, 2);
413 attr_init_APT(&sort_relation, zapt, 7);
414 attr_init_APT(&weight, zapt, 9);
415 attr_init_APT(&use, zapt, 1);
417 completeness_value = attr_find(&completeness, NULL);
418 structure_value = attr_find(&structure, NULL);
419 relation_value = attr_find(&relation, NULL);
420 sort_relation_value = attr_find(&sort_relation, NULL);
421 weight_value = attr_find(&weight, NULL);
422 use_value = attr_find(&use, NULL);
424 if (completeness_value == 2 || completeness_value == 3)
430 *sort_flag =(sort_relation_value > 0) ? 1 : 0;
431 *search_type = "phrase";
432 strcpy(rank_type, "void");
433 if (relation_value == 102)
435 if (weight_value == -1)
437 sprintf(rank_type, "rank,w=%d,u=%d", weight_value, use_value);
439 if (relation_value == 103)
441 *search_type = "always";
448 switch (structure_value)
450 case 6: /* word list */
451 *search_type = "and-list";
453 case 105: /* free-form-text */
454 *search_type = "or-list";
456 case 106: /* document-text */
457 *search_type = "or-list";
462 case 108: /* string */
463 *search_type = "phrase";
465 case 107: /* local-number */
466 *search_type = "local";
469 case 109: /* numeric string */
471 *search_type = "numeric";
475 *search_type = "phrase";
479 *search_type = "phrase";
483 *search_type = "phrase";
487 *search_type = "phrase";
495 WRBUF zebra_replace(ZebraMaps zms, unsigned reg_id, const char *ex_list,
496 const char *input_str, int input_len)
498 wrbuf_rewind(zms->wrbuf_1);
499 wrbuf_write(zms->wrbuf_1, input_str, input_len);