2 * Copyright (C) 1994-1998, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.9 1998-04-02 14:35:30 adam
8 * First version of Zebra that works with compiled ASN.1.
10 * Revision 1.8 1998/03/05 08:42:44 adam
11 * Minor changes to zebramap data structures. Query mapping rules changed.
13 * Revision 1.7 1998/02/10 12:03:07 adam
16 * Revision 1.6 1998/01/29 13:36:01 adam
17 * Structure word-list, free-form-text and document-text all
18 * trigger ranked search.
20 * Revision 1.5 1997/11/19 10:22:14 adam
21 * Bug fix (introduced by previous commit).
23 * Revision 1.4 1997/11/18 10:05:08 adam
24 * Changed character map facility so that admin can specify character
25 * mapping files for each register type, w, p, etc.
27 * Revision 1.3 1997/11/17 15:35:26 adam
28 * Bug fix. Relation=relevance wasn't observed.
30 * Revision 1.2 1997/10/31 12:39:30 adam
31 * Changed log message.
33 * Revision 1.1 1997/10/27 14:33:06 adam
34 * Moved towards generic character mapping depending on "structure"
35 * field in abstract syntax file. Fixed a few memory leaks. Fixed
36 * bug with negative integers when doing searches with relational
48 #define ZEBRA_MAP_TYPE_SORT 1
49 #define ZEBRA_MAP_TYPE_INDEX 2
64 const char *maptab_name;
65 struct zebra_map *next;
71 struct zebra_map *map_list;
73 const char *temp_map_ptr[2];
74 struct zebra_map **lookup_array;
77 void zebra_maps_close (ZebraMaps zms)
79 struct zebra_map *zm = zms->map_list;
83 chrmaptab_destroy (zm->maptab);
86 nmem_destroy (zms->nmem);
90 static void zebra_map_read (ZebraMaps zms, const char *name)
96 struct zebra_map **zm = 0, *zp;
98 if (!(f = yaz_path_fopen(zms->tabpath, name, "r")))
100 logf(LOG_WARN|LOG_ERRNO, "%s", name);
103 while ((argc = readconf_line(f, line, 512, argv, 10)))
105 if (!yaz_matchstr (argv[0], "index") && argc == 2)
111 *zm = nmem_malloc (zms->nmem, sizeof(**zm));
112 (*zm)->reg_id = argv[1][0];
113 (*zm)->maptab_name = NULL;
114 (*zm)->maptab = NULL;
115 (*zm)->type = ZEBRA_MAP_TYPE_INDEX;
116 (*zm)->completeness = 0;
118 else if (!yaz_matchstr (argv[0], "sort") && argc == 2)
124 *zm = nmem_malloc (zms->nmem, sizeof(**zm));
125 (*zm)->reg_id = argv[1][0];
126 (*zm)->maptab_name = NULL;
127 (*zm)->type = ZEBRA_MAP_TYPE_SORT;
128 (*zm)->u.sort.entry_size = 80;
129 (*zm)->maptab = NULL;
130 (*zm)->completeness = 0;
132 else if (zm && !yaz_matchstr (argv[0], "charmap") && argc == 2)
134 (*zm)->maptab_name = nmem_strdup (zms->nmem, argv[1]);
136 else if (zm && !yaz_matchstr (argv[0], "completeness") && argc == 2)
138 (*zm)->completeness = atoi (argv[1]);
140 else if (zm && !yaz_matchstr (argv[0], "entrysize") && argc == 2)
142 if ((*zm)->type == ZEBRA_MAP_TYPE_SORT)
143 (*zm)->u.sort.entry_size = atoi (argv[1]);
150 for (zp = zms->map_list; zp; zp = zp->next)
151 zms->lookup_array[zp->reg_id] = zp;
154 static void zms_map_handle (void *p, const char *name, const char *value)
158 zebra_map_read (zms, value);
161 ZebraMaps zebra_maps_open (Res res)
163 ZebraMaps zms = xmalloc (sizeof(*zms));
166 zms->nmem = nmem_create ();
167 zms->tabpath = nmem_strdup (zms->nmem, res_get (res, "profilePath"));
168 zms->map_list = NULL;
170 zms->temp_map_str[0] = '\0';
171 zms->temp_map_str[1] = '\0';
173 zms->temp_map_ptr[0] = zms->temp_map_str;
174 zms->temp_map_ptr[1] = NULL;
177 nmem_malloc (zms->nmem, sizeof(*zms->lookup_array)*256);
178 for (i = 0; i<256; i++)
179 zms->lookup_array[i] = 0;
180 if (!res || !res_trav (res, "index", zms, zms_map_handle))
181 zebra_map_read (zms, "default.idx");
185 struct zebra_map *zebra_map_get (ZebraMaps zms, unsigned reg_id)
187 return zms->lookup_array[reg_id];
190 chrmaptab zebra_charmap_get (ZebraMaps zms, unsigned reg_id)
192 struct zebra_map *zm = zebra_map_get (zms, reg_id);
195 zm = nmem_malloc (zms->nmem, sizeof(*zm));
196 logf (LOG_WARN, "Unknown register type: %c", reg_id);
199 zm->maptab_name = NULL;
201 zm->type = ZEBRA_MAP_TYPE_INDEX;
202 zm->completeness = 0;
203 zm->next = zms->map_list;
204 zms->map_list = zm->next;
206 zms->lookup_array[zm->reg_id & 255] = zm;
210 if (!zm->maptab_name || !yaz_matchstr (zm->maptab_name, "@"))
212 if (!(zm->maptab = chrmaptab_create (zms->tabpath,
213 zm->maptab_name, 0)))
214 logf(LOG_WARN, "Failed to read character table %s",
217 logf(LOG_DEBUG, "Read character table %s", zm->maptab_name);
222 const char **zebra_maps_input (ZebraMaps zms, unsigned reg_id,
223 const char **from, int len)
227 maptab = zebra_charmap_get (zms, reg_id);
229 return chr_map_input(maptab, from, len);
231 zms->temp_map_str[0] = **from;
234 return zms->temp_map_ptr;
237 const char *zebra_maps_output(ZebraMaps zms, unsigned reg_id,
241 unsigned char i = (unsigned char) **from;
242 static char buf[2] = {0,0};
244 maptab = zebra_charmap_get (zms, reg_id);
246 return chr_map_output (maptab, from, 1);
253 /* ------------------------------------ */
259 Z_AttributeElement **attributeList;
263 static int attr_find (AttrType *src, oid_value *attributeSetP)
265 while (src->major < src->num_attributes)
267 Z_AttributeElement *element;
269 element = src->attributeList[src->major];
270 if (src->type == *element->attributeType)
272 switch (element->which)
274 case Z_AttributeValue_numeric:
276 if (element->attributeSet && attributeSetP)
280 attrset = oid_getentbyoid (element->attributeSet);
281 *attributeSetP = attrset->value;
283 return *element->value.numeric;
285 case Z_AttributeValue_complex:
286 if (src->minor >= element->value.complex->num_list ||
287 element->value.complex->list[src->minor]->which !=
288 Z_StringOrNumeric_numeric)
291 if (element->attributeSet && attributeSetP)
295 attrset = oid_getentbyoid (element->attributeSet);
296 *attributeSetP = attrset->value;
298 return *element->value.complex->list[src->minor-1]->u.numeric;
308 static void attr_init_APT (AttrType *src, Z_AttributesPlusTerm *zapt, int type)
311 src->attributeList = zapt->attributes->attributes;
312 src->num_attributes = zapt->attributes->num_attributes;
314 src->attributeList = zapt->attributeList;
315 src->num_attributes = zapt->num_attributes;
322 static void attr_init_AttrList (AttrType *src, Z_AttributeList *list, int type)
324 src->attributeList = list->attributes;
325 src->num_attributes = list->num_attributes;
331 /* ------------------------------------ */
333 int zebra_maps_is_complete (ZebraMaps zms, unsigned reg_id)
335 struct zebra_map *zm = zebra_map_get (zms, reg_id);
337 return zm->completeness;
341 int zebra_maps_is_sort (ZebraMaps zms, unsigned reg_id)
343 struct zebra_map *zm = zebra_map_get (zms, reg_id);
345 return zm->type == ZEBRA_MAP_TYPE_SORT;
349 int zebra_maps_sort (ZebraMaps zms, Z_SortAttributes *sortAttributes)
352 attr_init_AttrList (&use, sortAttributes->list, 1);
354 return attr_find (&use, NULL);
357 int zebra_maps_attr (ZebraMaps zms, Z_AttributesPlusTerm *zapt,
358 unsigned *reg_id, char **search_type, char **rank_type,
361 AttrType completeness;
364 int completeness_value;
368 attr_init_APT (&structure, zapt, 4);
369 attr_init_APT (&completeness, zapt, 6);
370 attr_init_APT (&relation, zapt, 2);
372 completeness_value = attr_find (&completeness, NULL);
373 structure_value = attr_find (&structure, NULL);
374 relation_value = attr_find (&relation, NULL);
376 if (completeness_value == 2 || completeness_value == 3)
382 *search_type = "phrase";
384 if (relation_value == 102)
391 switch (structure_value)
393 case 6: /* word list */
394 *search_type = "and-list";
396 case 105: /* free-form-text */
397 *search_type = "or-list";
399 case 106: /* document-text */
400 *search_type = "or-list";
406 case 108: /* string */
407 *search_type = "phrase";
409 case 107: /* local-number */
410 *search_type = "local";
413 case 109: /* numeric string */
415 *search_type = "phrase";
419 *search_type = "phrase";