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