2 * Copyright (C) 1994-1998, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.11 1998-10-13 20:09:19 adam
8 * Changed call to readconf_line.
10 * Revision 1.10 1998/06/23 15:33:37 adam
11 * Added feature to specify sort criteria in query (type 7 specifies
14 * Revision 1.9 1998/04/02 14:35:30 adam
15 * First version of Zebra that works with compiled ASN.1.
17 * Revision 1.8 1998/03/05 08:42:44 adam
18 * Minor changes to zebramap data structures. Query mapping rules changed.
20 * Revision 1.7 1998/02/10 12:03:07 adam
23 * Revision 1.6 1998/01/29 13:36:01 adam
24 * Structure word-list, free-form-text and document-text all
25 * trigger ranked search.
27 * Revision 1.5 1997/11/19 10:22:14 adam
28 * Bug fix (introduced by previous commit).
30 * Revision 1.4 1997/11/18 10:05:08 adam
31 * Changed character map facility so that admin can specify character
32 * mapping files for each register type, w, p, etc.
34 * Revision 1.3 1997/11/17 15:35:26 adam
35 * Bug fix. Relation=relevance wasn't observed.
37 * Revision 1.2 1997/10/31 12:39:30 adam
38 * Changed log message.
40 * Revision 1.1 1997/10/27 14:33:06 adam
41 * Moved towards generic character mapping depending on "structure"
42 * field in abstract syntax file. Fixed a few memory leaks. Fixed
43 * bug with negative integers when doing searches with relational
55 #define ZEBRA_MAP_TYPE_SORT 1
56 #define ZEBRA_MAP_TYPE_INDEX 2
71 const char *maptab_name;
72 struct zebra_map *next;
78 struct zebra_map *map_list;
80 const char *temp_map_ptr[2];
81 struct zebra_map **lookup_array;
84 void zebra_maps_close (ZebraMaps zms)
86 struct zebra_map *zm = zms->map_list;
90 chrmaptab_destroy (zm->maptab);
93 nmem_destroy (zms->nmem);
97 static void zebra_map_read (ZebraMaps zms, const char *name)
104 struct zebra_map **zm = 0, *zp;
106 if (!(f = yaz_path_fopen(zms->tabpath, name, "r")))
108 logf(LOG_WARN|LOG_ERRNO, "%s", name);
111 while ((argc = readconf_line(f, &lineno, line, 512, argv, 10)))
113 if (!yaz_matchstr (argv[0], "index") && argc == 2)
119 *zm = nmem_malloc (zms->nmem, sizeof(**zm));
120 (*zm)->reg_id = argv[1][0];
121 (*zm)->maptab_name = NULL;
122 (*zm)->maptab = NULL;
123 (*zm)->type = ZEBRA_MAP_TYPE_INDEX;
124 (*zm)->completeness = 0;
126 else if (!yaz_matchstr (argv[0], "sort") && argc == 2)
132 *zm = nmem_malloc (zms->nmem, sizeof(**zm));
133 (*zm)->reg_id = argv[1][0];
134 (*zm)->maptab_name = NULL;
135 (*zm)->type = ZEBRA_MAP_TYPE_SORT;
136 (*zm)->u.sort.entry_size = 80;
137 (*zm)->maptab = NULL;
138 (*zm)->completeness = 0;
140 else if (zm && !yaz_matchstr (argv[0], "charmap") && argc == 2)
142 (*zm)->maptab_name = nmem_strdup (zms->nmem, argv[1]);
144 else if (zm && !yaz_matchstr (argv[0], "completeness") && argc == 2)
146 (*zm)->completeness = atoi (argv[1]);
148 else if (zm && !yaz_matchstr (argv[0], "entrysize") && argc == 2)
150 if ((*zm)->type == ZEBRA_MAP_TYPE_SORT)
151 (*zm)->u.sort.entry_size = atoi (argv[1]);
158 for (zp = zms->map_list; zp; zp = zp->next)
159 zms->lookup_array[zp->reg_id] = zp;
162 static void zms_map_handle (void *p, const char *name, const char *value)
166 zebra_map_read (zms, value);
169 ZebraMaps zebra_maps_open (Res res)
171 ZebraMaps zms = xmalloc (sizeof(*zms));
174 zms->nmem = nmem_create ();
175 zms->tabpath = nmem_strdup (zms->nmem, res_get (res, "profilePath"));
176 zms->map_list = NULL;
178 zms->temp_map_str[0] = '\0';
179 zms->temp_map_str[1] = '\0';
181 zms->temp_map_ptr[0] = zms->temp_map_str;
182 zms->temp_map_ptr[1] = NULL;
185 nmem_malloc (zms->nmem, sizeof(*zms->lookup_array)*256);
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 zebra_map_read (zms, "default.idx");
193 struct zebra_map *zebra_map_get (ZebraMaps zms, unsigned reg_id)
195 return zms->lookup_array[reg_id];
198 chrmaptab zebra_charmap_get (ZebraMaps zms, unsigned reg_id)
200 struct zebra_map *zm = zebra_map_get (zms, reg_id);
203 zm = nmem_malloc (zms->nmem, sizeof(*zm));
204 logf (LOG_WARN, "Unknown register type: %c", reg_id);
207 zm->maptab_name = NULL;
209 zm->type = ZEBRA_MAP_TYPE_INDEX;
210 zm->completeness = 0;
211 zm->next = zms->map_list;
212 zms->map_list = zm->next;
214 zms->lookup_array[zm->reg_id & 255] = zm;
218 if (!zm->maptab_name || !yaz_matchstr (zm->maptab_name, "@"))
220 if (!(zm->maptab = chrmaptab_create (zms->tabpath,
221 zm->maptab_name, 0)))
222 logf(LOG_WARN, "Failed to read character table %s",
225 logf(LOG_DEBUG, "Read character table %s", zm->maptab_name);
230 const char **zebra_maps_input (ZebraMaps zms, unsigned reg_id,
231 const char **from, int len)
235 maptab = zebra_charmap_get (zms, reg_id);
237 return chr_map_input(maptab, from, len);
239 zms->temp_map_str[0] = **from;
242 return zms->temp_map_ptr;
245 const char *zebra_maps_output(ZebraMaps zms, unsigned reg_id,
249 unsigned char i = (unsigned char) **from;
250 static char buf[2] = {0,0};
252 maptab = zebra_charmap_get (zms, reg_id);
254 return chr_map_output (maptab, from, 1);
261 /* ------------------------------------ */
267 Z_AttributeElement **attributeList;
271 static int attr_find (AttrType *src, oid_value *attributeSetP)
273 while (src->major < src->num_attributes)
275 Z_AttributeElement *element;
277 element = src->attributeList[src->major];
278 if (src->type == *element->attributeType)
280 switch (element->which)
282 case Z_AttributeValue_numeric:
284 if (element->attributeSet && attributeSetP)
288 attrset = oid_getentbyoid (element->attributeSet);
289 *attributeSetP = attrset->value;
291 return *element->value.numeric;
293 case Z_AttributeValue_complex:
294 if (src->minor >= element->value.complex->num_list ||
295 element->value.complex->list[src->minor]->which !=
296 Z_StringOrNumeric_numeric)
299 if (element->attributeSet && attributeSetP)
303 attrset = oid_getentbyoid (element->attributeSet);
304 *attributeSetP = attrset->value;
306 return *element->value.complex->list[src->minor-1]->u.numeric;
316 static void attr_init_APT (AttrType *src, Z_AttributesPlusTerm *zapt, int type)
319 src->attributeList = zapt->attributes->attributes;
320 src->num_attributes = zapt->attributes->num_attributes;
322 src->attributeList = zapt->attributeList;
323 src->num_attributes = zapt->num_attributes;
330 static void attr_init_AttrList (AttrType *src, Z_AttributeList *list, int type)
332 src->attributeList = list->attributes;
333 src->num_attributes = list->num_attributes;
339 /* ------------------------------------ */
341 int zebra_maps_is_complete (ZebraMaps zms, unsigned reg_id)
343 struct zebra_map *zm = zebra_map_get (zms, reg_id);
345 return zm->completeness;
349 int zebra_maps_is_sort (ZebraMaps zms, unsigned reg_id)
351 struct zebra_map *zm = zebra_map_get (zms, reg_id);
353 return zm->type == ZEBRA_MAP_TYPE_SORT;
357 int zebra_maps_sort (ZebraMaps zms, Z_SortAttributes *sortAttributes)
360 attr_init_AttrList (&use, sortAttributes->list, 1);
362 return attr_find (&use, NULL);
365 int zebra_maps_attr (ZebraMaps zms, Z_AttributesPlusTerm *zapt,
366 unsigned *reg_id, char **search_type, char **rank_type,
367 int *complete_flag, int *sort_flag)
369 AttrType completeness;
372 AttrType sort_relation;
373 int completeness_value;
376 int sort_relation_value;
378 attr_init_APT (&structure, zapt, 4);
379 attr_init_APT (&completeness, zapt, 6);
380 attr_init_APT (&relation, zapt, 2);
381 attr_init_APT (&sort_relation, zapt, 7);
383 completeness_value = attr_find (&completeness, NULL);
384 structure_value = attr_find (&structure, NULL);
385 relation_value = attr_find (&relation, NULL);
386 sort_relation_value = attr_find (&sort_relation, NULL);
388 if (completeness_value == 2 || completeness_value == 3)
394 *sort_flag = (sort_relation_value > 0) ? 1 : 0;
395 *search_type = "phrase";
397 if (relation_value == 102)
404 switch (structure_value)
406 case 6: /* word list */
407 *search_type = "and-list";
409 case 105: /* free-form-text */
410 *search_type = "or-list";
412 case 106: /* document-text */
413 *search_type = "or-list";
419 case 108: /* string */
420 *search_type = "phrase";
422 case 107: /* local-number */
423 *search_type = "local";
426 case 109: /* numeric string */
428 *search_type = "numeric";
432 *search_type = "phrase";