Fix zebra_maps_open not to leak if default.idx cannot be found
[idzebra-moved-to-github.git] / util / zebramap.c
1 /* $Id: zebramap.c,v 1.45 2006-03-26 14:05:19 adam Exp $
2    Copyright (C) 1995-2005
3    Index Data ApS
4
5 This file is part of the Zebra server.
6
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra.  If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
21 */
22
23 #include <assert.h>
24 #include <stdlib.h>
25 #include <ctype.h>
26
27 #include <charmap.h>
28 #include <yaz/yaz-util.h>
29
30 #include <idzebra/zebramap.h>
31
32 #define ZEBRA_MAP_TYPE_SORT  1
33 #define ZEBRA_MAP_TYPE_INDEX 2
34
35 #define ZEBRA_REPLACE_ANY  300
36
37 struct zebra_map {
38     unsigned reg_id;
39     int completeness;
40     int positioned;
41     int type;
42     union {
43         struct {
44             int dummy;
45         } index;
46         struct {
47             int entry_size;
48         } sort;
49     } u;
50     chrmaptab maptab;
51     const char *maptab_name;
52     struct zebra_map *next;
53 };
54
55 struct zebra_maps {
56     char *tabpath;
57     char *tabroot;
58     NMEM nmem;
59     struct zebra_map *map_list;
60     char temp_map_str[2];
61     const char *temp_map_ptr[2];
62     struct zebra_map **lookup_array;
63     WRBUF wrbuf_1;
64 };
65
66 void zebra_maps_close (ZebraMaps zms)
67 {
68     struct zebra_map *zm = zms->map_list;
69     while (zm)
70     {
71         if (zm->maptab)
72             chrmaptab_destroy (zm->maptab);
73         zm = zm->next;
74     }
75     wrbuf_free (zms->wrbuf_1, 1);
76     nmem_destroy (zms->nmem);
77     xfree (zms);
78 }
79
80 /* Returns 0 if all is well, -1 on fatal error */
81 static int zebra_map_read (ZebraMaps zms, const char *name, int fail_fatal)
82 {
83     FILE *f;
84     char line[512];
85     char *argv[10];
86     int argc;
87     int lineno = 0;
88     struct zebra_map **zm = 0, *zp;
89
90     if (!(f = yaz_fopen(zms->tabpath, name, "r", zms->tabroot)))
91     {
92         int level = fail_fatal ? YLOG_FATAL : YLOG_WARN;
93         yaz_log(level|YLOG_ERRNO, "%s", name);
94         return -1;
95     }
96     while ((argc = readconf_line(f, &lineno, line, 512, argv, 10)))
97     {
98         if (!yaz_matchstr (argv[0], "index") && argc == 2)
99         {
100             if (!zm)
101                 zm = &zms->map_list;
102             else
103                 zm = &(*zm)->next;
104             *zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(**zm));
105             (*zm)->reg_id = argv[1][0];
106             (*zm)->maptab_name = NULL;
107             (*zm)->maptab = NULL;
108             (*zm)->type = ZEBRA_MAP_TYPE_INDEX;
109             (*zm)->completeness = 0;
110             (*zm)->positioned = 1;
111         }
112         else if (!yaz_matchstr (argv[0], "sort") && argc == 2)
113         {
114             if (!zm)
115                 zm = &zms->map_list;
116             else
117                 zm = &(*zm)->next;
118             *zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(**zm));
119             (*zm)->reg_id = argv[1][0];
120             (*zm)->maptab_name = NULL;
121             (*zm)->type = ZEBRA_MAP_TYPE_SORT;
122             (*zm)->u.sort.entry_size = 80;
123             (*zm)->maptab = NULL;
124             (*zm)->completeness = 0;
125             (*zm)->positioned = 0;
126         }
127         else if (zm && !yaz_matchstr (argv[0], "charmap") && argc == 2)
128         {
129             (*zm)->maptab_name = nmem_strdup (zms->nmem, argv[1]);
130         }
131         else if (zm && !yaz_matchstr (argv[0], "completeness") && argc == 2)
132         {
133             (*zm)->completeness = atoi (argv[1]);
134         }
135         else if (zm && !yaz_matchstr (argv[0], "position") && argc == 2)
136         {
137             (*zm)->positioned = atoi (argv[1]);
138         }
139         else if (zm && !yaz_matchstr (argv[0], "entrysize") && argc == 2)
140         {
141             if ((*zm)->type == ZEBRA_MAP_TYPE_SORT)
142                 (*zm)->u.sort.entry_size = atoi (argv[1]);
143         }
144     }
145     if (zm)
146         (*zm)->next = NULL;
147     yaz_fclose (f);
148
149     for (zp = zms->map_list; zp; zp = zp->next)
150         zms->lookup_array[zp->reg_id] = zp;
151
152     return 0;
153 }
154
155 static void zms_map_handle (void *p, const char *name, const char *value)
156 {
157     ZebraMaps zms = (ZebraMaps) p;
158     
159     (void) zebra_map_read (zms, value, 0);
160 }
161
162 ZebraMaps zebra_maps_open (Res res, const char *base)
163 {
164     ZebraMaps zms = (ZebraMaps) xmalloc (sizeof(*zms));
165     int i;
166
167     zms->nmem = nmem_create ();
168     zms->tabpath = nmem_strdup (zms->nmem,
169                                 res_get_def (res, "profilePath",
170                                              DEFAULT_PROFILE_PATH));
171     zms->tabroot = 0;
172     if (base)
173         zms->tabroot = nmem_strdup (zms->nmem, base);
174     zms->map_list = NULL;
175
176     zms->temp_map_str[0] = '\0';
177     zms->temp_map_str[1] = '\0';
178
179     zms->temp_map_ptr[0] = zms->temp_map_str;
180     zms->temp_map_ptr[1] = NULL;
181
182     zms->lookup_array = (struct zebra_map**)
183         nmem_malloc (zms->nmem, sizeof(*zms->lookup_array)*256);
184     zms->wrbuf_1 = wrbuf_alloc();
185
186     for (i = 0; i<256; i++)
187         zms->lookup_array[i] = 0;
188     if (!res || !res_trav (res, "index", zms, zms_map_handle))
189         if (zebra_map_read (zms, "default.idx", 1) < 0)
190         {
191             zebra_maps_close(zms);
192             return 0;
193         }
194     return zms;
195 }
196
197 struct zebra_map *zebra_map_get (ZebraMaps zms, unsigned reg_id)
198 {
199     assert(reg_id >= 0 && reg_id <= 255);
200     return zms->lookup_array[reg_id];
201 }
202
203 chrmaptab zebra_charmap_get (ZebraMaps zms, unsigned reg_id)
204 {
205     struct zebra_map *zm = zebra_map_get (zms, reg_id);
206     if (!zm)
207     {
208         zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(*zm));
209         yaz_log (YLOG_WARN, "Unknown register type: %c", reg_id);
210
211         zm->reg_id = reg_id;
212         zm->maptab_name = nmem_strdup (zms->nmem, "@");
213         zm->maptab = NULL;
214         zm->type = ZEBRA_MAP_TYPE_INDEX;
215         zm->completeness = 0;
216         zm->next = zms->map_list;
217         zms->map_list = zm->next;
218
219         zms->lookup_array[zm->reg_id & 255] = zm;
220     }
221     if (!zm->maptab)
222     {
223         if (!zm->maptab_name || !yaz_matchstr (zm->maptab_name, "@"))
224             return NULL;
225         if (!(zm->maptab = chrmaptab_create (zms->tabpath,
226                                              zm->maptab_name,
227                                              zms->tabroot)))
228             yaz_log(YLOG_WARN, "Failed to read character table %s",
229                  zm->maptab_name);
230         else
231             yaz_log(YLOG_DEBUG, "Read character table %s", zm->maptab_name);
232     }
233     return zm->maptab;
234 }
235
236 const char **zebra_maps_input (ZebraMaps zms, unsigned reg_id,
237                                const char **from, int len, int first)
238 {
239     chrmaptab maptab;
240
241     maptab = zebra_charmap_get (zms, reg_id);
242     if (maptab)
243         return chr_map_input(maptab, from, len, first);
244     
245     zms->temp_map_str[0] = **from;
246
247     (*from)++;
248     return zms->temp_map_ptr;
249 }
250
251 const char **zebra_maps_search(ZebraMaps zms, unsigned reg_id,
252                                const char **from, int len,  int *q_map_match)
253 {
254     chrmaptab maptab;
255     
256     *q_map_match = 0;
257     maptab = zebra_charmap_get (zms, reg_id);
258     if (maptab)
259     {
260         const char **map;
261         map = chr_map_q_input(maptab, from, len, 0);
262         if (map && map[0])
263         {
264             *q_map_match = 1;
265             return map;
266         }
267         map = chr_map_input(maptab, from, len, 0);
268         if (map)
269             return map;
270     }
271     zms->temp_map_str[0] = **from;
272
273     (*from)++;
274     return zms->temp_map_ptr;
275 }
276
277 const char *zebra_maps_output(ZebraMaps zms, unsigned reg_id,
278                               const char **from)
279 {
280     chrmaptab maptab = zebra_charmap_get (zms, reg_id);
281     if (!maptab)
282         return 0;
283     return chr_map_output (maptab, from, 1);
284 }
285
286
287 /* ------------------------------------ */
288
289 typedef struct {
290     int type;
291     int major;
292     int minor;
293     Z_AttributeElement **attributeList;
294     int num_attributes;
295 } AttrType;
296
297 static int attr_find (AttrType *src, oid_value *attributeSetP)
298 {
299     while (src->major < src->num_attributes)
300     {
301         Z_AttributeElement *element;
302
303         element = src->attributeList[src->major];
304         if (src->type == *element->attributeType)
305         {
306             switch (element->which) 
307             {
308             case Z_AttributeValue_numeric:
309                 ++(src->major);
310                 if (element->attributeSet && attributeSetP)
311                 {
312                     oident *attrset;
313
314                     attrset = oid_getentbyoid (element->attributeSet);
315                     *attributeSetP = attrset->value;
316                 }
317                 return *element->value.numeric;
318                 break;
319             case Z_AttributeValue_complex:
320                 if (src->minor >= element->value.complex->num_list ||
321                     element->value.complex->list[src->minor]->which !=  
322                     Z_StringOrNumeric_numeric)
323                     break;
324                 ++(src->minor);
325                 if (element->attributeSet && attributeSetP)
326                 {
327                     oident *attrset;
328
329                     attrset = oid_getentbyoid (element->attributeSet);
330                     *attributeSetP = attrset->value;
331                 }
332                 return *element->value.complex->list[src->minor-1]->u.numeric;
333             default:
334                 assert (0);
335             }
336         }
337         ++(src->major);
338     }
339     return -1;
340 }
341
342 static void attr_init_APT (AttrType *src, Z_AttributesPlusTerm *zapt, int type)
343 {
344     src->attributeList = zapt->attributes->attributes;
345     src->num_attributes = zapt->attributes->num_attributes;
346     src->type = type;
347     src->major = 0;
348     src->minor = 0;
349 }
350
351 static void attr_init_AttrList (AttrType *src, Z_AttributeList *list, int type)
352 {
353     src->attributeList = list->attributes;
354     src->num_attributes = list->num_attributes;
355     src->type = type;
356     src->major = 0;
357     src->minor = 0;
358 }
359
360 /* ------------------------------------ */
361
362 int zebra_maps_is_complete (ZebraMaps zms, unsigned reg_id)
363
364     struct zebra_map *zm = zebra_map_get (zms, reg_id);
365     if (zm)
366         return zm->completeness;
367     return 0;
368 }
369
370 int zebra_maps_is_positioned (ZebraMaps zms, unsigned reg_id)
371 {
372     struct zebra_map *zm = zebra_map_get (zms, reg_id);
373     if (zm)
374         return zm->positioned;
375     return 0;
376 }
377     
378 int zebra_maps_is_sort (ZebraMaps zms, unsigned reg_id)
379 {
380     struct zebra_map *zm = zebra_map_get (zms, reg_id);
381     if (zm)
382         return zm->type == ZEBRA_MAP_TYPE_SORT;
383     return 0;
384 }
385
386 int zebra_maps_sort (ZebraMaps zms, Z_SortAttributes *sortAttributes,
387                      int *numerical)
388 {
389     AttrType use;
390     AttrType structure;
391     int structure_value;
392     attr_init_AttrList (&use, sortAttributes->list, 1);
393     attr_init_AttrList (&structure, sortAttributes->list, 4);
394
395     *numerical = 0;
396     structure_value = attr_find (&structure, 0);
397     if (structure_value == 109)
398         *numerical = 1;
399     return attr_find (&use, NULL);
400 }
401
402 int zebra_maps_attr (ZebraMaps zms, Z_AttributesPlusTerm *zapt,
403                      unsigned *reg_id, char **search_type, char *rank_type,
404                      int *complete_flag, int *sort_flag)
405 {
406     AttrType completeness;
407     AttrType structure;
408     AttrType relation;
409     AttrType sort_relation;
410     AttrType weight;
411     AttrType use;
412     int completeness_value;
413     int structure_value;
414     int relation_value;
415     int sort_relation_value;
416     int weight_value;
417     int use_value;
418
419     attr_init_APT (&structure, zapt, 4);
420     attr_init_APT (&completeness, zapt, 6);
421     attr_init_APT (&relation, zapt, 2);
422     attr_init_APT (&sort_relation, zapt, 7);
423     attr_init_APT (&weight, zapt, 9);
424     attr_init_APT (&use, zapt, 1);
425
426     completeness_value = attr_find (&completeness, NULL);
427     structure_value = attr_find (&structure, NULL);
428     relation_value = attr_find (&relation, NULL);
429     sort_relation_value = attr_find (&sort_relation, NULL);
430     weight_value = attr_find (&weight, NULL);
431     use_value = attr_find(&use, NULL);
432
433     if (completeness_value == 2 || completeness_value == 3)
434         *complete_flag = 1;
435     else
436         *complete_flag = 0;
437     *reg_id = 0;
438
439     *sort_flag = (sort_relation_value > 0) ? 1 : 0;
440     *search_type = "phrase";
441     strcpy (rank_type, "void");
442     if (relation_value == 102)
443     {
444         if (weight_value == -1)
445             weight_value = 34;
446         sprintf (rank_type, "rank,w=%d,u=%d", weight_value, use_value);
447     }
448     if (relation_value == 103)
449     {
450         *search_type = "always";
451         return 0;
452     }
453     if (*complete_flag)
454         *reg_id = 'p';
455     else
456         *reg_id = 'w';
457     switch (structure_value)
458     {
459     case 6:   /* word list */
460         *search_type = "and-list";
461         break;
462     case 105: /* free-form-text */
463         *search_type = "or-list";
464         break;
465     case 106: /* document-text */
466         *search_type = "or-list";
467         break;  
468     case -1:
469     case 1:   /* phrase */
470     case 2:   /* word */
471     case 108: /* string */ 
472         *search_type = "phrase";
473         break;
474     case 107: /* local-number */
475         *search_type = "local";
476         *reg_id = 0;
477         break;
478     case 109: /* numeric string */
479         *reg_id = 'n';
480         *search_type = "numeric";
481         break;
482     case 104: /* urx */
483         *reg_id = 'u';
484         *search_type = "phrase";
485         break;
486     case 3:   /* key */
487         *reg_id = '0';
488         *search_type = "phrase";
489         break;
490     case 4:  /* year */
491         *reg_id = 'y';
492         *search_type = "phrase";
493         break;
494     case 5:  /* date */
495         *reg_id = 'd';
496         *search_type = "phrase";
497         break;
498     default:
499         return -1;
500     }
501     return 0;
502 }
503
504 WRBUF zebra_replace(ZebraMaps zms, unsigned reg_id, const char *ex_list,
505                     const char *input_str, int input_len)
506 {
507     wrbuf_rewind(zms->wrbuf_1);
508     wrbuf_write(zms->wrbuf_1, input_str, input_len);
509     return zms->wrbuf_1;
510 }
511