1 /* $Id: zebramap.c,v 1.45 2006-03-26 14:05:19 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;
66 void zebra_maps_close (ZebraMaps zms)
68 struct zebra_map *zm = zms->map_list;
72 chrmaptab_destroy (zm->maptab);
75 wrbuf_free (zms->wrbuf_1, 1);
76 nmem_destroy (zms->nmem);
80 /* Returns 0 if all is well, -1 on fatal error */
81 static int zebra_map_read (ZebraMaps zms, const char *name, int fail_fatal)
88 struct zebra_map **zm = 0, *zp;
90 if (!(f = yaz_fopen(zms->tabpath, name, "r", zms->tabroot)))
92 int level = fail_fatal ? YLOG_FATAL : YLOG_WARN;
93 yaz_log(level|YLOG_ERRNO, "%s", name);
96 while ((argc = readconf_line(f, &lineno, line, 512, argv, 10)))
98 if (!yaz_matchstr (argv[0], "index") && argc == 2)
104 *zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(**zm));
105 (*zm)->reg_id = argv[1][0];
106 (*zm)->maptab_name = NULL;
107 (*zm)->maptab = NULL;
108 (*zm)->type = ZEBRA_MAP_TYPE_INDEX;
109 (*zm)->completeness = 0;
110 (*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;
127 else if (zm && !yaz_matchstr (argv[0], "charmap") && argc == 2)
129 (*zm)->maptab_name = nmem_strdup (zms->nmem, argv[1]);
131 else if (zm && !yaz_matchstr (argv[0], "completeness") && argc == 2)
133 (*zm)->completeness = atoi (argv[1]);
135 else if (zm && !yaz_matchstr (argv[0], "position") && argc == 2)
137 (*zm)->positioned = atoi (argv[1]);
139 else if (zm && !yaz_matchstr (argv[0], "entrysize") && argc == 2)
141 if ((*zm)->type == ZEBRA_MAP_TYPE_SORT)
142 (*zm)->u.sort.entry_size = atoi (argv[1]);
149 for (zp = zms->map_list; zp; zp = zp->next)
150 zms->lookup_array[zp->reg_id] = zp;
155 static void zms_map_handle (void *p, const char *name, const char *value)
157 ZebraMaps zms = (ZebraMaps) p;
159 (void) zebra_map_read (zms, value, 0);
162 ZebraMaps zebra_maps_open (Res res, const char *base)
164 ZebraMaps zms = (ZebraMaps) xmalloc (sizeof(*zms));
167 zms->nmem = nmem_create ();
168 zms->tabpath = nmem_strdup (zms->nmem,
169 res_get_def (res, "profilePath",
170 DEFAULT_PROFILE_PATH));
173 zms->tabroot = nmem_strdup (zms->nmem, base);
174 zms->map_list = NULL;
176 zms->temp_map_str[0] = '\0';
177 zms->temp_map_str[1] = '\0';
179 zms->temp_map_ptr[0] = zms->temp_map_str;
180 zms->temp_map_ptr[1] = NULL;
182 zms->lookup_array = (struct zebra_map**)
183 nmem_malloc (zms->nmem, sizeof(*zms->lookup_array)*256);
184 zms->wrbuf_1 = wrbuf_alloc();
186 for (i = 0; i<256; i++)
187 zms->lookup_array[i] = 0;
188 if (!res || !res_trav (res, "index", zms, zms_map_handle))
189 if (zebra_map_read (zms, "default.idx", 1) < 0)
191 zebra_maps_close(zms);
197 struct zebra_map *zebra_map_get (ZebraMaps zms, unsigned reg_id)
199 assert(reg_id >= 0 && reg_id <= 255);
200 return zms->lookup_array[reg_id];
203 chrmaptab zebra_charmap_get (ZebraMaps zms, unsigned reg_id)
205 struct zebra_map *zm = zebra_map_get (zms, reg_id);
208 zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(*zm));
209 yaz_log (YLOG_WARN, "Unknown register type: %c", reg_id);
212 zm->maptab_name = nmem_strdup (zms->nmem, "@");
214 zm->type = ZEBRA_MAP_TYPE_INDEX;
215 zm->completeness = 0;
216 zm->next = zms->map_list;
217 zms->map_list = zm->next;
219 zms->lookup_array[zm->reg_id & 255] = zm;
223 if (!zm->maptab_name || !yaz_matchstr (zm->maptab_name, "@"))
225 if (!(zm->maptab = chrmaptab_create (zms->tabpath,
228 yaz_log(YLOG_WARN, "Failed to read character table %s",
231 yaz_log(YLOG_DEBUG, "Read character table %s", zm->maptab_name);
236 const char **zebra_maps_input (ZebraMaps zms, unsigned reg_id,
237 const char **from, int len, int first)
241 maptab = zebra_charmap_get (zms, reg_id);
243 return chr_map_input(maptab, from, len, first);
245 zms->temp_map_str[0] = **from;
248 return zms->temp_map_ptr;
251 const char **zebra_maps_search(ZebraMaps zms, unsigned reg_id,
252 const char **from, int len, int *q_map_match)
257 maptab = zebra_charmap_get (zms, reg_id);
261 map = chr_map_q_input(maptab, from, len, 0);
267 map = chr_map_input(maptab, from, len, 0);
271 zms->temp_map_str[0] = **from;
274 return zms->temp_map_ptr;
277 const char *zebra_maps_output(ZebraMaps zms, unsigned reg_id,
280 chrmaptab maptab = zebra_charmap_get (zms, reg_id);
283 return chr_map_output (maptab, from, 1);
287 /* ------------------------------------ */
293 Z_AttributeElement **attributeList;
297 static int attr_find (AttrType *src, oid_value *attributeSetP)
299 while (src->major < src->num_attributes)
301 Z_AttributeElement *element;
303 element = src->attributeList[src->major];
304 if (src->type == *element->attributeType)
306 switch (element->which)
308 case Z_AttributeValue_numeric:
310 if (element->attributeSet && attributeSetP)
314 attrset = oid_getentbyoid (element->attributeSet);
315 *attributeSetP = attrset->value;
317 return *element->value.numeric;
319 case Z_AttributeValue_complex:
320 if (src->minor >= element->value.complex->num_list ||
321 element->value.complex->list[src->minor]->which !=
322 Z_StringOrNumeric_numeric)
325 if (element->attributeSet && attributeSetP)
329 attrset = oid_getentbyoid (element->attributeSet);
330 *attributeSetP = attrset->value;
332 return *element->value.complex->list[src->minor-1]->u.numeric;
342 static void attr_init_APT (AttrType *src, Z_AttributesPlusTerm *zapt, int type)
344 src->attributeList = zapt->attributes->attributes;
345 src->num_attributes = zapt->attributes->num_attributes;
351 static void attr_init_AttrList (AttrType *src, Z_AttributeList *list, int type)
353 src->attributeList = list->attributes;
354 src->num_attributes = list->num_attributes;
360 /* ------------------------------------ */
362 int zebra_maps_is_complete (ZebraMaps zms, unsigned reg_id)
364 struct zebra_map *zm = zebra_map_get (zms, reg_id);
366 return zm->completeness;
370 int zebra_maps_is_positioned (ZebraMaps zms, unsigned reg_id)
372 struct zebra_map *zm = zebra_map_get (zms, reg_id);
374 return zm->positioned;
378 int zebra_maps_is_sort (ZebraMaps zms, unsigned reg_id)
380 struct zebra_map *zm = zebra_map_get (zms, reg_id);
382 return zm->type == ZEBRA_MAP_TYPE_SORT;
386 int zebra_maps_sort (ZebraMaps zms, Z_SortAttributes *sortAttributes,
392 attr_init_AttrList (&use, sortAttributes->list, 1);
393 attr_init_AttrList (&structure, sortAttributes->list, 4);
396 structure_value = attr_find (&structure, 0);
397 if (structure_value == 109)
399 return attr_find (&use, NULL);
402 int zebra_maps_attr (ZebraMaps zms, Z_AttributesPlusTerm *zapt,
403 unsigned *reg_id, char **search_type, char *rank_type,
404 int *complete_flag, int *sort_flag)
406 AttrType completeness;
409 AttrType sort_relation;
412 int completeness_value;
415 int sort_relation_value;
419 attr_init_APT (&structure, zapt, 4);
420 attr_init_APT (&completeness, zapt, 6);
421 attr_init_APT (&relation, zapt, 2);
422 attr_init_APT (&sort_relation, zapt, 7);
423 attr_init_APT (&weight, zapt, 9);
424 attr_init_APT (&use, zapt, 1);
426 completeness_value = attr_find (&completeness, NULL);
427 structure_value = attr_find (&structure, NULL);
428 relation_value = attr_find (&relation, NULL);
429 sort_relation_value = attr_find (&sort_relation, NULL);
430 weight_value = attr_find (&weight, NULL);
431 use_value = attr_find(&use, NULL);
433 if (completeness_value == 2 || completeness_value == 3)
439 *sort_flag = (sort_relation_value > 0) ? 1 : 0;
440 *search_type = "phrase";
441 strcpy (rank_type, "void");
442 if (relation_value == 102)
444 if (weight_value == -1)
446 sprintf (rank_type, "rank,w=%d,u=%d", weight_value, use_value);
448 if (relation_value == 103)
450 *search_type = "always";
457 switch (structure_value)
459 case 6: /* word list */
460 *search_type = "and-list";
462 case 105: /* free-form-text */
463 *search_type = "or-list";
465 case 106: /* document-text */
466 *search_type = "or-list";
471 case 108: /* string */
472 *search_type = "phrase";
474 case 107: /* local-number */
475 *search_type = "local";
478 case 109: /* numeric string */
480 *search_type = "numeric";
484 *search_type = "phrase";
488 *search_type = "phrase";
492 *search_type = "phrase";
496 *search_type = "phrase";
504 WRBUF zebra_replace(ZebraMaps zms, unsigned reg_id, const char *ex_list,
505 const char *input_str, int input_len)
507 wrbuf_rewind(zms->wrbuf_1);
508 wrbuf_write(zms->wrbuf_1, input_str, input_len);