1 /* $Id: zebramap.c,v 1.43 2005-08-30 12:23:02 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 static void zebra_map_read (ZebraMaps zms, const char *name)
87 struct zebra_map **zm = 0, *zp;
89 if (!(f = yaz_fopen(zms->tabpath, name, "r", zms->tabroot)))
91 yaz_log(YLOG_WARN|YLOG_ERRNO, "%s", name);
94 while ((argc = readconf_line(f, &lineno, line, 512, argv, 10)))
96 if (!yaz_matchstr (argv[0], "index") && argc == 2)
102 *zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(**zm));
103 (*zm)->reg_id = argv[1][0];
104 (*zm)->maptab_name = NULL;
105 (*zm)->maptab = NULL;
106 (*zm)->type = ZEBRA_MAP_TYPE_INDEX;
107 (*zm)->completeness = 0;
108 (*zm)->positioned = 1;
110 else if (!yaz_matchstr (argv[0], "sort") && argc == 2)
116 *zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(**zm));
117 (*zm)->reg_id = argv[1][0];
118 (*zm)->maptab_name = NULL;
119 (*zm)->type = ZEBRA_MAP_TYPE_SORT;
120 (*zm)->u.sort.entry_size = 80;
121 (*zm)->maptab = NULL;
122 (*zm)->completeness = 0;
123 (*zm)->positioned = 0;
125 else if (zm && !yaz_matchstr (argv[0], "charmap") && argc == 2)
127 (*zm)->maptab_name = nmem_strdup (zms->nmem, argv[1]);
129 else if (zm && !yaz_matchstr (argv[0], "completeness") && argc == 2)
131 (*zm)->completeness = atoi (argv[1]);
133 else if (zm && !yaz_matchstr (argv[0], "position") && argc == 2)
135 (*zm)->positioned = atoi (argv[1]);
137 else if (zm && !yaz_matchstr (argv[0], "entrysize") && argc == 2)
139 if ((*zm)->type == ZEBRA_MAP_TYPE_SORT)
140 (*zm)->u.sort.entry_size = atoi (argv[1]);
147 for (zp = zms->map_list; zp; zp = zp->next)
148 zms->lookup_array[zp->reg_id] = zp;
151 static void zms_map_handle (void *p, const char *name, const char *value)
153 ZebraMaps zms = (ZebraMaps) p;
155 zebra_map_read (zms, value);
158 ZebraMaps zebra_maps_open (Res res, const char *base)
160 ZebraMaps zms = (ZebraMaps) xmalloc (sizeof(*zms));
163 zms->nmem = nmem_create ();
164 zms->tabpath = nmem_strdup (zms->nmem,
165 res_get_def (res, "profilePath",
166 DEFAULT_PROFILE_PATH));
169 zms->tabroot = nmem_strdup (zms->nmem, base);
170 zms->map_list = NULL;
172 zms->temp_map_str[0] = '\0';
173 zms->temp_map_str[1] = '\0';
175 zms->temp_map_ptr[0] = zms->temp_map_str;
176 zms->temp_map_ptr[1] = NULL;
178 zms->lookup_array = (struct zebra_map**)
179 nmem_malloc (zms->nmem, sizeof(*zms->lookup_array)*256);
180 for (i = 0; i<256; i++)
181 zms->lookup_array[i] = 0;
182 if (!res || !res_trav (res, "index", zms, zms_map_handle))
183 zebra_map_read (zms, "default.idx");
185 zms->wrbuf_1 = wrbuf_alloc();
190 struct zebra_map *zebra_map_get (ZebraMaps zms, unsigned reg_id)
192 assert(reg_id >= 0 && reg_id <= 255);
193 return zms->lookup_array[reg_id];
196 chrmaptab zebra_charmap_get (ZebraMaps zms, unsigned reg_id)
198 struct zebra_map *zm = zebra_map_get (zms, reg_id);
201 zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(*zm));
202 yaz_log (YLOG_WARN, "Unknown register type: %c", reg_id);
205 zm->maptab_name = nmem_strdup (zms->nmem, "@");
207 zm->type = ZEBRA_MAP_TYPE_INDEX;
208 zm->completeness = 0;
209 zm->next = zms->map_list;
210 zms->map_list = zm->next;
212 zms->lookup_array[zm->reg_id & 255] = zm;
216 if (!zm->maptab_name || !yaz_matchstr (zm->maptab_name, "@"))
218 if (!(zm->maptab = chrmaptab_create (zms->tabpath,
221 yaz_log(YLOG_WARN, "Failed to read character table %s",
224 yaz_log(YLOG_DEBUG, "Read character table %s", zm->maptab_name);
229 const char **zebra_maps_input (ZebraMaps zms, unsigned reg_id,
230 const char **from, int len, int first)
234 maptab = zebra_charmap_get (zms, reg_id);
236 return chr_map_input(maptab, from, len, first);
238 zms->temp_map_str[0] = **from;
241 return zms->temp_map_ptr;
244 const char **zebra_maps_search(ZebraMaps zms, unsigned reg_id,
245 const char **from, int len, int *q_map_match)
250 maptab = zebra_charmap_get (zms, reg_id);
254 map = chr_map_q_input(maptab, from, len, 0);
260 map = chr_map_input(maptab, from, len, 0);
264 zms->temp_map_str[0] = **from;
267 return zms->temp_map_ptr;
270 const char *zebra_maps_output(ZebraMaps zms, unsigned reg_id,
273 chrmaptab maptab = zebra_charmap_get (zms, reg_id);
276 return chr_map_output (maptab, from, 1);
280 /* ------------------------------------ */
286 Z_AttributeElement **attributeList;
290 static int attr_find (AttrType *src, oid_value *attributeSetP)
292 while (src->major < src->num_attributes)
294 Z_AttributeElement *element;
296 element = src->attributeList[src->major];
297 if (src->type == *element->attributeType)
299 switch (element->which)
301 case Z_AttributeValue_numeric:
303 if (element->attributeSet && attributeSetP)
307 attrset = oid_getentbyoid (element->attributeSet);
308 *attributeSetP = attrset->value;
310 return *element->value.numeric;
312 case Z_AttributeValue_complex:
313 if (src->minor >= element->value.complex->num_list ||
314 element->value.complex->list[src->minor]->which !=
315 Z_StringOrNumeric_numeric)
318 if (element->attributeSet && attributeSetP)
322 attrset = oid_getentbyoid (element->attributeSet);
323 *attributeSetP = attrset->value;
325 return *element->value.complex->list[src->minor-1]->u.numeric;
335 static void attr_init_APT (AttrType *src, Z_AttributesPlusTerm *zapt, int type)
337 src->attributeList = zapt->attributes->attributes;
338 src->num_attributes = zapt->attributes->num_attributes;
344 static void attr_init_AttrList (AttrType *src, Z_AttributeList *list, int type)
346 src->attributeList = list->attributes;
347 src->num_attributes = list->num_attributes;
353 /* ------------------------------------ */
355 int zebra_maps_is_complete (ZebraMaps zms, unsigned reg_id)
357 struct zebra_map *zm = zebra_map_get (zms, reg_id);
359 return zm->completeness;
363 int zebra_maps_is_positioned (ZebraMaps zms, unsigned reg_id)
365 struct zebra_map *zm = zebra_map_get (zms, reg_id);
367 return zm->positioned;
371 int zebra_maps_is_sort (ZebraMaps zms, unsigned reg_id)
373 struct zebra_map *zm = zebra_map_get (zms, reg_id);
375 return zm->type == ZEBRA_MAP_TYPE_SORT;
379 int zebra_maps_sort (ZebraMaps zms, Z_SortAttributes *sortAttributes,
385 attr_init_AttrList (&use, sortAttributes->list, 1);
386 attr_init_AttrList (&structure, sortAttributes->list, 4);
389 structure_value = attr_find (&structure, 0);
390 if (structure_value == 109)
392 return attr_find (&use, NULL);
395 int zebra_maps_attr (ZebraMaps zms, Z_AttributesPlusTerm *zapt,
396 unsigned *reg_id, char **search_type, char *rank_type,
397 int *complete_flag, int *sort_flag)
399 AttrType completeness;
402 AttrType sort_relation;
405 int completeness_value;
408 int sort_relation_value;
412 attr_init_APT (&structure, zapt, 4);
413 attr_init_APT (&completeness, zapt, 6);
414 attr_init_APT (&relation, zapt, 2);
415 attr_init_APT (&sort_relation, zapt, 7);
416 attr_init_APT (&weight, zapt, 9);
417 attr_init_APT (&use, zapt, 1);
419 completeness_value = attr_find (&completeness, NULL);
420 structure_value = attr_find (&structure, NULL);
421 relation_value = attr_find (&relation, NULL);
422 sort_relation_value = attr_find (&sort_relation, NULL);
423 weight_value = attr_find (&weight, NULL);
424 use_value = attr_find(&use, NULL);
426 if (completeness_value == 2 || completeness_value == 3)
432 *sort_flag = (sort_relation_value > 0) ? 1 : 0;
433 *search_type = "phrase";
434 strcpy (rank_type, "void");
435 if (relation_value == 102)
437 if (weight_value == -1)
439 sprintf (rank_type, "rank,w=%d,u=%d", weight_value, use_value);
441 if (relation_value == 103)
443 *search_type = "always";
450 switch (structure_value)
452 case 6: /* word list */
453 *search_type = "and-list";
455 case 105: /* free-form-text */
456 *search_type = "or-list";
458 case 106: /* document-text */
459 *search_type = "or-list";
464 case 108: /* string */
465 *search_type = "phrase";
467 case 107: /* local-number */
468 *search_type = "local";
471 case 109: /* numeric string */
473 *search_type = "numeric";
477 *search_type = "phrase";
481 *search_type = "phrase";
485 *search_type = "phrase";
489 *search_type = "phrase";
497 WRBUF zebra_replace(ZebraMaps zms, unsigned reg_id, const char *ex_list,
498 const char *input_str, int input_len)
500 wrbuf_rewind(zms->wrbuf_1);
501 wrbuf_write(zms->wrbuf_1, input_str, input_len);