Implemented Sort.
[idzebra-moved-to-github.git] / util / zebramap.c
1 /*
2  * Copyright (C) 1994-1998, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: zebramap.c,v $
7  * Revision 1.7  1998-02-10 12:03:07  adam
8  * Implemented Sort.
9  *
10  * Revision 1.6  1998/01/29 13:36:01  adam
11  * Structure word-list, free-form-text and document-text all
12  * trigger ranked search.
13  *
14  * Revision 1.5  1997/11/19 10:22:14  adam
15  * Bug fix (introduced by previous commit).
16  *
17  * Revision 1.4  1997/11/18 10:05:08  adam
18  * Changed character map facility so that admin can specify character
19  * mapping files for each register type, w, p, etc.
20  *
21  * Revision 1.3  1997/11/17 15:35:26  adam
22  * Bug fix. Relation=relevance wasn't observed.
23  *
24  * Revision 1.2  1997/10/31 12:39:30  adam
25  * Changed log message.
26  *
27  * Revision 1.1  1997/10/27 14:33:06  adam
28  * Moved towards generic character mapping depending on "structure"
29  * field in abstract syntax file. Fixed a few memory leaks. Fixed
30  * bug with negative integers when doing searches with relational
31  * operators.
32  *
33  */
34
35 #include <assert.h>
36 #include <ctype.h>
37
38 #include <yaz-util.h>
39 #include <charmap.h>
40 #include <zebramap.h>
41
42 struct zebra_map {
43     int reg_type;
44     int completeness;
45     int sort_flag;
46     chrmaptab maptab;
47     const char *maptab_name;
48     struct zebra_map *next;
49 };
50
51 struct zebra_maps {
52     char *tabpath;
53     NMEM nmem;
54     struct zebra_map *map_list;
55     char temp_map_str[2];
56     const char *temp_map_ptr[2];
57     struct zebra_map **lookup_array;
58 };
59
60 void zebra_maps_close (ZebraMaps zms)
61 {
62     struct zebra_map *zm = zms->map_list;
63     while (zm)
64     {
65         if (zm->maptab)
66             chrmaptab_destroy (zm->maptab);
67         zm = zm->next;
68     }
69     nmem_destroy (zms->nmem);
70     xfree (zms);
71 }
72
73 static void zebra_map_read (ZebraMaps zms, const char *name)
74 {
75     FILE *f;
76     char line[512];
77     char *argv[10];
78     int argc;
79     struct zebra_map **zm = 0, *zp;
80
81     if (!(f = yaz_path_fopen(zms->tabpath, name, "r")))
82     {
83         logf(LOG_WARN|LOG_ERRNO, "%s", name);
84         return ;
85     }
86     while ((argc = readconf_line(f, line, 512, argv, 10)))
87     {
88         if (!strcmp (argv[0], "index") && argc == 2)
89         {
90             if (!zm)
91                 zm = &zms->map_list;
92             else
93                 zm = &(*zm)->next;
94             *zm = nmem_malloc (zms->nmem, sizeof(**zm));
95             (*zm)->reg_type = argv[1][0];
96             (*zm)->maptab_name = NULL;
97             (*zm)->maptab = NULL;
98             (*zm)->sort_flag = 0;
99             (*zm)->completeness = 0;
100         }
101         else if (!strcmp (argv[0], "sort") && argc == 2)
102         {
103             if (!zm)
104                 zm = &zms->map_list;
105             else
106                 zm = &(*zm)->next;
107             *zm = nmem_malloc (zms->nmem, sizeof(**zm));
108             (*zm)->reg_type = argv[1][0];
109             (*zm)->maptab_name = NULL;
110             (*zm)->sort_flag = 1;
111             (*zm)->maptab = NULL;
112             (*zm)->completeness = 0;
113         }
114         else if (zm && !strcmp (argv[0], "charmap") && argc == 2)
115         {
116             (*zm)->maptab_name = nmem_strdup (zms->nmem, argv[1]);
117         }
118         else if (zm && !strcmp (argv[0], "completeness") && argc == 2)
119         {
120             (*zm)->completeness = atoi (argv[1]);
121         }
122     }
123     if (zm)
124         (*zm)->next = NULL;
125     fclose (f);
126
127     for (zp = zms->map_list; zp; zp = zp->next)
128         zms->lookup_array[zp->reg_type & 255] = zp;
129 }
130
131 static void zms_map_handle (void *p, const char *name, const char *value)
132 {
133     ZebraMaps zms = p;
134     
135     zebra_map_read (zms, value);
136 }
137
138 ZebraMaps zebra_maps_open (const char *tabpath, Res res)
139 {
140     ZebraMaps zms = xmalloc (sizeof(*zms));
141     int i;
142
143     zms->nmem = nmem_create ();
144     zms->tabpath = nmem_strdup (zms->nmem, tabpath);
145     zms->map_list = NULL;
146
147     zms->temp_map_str[0] = '\0';
148     zms->temp_map_str[1] = '\0';
149
150     zms->temp_map_ptr[0] = zms->temp_map_str;
151     zms->temp_map_ptr[1] = NULL;
152
153     zms->lookup_array =
154         nmem_malloc (zms->nmem, sizeof(*zms->lookup_array)*256);
155     for (i = 0; i<256; i++)
156         zms->lookup_array[i] = 0;
157     if (!res_trav (res, "index", zms, zms_map_handle))
158         zebra_map_read (zms, "default.idx");
159     return zms;
160 }
161
162 struct zebra_map *zebra_map_get (ZebraMaps zms, int reg_type)
163 {
164     return zms->lookup_array[reg_type];
165 }
166
167 chrmaptab zebra_charmap_get (ZebraMaps zms, int reg_type)
168 {
169     struct zebra_map *zm = zebra_map_get (zms, reg_type);
170     if (!zm)
171     {
172         logf (LOG_WARN, "Unknown register type: %c", reg_type);
173         return NULL;
174     }
175     if (!zm->maptab)
176     {
177         if (!strcmp (zm->maptab_name, "@"))
178             return NULL;
179         if (!(zm->maptab = chrmaptab_create (zms->tabpath,
180                                              zm->maptab_name, 0)))
181             logf(LOG_WARN, "Failed to read character table %s",
182                  zm->maptab_name);
183         else
184             logf(LOG_DEBUG, "Read character table %s", zm->maptab_name);
185     }
186     return zm->maptab;
187 }
188
189 const char **zebra_maps_input (ZebraMaps zms, int reg_type,
190                                const char **from, int len)
191 {
192     chrmaptab maptab;
193
194     maptab = zebra_charmap_get (zms, reg_type);
195     if (maptab)
196         return chr_map_input(maptab, from, len);
197     
198     zms->temp_map_str[0] = **from;
199
200     (*from)++;
201     return zms->temp_map_ptr;
202 }
203
204 const char *zebra_maps_output(ZebraMaps zms, int reg_type, const char **from)
205 {
206     chrmaptab maptab;
207     unsigned char i = (unsigned char) **from;
208     static char buf[2] = {0,0};
209
210     maptab = zebra_charmap_get (zms, reg_type);
211     if (maptab)
212         return chr_map_output (maptab, from, 1);
213     (*from)++;
214     buf[0] = i;
215     return buf;
216 }
217
218
219 /* ------------------------------------ */
220
221 typedef struct {
222     int type;
223     int major;
224     int minor;
225     Z_AttributeElement **attributeList;
226     int num_attributes;
227 } AttrType;
228
229 static int attr_find (AttrType *src, oid_value *attributeSetP)
230 {
231     while (src->major < src->num_attributes)
232     {
233         Z_AttributeElement *element;
234
235         element = src->attributeList[src->major];
236         if (src->type == *element->attributeType)
237         {
238             switch (element->which) 
239             {
240             case Z_AttributeValue_numeric:
241                 ++(src->major);
242                 if (element->attributeSet && attributeSetP)
243                 {
244                     oident *attrset;
245
246                     attrset = oid_getentbyoid (element->attributeSet);
247                     *attributeSetP = attrset->value;
248                 }
249                 return *element->value.numeric;
250                 break;
251             case Z_AttributeValue_complex:
252                 if (src->minor >= element->value.complex->num_list ||
253                     element->value.complex->list[src->minor]->which !=  
254                     Z_StringOrNumeric_numeric)
255                     break;
256                 ++(src->minor);
257                 if (element->attributeSet && attributeSetP)
258                 {
259                     oident *attrset;
260
261                     attrset = oid_getentbyoid (element->attributeSet);
262                     *attributeSetP = attrset->value;
263                 }
264                 return *element->value.complex->list[src->minor-1]->u.numeric;
265             default:
266                 assert (0);
267             }
268         }
269         ++(src->major);
270     }
271     return -1;
272 }
273
274 static void attr_init_APT (AttrType *src, Z_AttributesPlusTerm *zapt, int type)
275 {
276
277     src->attributeList = zapt->attributeList;
278     src->num_attributes = zapt->num_attributes;
279     src->type = type;
280     src->major = 0;
281     src->minor = 0;
282 }
283
284 static void attr_init_AttrList (AttrType *src, Z_AttributeList *list, int type)
285 {
286     src->attributeList = list->attributes;
287     src->num_attributes = list->num_attributes;
288     src->type = type;
289     src->major = 0;
290     src->minor = 0;
291 }
292
293 /* ------------------------------------ */
294
295 int zebra_maps_is_complete (ZebraMaps zms, int reg_type)
296
297     struct zebra_map *zm = zebra_map_get (zms, reg_type);
298     if (zm)
299         return zm->completeness;
300     return 0;
301 }
302
303 int zebra_maps_is_sort (ZebraMaps zms, int reg_type)
304 {
305     struct zebra_map *zm = zebra_map_get (zms, reg_type);
306     if (zm)
307         return zm->sort_flag;
308     return 0;
309 }
310
311 int zebra_maps_sort (ZebraMaps zms, Z_SortAttributes *sortAttributes)
312 {
313     AttrType use;
314     attr_init_AttrList (&use, sortAttributes->list, 1);
315
316     return attr_find (&use, NULL);
317 }
318
319 int zebra_maps_attr (ZebraMaps zms, Z_AttributesPlusTerm *zapt,
320                      int *reg_type, char **search_type, int *complete_flag)
321 {
322     AttrType completeness;
323     AttrType structure;
324     AttrType relation;
325     int completeness_value;
326     int structure_value;
327     int relation_value;
328
329     attr_init_APT (&structure, zapt, 4);
330     attr_init_APT (&completeness, zapt, 6);
331     attr_init_APT (&relation, zapt, 2);
332
333     completeness_value = attr_find (&completeness, NULL);
334     structure_value = attr_find (&structure, NULL);
335     relation_value = attr_find (&relation, NULL);
336
337     if (completeness_value == 2 || completeness_value == 3)
338         *complete_flag = 1;
339     else
340         *complete_flag = 0;
341     *reg_type = 0;
342
343     *search_type = "phrase";
344     if (relation_value == 102)
345         *search_type = "ranked";
346     
347     switch (structure_value)
348     {
349     case 6:   /* word list */
350     case 105: /* free-form-text */
351     case 106: /* document-text */
352         *search_type = "ranked";
353         *reg_type = 'w';
354         break;
355     case -1:
356     case 1:   /* phrase */
357     case 2:   /* word */
358     case 3:   /* key */
359     case 108: /* string */ 
360         if (*complete_flag)
361             *reg_type = 'p';
362         else
363             *reg_type = 'w';
364         break;
365     case 107: /* local-number */
366         *search_type = "local";
367         *reg_type = 0;
368     case 109: /* numeric string */
369         *reg_type = 'n';
370         break;
371     case 104: /* urx */
372         *reg_type = 'u';
373         break;
374     default:
375         return -1;
376     }
377     return 0;
378 }