2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.12 1999-02-12 13:29:25 adam
8 * Implemented position-flag for registers.
10 * Revision 1.11 1998/10/13 20:09:19 adam
11 * Changed call to readconf_line.
13 * Revision 1.10 1998/06/23 15:33:37 adam
14 * Added feature to specify sort criteria in query (type 7 specifies
17 * Revision 1.9 1998/04/02 14:35:30 adam
18 * First version of Zebra that works with compiled ASN.1.
20 * Revision 1.8 1998/03/05 08:42:44 adam
21 * Minor changes to zebramap data structures. Query mapping rules changed.
23 * Revision 1.7 1998/02/10 12:03:07 adam
26 * Revision 1.6 1998/01/29 13:36:01 adam
27 * Structure word-list, free-form-text and document-text all
28 * trigger ranked search.
30 * Revision 1.5 1997/11/19 10:22:14 adam
31 * Bug fix (introduced by previous commit).
33 * Revision 1.4 1997/11/18 10:05:08 adam
34 * Changed character map facility so that admin can specify character
35 * mapping files for each register type, w, p, etc.
37 * Revision 1.3 1997/11/17 15:35:26 adam
38 * Bug fix. Relation=relevance wasn't observed.
40 * Revision 1.2 1997/10/31 12:39:30 adam
41 * Changed log message.
43 * Revision 1.1 1997/10/27 14:33:06 adam
44 * Moved towards generic character mapping depending on "structure"
45 * field in abstract syntax file. Fixed a few memory leaks. Fixed
46 * bug with negative integers when doing searches with relational
58 #define ZEBRA_MAP_TYPE_SORT 1
59 #define ZEBRA_MAP_TYPE_INDEX 2
75 const char *maptab_name;
76 struct zebra_map *next;
82 struct zebra_map *map_list;
84 const char *temp_map_ptr[2];
85 struct zebra_map **lookup_array;
88 void zebra_maps_close (ZebraMaps zms)
90 struct zebra_map *zm = zms->map_list;
94 chrmaptab_destroy (zm->maptab);
97 nmem_destroy (zms->nmem);
101 static void zebra_map_read (ZebraMaps zms, const char *name)
108 struct zebra_map **zm = 0, *zp;
110 if (!(f = yaz_path_fopen(zms->tabpath, name, "r")))
112 logf(LOG_WARN|LOG_ERRNO, "%s", name);
115 while ((argc = readconf_line(f, &lineno, line, 512, argv, 10)))
117 if (!yaz_matchstr (argv[0], "index") && argc == 2)
123 *zm = nmem_malloc (zms->nmem, sizeof(**zm));
124 (*zm)->reg_id = argv[1][0];
125 (*zm)->maptab_name = NULL;
126 (*zm)->maptab = NULL;
127 (*zm)->type = ZEBRA_MAP_TYPE_INDEX;
128 (*zm)->completeness = 0;
129 (*zm)->positioned = 1;
131 else if (!yaz_matchstr (argv[0], "sort") && argc == 2)
137 *zm = nmem_malloc (zms->nmem, sizeof(**zm));
138 (*zm)->reg_id = argv[1][0];
139 (*zm)->maptab_name = NULL;
140 (*zm)->type = ZEBRA_MAP_TYPE_SORT;
141 (*zm)->u.sort.entry_size = 80;
142 (*zm)->maptab = NULL;
143 (*zm)->completeness = 0;
144 (*zm)->positioned = 0;
146 else if (zm && !yaz_matchstr (argv[0], "charmap") && argc == 2)
148 (*zm)->maptab_name = nmem_strdup (zms->nmem, argv[1]);
150 else if (zm && !yaz_matchstr (argv[0], "completeness") && argc == 2)
152 (*zm)->completeness = atoi (argv[1]);
154 else if (zm && !yaz_matchstr (argv[0], "position") && argc == 2)
156 (*zm)->positioned = atoi (argv[1]);
158 else if (zm && !yaz_matchstr (argv[0], "entrysize") && argc == 2)
160 if ((*zm)->type == ZEBRA_MAP_TYPE_SORT)
161 (*zm)->u.sort.entry_size = atoi (argv[1]);
168 for (zp = zms->map_list; zp; zp = zp->next)
169 zms->lookup_array[zp->reg_id] = zp;
172 static void zms_map_handle (void *p, const char *name, const char *value)
176 zebra_map_read (zms, value);
179 ZebraMaps zebra_maps_open (Res res)
181 ZebraMaps zms = xmalloc (sizeof(*zms));
184 zms->nmem = nmem_create ();
185 zms->tabpath = nmem_strdup (zms->nmem, res_get (res, "profilePath"));
186 zms->map_list = NULL;
188 zms->temp_map_str[0] = '\0';
189 zms->temp_map_str[1] = '\0';
191 zms->temp_map_ptr[0] = zms->temp_map_str;
192 zms->temp_map_ptr[1] = NULL;
195 nmem_malloc (zms->nmem, sizeof(*zms->lookup_array)*256);
196 for (i = 0; i<256; i++)
197 zms->lookup_array[i] = 0;
198 if (!res || !res_trav (res, "index", zms, zms_map_handle))
199 zebra_map_read (zms, "default.idx");
203 struct zebra_map *zebra_map_get (ZebraMaps zms, unsigned reg_id)
205 return zms->lookup_array[reg_id];
208 chrmaptab zebra_charmap_get (ZebraMaps zms, unsigned reg_id)
210 struct zebra_map *zm = zebra_map_get (zms, reg_id);
213 zm = nmem_malloc (zms->nmem, sizeof(*zm));
214 logf (LOG_WARN, "Unknown register type: %c", reg_id);
217 zm->maptab_name = NULL;
219 zm->type = ZEBRA_MAP_TYPE_INDEX;
220 zm->completeness = 0;
221 zm->next = zms->map_list;
222 zms->map_list = zm->next;
224 zms->lookup_array[zm->reg_id & 255] = zm;
228 if (!zm->maptab_name || !yaz_matchstr (zm->maptab_name, "@"))
230 if (!(zm->maptab = chrmaptab_create (zms->tabpath,
231 zm->maptab_name, 0)))
232 logf(LOG_WARN, "Failed to read character table %s",
235 logf(LOG_DEBUG, "Read character table %s", zm->maptab_name);
240 const char **zebra_maps_input (ZebraMaps zms, unsigned reg_id,
241 const char **from, int len)
245 maptab = zebra_charmap_get (zms, reg_id);
247 return chr_map_input(maptab, from, len);
249 zms->temp_map_str[0] = **from;
252 return zms->temp_map_ptr;
255 const char *zebra_maps_output(ZebraMaps zms, unsigned reg_id,
259 unsigned char i = (unsigned char) **from;
260 static char buf[2] = {0,0};
262 maptab = zebra_charmap_get (zms, reg_id);
264 return chr_map_output (maptab, from, 1);
271 /* ------------------------------------ */
277 Z_AttributeElement **attributeList;
281 static int attr_find (AttrType *src, oid_value *attributeSetP)
283 while (src->major < src->num_attributes)
285 Z_AttributeElement *element;
287 element = src->attributeList[src->major];
288 if (src->type == *element->attributeType)
290 switch (element->which)
292 case Z_AttributeValue_numeric:
294 if (element->attributeSet && attributeSetP)
298 attrset = oid_getentbyoid (element->attributeSet);
299 *attributeSetP = attrset->value;
301 return *element->value.numeric;
303 case Z_AttributeValue_complex:
304 if (src->minor >= element->value.complex->num_list ||
305 element->value.complex->list[src->minor]->which !=
306 Z_StringOrNumeric_numeric)
309 if (element->attributeSet && attributeSetP)
313 attrset = oid_getentbyoid (element->attributeSet);
314 *attributeSetP = attrset->value;
316 return *element->value.complex->list[src->minor-1]->u.numeric;
326 static void attr_init_APT (AttrType *src, Z_AttributesPlusTerm *zapt, int type)
329 src->attributeList = zapt->attributes->attributes;
330 src->num_attributes = zapt->attributes->num_attributes;
332 src->attributeList = zapt->attributeList;
333 src->num_attributes = zapt->num_attributes;
340 static void attr_init_AttrList (AttrType *src, Z_AttributeList *list, int type)
342 src->attributeList = list->attributes;
343 src->num_attributes = list->num_attributes;
349 /* ------------------------------------ */
351 int zebra_maps_is_complete (ZebraMaps zms, unsigned reg_id)
353 struct zebra_map *zm = zebra_map_get (zms, reg_id);
355 return zm->completeness;
359 int zebra_maps_is_positioned (ZebraMaps zms, unsigned reg_id)
361 struct zebra_map *zm = zebra_map_get (zms, reg_id);
363 return zm->positioned;
367 int zebra_maps_is_sort (ZebraMaps zms, unsigned reg_id)
369 struct zebra_map *zm = zebra_map_get (zms, reg_id);
371 return zm->type == ZEBRA_MAP_TYPE_SORT;
375 int zebra_maps_sort (ZebraMaps zms, Z_SortAttributes *sortAttributes)
378 attr_init_AttrList (&use, sortAttributes->list, 1);
380 return attr_find (&use, NULL);
383 int zebra_maps_attr (ZebraMaps zms, Z_AttributesPlusTerm *zapt,
384 unsigned *reg_id, char **search_type, char **rank_type,
385 int *complete_flag, int *sort_flag)
387 AttrType completeness;
390 AttrType sort_relation;
391 int completeness_value;
394 int sort_relation_value;
396 attr_init_APT (&structure, zapt, 4);
397 attr_init_APT (&completeness, zapt, 6);
398 attr_init_APT (&relation, zapt, 2);
399 attr_init_APT (&sort_relation, zapt, 7);
401 completeness_value = attr_find (&completeness, NULL);
402 structure_value = attr_find (&structure, NULL);
403 relation_value = attr_find (&relation, NULL);
404 sort_relation_value = attr_find (&sort_relation, NULL);
406 if (completeness_value == 2 || completeness_value == 3)
412 *sort_flag = (sort_relation_value > 0) ? 1 : 0;
413 *search_type = "phrase";
415 if (relation_value == 102)
422 switch (structure_value)
424 case 6: /* word list */
425 *search_type = "and-list";
427 case 105: /* free-form-text */
428 *search_type = "or-list";
430 case 106: /* document-text */
431 *search_type = "or-list";
437 case 108: /* string */
438 *search_type = "phrase";
440 case 107: /* local-number */
441 *search_type = "local";
444 case 109: /* numeric string */
446 *search_type = "numeric";
450 *search_type = "phrase";