2 * Copyright (C) 1994-1998, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.8 1998-03-05 08:42:44 adam
8 * Minor changes to zebramap data structures. Query mapping rules changed.
10 * Revision 1.7 1998/02/10 12:03:07 adam
13 * Revision 1.6 1998/01/29 13:36:01 adam
14 * Structure word-list, free-form-text and document-text all
15 * trigger ranked search.
17 * Revision 1.5 1997/11/19 10:22:14 adam
18 * Bug fix (introduced by previous commit).
20 * Revision 1.4 1997/11/18 10:05:08 adam
21 * Changed character map facility so that admin can specify character
22 * mapping files for each register type, w, p, etc.
24 * Revision 1.3 1997/11/17 15:35:26 adam
25 * Bug fix. Relation=relevance wasn't observed.
27 * Revision 1.2 1997/10/31 12:39:30 adam
28 * Changed log message.
30 * Revision 1.1 1997/10/27 14:33:06 adam
31 * Moved towards generic character mapping depending on "structure"
32 * field in abstract syntax file. Fixed a few memory leaks. Fixed
33 * bug with negative integers when doing searches with relational
45 #define ZEBRA_MAP_TYPE_SORT 1
46 #define ZEBRA_MAP_TYPE_INDEX 2
61 const char *maptab_name;
62 struct zebra_map *next;
68 struct zebra_map *map_list;
70 const char *temp_map_ptr[2];
71 struct zebra_map **lookup_array;
74 void zebra_maps_close (ZebraMaps zms)
76 struct zebra_map *zm = zms->map_list;
80 chrmaptab_destroy (zm->maptab);
83 nmem_destroy (zms->nmem);
87 static void zebra_map_read (ZebraMaps zms, const char *name)
93 struct zebra_map **zm = 0, *zp;
95 if (!(f = yaz_path_fopen(zms->tabpath, name, "r")))
97 logf(LOG_WARN|LOG_ERRNO, "%s", name);
100 while ((argc = readconf_line(f, line, 512, argv, 10)))
102 if (!yaz_matchstr (argv[0], "index") && argc == 2)
108 *zm = nmem_malloc (zms->nmem, sizeof(**zm));
109 (*zm)->reg_id = argv[1][0];
110 (*zm)->maptab_name = NULL;
111 (*zm)->maptab = NULL;
112 (*zm)->type = ZEBRA_MAP_TYPE_INDEX;
113 (*zm)->completeness = 0;
115 else if (!yaz_matchstr (argv[0], "sort") && argc == 2)
121 *zm = nmem_malloc (zms->nmem, sizeof(**zm));
122 (*zm)->reg_id = argv[1][0];
123 (*zm)->maptab_name = NULL;
124 (*zm)->type = ZEBRA_MAP_TYPE_SORT;
125 (*zm)->u.sort.entry_size = 80;
126 (*zm)->maptab = NULL;
127 (*zm)->completeness = 0;
129 else if (zm && !yaz_matchstr (argv[0], "charmap") && argc == 2)
131 (*zm)->maptab_name = nmem_strdup (zms->nmem, argv[1]);
133 else if (zm && !yaz_matchstr (argv[0], "completeness") && argc == 2)
135 (*zm)->completeness = atoi (argv[1]);
137 else if (zm && !yaz_matchstr (argv[0], "entrysize") && argc == 2)
139 if ((*zm)->type == ZEBRA_MAP_TYPE_SORT)
140 (*zm)->u.sort.entry_size = atoi (argv[1]);
147 for (zp = zms->map_list; zp; zp = zp->next)
148 zms->lookup_array[zp->reg_id] = zp;
151 static void zms_map_handle (void *p, const char *name, const char *value)
155 zebra_map_read (zms, value);
158 ZebraMaps zebra_maps_open (Res res)
160 ZebraMaps zms = xmalloc (sizeof(*zms));
163 zms->nmem = nmem_create ();
164 zms->tabpath = nmem_strdup (zms->nmem, res_get (res, "profilePath"));
165 zms->map_list = NULL;
167 zms->temp_map_str[0] = '\0';
168 zms->temp_map_str[1] = '\0';
170 zms->temp_map_ptr[0] = zms->temp_map_str;
171 zms->temp_map_ptr[1] = NULL;
174 nmem_malloc (zms->nmem, sizeof(*zms->lookup_array)*256);
175 for (i = 0; i<256; i++)
176 zms->lookup_array[i] = 0;
177 if (!res || !res_trav (res, "index", zms, zms_map_handle))
178 zebra_map_read (zms, "default.idx");
182 struct zebra_map *zebra_map_get (ZebraMaps zms, unsigned reg_id)
184 return zms->lookup_array[reg_id];
187 chrmaptab zebra_charmap_get (ZebraMaps zms, unsigned reg_id)
189 struct zebra_map *zm = zebra_map_get (zms, reg_id);
192 zm = nmem_malloc (zms->nmem, sizeof(*zm));
193 logf (LOG_WARN, "Unknown register type: %c", reg_id);
196 zm->maptab_name = NULL;
198 zm->type = ZEBRA_MAP_TYPE_INDEX;
199 zm->completeness = 0;
200 zm->next = zms->map_list;
201 zms->map_list = zm->next;
203 zms->lookup_array[zm->reg_id & 255] = zm;
207 if (!zm->maptab_name || !yaz_matchstr (zm->maptab_name, "@"))
209 if (!(zm->maptab = chrmaptab_create (zms->tabpath,
210 zm->maptab_name, 0)))
211 logf(LOG_WARN, "Failed to read character table %s",
214 logf(LOG_DEBUG, "Read character table %s", zm->maptab_name);
219 const char **zebra_maps_input (ZebraMaps zms, unsigned reg_id,
220 const char **from, int len)
224 maptab = zebra_charmap_get (zms, reg_id);
226 return chr_map_input(maptab, from, len);
228 zms->temp_map_str[0] = **from;
231 return zms->temp_map_ptr;
234 const char *zebra_maps_output(ZebraMaps zms, unsigned reg_id,
238 unsigned char i = (unsigned char) **from;
239 static char buf[2] = {0,0};
241 maptab = zebra_charmap_get (zms, reg_id);
243 return chr_map_output (maptab, from, 1);
250 /* ------------------------------------ */
256 Z_AttributeElement **attributeList;
260 static int attr_find (AttrType *src, oid_value *attributeSetP)
262 while (src->major < src->num_attributes)
264 Z_AttributeElement *element;
266 element = src->attributeList[src->major];
267 if (src->type == *element->attributeType)
269 switch (element->which)
271 case Z_AttributeValue_numeric:
273 if (element->attributeSet && attributeSetP)
277 attrset = oid_getentbyoid (element->attributeSet);
278 *attributeSetP = attrset->value;
280 return *element->value.numeric;
282 case Z_AttributeValue_complex:
283 if (src->minor >= element->value.complex->num_list ||
284 element->value.complex->list[src->minor]->which !=
285 Z_StringOrNumeric_numeric)
288 if (element->attributeSet && attributeSetP)
292 attrset = oid_getentbyoid (element->attributeSet);
293 *attributeSetP = attrset->value;
295 return *element->value.complex->list[src->minor-1]->u.numeric;
305 static void attr_init_APT (AttrType *src, Z_AttributesPlusTerm *zapt, int type)
308 src->attributeList = zapt->attributeList;
309 src->num_attributes = zapt->num_attributes;
315 static void attr_init_AttrList (AttrType *src, Z_AttributeList *list, int type)
317 src->attributeList = list->attributes;
318 src->num_attributes = list->num_attributes;
324 /* ------------------------------------ */
326 int zebra_maps_is_complete (ZebraMaps zms, unsigned reg_id)
328 struct zebra_map *zm = zebra_map_get (zms, reg_id);
330 return zm->completeness;
334 int zebra_maps_is_sort (ZebraMaps zms, unsigned reg_id)
336 struct zebra_map *zm = zebra_map_get (zms, reg_id);
338 return zm->type == ZEBRA_MAP_TYPE_SORT;
342 int zebra_maps_sort (ZebraMaps zms, Z_SortAttributes *sortAttributes)
345 attr_init_AttrList (&use, sortAttributes->list, 1);
347 return attr_find (&use, NULL);
350 int zebra_maps_attr (ZebraMaps zms, Z_AttributesPlusTerm *zapt,
351 unsigned *reg_id, char **search_type, char **rank_type,
354 AttrType completeness;
357 int completeness_value;
361 attr_init_APT (&structure, zapt, 4);
362 attr_init_APT (&completeness, zapt, 6);
363 attr_init_APT (&relation, zapt, 2);
365 completeness_value = attr_find (&completeness, NULL);
366 structure_value = attr_find (&structure, NULL);
367 relation_value = attr_find (&relation, NULL);
369 if (completeness_value == 2 || completeness_value == 3)
375 *search_type = "phrase";
377 if (relation_value == 102)
384 switch (structure_value)
386 case 6: /* word list */
387 *search_type = "and-list";
389 case 105: /* free-form-text */
390 *search_type = "or-list";
392 case 106: /* document-text */
393 *search_type = "or-list";
399 case 108: /* string */
400 *search_type = "phrase";
402 case 107: /* local-number */
403 *search_type = "local";
406 case 109: /* numeric string */
408 *search_type = "phrase";
412 *search_type = "phrase";