The static function zebra_map_read() now takes an additional boolean
[idzebra-moved-to-github.git] / util / zebramap.c
1 /* $Id: zebramap.c,v 1.44 2006-03-13 17:40:28 mike 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     for (i = 0; i<256; i++)
185         zms->lookup_array[i] = 0;
186     if (!res || !res_trav (res, "index", zms, zms_map_handle))
187         if (zebra_map_read (zms, "default.idx", 1) < 0)
188             return 0;
189
190     zms->wrbuf_1 = wrbuf_alloc();
191
192     return zms;
193 }
194
195 struct zebra_map *zebra_map_get (ZebraMaps zms, unsigned reg_id)
196 {
197     assert(reg_id >= 0 && reg_id <= 255);
198     return zms->lookup_array[reg_id];
199 }
200
201 chrmaptab zebra_charmap_get (ZebraMaps zms, unsigned reg_id)
202 {
203     struct zebra_map *zm = zebra_map_get (zms, reg_id);
204     if (!zm)
205     {
206         zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(*zm));
207         yaz_log (YLOG_WARN, "Unknown register type: %c", reg_id);
208
209         zm->reg_id = reg_id;
210         zm->maptab_name = nmem_strdup (zms->nmem, "@");
211         zm->maptab = NULL;
212         zm->type = ZEBRA_MAP_TYPE_INDEX;
213         zm->completeness = 0;
214         zm->next = zms->map_list;
215         zms->map_list = zm->next;
216
217         zms->lookup_array[zm->reg_id & 255] = zm;
218     }
219     if (!zm->maptab)
220     {
221         if (!zm->maptab_name || !yaz_matchstr (zm->maptab_name, "@"))
222             return NULL;
223         if (!(zm->maptab = chrmaptab_create (zms->tabpath,
224                                              zm->maptab_name,
225                                              zms->tabroot)))
226             yaz_log(YLOG_WARN, "Failed to read character table %s",
227                  zm->maptab_name);
228         else
229             yaz_log(YLOG_DEBUG, "Read character table %s", zm->maptab_name);
230     }
231     return zm->maptab;
232 }
233
234 const char **zebra_maps_input (ZebraMaps zms, unsigned reg_id,
235                                const char **from, int len, int first)
236 {
237     chrmaptab maptab;
238
239     maptab = zebra_charmap_get (zms, reg_id);
240     if (maptab)
241         return chr_map_input(maptab, from, len, first);
242     
243     zms->temp_map_str[0] = **from;
244
245     (*from)++;
246     return zms->temp_map_ptr;
247 }
248
249 const char **zebra_maps_search(ZebraMaps zms, unsigned reg_id,
250                                const char **from, int len,  int *q_map_match)
251 {
252     chrmaptab maptab;
253     
254     *q_map_match = 0;
255     maptab = zebra_charmap_get (zms, reg_id);
256     if (maptab)
257     {
258         const char **map;
259         map = chr_map_q_input(maptab, from, len, 0);
260         if (map && map[0])
261         {
262             *q_map_match = 1;
263             return map;
264         }
265         map = chr_map_input(maptab, from, len, 0);
266         if (map)
267             return map;
268     }
269     zms->temp_map_str[0] = **from;
270
271     (*from)++;
272     return zms->temp_map_ptr;
273 }
274
275 const char *zebra_maps_output(ZebraMaps zms, unsigned reg_id,
276                               const char **from)
277 {
278     chrmaptab maptab = zebra_charmap_get (zms, reg_id);
279     if (!maptab)
280         return 0;
281     return chr_map_output (maptab, from, 1);
282 }
283
284
285 /* ------------------------------------ */
286
287 typedef struct {
288     int type;
289     int major;
290     int minor;
291     Z_AttributeElement **attributeList;
292     int num_attributes;
293 } AttrType;
294
295 static int attr_find (AttrType *src, oid_value *attributeSetP)
296 {
297     while (src->major < src->num_attributes)
298     {
299         Z_AttributeElement *element;
300
301         element = src->attributeList[src->major];
302         if (src->type == *element->attributeType)
303         {
304             switch (element->which) 
305             {
306             case Z_AttributeValue_numeric:
307                 ++(src->major);
308                 if (element->attributeSet && attributeSetP)
309                 {
310                     oident *attrset;
311
312                     attrset = oid_getentbyoid (element->attributeSet);
313                     *attributeSetP = attrset->value;
314                 }
315                 return *element->value.numeric;
316                 break;
317             case Z_AttributeValue_complex:
318                 if (src->minor >= element->value.complex->num_list ||
319                     element->value.complex->list[src->minor]->which !=  
320                     Z_StringOrNumeric_numeric)
321                     break;
322                 ++(src->minor);
323                 if (element->attributeSet && attributeSetP)
324                 {
325                     oident *attrset;
326
327                     attrset = oid_getentbyoid (element->attributeSet);
328                     *attributeSetP = attrset->value;
329                 }
330                 return *element->value.complex->list[src->minor-1]->u.numeric;
331             default:
332                 assert (0);
333             }
334         }
335         ++(src->major);
336     }
337     return -1;
338 }
339
340 static void attr_init_APT (AttrType *src, Z_AttributesPlusTerm *zapt, int type)
341 {
342     src->attributeList = zapt->attributes->attributes;
343     src->num_attributes = zapt->attributes->num_attributes;
344     src->type = type;
345     src->major = 0;
346     src->minor = 0;
347 }
348
349 static void attr_init_AttrList (AttrType *src, Z_AttributeList *list, int type)
350 {
351     src->attributeList = list->attributes;
352     src->num_attributes = list->num_attributes;
353     src->type = type;
354     src->major = 0;
355     src->minor = 0;
356 }
357
358 /* ------------------------------------ */
359
360 int zebra_maps_is_complete (ZebraMaps zms, unsigned reg_id)
361
362     struct zebra_map *zm = zebra_map_get (zms, reg_id);
363     if (zm)
364         return zm->completeness;
365     return 0;
366 }
367
368 int zebra_maps_is_positioned (ZebraMaps zms, unsigned reg_id)
369 {
370     struct zebra_map *zm = zebra_map_get (zms, reg_id);
371     if (zm)
372         return zm->positioned;
373     return 0;
374 }
375     
376 int zebra_maps_is_sort (ZebraMaps zms, unsigned reg_id)
377 {
378     struct zebra_map *zm = zebra_map_get (zms, reg_id);
379     if (zm)
380         return zm->type == ZEBRA_MAP_TYPE_SORT;
381     return 0;
382 }
383
384 int zebra_maps_sort (ZebraMaps zms, Z_SortAttributes *sortAttributes,
385                      int *numerical)
386 {
387     AttrType use;
388     AttrType structure;
389     int structure_value;
390     attr_init_AttrList (&use, sortAttributes->list, 1);
391     attr_init_AttrList (&structure, sortAttributes->list, 4);
392
393     *numerical = 0;
394     structure_value = attr_find (&structure, 0);
395     if (structure_value == 109)
396         *numerical = 1;
397     return attr_find (&use, NULL);
398 }
399
400 int zebra_maps_attr (ZebraMaps zms, Z_AttributesPlusTerm *zapt,
401                      unsigned *reg_id, char **search_type, char *rank_type,
402                      int *complete_flag, int *sort_flag)
403 {
404     AttrType completeness;
405     AttrType structure;
406     AttrType relation;
407     AttrType sort_relation;
408     AttrType weight;
409     AttrType use;
410     int completeness_value;
411     int structure_value;
412     int relation_value;
413     int sort_relation_value;
414     int weight_value;
415     int use_value;
416
417     attr_init_APT (&structure, zapt, 4);
418     attr_init_APT (&completeness, zapt, 6);
419     attr_init_APT (&relation, zapt, 2);
420     attr_init_APT (&sort_relation, zapt, 7);
421     attr_init_APT (&weight, zapt, 9);
422     attr_init_APT (&use, zapt, 1);
423
424     completeness_value = attr_find (&completeness, NULL);
425     structure_value = attr_find (&structure, NULL);
426     relation_value = attr_find (&relation, NULL);
427     sort_relation_value = attr_find (&sort_relation, NULL);
428     weight_value = attr_find (&weight, NULL);
429     use_value = attr_find(&use, NULL);
430
431     if (completeness_value == 2 || completeness_value == 3)
432         *complete_flag = 1;
433     else
434         *complete_flag = 0;
435     *reg_id = 0;
436
437     *sort_flag = (sort_relation_value > 0) ? 1 : 0;
438     *search_type = "phrase";
439     strcpy (rank_type, "void");
440     if (relation_value == 102)
441     {
442         if (weight_value == -1)
443             weight_value = 34;
444         sprintf (rank_type, "rank,w=%d,u=%d", weight_value, use_value);
445     }
446     if (relation_value == 103)
447     {
448         *search_type = "always";
449         return 0;
450     }
451     if (*complete_flag)
452         *reg_id = 'p';
453     else
454         *reg_id = 'w';
455     switch (structure_value)
456     {
457     case 6:   /* word list */
458         *search_type = "and-list";
459         break;
460     case 105: /* free-form-text */
461         *search_type = "or-list";
462         break;
463     case 106: /* document-text */
464         *search_type = "or-list";
465         break;  
466     case -1:
467     case 1:   /* phrase */
468     case 2:   /* word */
469     case 108: /* string */ 
470         *search_type = "phrase";
471         break;
472     case 107: /* local-number */
473         *search_type = "local";
474         *reg_id = 0;
475         break;
476     case 109: /* numeric string */
477         *reg_id = 'n';
478         *search_type = "numeric";
479         break;
480     case 104: /* urx */
481         *reg_id = 'u';
482         *search_type = "phrase";
483         break;
484     case 3:   /* key */
485         *reg_id = '0';
486         *search_type = "phrase";
487         break;
488     case 4:  /* year */
489         *reg_id = 'y';
490         *search_type = "phrase";
491         break;
492     case 5:  /* date */
493         *reg_id = 'd';
494         *search_type = "phrase";
495         break;
496     default:
497         return -1;
498     }
499     return 0;
500 }
501
502 WRBUF zebra_replace(ZebraMaps zms, unsigned reg_id, const char *ex_list,
503                     const char *input_str, int input_len)
504 {
505     wrbuf_rewind(zms->wrbuf_1);
506     wrbuf_write(zms->wrbuf_1, input_str, input_len);
507     return zms->wrbuf_1;
508 }
509