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