1 /* $Id: zebramap.c,v 1.42 2005-06-15 21:31:45 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 return zms->lookup_array[reg_id];
195 chrmaptab zebra_charmap_get (ZebraMaps zms, unsigned reg_id)
197 struct zebra_map *zm = zebra_map_get (zms, reg_id);
200 zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(*zm));
201 yaz_log (YLOG_WARN, "Unknown register type: %c", reg_id);
204 zm->maptab_name = nmem_strdup (zms->nmem, "@");
206 zm->type = ZEBRA_MAP_TYPE_INDEX;
207 zm->completeness = 0;
208 zm->next = zms->map_list;
209 zms->map_list = zm->next;
211 zms->lookup_array[zm->reg_id & 255] = zm;
215 if (!zm->maptab_name || !yaz_matchstr (zm->maptab_name, "@"))
217 if (!(zm->maptab = chrmaptab_create (zms->tabpath,
220 yaz_log(YLOG_WARN, "Failed to read character table %s",
223 yaz_log(YLOG_DEBUG, "Read character table %s", zm->maptab_name);
228 const char **zebra_maps_input (ZebraMaps zms, unsigned reg_id,
229 const char **from, int len, int first)
233 maptab = zebra_charmap_get (zms, reg_id);
235 return chr_map_input(maptab, from, len, first);
237 zms->temp_map_str[0] = **from;
240 return zms->temp_map_ptr;
243 const char **zebra_maps_search(ZebraMaps zms, unsigned reg_id,
244 const char **from, int len, int *q_map_match)
249 maptab = zebra_charmap_get (zms, reg_id);
253 map = chr_map_q_input(maptab, from, len, 0);
259 map = chr_map_input(maptab, from, len, 0);
263 zms->temp_map_str[0] = **from;
266 return zms->temp_map_ptr;
269 const char *zebra_maps_output(ZebraMaps zms, unsigned reg_id,
272 chrmaptab maptab = zebra_charmap_get (zms, reg_id);
275 return chr_map_output (maptab, from, 1);
279 /* ------------------------------------ */
285 Z_AttributeElement **attributeList;
289 static int attr_find (AttrType *src, oid_value *attributeSetP)
291 while (src->major < src->num_attributes)
293 Z_AttributeElement *element;
295 element = src->attributeList[src->major];
296 if (src->type == *element->attributeType)
298 switch (element->which)
300 case Z_AttributeValue_numeric:
302 if (element->attributeSet && attributeSetP)
306 attrset = oid_getentbyoid (element->attributeSet);
307 *attributeSetP = attrset->value;
309 return *element->value.numeric;
311 case Z_AttributeValue_complex:
312 if (src->minor >= element->value.complex->num_list ||
313 element->value.complex->list[src->minor]->which !=
314 Z_StringOrNumeric_numeric)
317 if (element->attributeSet && attributeSetP)
321 attrset = oid_getentbyoid (element->attributeSet);
322 *attributeSetP = attrset->value;
324 return *element->value.complex->list[src->minor-1]->u.numeric;
334 static void attr_init_APT (AttrType *src, Z_AttributesPlusTerm *zapt, int type)
336 src->attributeList = zapt->attributes->attributes;
337 src->num_attributes = zapt->attributes->num_attributes;
343 static void attr_init_AttrList (AttrType *src, Z_AttributeList *list, int type)
345 src->attributeList = list->attributes;
346 src->num_attributes = list->num_attributes;
352 /* ------------------------------------ */
354 int zebra_maps_is_complete (ZebraMaps zms, unsigned reg_id)
356 struct zebra_map *zm = zebra_map_get (zms, reg_id);
358 return zm->completeness;
362 int zebra_maps_is_positioned (ZebraMaps zms, unsigned reg_id)
364 struct zebra_map *zm = zebra_map_get (zms, reg_id);
366 return zm->positioned;
370 int zebra_maps_is_sort (ZebraMaps zms, unsigned reg_id)
372 struct zebra_map *zm = zebra_map_get (zms, reg_id);
374 return zm->type == ZEBRA_MAP_TYPE_SORT;
378 int zebra_maps_sort (ZebraMaps zms, Z_SortAttributes *sortAttributes,
384 attr_init_AttrList (&use, sortAttributes->list, 1);
385 attr_init_AttrList (&structure, sortAttributes->list, 4);
388 structure_value = attr_find (&structure, 0);
389 if (structure_value == 109)
391 return attr_find (&use, NULL);
394 int zebra_maps_attr (ZebraMaps zms, Z_AttributesPlusTerm *zapt,
395 unsigned *reg_id, char **search_type, char *rank_type,
396 int *complete_flag, int *sort_flag)
398 AttrType completeness;
401 AttrType sort_relation;
404 int completeness_value;
407 int sort_relation_value;
411 attr_init_APT (&structure, zapt, 4);
412 attr_init_APT (&completeness, zapt, 6);
413 attr_init_APT (&relation, zapt, 2);
414 attr_init_APT (&sort_relation, zapt, 7);
415 attr_init_APT (&weight, zapt, 9);
416 attr_init_APT (&use, zapt, 1);
418 completeness_value = attr_find (&completeness, NULL);
419 structure_value = attr_find (&structure, NULL);
420 relation_value = attr_find (&relation, NULL);
421 sort_relation_value = attr_find (&sort_relation, NULL);
422 weight_value = attr_find (&weight, NULL);
423 use_value = attr_find(&use, NULL);
425 if (completeness_value == 2 || completeness_value == 3)
431 *sort_flag = (sort_relation_value > 0) ? 1 : 0;
432 *search_type = "phrase";
433 strcpy (rank_type, "void");
434 if (relation_value == 102)
436 if (weight_value == -1)
438 sprintf (rank_type, "rank,w=%d,u=%d", weight_value, use_value);
440 if (relation_value == 103)
442 *search_type = "always";
449 switch (structure_value)
451 case 6: /* word list */
452 *search_type = "and-list";
454 case 105: /* free-form-text */
455 *search_type = "or-list";
457 case 106: /* document-text */
458 *search_type = "or-list";
463 case 108: /* string */
464 *search_type = "phrase";
466 case 107: /* local-number */
467 *search_type = "local";
470 case 109: /* numeric string */
472 *search_type = "numeric";
476 *search_type = "phrase";
480 *search_type = "phrase";
484 *search_type = "phrase";
488 *search_type = "phrase";
496 WRBUF zebra_replace(ZebraMaps zms, unsigned reg_id, const char *ex_list,
497 const char *input_str, int input_len)
499 wrbuf_rewind(zms->wrbuf_1);
500 wrbuf_write(zms->wrbuf_1, input_str, input_len);