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