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