C++ compilation.
[idzebra-moved-to-github.git] / util / zebramap.c
1 /*
2  * Copyright (C) 1994-1999, Index Data 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: zebramap.c,v $
7  * Revision 1.15  1999-05-26 07:49:14  adam
8  * C++ compilation.
9  *
10  * Revision 1.14  1999/02/19 10:37:40  adam
11  * Minor fix.
12  *
13  * Revision 1.13  1999/02/18 15:01:04  adam
14  * Structure=key uses register type 0.
15  *
16  * Revision 1.12  1999/02/12 13:29:25  adam
17  * Implemented position-flag for registers.
18  *
19  * Revision 1.11  1998/10/13 20:09:19  adam
20  * Changed call to readconf_line.
21  *
22  * Revision 1.10  1998/06/23 15:33:37  adam
23  * Added feature to specify sort criteria in query (type 7 specifies
24  * sort flags).
25  *
26  * Revision 1.9  1998/04/02 14:35:30  adam
27  * First version of Zebra that works with compiled ASN.1.
28  *
29  * Revision 1.8  1998/03/05 08:42:44  adam
30  * Minor changes to zebramap data structures. Query mapping rules changed.
31  *
32  * Revision 1.7  1998/02/10 12:03:07  adam
33  * Implemented Sort.
34  *
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.
38  *
39  * Revision 1.5  1997/11/19 10:22:14  adam
40  * Bug fix (introduced by previous commit).
41  *
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.
45  *
46  * Revision 1.3  1997/11/17 15:35:26  adam
47  * Bug fix. Relation=relevance wasn't observed.
48  *
49  * Revision 1.2  1997/10/31 12:39:30  adam
50  * Changed log message.
51  *
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
56  * operators.
57  *
58  */
59
60 #include <assert.h>
61 #include <ctype.h>
62
63 #include <yaz-util.h>
64 #include <charmap.h>
65 #include <zebramap.h>
66
67 #define ZEBRA_MAP_TYPE_SORT  1
68 #define ZEBRA_MAP_TYPE_INDEX 2
69
70 struct zebra_map {
71     unsigned reg_id;
72     int completeness;
73     int positioned;
74     int type;
75     union {
76         struct {
77             int dummy;
78         } index;
79         struct {
80             int entry_size;
81         } sort;
82     } u;
83     chrmaptab maptab;
84     const char *maptab_name;
85     struct zebra_map *next;
86 };
87
88 struct zebra_maps {
89     char *tabpath;
90     NMEM nmem;
91     struct zebra_map *map_list;
92     char temp_map_str[2];
93     const char *temp_map_ptr[2];
94     struct zebra_map **lookup_array;
95 };
96
97 void zebra_maps_close (ZebraMaps zms)
98 {
99     struct zebra_map *zm = zms->map_list;
100     while (zm)
101     {
102         if (zm->maptab)
103             chrmaptab_destroy (zm->maptab);
104         zm = zm->next;
105     }
106     nmem_destroy (zms->nmem);
107     xfree (zms);
108 }
109
110 static void zebra_map_read (ZebraMaps zms, const char *name)
111 {
112     FILE *f;
113     char line[512];
114     char *argv[10];
115     int argc;
116     int lineno = 0;
117     struct zebra_map **zm = 0, *zp;
118
119     if (!(f = yaz_path_fopen(zms->tabpath, name, "r")))
120     {
121         logf(LOG_WARN|LOG_ERRNO, "%s", name);
122         return ;
123     }
124     while ((argc = readconf_line(f, &lineno, line, 512, argv, 10)))
125     {
126         if (!yaz_matchstr (argv[0], "index") && argc == 2)
127         {
128             if (!zm)
129                 zm = &zms->map_list;
130             else
131                 zm = &(*zm)->next;
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;
139         }
140         else if (!yaz_matchstr (argv[0], "sort") && argc == 2)
141         {
142             if (!zm)
143                 zm = &zms->map_list;
144             else
145                 zm = &(*zm)->next;
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;
154         }
155         else if (zm && !yaz_matchstr (argv[0], "charmap") && argc == 2)
156         {
157             (*zm)->maptab_name = nmem_strdup (zms->nmem, argv[1]);
158         }
159         else if (zm && !yaz_matchstr (argv[0], "completeness") && argc == 2)
160         {
161             (*zm)->completeness = atoi (argv[1]);
162         }
163         else if (zm && !yaz_matchstr (argv[0], "position") && argc == 2)
164         {
165             (*zm)->positioned = atoi (argv[1]);
166         }
167         else if (zm && !yaz_matchstr (argv[0], "entrysize") && argc == 2)
168         {
169             if ((*zm)->type == ZEBRA_MAP_TYPE_SORT)
170                 (*zm)->u.sort.entry_size = atoi (argv[1]);
171         }
172     }
173     if (zm)
174         (*zm)->next = NULL;
175     fclose (f);
176
177     for (zp = zms->map_list; zp; zp = zp->next)
178         zms->lookup_array[zp->reg_id] = zp;
179 }
180
181 static void zms_map_handle (void *p, const char *name, const char *value)
182 {
183     ZebraMaps zms = (ZebraMaps) p;
184     
185     zebra_map_read (zms, value);
186 }
187
188 ZebraMaps zebra_maps_open (Res res)
189 {
190     ZebraMaps zms = (ZebraMaps) xmalloc (sizeof(*zms));
191     int i;
192
193     zms->nmem = nmem_create ();
194     zms->tabpath = nmem_strdup (zms->nmem, res_get_def (res, "profilePath", "."));
195     zms->map_list = NULL;
196
197     zms->temp_map_str[0] = '\0';
198     zms->temp_map_str[1] = '\0';
199
200     zms->temp_map_ptr[0] = zms->temp_map_str;
201     zms->temp_map_ptr[1] = NULL;
202
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");
209     return zms;
210 }
211
212 struct zebra_map *zebra_map_get (ZebraMaps zms, unsigned reg_id)
213 {
214     return zms->lookup_array[reg_id];
215 }
216
217 chrmaptab zebra_charmap_get (ZebraMaps zms, unsigned reg_id)
218 {
219     struct zebra_map *zm = zebra_map_get (zms, reg_id);
220     if (!zm)
221     {
222         zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(*zm));
223         logf (LOG_WARN, "Unknown register type: %c", reg_id);
224
225         zm->reg_id = reg_id;
226         zm->maptab_name = NULL;
227         zm->maptab = NULL;
228         zm->type = ZEBRA_MAP_TYPE_INDEX;
229         zm->completeness = 0;
230         zm->next = zms->map_list;
231         zms->map_list = zm->next;
232
233         zms->lookup_array[zm->reg_id & 255] = zm;
234     }
235     if (!zm->maptab)
236     {
237         if (!zm->maptab_name || !yaz_matchstr (zm->maptab_name, "@"))
238             return NULL;
239         if (!(zm->maptab = chrmaptab_create (zms->tabpath,
240                                              zm->maptab_name, 0)))
241             logf(LOG_WARN, "Failed to read character table %s",
242                  zm->maptab_name);
243         else
244             logf(LOG_DEBUG, "Read character table %s", zm->maptab_name);
245     }
246     return zm->maptab;
247 }
248
249 const char **zebra_maps_input (ZebraMaps zms, unsigned reg_id,
250                                const char **from, int len)
251 {
252     chrmaptab maptab;
253
254     maptab = zebra_charmap_get (zms, reg_id);
255     if (maptab)
256         return chr_map_input(maptab, from, len);
257     
258     zms->temp_map_str[0] = **from;
259
260     (*from)++;
261     return zms->temp_map_ptr;
262 }
263
264 const char *zebra_maps_output(ZebraMaps zms, unsigned reg_id,
265                               const char **from)
266 {
267     chrmaptab maptab;
268     unsigned char i = (unsigned char) **from;
269     static char buf[2] = {0,0};
270
271     maptab = zebra_charmap_get (zms, reg_id);
272     if (maptab)
273         return chr_map_output (maptab, from, 1);
274     (*from)++;
275     buf[0] = i;
276     return buf;
277 }
278
279
280 /* ------------------------------------ */
281
282 typedef struct {
283     int type;
284     int major;
285     int minor;
286     Z_AttributeElement **attributeList;
287     int num_attributes;
288 } AttrType;
289
290 static int attr_find (AttrType *src, oid_value *attributeSetP)
291 {
292     while (src->major < src->num_attributes)
293     {
294         Z_AttributeElement *element;
295
296         element = src->attributeList[src->major];
297         if (src->type == *element->attributeType)
298         {
299             switch (element->which) 
300             {
301             case Z_AttributeValue_numeric:
302                 ++(src->major);
303                 if (element->attributeSet && attributeSetP)
304                 {
305                     oident *attrset;
306
307                     attrset = oid_getentbyoid (element->attributeSet);
308                     *attributeSetP = attrset->value;
309                 }
310                 return *element->value.numeric;
311                 break;
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)
316                     break;
317                 ++(src->minor);
318                 if (element->attributeSet && attributeSetP)
319                 {
320                     oident *attrset;
321
322                     attrset = oid_getentbyoid (element->attributeSet);
323                     *attributeSetP = attrset->value;
324                 }
325                 return *element->value.complex->list[src->minor-1]->u.numeric;
326             default:
327                 assert (0);
328             }
329         }
330         ++(src->major);
331     }
332     return -1;
333 }
334
335 static void attr_init_APT (AttrType *src, Z_AttributesPlusTerm *zapt, int type)
336 {
337 #ifdef ASN_COMPILED
338     src->attributeList = zapt->attributes->attributes;
339     src->num_attributes = zapt->attributes->num_attributes;
340 #else
341     src->attributeList = zapt->attributeList;
342     src->num_attributes = zapt->num_attributes;
343 #endif
344     src->type = type;
345     src->major = 0;
346     src->minor = 0;
347 }
348
349 static void attr_init_AttrList (AttrType *src, Z_AttributeList *list, int type)
350 {
351     src->attributeList = list->attributes;
352     src->num_attributes = list->num_attributes;
353     src->type = type;
354     src->major = 0;
355     src->minor = 0;
356 }
357
358 /* ------------------------------------ */
359
360 int zebra_maps_is_complete (ZebraMaps zms, unsigned reg_id)
361
362     struct zebra_map *zm = zebra_map_get (zms, reg_id);
363     if (zm)
364         return zm->completeness;
365     return 0;
366 }
367
368 int zebra_maps_is_positioned (ZebraMaps zms, unsigned reg_id)
369 {
370     struct zebra_map *zm = zebra_map_get (zms, reg_id);
371     if (zm)
372         return zm->positioned;
373     return 0;
374 }
375     
376 int zebra_maps_is_sort (ZebraMaps zms, unsigned reg_id)
377 {
378     struct zebra_map *zm = zebra_map_get (zms, reg_id);
379     if (zm)
380         return zm->type == ZEBRA_MAP_TYPE_SORT;
381     return 0;
382 }
383
384 int zebra_maps_sort (ZebraMaps zms, Z_SortAttributes *sortAttributes)
385 {
386     AttrType use;
387     attr_init_AttrList (&use, sortAttributes->list, 1);
388
389     return attr_find (&use, NULL);
390 }
391
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)
395 {
396     AttrType completeness;
397     AttrType structure;
398     AttrType relation;
399     AttrType sort_relation;
400     int completeness_value;
401     int structure_value;
402     int relation_value;
403     int sort_relation_value;
404
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);
409
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);
414
415     if (completeness_value == 2 || completeness_value == 3)
416         *complete_flag = 1;
417     else
418         *complete_flag = 0;
419     *reg_id = 0;
420
421     *sort_flag = (sort_relation_value > 0) ? 1 : 0;
422     *search_type = "phrase";
423     *rank_type = "void";
424     if (relation_value == 102)
425         *rank_type = "rank";
426     
427     if (*complete_flag)
428         *reg_id = 'p';
429     else
430         *reg_id = 'w';
431     switch (structure_value)
432     {
433     case 6:   /* word list */
434         *search_type = "and-list";
435         break;
436     case 105: /* free-form-text */
437         *search_type = "or-list";
438         break;
439     case 106: /* document-text */
440         *search_type = "or-list";
441         break;  
442     case -1:
443     case 1:   /* phrase */
444     case 2:   /* word */
445     case 108: /* string */ 
446         *search_type = "phrase";
447         break;
448     case 107: /* local-number */
449         *search_type = "local";
450         *reg_id = 0;
451         break;
452     case 109: /* numeric string */
453         *reg_id = 'n';
454         *search_type = "numeric";
455         break;
456     case 104: /* urx */
457         *reg_id = 'u';
458         *search_type = "phrase";
459         break;
460     case 3:   /* key */
461         *reg_id = '0';
462         *search_type = "phrase";
463         break;
464     default:
465         return -1;
466     }
467     return 0;
468 }