2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.14 1999-02-19 10:37:40 adam
10 * Revision 1.13 1999/02/18 15:01:04 adam
11 * Structure=key uses register type 0.
13 * Revision 1.12 1999/02/12 13:29:25 adam
14 * Implemented position-flag for registers.
16 * Revision 1.11 1998/10/13 20:09:19 adam
17 * Changed call to readconf_line.
19 * Revision 1.10 1998/06/23 15:33:37 adam
20 * Added feature to specify sort criteria in query (type 7 specifies
23 * Revision 1.9 1998/04/02 14:35:30 adam
24 * First version of Zebra that works with compiled ASN.1.
26 * Revision 1.8 1998/03/05 08:42:44 adam
27 * Minor changes to zebramap data structures. Query mapping rules changed.
29 * Revision 1.7 1998/02/10 12:03:07 adam
32 * Revision 1.6 1998/01/29 13:36:01 adam
33 * Structure word-list, free-form-text and document-text all
34 * trigger ranked search.
36 * Revision 1.5 1997/11/19 10:22:14 adam
37 * Bug fix (introduced by previous commit).
39 * Revision 1.4 1997/11/18 10:05:08 adam
40 * Changed character map facility so that admin can specify character
41 * mapping files for each register type, w, p, etc.
43 * Revision 1.3 1997/11/17 15:35:26 adam
44 * Bug fix. Relation=relevance wasn't observed.
46 * Revision 1.2 1997/10/31 12:39:30 adam
47 * Changed log message.
49 * Revision 1.1 1997/10/27 14:33:06 adam
50 * Moved towards generic character mapping depending on "structure"
51 * field in abstract syntax file. Fixed a few memory leaks. Fixed
52 * bug with negative integers when doing searches with relational
64 #define ZEBRA_MAP_TYPE_SORT 1
65 #define ZEBRA_MAP_TYPE_INDEX 2
81 const char *maptab_name;
82 struct zebra_map *next;
88 struct zebra_map *map_list;
90 const char *temp_map_ptr[2];
91 struct zebra_map **lookup_array;
94 void zebra_maps_close (ZebraMaps zms)
96 struct zebra_map *zm = zms->map_list;
100 chrmaptab_destroy (zm->maptab);
103 nmem_destroy (zms->nmem);
107 static void zebra_map_read (ZebraMaps zms, const char *name)
114 struct zebra_map **zm = 0, *zp;
116 if (!(f = yaz_path_fopen(zms->tabpath, name, "r")))
118 logf(LOG_WARN|LOG_ERRNO, "%s", name);
121 while ((argc = readconf_line(f, &lineno, line, 512, argv, 10)))
123 if (!yaz_matchstr (argv[0], "index") && argc == 2)
129 *zm = nmem_malloc (zms->nmem, sizeof(**zm));
130 (*zm)->reg_id = argv[1][0];
131 (*zm)->maptab_name = NULL;
132 (*zm)->maptab = NULL;
133 (*zm)->type = ZEBRA_MAP_TYPE_INDEX;
134 (*zm)->completeness = 0;
135 (*zm)->positioned = 1;
137 else if (!yaz_matchstr (argv[0], "sort") && argc == 2)
143 *zm = nmem_malloc (zms->nmem, sizeof(**zm));
144 (*zm)->reg_id = argv[1][0];
145 (*zm)->maptab_name = NULL;
146 (*zm)->type = ZEBRA_MAP_TYPE_SORT;
147 (*zm)->u.sort.entry_size = 80;
148 (*zm)->maptab = NULL;
149 (*zm)->completeness = 0;
150 (*zm)->positioned = 0;
152 else if (zm && !yaz_matchstr (argv[0], "charmap") && argc == 2)
154 (*zm)->maptab_name = nmem_strdup (zms->nmem, argv[1]);
156 else if (zm && !yaz_matchstr (argv[0], "completeness") && argc == 2)
158 (*zm)->completeness = atoi (argv[1]);
160 else if (zm && !yaz_matchstr (argv[0], "position") && argc == 2)
162 (*zm)->positioned = atoi (argv[1]);
164 else if (zm && !yaz_matchstr (argv[0], "entrysize") && argc == 2)
166 if ((*zm)->type == ZEBRA_MAP_TYPE_SORT)
167 (*zm)->u.sort.entry_size = atoi (argv[1]);
174 for (zp = zms->map_list; zp; zp = zp->next)
175 zms->lookup_array[zp->reg_id] = zp;
178 static void zms_map_handle (void *p, const char *name, const char *value)
182 zebra_map_read (zms, value);
185 ZebraMaps zebra_maps_open (Res res)
187 ZebraMaps zms = xmalloc (sizeof(*zms));
190 zms->nmem = nmem_create ();
191 zms->tabpath = nmem_strdup (zms->nmem, res_get_def (res, "profilePath", "."));
192 zms->map_list = NULL;
194 zms->temp_map_str[0] = '\0';
195 zms->temp_map_str[1] = '\0';
197 zms->temp_map_ptr[0] = zms->temp_map_str;
198 zms->temp_map_ptr[1] = NULL;
201 nmem_malloc (zms->nmem, sizeof(*zms->lookup_array)*256);
202 for (i = 0; i<256; i++)
203 zms->lookup_array[i] = 0;
204 if (!res || !res_trav (res, "index", zms, zms_map_handle))
205 zebra_map_read (zms, "default.idx");
209 struct zebra_map *zebra_map_get (ZebraMaps zms, unsigned reg_id)
211 return zms->lookup_array[reg_id];
214 chrmaptab zebra_charmap_get (ZebraMaps zms, unsigned reg_id)
216 struct zebra_map *zm = zebra_map_get (zms, reg_id);
219 zm = nmem_malloc (zms->nmem, sizeof(*zm));
220 logf (LOG_WARN, "Unknown register type: %c", reg_id);
223 zm->maptab_name = NULL;
225 zm->type = ZEBRA_MAP_TYPE_INDEX;
226 zm->completeness = 0;
227 zm->next = zms->map_list;
228 zms->map_list = zm->next;
230 zms->lookup_array[zm->reg_id & 255] = zm;
234 if (!zm->maptab_name || !yaz_matchstr (zm->maptab_name, "@"))
236 if (!(zm->maptab = chrmaptab_create (zms->tabpath,
237 zm->maptab_name, 0)))
238 logf(LOG_WARN, "Failed to read character table %s",
241 logf(LOG_DEBUG, "Read character table %s", zm->maptab_name);
246 const char **zebra_maps_input (ZebraMaps zms, unsigned reg_id,
247 const char **from, int len)
251 maptab = zebra_charmap_get (zms, reg_id);
253 return chr_map_input(maptab, from, len);
255 zms->temp_map_str[0] = **from;
258 return zms->temp_map_ptr;
261 const char *zebra_maps_output(ZebraMaps zms, unsigned reg_id,
265 unsigned char i = (unsigned char) **from;
266 static char buf[2] = {0,0};
268 maptab = zebra_charmap_get (zms, reg_id);
270 return chr_map_output (maptab, from, 1);
277 /* ------------------------------------ */
283 Z_AttributeElement **attributeList;
287 static int attr_find (AttrType *src, oid_value *attributeSetP)
289 while (src->major < src->num_attributes)
291 Z_AttributeElement *element;
293 element = src->attributeList[src->major];
294 if (src->type == *element->attributeType)
296 switch (element->which)
298 case Z_AttributeValue_numeric:
300 if (element->attributeSet && attributeSetP)
304 attrset = oid_getentbyoid (element->attributeSet);
305 *attributeSetP = attrset->value;
307 return *element->value.numeric;
309 case Z_AttributeValue_complex:
310 if (src->minor >= element->value.complex->num_list ||
311 element->value.complex->list[src->minor]->which !=
312 Z_StringOrNumeric_numeric)
315 if (element->attributeSet && attributeSetP)
319 attrset = oid_getentbyoid (element->attributeSet);
320 *attributeSetP = attrset->value;
322 return *element->value.complex->list[src->minor-1]->u.numeric;
332 static void attr_init_APT (AttrType *src, Z_AttributesPlusTerm *zapt, int type)
335 src->attributeList = zapt->attributes->attributes;
336 src->num_attributes = zapt->attributes->num_attributes;
338 src->attributeList = zapt->attributeList;
339 src->num_attributes = zapt->num_attributes;
346 static void attr_init_AttrList (AttrType *src, Z_AttributeList *list, int type)
348 src->attributeList = list->attributes;
349 src->num_attributes = list->num_attributes;
355 /* ------------------------------------ */
357 int zebra_maps_is_complete (ZebraMaps zms, unsigned reg_id)
359 struct zebra_map *zm = zebra_map_get (zms, reg_id);
361 return zm->completeness;
365 int zebra_maps_is_positioned (ZebraMaps zms, unsigned reg_id)
367 struct zebra_map *zm = zebra_map_get (zms, reg_id);
369 return zm->positioned;
373 int zebra_maps_is_sort (ZebraMaps zms, unsigned reg_id)
375 struct zebra_map *zm = zebra_map_get (zms, reg_id);
377 return zm->type == ZEBRA_MAP_TYPE_SORT;
381 int zebra_maps_sort (ZebraMaps zms, Z_SortAttributes *sortAttributes)
384 attr_init_AttrList (&use, sortAttributes->list, 1);
386 return attr_find (&use, NULL);
389 int zebra_maps_attr (ZebraMaps zms, Z_AttributesPlusTerm *zapt,
390 unsigned *reg_id, char **search_type, char **rank_type,
391 int *complete_flag, int *sort_flag)
393 AttrType completeness;
396 AttrType sort_relation;
397 int completeness_value;
400 int sort_relation_value;
402 attr_init_APT (&structure, zapt, 4);
403 attr_init_APT (&completeness, zapt, 6);
404 attr_init_APT (&relation, zapt, 2);
405 attr_init_APT (&sort_relation, zapt, 7);
407 completeness_value = attr_find (&completeness, NULL);
408 structure_value = attr_find (&structure, NULL);
409 relation_value = attr_find (&relation, NULL);
410 sort_relation_value = attr_find (&sort_relation, NULL);
412 if (completeness_value == 2 || completeness_value == 3)
418 *sort_flag = (sort_relation_value > 0) ? 1 : 0;
419 *search_type = "phrase";
421 if (relation_value == 102)
428 switch (structure_value)
430 case 6: /* word list */
431 *search_type = "and-list";
433 case 105: /* free-form-text */
434 *search_type = "or-list";
436 case 106: /* document-text */
437 *search_type = "or-list";
442 case 108: /* string */
443 *search_type = "phrase";
445 case 107: /* local-number */
446 *search_type = "local";
449 case 109: /* numeric string */
451 *search_type = "numeric";
455 *search_type = "phrase";
459 *search_type = "phrase";