2 * Copyright (C) 1994-1998, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.10 1998-06-23 15:33:37 adam
8 * Added feature to specify sort criteria in query (type 7 specifies
11 * Revision 1.9 1998/04/02 14:35:30 adam
12 * First version of Zebra that works with compiled ASN.1.
14 * Revision 1.8 1998/03/05 08:42:44 adam
15 * Minor changes to zebramap data structures. Query mapping rules changed.
17 * Revision 1.7 1998/02/10 12:03:07 adam
20 * Revision 1.6 1998/01/29 13:36:01 adam
21 * Structure word-list, free-form-text and document-text all
22 * trigger ranked search.
24 * Revision 1.5 1997/11/19 10:22:14 adam
25 * Bug fix (introduced by previous commit).
27 * Revision 1.4 1997/11/18 10:05:08 adam
28 * Changed character map facility so that admin can specify character
29 * mapping files for each register type, w, p, etc.
31 * Revision 1.3 1997/11/17 15:35:26 adam
32 * Bug fix. Relation=relevance wasn't observed.
34 * Revision 1.2 1997/10/31 12:39:30 adam
35 * Changed log message.
37 * Revision 1.1 1997/10/27 14:33:06 adam
38 * Moved towards generic character mapping depending on "structure"
39 * field in abstract syntax file. Fixed a few memory leaks. Fixed
40 * bug with negative integers when doing searches with relational
52 #define ZEBRA_MAP_TYPE_SORT 1
53 #define ZEBRA_MAP_TYPE_INDEX 2
68 const char *maptab_name;
69 struct zebra_map *next;
75 struct zebra_map *map_list;
77 const char *temp_map_ptr[2];
78 struct zebra_map **lookup_array;
81 void zebra_maps_close (ZebraMaps zms)
83 struct zebra_map *zm = zms->map_list;
87 chrmaptab_destroy (zm->maptab);
90 nmem_destroy (zms->nmem);
94 static void zebra_map_read (ZebraMaps zms, const char *name)
100 struct zebra_map **zm = 0, *zp;
102 if (!(f = yaz_path_fopen(zms->tabpath, name, "r")))
104 logf(LOG_WARN|LOG_ERRNO, "%s", name);
107 while ((argc = readconf_line(f, line, 512, argv, 10)))
109 if (!yaz_matchstr (argv[0], "index") && argc == 2)
115 *zm = nmem_malloc (zms->nmem, sizeof(**zm));
116 (*zm)->reg_id = argv[1][0];
117 (*zm)->maptab_name = NULL;
118 (*zm)->maptab = NULL;
119 (*zm)->type = ZEBRA_MAP_TYPE_INDEX;
120 (*zm)->completeness = 0;
122 else if (!yaz_matchstr (argv[0], "sort") && argc == 2)
128 *zm = nmem_malloc (zms->nmem, sizeof(**zm));
129 (*zm)->reg_id = argv[1][0];
130 (*zm)->maptab_name = NULL;
131 (*zm)->type = ZEBRA_MAP_TYPE_SORT;
132 (*zm)->u.sort.entry_size = 80;
133 (*zm)->maptab = NULL;
134 (*zm)->completeness = 0;
136 else if (zm && !yaz_matchstr (argv[0], "charmap") && argc == 2)
138 (*zm)->maptab_name = nmem_strdup (zms->nmem, argv[1]);
140 else if (zm && !yaz_matchstr (argv[0], "completeness") && argc == 2)
142 (*zm)->completeness = atoi (argv[1]);
144 else if (zm && !yaz_matchstr (argv[0], "entrysize") && argc == 2)
146 if ((*zm)->type == ZEBRA_MAP_TYPE_SORT)
147 (*zm)->u.sort.entry_size = atoi (argv[1]);
154 for (zp = zms->map_list; zp; zp = zp->next)
155 zms->lookup_array[zp->reg_id] = zp;
158 static void zms_map_handle (void *p, const char *name, const char *value)
162 zebra_map_read (zms, value);
165 ZebraMaps zebra_maps_open (Res res)
167 ZebraMaps zms = xmalloc (sizeof(*zms));
170 zms->nmem = nmem_create ();
171 zms->tabpath = nmem_strdup (zms->nmem, res_get (res, "profilePath"));
172 zms->map_list = NULL;
174 zms->temp_map_str[0] = '\0';
175 zms->temp_map_str[1] = '\0';
177 zms->temp_map_ptr[0] = zms->temp_map_str;
178 zms->temp_map_ptr[1] = NULL;
181 nmem_malloc (zms->nmem, sizeof(*zms->lookup_array)*256);
182 for (i = 0; i<256; i++)
183 zms->lookup_array[i] = 0;
184 if (!res || !res_trav (res, "index", zms, zms_map_handle))
185 zebra_map_read (zms, "default.idx");
189 struct zebra_map *zebra_map_get (ZebraMaps zms, unsigned reg_id)
191 return zms->lookup_array[reg_id];
194 chrmaptab zebra_charmap_get (ZebraMaps zms, unsigned reg_id)
196 struct zebra_map *zm = zebra_map_get (zms, reg_id);
199 zm = nmem_malloc (zms->nmem, sizeof(*zm));
200 logf (LOG_WARN, "Unknown register type: %c", reg_id);
203 zm->maptab_name = NULL;
205 zm->type = ZEBRA_MAP_TYPE_INDEX;
206 zm->completeness = 0;
207 zm->next = zms->map_list;
208 zms->map_list = zm->next;
210 zms->lookup_array[zm->reg_id & 255] = zm;
214 if (!zm->maptab_name || !yaz_matchstr (zm->maptab_name, "@"))
216 if (!(zm->maptab = chrmaptab_create (zms->tabpath,
217 zm->maptab_name, 0)))
218 logf(LOG_WARN, "Failed to read character table %s",
221 logf(LOG_DEBUG, "Read character table %s", zm->maptab_name);
226 const char **zebra_maps_input (ZebraMaps zms, unsigned reg_id,
227 const char **from, int len)
231 maptab = zebra_charmap_get (zms, reg_id);
233 return chr_map_input(maptab, from, len);
235 zms->temp_map_str[0] = **from;
238 return zms->temp_map_ptr;
241 const char *zebra_maps_output(ZebraMaps zms, unsigned reg_id,
245 unsigned char i = (unsigned char) **from;
246 static char buf[2] = {0,0};
248 maptab = zebra_charmap_get (zms, reg_id);
250 return chr_map_output (maptab, from, 1);
257 /* ------------------------------------ */
263 Z_AttributeElement **attributeList;
267 static int attr_find (AttrType *src, oid_value *attributeSetP)
269 while (src->major < src->num_attributes)
271 Z_AttributeElement *element;
273 element = src->attributeList[src->major];
274 if (src->type == *element->attributeType)
276 switch (element->which)
278 case Z_AttributeValue_numeric:
280 if (element->attributeSet && attributeSetP)
284 attrset = oid_getentbyoid (element->attributeSet);
285 *attributeSetP = attrset->value;
287 return *element->value.numeric;
289 case Z_AttributeValue_complex:
290 if (src->minor >= element->value.complex->num_list ||
291 element->value.complex->list[src->minor]->which !=
292 Z_StringOrNumeric_numeric)
295 if (element->attributeSet && attributeSetP)
299 attrset = oid_getentbyoid (element->attributeSet);
300 *attributeSetP = attrset->value;
302 return *element->value.complex->list[src->minor-1]->u.numeric;
312 static void attr_init_APT (AttrType *src, Z_AttributesPlusTerm *zapt, int type)
315 src->attributeList = zapt->attributes->attributes;
316 src->num_attributes = zapt->attributes->num_attributes;
318 src->attributeList = zapt->attributeList;
319 src->num_attributes = zapt->num_attributes;
326 static void attr_init_AttrList (AttrType *src, Z_AttributeList *list, int type)
328 src->attributeList = list->attributes;
329 src->num_attributes = list->num_attributes;
335 /* ------------------------------------ */
337 int zebra_maps_is_complete (ZebraMaps zms, unsigned reg_id)
339 struct zebra_map *zm = zebra_map_get (zms, reg_id);
341 return zm->completeness;
345 int zebra_maps_is_sort (ZebraMaps zms, unsigned reg_id)
347 struct zebra_map *zm = zebra_map_get (zms, reg_id);
349 return zm->type == ZEBRA_MAP_TYPE_SORT;
353 int zebra_maps_sort (ZebraMaps zms, Z_SortAttributes *sortAttributes)
356 attr_init_AttrList (&use, sortAttributes->list, 1);
358 return attr_find (&use, NULL);
361 int zebra_maps_attr (ZebraMaps zms, Z_AttributesPlusTerm *zapt,
362 unsigned *reg_id, char **search_type, char **rank_type,
363 int *complete_flag, int *sort_flag)
365 AttrType completeness;
368 AttrType sort_relation;
369 int completeness_value;
372 int sort_relation_value;
374 attr_init_APT (&structure, zapt, 4);
375 attr_init_APT (&completeness, zapt, 6);
376 attr_init_APT (&relation, zapt, 2);
377 attr_init_APT (&sort_relation, zapt, 7);
379 completeness_value = attr_find (&completeness, NULL);
380 structure_value = attr_find (&structure, NULL);
381 relation_value = attr_find (&relation, NULL);
382 sort_relation_value = attr_find (&sort_relation, NULL);
384 if (completeness_value == 2 || completeness_value == 3)
390 *sort_flag = (sort_relation_value > 0) ? 1 : 0;
391 *search_type = "phrase";
393 if (relation_value == 102)
400 switch (structure_value)
402 case 6: /* word list */
403 *search_type = "and-list";
405 case 105: /* free-form-text */
406 *search_type = "or-list";
408 case 106: /* document-text */
409 *search_type = "or-list";
415 case 108: /* string */
416 *search_type = "phrase";
418 case 107: /* local-number */
419 *search_type = "local";
422 case 109: /* numeric string */
424 *search_type = "numeric";
428 *search_type = "phrase";