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