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