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