2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.15 1999-05-26 07:49:14 adam
10 * Revision 1.14 1999/02/19 10:37:40 adam
13 * Revision 1.13 1999/02/18 15:01:04 adam
14 * Structure=key uses register type 0.
16 * Revision 1.12 1999/02/12 13:29:25 adam
17 * Implemented position-flag for registers.
19 * Revision 1.11 1998/10/13 20:09:19 adam
20 * Changed call to readconf_line.
22 * Revision 1.10 1998/06/23 15:33:37 adam
23 * Added feature to specify sort criteria in query (type 7 specifies
26 * Revision 1.9 1998/04/02 14:35:30 adam
27 * First version of Zebra that works with compiled ASN.1.
29 * Revision 1.8 1998/03/05 08:42:44 adam
30 * Minor changes to zebramap data structures. Query mapping rules changed.
32 * Revision 1.7 1998/02/10 12:03:07 adam
35 * Revision 1.6 1998/01/29 13:36:01 adam
36 * Structure word-list, free-form-text and document-text all
37 * trigger ranked search.
39 * Revision 1.5 1997/11/19 10:22:14 adam
40 * Bug fix (introduced by previous commit).
42 * Revision 1.4 1997/11/18 10:05:08 adam
43 * Changed character map facility so that admin can specify character
44 * mapping files for each register type, w, p, etc.
46 * Revision 1.3 1997/11/17 15:35:26 adam
47 * Bug fix. Relation=relevance wasn't observed.
49 * Revision 1.2 1997/10/31 12:39:30 adam
50 * Changed log message.
52 * Revision 1.1 1997/10/27 14:33:06 adam
53 * Moved towards generic character mapping depending on "structure"
54 * field in abstract syntax file. Fixed a few memory leaks. Fixed
55 * bug with negative integers when doing searches with relational
67 #define ZEBRA_MAP_TYPE_SORT 1
68 #define ZEBRA_MAP_TYPE_INDEX 2
84 const char *maptab_name;
85 struct zebra_map *next;
91 struct zebra_map *map_list;
93 const char *temp_map_ptr[2];
94 struct zebra_map **lookup_array;
97 void zebra_maps_close (ZebraMaps zms)
99 struct zebra_map *zm = zms->map_list;
103 chrmaptab_destroy (zm->maptab);
106 nmem_destroy (zms->nmem);
110 static void zebra_map_read (ZebraMaps zms, const char *name)
117 struct zebra_map **zm = 0, *zp;
119 if (!(f = yaz_path_fopen(zms->tabpath, name, "r")))
121 logf(LOG_WARN|LOG_ERRNO, "%s", name);
124 while ((argc = readconf_line(f, &lineno, line, 512, argv, 10)))
126 if (!yaz_matchstr (argv[0], "index") && argc == 2)
132 *zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(**zm));
133 (*zm)->reg_id = argv[1][0];
134 (*zm)->maptab_name = NULL;
135 (*zm)->maptab = NULL;
136 (*zm)->type = ZEBRA_MAP_TYPE_INDEX;
137 (*zm)->completeness = 0;
138 (*zm)->positioned = 1;
140 else if (!yaz_matchstr (argv[0], "sort") && argc == 2)
146 *zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(**zm));
147 (*zm)->reg_id = argv[1][0];
148 (*zm)->maptab_name = NULL;
149 (*zm)->type = ZEBRA_MAP_TYPE_SORT;
150 (*zm)->u.sort.entry_size = 80;
151 (*zm)->maptab = NULL;
152 (*zm)->completeness = 0;
153 (*zm)->positioned = 0;
155 else if (zm && !yaz_matchstr (argv[0], "charmap") && argc == 2)
157 (*zm)->maptab_name = nmem_strdup (zms->nmem, argv[1]);
159 else if (zm && !yaz_matchstr (argv[0], "completeness") && argc == 2)
161 (*zm)->completeness = atoi (argv[1]);
163 else if (zm && !yaz_matchstr (argv[0], "position") && argc == 2)
165 (*zm)->positioned = atoi (argv[1]);
167 else if (zm && !yaz_matchstr (argv[0], "entrysize") && argc == 2)
169 if ((*zm)->type == ZEBRA_MAP_TYPE_SORT)
170 (*zm)->u.sort.entry_size = atoi (argv[1]);
177 for (zp = zms->map_list; zp; zp = zp->next)
178 zms->lookup_array[zp->reg_id] = zp;
181 static void zms_map_handle (void *p, const char *name, const char *value)
183 ZebraMaps zms = (ZebraMaps) p;
185 zebra_map_read (zms, value);
188 ZebraMaps zebra_maps_open (Res res)
190 ZebraMaps zms = (ZebraMaps) xmalloc (sizeof(*zms));
193 zms->nmem = nmem_create ();
194 zms->tabpath = nmem_strdup (zms->nmem, res_get_def (res, "profilePath", "."));
195 zms->map_list = NULL;
197 zms->temp_map_str[0] = '\0';
198 zms->temp_map_str[1] = '\0';
200 zms->temp_map_ptr[0] = zms->temp_map_str;
201 zms->temp_map_ptr[1] = NULL;
203 zms->lookup_array = (struct zebra_map**)
204 nmem_malloc (zms->nmem, sizeof(*zms->lookup_array)*256);
205 for (i = 0; i<256; i++)
206 zms->lookup_array[i] = 0;
207 if (!res || !res_trav (res, "index", zms, zms_map_handle))
208 zebra_map_read (zms, "default.idx");
212 struct zebra_map *zebra_map_get (ZebraMaps zms, unsigned reg_id)
214 return zms->lookup_array[reg_id];
217 chrmaptab zebra_charmap_get (ZebraMaps zms, unsigned reg_id)
219 struct zebra_map *zm = zebra_map_get (zms, reg_id);
222 zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(*zm));
223 logf (LOG_WARN, "Unknown register type: %c", reg_id);
226 zm->maptab_name = NULL;
228 zm->type = ZEBRA_MAP_TYPE_INDEX;
229 zm->completeness = 0;
230 zm->next = zms->map_list;
231 zms->map_list = zm->next;
233 zms->lookup_array[zm->reg_id & 255] = zm;
237 if (!zm->maptab_name || !yaz_matchstr (zm->maptab_name, "@"))
239 if (!(zm->maptab = chrmaptab_create (zms->tabpath,
240 zm->maptab_name, 0)))
241 logf(LOG_WARN, "Failed to read character table %s",
244 logf(LOG_DEBUG, "Read character table %s", zm->maptab_name);
249 const char **zebra_maps_input (ZebraMaps zms, unsigned reg_id,
250 const char **from, int len)
254 maptab = zebra_charmap_get (zms, reg_id);
256 return chr_map_input(maptab, from, len);
258 zms->temp_map_str[0] = **from;
261 return zms->temp_map_ptr;
264 const char *zebra_maps_output(ZebraMaps zms, unsigned reg_id,
268 unsigned char i = (unsigned char) **from;
269 static char buf[2] = {0,0};
271 maptab = zebra_charmap_get (zms, reg_id);
273 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)
338 src->attributeList = zapt->attributes->attributes;
339 src->num_attributes = zapt->attributes->num_attributes;
341 src->attributeList = zapt->attributeList;
342 src->num_attributes = zapt->num_attributes;
349 static void attr_init_AttrList (AttrType *src, Z_AttributeList *list, int type)
351 src->attributeList = list->attributes;
352 src->num_attributes = list->num_attributes;
358 /* ------------------------------------ */
360 int zebra_maps_is_complete (ZebraMaps zms, unsigned reg_id)
362 struct zebra_map *zm = zebra_map_get (zms, reg_id);
364 return zm->completeness;
368 int zebra_maps_is_positioned (ZebraMaps zms, unsigned reg_id)
370 struct zebra_map *zm = zebra_map_get (zms, reg_id);
372 return zm->positioned;
376 int zebra_maps_is_sort (ZebraMaps zms, unsigned reg_id)
378 struct zebra_map *zm = zebra_map_get (zms, reg_id);
380 return zm->type == ZEBRA_MAP_TYPE_SORT;
384 int zebra_maps_sort (ZebraMaps zms, Z_SortAttributes *sortAttributes)
387 attr_init_AttrList (&use, sortAttributes->list, 1);
389 return attr_find (&use, NULL);
392 int zebra_maps_attr (ZebraMaps zms, Z_AttributesPlusTerm *zapt,
393 unsigned *reg_id, char **search_type, char **rank_type,
394 int *complete_flag, int *sort_flag)
396 AttrType completeness;
399 AttrType sort_relation;
400 int completeness_value;
403 int sort_relation_value;
405 attr_init_APT (&structure, zapt, 4);
406 attr_init_APT (&completeness, zapt, 6);
407 attr_init_APT (&relation, zapt, 2);
408 attr_init_APT (&sort_relation, zapt, 7);
410 completeness_value = attr_find (&completeness, NULL);
411 structure_value = attr_find (&structure, NULL);
412 relation_value = attr_find (&relation, NULL);
413 sort_relation_value = attr_find (&sort_relation, NULL);
415 if (completeness_value == 2 || completeness_value == 3)
421 *sort_flag = (sort_relation_value > 0) ? 1 : 0;
422 *search_type = "phrase";
424 if (relation_value == 102)
431 switch (structure_value)
433 case 6: /* word list */
434 *search_type = "and-list";
436 case 105: /* free-form-text */
437 *search_type = "or-list";
439 case 106: /* document-text */
440 *search_type = "or-list";
445 case 108: /* string */
446 *search_type = "phrase";
448 case 107: /* local-number */
449 *search_type = "local";
452 case 109: /* numeric string */
454 *search_type = "numeric";
458 *search_type = "phrase";
462 *search_type = "phrase";