b075257e24094de70f3f1c9a8af9e6d4cb3747c9
[idzebra-moved-to-github.git] / util / zebramap.c
1 /* $Id: zebramap.c,v 1.53 2006-09-08 14:41:00 adam Exp $
2    Copyright (C) 1995-2006
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 <attrfind.h>
29 #include <yaz/yaz-util.h>
30
31 #include <zebramap.h>
32
33 #define ZEBRA_MAP_TYPE_SORT  1
34 #define ZEBRA_MAP_TYPE_INDEX 2
35
36 #define ZEBRA_REPLACE_ANY  300
37
38 struct zebra_map {
39     unsigned reg_id;
40     int completeness;
41     int positioned;
42     int alwaysmatches;
43     int first_in_field;
44     int type;
45     union {
46         struct {
47             int dummy;
48         } index;
49         struct {
50             int entry_size;
51         } sort;
52     } u;
53     chrmaptab maptab;
54     const char *maptab_name;
55     struct zebra_map *next;
56 };
57
58 struct zebra_maps {
59     char *tabpath;
60     char *tabroot;
61     NMEM nmem;
62     struct zebra_map *map_list;
63     char temp_map_str[2];
64     const char *temp_map_ptr[2];
65     struct zebra_map **lookup_array;
66     WRBUF wrbuf_1;
67     int no_maps;
68 };
69
70 void zebra_maps_close(ZebraMaps zms)
71 {
72     struct zebra_map *zm = zms->map_list;
73     while (zm)
74     {
75         if (zm->maptab)
76             chrmaptab_destroy(zm->maptab);
77         zm = zm->next;
78     }
79     wrbuf_free(zms->wrbuf_1, 1);
80     nmem_destroy(zms->nmem);
81     xfree(zms);
82 }
83
84 ZEBRA_RES zebra_maps_read_file(ZebraMaps zms, const char *fname)
85 {
86     FILE *f;
87     char line[512];
88     char *argv[10];
89     int argc;
90     int lineno = 0;
91     int failures = 0;
92     struct zebra_map **zm = 0, *zp;
93
94     if (!(f = yaz_fopen(zms->tabpath, fname, "r", zms->tabroot)))
95     {
96         yaz_log(YLOG_ERRNO|YLOG_FATAL, "%s", fname);
97         return ZEBRA_FAIL;
98     }
99     while ((argc = readconf_line(f, &lineno, line, 512, argv, 10)))
100     {
101         if (argc == 1)
102         {
103             yaz_log(YLOG_WARN, "%s:%d: Missing arguments for '%s'",
104                     fname, lineno, argv[0]);
105             failures++;
106             break;
107         }
108         if (argc > 2)
109         {
110             yaz_log(YLOG_WARN, "%s:%d: Too many arguments for '%s'",
111                     fname, lineno, argv[0]);
112             failures++;
113             break;
114         }
115         if (!yaz_matchstr(argv[0], "index"))
116         {
117             if (!zm)
118                 zm = &zms->map_list;
119             else
120                 zm = &(*zm)->next;
121             *zm = (struct zebra_map *) nmem_malloc(zms->nmem, sizeof(**zm));
122             (*zm)->reg_id = argv[1][0];
123             (*zm)->maptab_name = NULL;
124             (*zm)->maptab = NULL;
125             (*zm)->type = ZEBRA_MAP_TYPE_INDEX;
126             (*zm)->completeness = 0;
127             (*zm)->positioned = 1;
128             (*zm)->alwaysmatches = 0;
129             (*zm)->first_in_field = 0;
130             zms->no_maps++;
131         }
132         else if (!yaz_matchstr(argv[0], "sort"))
133         {
134             if (!zm)
135                 zm = &zms->map_list;
136             else
137                 zm = &(*zm)->next;
138             *zm = (struct zebra_map *) nmem_malloc(zms->nmem, sizeof(**zm));
139             (*zm)->reg_id = argv[1][0];
140             (*zm)->maptab_name = NULL;
141             (*zm)->type = ZEBRA_MAP_TYPE_SORT;
142             (*zm)->u.sort.entry_size = 80;
143             (*zm)->maptab = NULL;
144             (*zm)->completeness = 0;
145             (*zm)->positioned = 0;
146             (*zm)->alwaysmatches = 0;
147             (*zm)->first_in_field = 0;
148             zms->no_maps++;
149         }
150         else if (!zm)
151         {
152             yaz_log(YLOG_WARN, "%s:%d: Missing sort/index before '%s'",  
153                     fname, lineno, argv[0]);
154             failures++;
155         }
156         else if (!yaz_matchstr(argv[0], "charmap") && argc == 2)
157         {
158             (*zm)->maptab_name = nmem_strdup(zms->nmem, argv[1]);
159         }
160         else if (!yaz_matchstr(argv[0], "completeness") && argc == 2)
161         {
162             (*zm)->completeness = atoi(argv[1]);
163         }
164         else if (!yaz_matchstr(argv[0], "position") && argc == 2)
165         {
166             (*zm)->positioned = atoi(argv[1]);
167         }
168         else if (!yaz_matchstr(argv[0], "alwaysmatches") && argc == 2)
169         {
170             (*zm)->alwaysmatches = atoi(argv[1]);
171         }
172         else if (!yaz_matchstr(argv[0], "firstinfield") && argc == 2)
173         {
174             (*zm)->first_in_field = atoi(argv[1]);
175         }
176         else if (!yaz_matchstr(argv[0], "entrysize") && argc == 2)
177         {
178             if ((*zm)->type == ZEBRA_MAP_TYPE_SORT)
179                 (*zm)->u.sort.entry_size = atoi(argv[1]);
180         }
181         else
182         {
183             yaz_log(YLOG_WARN, "%s:%d: Unrecognized directive '%s'",  
184                     fname, lineno, argv[0]);
185             failures++;
186         }
187     }
188     if (zm)
189         (*zm)->next = NULL;
190     yaz_fclose(f);
191
192     for (zp = zms->map_list; zp; zp = zp->next)
193         zms->lookup_array[zp->reg_id] = zp;
194
195     if (failures)
196         return ZEBRA_FAIL;
197     return ZEBRA_OK;
198 }
199
200 ZebraMaps zebra_maps_open(Res res, const char *base_path,
201                           const char *profile_path)
202 {
203     ZebraMaps zms = (ZebraMaps) xmalloc(sizeof(*zms));
204     int i;
205
206     zms->nmem = nmem_create();
207     zms->no_maps = 0;
208     zms->tabpath = nmem_strdup(zms->nmem, profile_path);
209     zms->tabroot = 0;
210     if (base_path)
211         zms->tabroot = nmem_strdup(zms->nmem, base_path);
212     zms->map_list = NULL;
213
214     zms->temp_map_str[0] = '\0';
215     zms->temp_map_str[1] = '\0';
216
217     zms->temp_map_ptr[0] = zms->temp_map_str;
218     zms->temp_map_ptr[1] = NULL;
219
220     zms->lookup_array = (struct zebra_map**)
221         nmem_malloc(zms->nmem, sizeof(*zms->lookup_array)*256);
222     zms->wrbuf_1 = wrbuf_alloc();
223
224     for (i = 0; i<256; i++)
225         zms->lookup_array[i] = 0;
226     return zms;
227 }
228
229 struct zebra_map *zebra_map_get(ZebraMaps zms, unsigned reg_id)
230 {
231     assert(reg_id >= 0 && reg_id <= 255);
232     return zms->lookup_array[reg_id];
233 }
234
235 chrmaptab zebra_charmap_get(ZebraMaps zms, unsigned reg_id)
236 {
237     struct zebra_map *zm = zebra_map_get(zms, reg_id);
238     if (!zm)
239     {
240         zm = (struct zebra_map *) nmem_malloc(zms->nmem, sizeof(*zm));
241         
242         /* no reason to warn if no maps are installed at ALL */
243         if (zms->no_maps)
244             yaz_log(YLOG_WARN, "Unknown register type: %c", reg_id);
245
246         zm->reg_id = reg_id;
247         zm->maptab_name = nmem_strdup(zms->nmem, "@");
248         zm->maptab = NULL;
249         zm->type = ZEBRA_MAP_TYPE_INDEX;
250         zm->completeness = 0;
251         zm->next = zms->map_list;
252         zms->map_list = zm->next;
253
254         zms->lookup_array[zm->reg_id & 255] = zm;
255     }
256     if (!zm->maptab)
257     {
258         if (!zm->maptab_name || !yaz_matchstr(zm->maptab_name, "@"))
259             return NULL;
260         if (!(zm->maptab = chrmaptab_create(zms->tabpath,
261                                             zm->maptab_name,
262                                             zms->tabroot)))
263             yaz_log(YLOG_WARN, "Failed to read character table %s",
264                     zm->maptab_name);
265         else
266             yaz_log(YLOG_DEBUG, "Read character table %s", zm->maptab_name);
267     }
268     return zm->maptab;
269 }
270
271 const char **zebra_maps_input(ZebraMaps zms, unsigned reg_id,
272                               const char **from, int len, int first)
273 {
274     chrmaptab maptab;
275
276     maptab = zebra_charmap_get(zms, reg_id);
277     if (maptab)
278         return chr_map_input(maptab, from, len, first);
279     
280     zms->temp_map_str[0] = **from;
281
282     (*from)++;
283     return zms->temp_map_ptr;
284 }
285
286 const char **zebra_maps_search(ZebraMaps zms, unsigned reg_id,
287                                const char **from, int len,  int *q_map_match)
288 {
289     chrmaptab maptab;
290     
291     *q_map_match = 0;
292     maptab = zebra_charmap_get(zms, reg_id);
293     if (maptab)
294     {
295         const char **map;
296         map = chr_map_q_input(maptab, from, len, 0);
297         if (map && map[0])
298         {
299             *q_map_match = 1;
300             return map;
301         }
302         map = chr_map_input(maptab, from, len, 0);
303         if (map)
304             return map;
305     }
306     zms->temp_map_str[0] = **from;
307
308     (*from)++;
309     return zms->temp_map_ptr;
310 }
311
312 const char *zebra_maps_output(ZebraMaps zms, unsigned reg_id,
313                               const char **from)
314 {
315     chrmaptab maptab = zebra_charmap_get(zms, reg_id);
316     if (!maptab)
317         return 0;
318     return chr_map_output(maptab, from, 1);
319 }
320
321
322 /* ------------------------------------ */
323
324 int zebra_maps_is_complete(ZebraMaps zms, unsigned reg_id)
325
326     struct zebra_map *zm = zebra_map_get(zms, reg_id);
327     if (zm)
328         return zm->completeness;
329     return 0;
330 }
331
332 int zebra_maps_is_positioned(ZebraMaps zms, unsigned reg_id)
333 {
334     struct zebra_map *zm = zebra_map_get(zms, reg_id);
335     if (zm)
336         return zm->positioned;
337     return 0;
338 }
339     
340 int zebra_maps_is_sort(ZebraMaps zms, unsigned reg_id)
341 {
342     struct zebra_map *zm = zebra_map_get(zms, reg_id);
343     if (zm)
344         return zm->type == ZEBRA_MAP_TYPE_SORT;
345     return 0;
346 }
347
348 int zebra_maps_is_alwaysmatches(ZebraMaps zms, unsigned reg_id)
349 {
350     struct zebra_map *zm = zebra_map_get(zms, reg_id);
351     if (zm)
352         return zm->alwaysmatches;
353     return 0;
354 }
355
356 int zebra_maps_is_first_in_field(ZebraMaps zms, unsigned reg_id)
357 {
358     struct zebra_map *zm = zebra_map_get(zms, reg_id);
359     if (zm)
360         return zm->first_in_field;
361     return 0;
362 }
363
364 int zebra_maps_sort(ZebraMaps zms, Z_SortAttributes *sortAttributes,
365                     int *numerical)
366 {
367     AttrType use;
368     AttrType structure;
369     int structure_value;
370     attr_init_AttrList(&use, sortAttributes->list, 1);
371     attr_init_AttrList(&structure, sortAttributes->list, 4);
372
373     *numerical = 0;
374     structure_value = attr_find(&structure, 0);
375     if (structure_value == 109)
376         *numerical = 1;
377     return attr_find(&use, NULL);
378 }
379
380 int zebra_maps_attr(ZebraMaps zms, Z_AttributesPlusTerm *zapt,
381                     unsigned *reg_id, char **search_type, char *rank_type,
382                     int *complete_flag, int *sort_flag)
383 {
384     AttrType completeness;
385     AttrType structure;
386     AttrType relation;
387     AttrType sort_relation;
388     AttrType weight;
389     AttrType use;
390     int completeness_value;
391     int structure_value;
392     int relation_value;
393     int sort_relation_value;
394     int weight_value;
395     int use_value;
396
397     attr_init_APT(&structure, zapt, 4);
398     attr_init_APT(&completeness, zapt, 6);
399     attr_init_APT(&relation, zapt, 2);
400     attr_init_APT(&sort_relation, zapt, 7);
401     attr_init_APT(&weight, zapt, 9);
402     attr_init_APT(&use, zapt, 1);
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     weight_value = attr_find(&weight, NULL);
409     use_value = attr_find(&use, NULL);
410
411     if (completeness_value == 2 || completeness_value == 3)
412         *complete_flag = 1;
413     else
414         *complete_flag = 0;
415     *reg_id = 0;
416
417     *sort_flag =(sort_relation_value > 0) ? 1 : 0;
418     *search_type = "phrase";
419     strcpy(rank_type, "void");
420     if (relation_value == 102)
421     {
422         if (weight_value == -1)
423             weight_value = 34;
424         sprintf(rank_type, "rank,w=%d,u=%d", weight_value, use_value);
425     }
426     if (*complete_flag)
427         *reg_id = 'p';
428     else
429         *reg_id = 'w';
430     switch (structure_value)
431     {
432     case 6:   /* word list */
433         *search_type = "and-list";
434         break;
435     case 105: /* free-form-text */
436         *search_type = "or-list";
437         break;
438     case 106: /* document-text */
439         *search_type = "or-list";
440         break;  
441     case -1:
442     case 1:   /* phrase */
443     case 2:   /* word */
444     case 108: /* string */ 
445         *search_type = "phrase";
446         break;
447     case 107: /* local-number */
448         *search_type = "local";
449         *reg_id = 0;
450         break;
451     case 109: /* numeric string */
452         *reg_id = 'n';
453         *search_type = "numeric";
454         break;
455     case 104: /* urx */
456         *reg_id = 'u';
457         *search_type = "phrase";
458         break;
459     case 3:   /* key */
460         *reg_id = '0';
461         *search_type = "phrase";
462         break;
463     case 4:  /* year */
464         *reg_id = 'y';
465         *search_type = "phrase";
466         break;
467     case 5:  /* date */
468         *reg_id = 'd';
469         *search_type = "phrase";
470         break;
471     default:
472         return -1;
473     }
474     return 0;
475 }
476
477 WRBUF zebra_replace(ZebraMaps zms, unsigned reg_id, const char *ex_list,
478                     const char *input_str, int input_len)
479 {
480     wrbuf_rewind(zms->wrbuf_1);
481     wrbuf_write(zms->wrbuf_1, input_str, input_len);
482     return zms->wrbuf_1;
483 }
484
485 /*
486  * Local variables:
487  * c-basic-offset: 4
488  * indent-tabs-mode: nil
489  * End:
490  * vim: shiftwidth=4 tabstop=8 expandtab
491  */
492