2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.24 2002-04-04 20:50:37 adam
8 * Multi register works with record paths and data1 profile path
10 * Revision 1.23 2001/11/15 08:41:24 adam
11 * Fix for weight (bug introduced by previous commit).
13 * Revision 1.22 2001/11/14 22:06:27 adam
14 * Rank-weight may be controlled via query.
16 * Revision 1.21 2001/01/22 10:42:56 adam
17 * Added numerical sort.
19 * Revision 1.20 2000/03/02 14:35:19 adam
20 * Added structure year and date.
22 * Revision 1.19 1999/11/30 13:48:04 adam
23 * Improved installation. Updated for inclusion of YAZ header files.
25 * Revision 1.18 1999/10/15 08:27:46 adam
26 * Fixed replace handler. 8-bit fix.
28 * Revision 1.17 1999/09/08 12:13:21 adam
29 * Fixed minor bug "replace"-mappings. Removed some logging messages.
31 * Revision 1.16 1999/09/07 07:19:21 adam
32 * Work on character mapping. Implemented replace rules.
34 * Revision 1.15 1999/05/26 07:49:14 adam
37 * Revision 1.14 1999/02/19 10:37:40 adam
40 * Revision 1.13 1999/02/18 15:01:04 adam
41 * Structure=key uses register type 0.
43 * Revision 1.12 1999/02/12 13:29:25 adam
44 * Implemented position-flag for registers.
46 * Revision 1.11 1998/10/13 20:09:19 adam
47 * Changed call to readconf_line.
49 * Revision 1.10 1998/06/23 15:33:37 adam
50 * Added feature to specify sort criteria in query (type 7 specifies
53 * Revision 1.9 1998/04/02 14:35:30 adam
54 * First version of Zebra that works with compiled ASN.1.
56 * Revision 1.8 1998/03/05 08:42:44 adam
57 * Minor changes to zebramap data structures. Query mapping rules changed.
59 * Revision 1.7 1998/02/10 12:03:07 adam
62 * Revision 1.6 1998/01/29 13:36:01 adam
63 * Structure word-list, free-form-text and document-text all
64 * trigger ranked search.
66 * Revision 1.5 1997/11/19 10:22:14 adam
67 * Bug fix (introduced by previous commit).
69 * Revision 1.4 1997/11/18 10:05:08 adam
70 * Changed character map facility so that admin can specify character
71 * mapping files for each register type, w, p, etc.
73 * Revision 1.3 1997/11/17 15:35:26 adam
74 * Bug fix. Relation=relevance wasn't observed.
76 * Revision 1.2 1997/10/31 12:39:30 adam
77 * Changed log message.
79 * Revision 1.1 1997/10/27 14:33:06 adam
80 * Moved towards generic character mapping depending on "structure"
81 * field in abstract syntax file. Fixed a few memory leaks. Fixed
82 * bug with negative integers when doing searches with relational
90 #include <yaz/yaz-util.h>
94 #define ZEBRA_MAP_TYPE_SORT 1
95 #define ZEBRA_MAP_TYPE_INDEX 2
97 #define ZEBRA_REPLACE_ANY 300
103 struct zm_token *next;
120 const char *maptab_name;
121 struct zebra_map *next;
122 struct zm_token *replace_tokens;
129 struct zebra_map *map_list;
130 char temp_map_str[2];
131 const char *temp_map_ptr[2];
132 struct zebra_map **lookup_array;
133 WRBUF wrbuf_1, wrbuf_2;
136 void zebra_maps_close (ZebraMaps zms)
138 struct zebra_map *zm = zms->map_list;
142 chrmaptab_destroy (zm->maptab);
145 wrbuf_free (zms->wrbuf_1, 1);
146 wrbuf_free (zms->wrbuf_2, 1);
147 nmem_destroy (zms->nmem);
151 static void zebra_map_read (ZebraMaps zms, const char *name)
158 struct zebra_map **zm = 0, *zp;
160 if (!(f = yaz_path_fopen_base(zms->tabpath, name, "r", zms->tabroot)))
162 logf(LOG_WARN|LOG_ERRNO, "%s", name);
165 while ((argc = readconf_line(f, &lineno, line, 512, argv, 10)))
167 if (!yaz_matchstr (argv[0], "index") && argc == 2)
173 *zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(**zm));
174 (*zm)->reg_id = argv[1][0];
175 (*zm)->maptab_name = NULL;
176 (*zm)->maptab = NULL;
177 (*zm)->type = ZEBRA_MAP_TYPE_INDEX;
178 (*zm)->completeness = 0;
179 (*zm)->positioned = 1;
180 (*zm)->replace_tokens = 0;
182 else if (!yaz_matchstr (argv[0], "sort") && argc == 2)
188 *zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(**zm));
189 (*zm)->reg_id = argv[1][0];
190 (*zm)->maptab_name = NULL;
191 (*zm)->type = ZEBRA_MAP_TYPE_SORT;
192 (*zm)->u.sort.entry_size = 80;
193 (*zm)->maptab = NULL;
194 (*zm)->completeness = 0;
195 (*zm)->positioned = 0;
196 (*zm)->replace_tokens = 0;
198 else if (zm && !yaz_matchstr (argv[0], "charmap") && argc == 2)
200 (*zm)->maptab_name = nmem_strdup (zms->nmem, argv[1]);
202 else if (zm && !yaz_matchstr (argv[0], "completeness") && argc == 2)
204 (*zm)->completeness = atoi (argv[1]);
206 else if (zm && !yaz_matchstr (argv[0], "position") && argc == 2)
208 (*zm)->positioned = atoi (argv[1]);
210 else if (zm && !yaz_matchstr (argv[0], "entrysize") && argc == 2)
212 if ((*zm)->type == ZEBRA_MAP_TYPE_SORT)
213 (*zm)->u.sort.entry_size = atoi (argv[1]);
215 else if (zm && !yaz_matchstr (argv[0], "replace") && argc >= 2)
217 struct zm_token *token = nmem_malloc (zms->nmem, sizeof(*token));
218 token->next = (*zm)->replace_tokens;
219 (*zm)->replace_tokens = token;
221 logf (LOG_LOG, "replace %s", argv[1]);
223 token->token_from = 0;
227 int *dp = token->token_from = (int *)
228 nmem_malloc (zms->nmem, (1+strlen(cp))*sizeof(int));
237 *dp++ = ZEBRA_REPLACE_ANY;
242 *dp++ = zebra_prim(&cp);
244 logf (LOG_LOG, " char %2X %c", dp[-1], dp[-1]);
252 char *dp = token->token_to =
253 nmem_malloc (zms->nmem, strlen(cp)+1);
261 *dp++ = zebra_prim(&cp);
272 for (zp = zms->map_list; zp; zp = zp->next)
273 zms->lookup_array[zp->reg_id] = zp;
276 static void zms_map_handle (void *p, const char *name, const char *value)
278 ZebraMaps zms = (ZebraMaps) p;
280 zebra_map_read (zms, value);
283 ZebraMaps zebra_maps_open (Res res, const char *base)
285 ZebraMaps zms = (ZebraMaps) xmalloc (sizeof(*zms));
288 zms->nmem = nmem_create ();
289 zms->tabpath = nmem_strdup (zms->nmem,
290 res_get_def (res, "profilePath", "."));
293 zms->tabroot = nmem_strdup (zms->nmem, base);
294 zms->map_list = NULL;
296 zms->temp_map_str[0] = '\0';
297 zms->temp_map_str[1] = '\0';
299 zms->temp_map_ptr[0] = zms->temp_map_str;
300 zms->temp_map_ptr[1] = NULL;
302 zms->lookup_array = (struct zebra_map**)
303 nmem_malloc (zms->nmem, sizeof(*zms->lookup_array)*256);
304 for (i = 0; i<256; i++)
305 zms->lookup_array[i] = 0;
306 if (!res || !res_trav (res, "index", zms, zms_map_handle))
307 zebra_map_read (zms, "default.idx");
309 zms->wrbuf_1 = wrbuf_alloc();
310 zms->wrbuf_2 = wrbuf_alloc();
315 struct zebra_map *zebra_map_get (ZebraMaps zms, unsigned reg_id)
317 return zms->lookup_array[reg_id];
320 chrmaptab zebra_charmap_get (ZebraMaps zms, unsigned reg_id)
322 struct zebra_map *zm = zebra_map_get (zms, reg_id);
325 zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(*zm));
326 logf (LOG_WARN, "Unknown register type: %c", reg_id);
329 zm->maptab_name = nmem_strdup (zms->nmem, "@");
331 zm->type = ZEBRA_MAP_TYPE_INDEX;
332 zm->completeness = 0;
333 zm->next = zms->map_list;
334 zms->map_list = zm->next;
336 zms->lookup_array[zm->reg_id & 255] = zm;
340 if (!zm->maptab_name || !yaz_matchstr (zm->maptab_name, "@"))
342 if (!(zm->maptab = chrmaptab_create (zms->tabpath,
345 logf(LOG_WARN, "Failed to read character table %s",
348 logf(LOG_DEBUG, "Read character table %s", zm->maptab_name);
353 const char **zebra_maps_input (ZebraMaps zms, unsigned reg_id,
354 const char **from, int len)
358 maptab = zebra_charmap_get (zms, reg_id);
360 return chr_map_input(maptab, from, len);
362 zms->temp_map_str[0] = **from;
365 return zms->temp_map_ptr;
369 int zebra_maps_input_tokens (ZebraMaps zms, unsigned reg_id,
370 const char *input_str, int input_len,
373 chrmaptab maptab = zebra_charmap_get (zms, reg_id);
382 wrbuf_write (wrbuf, input_str, input_len);
393 out = chr_map_input (maptab, str, len);
396 while (out && *out && **out == *CHR_SPACE)
397 out = chr_map_input (maptab, str, len);
402 const char *zebra_maps_output(ZebraMaps zms, unsigned reg_id,
405 chrmaptab maptab = zebra_charmap_get (zms, reg_id);
408 return chr_map_output (maptab, from, 1);
412 /* ------------------------------------ */
418 Z_AttributeElement **attributeList;
422 static int attr_find (AttrType *src, oid_value *attributeSetP)
424 while (src->major < src->num_attributes)
426 Z_AttributeElement *element;
428 element = src->attributeList[src->major];
429 if (src->type == *element->attributeType)
431 switch (element->which)
433 case Z_AttributeValue_numeric:
435 if (element->attributeSet && attributeSetP)
439 attrset = oid_getentbyoid (element->attributeSet);
440 *attributeSetP = attrset->value;
442 return *element->value.numeric;
444 case Z_AttributeValue_complex:
445 if (src->minor >= element->value.complex->num_list ||
446 element->value.complex->list[src->minor]->which !=
447 Z_StringOrNumeric_numeric)
450 if (element->attributeSet && attributeSetP)
454 attrset = oid_getentbyoid (element->attributeSet);
455 *attributeSetP = attrset->value;
457 return *element->value.complex->list[src->minor-1]->u.numeric;
467 static void attr_init_APT (AttrType *src, Z_AttributesPlusTerm *zapt, int type)
470 src->attributeList = zapt->attributes->attributes;
471 src->num_attributes = zapt->attributes->num_attributes;
473 src->attributeList = zapt->attributeList;
474 src->num_attributes = zapt->num_attributes;
481 static void attr_init_AttrList (AttrType *src, Z_AttributeList *list, int type)
483 src->attributeList = list->attributes;
484 src->num_attributes = list->num_attributes;
490 /* ------------------------------------ */
492 int zebra_maps_is_complete (ZebraMaps zms, unsigned reg_id)
494 struct zebra_map *zm = zebra_map_get (zms, reg_id);
496 return zm->completeness;
500 int zebra_maps_is_positioned (ZebraMaps zms, unsigned reg_id)
502 struct zebra_map *zm = zebra_map_get (zms, reg_id);
504 return zm->positioned;
508 int zebra_maps_is_sort (ZebraMaps zms, unsigned reg_id)
510 struct zebra_map *zm = zebra_map_get (zms, reg_id);
512 return zm->type == ZEBRA_MAP_TYPE_SORT;
516 int zebra_maps_sort (ZebraMaps zms, Z_SortAttributes *sortAttributes,
522 attr_init_AttrList (&use, sortAttributes->list, 1);
523 attr_init_AttrList (&structure, sortAttributes->list, 4);
526 structure_value = attr_find (&structure, 0);
527 if (structure_value == 109)
529 return attr_find (&use, NULL);
532 int zebra_maps_attr (ZebraMaps zms, Z_AttributesPlusTerm *zapt,
533 unsigned *reg_id, char **search_type, char *rank_type,
534 int *complete_flag, int *sort_flag)
536 AttrType completeness;
539 AttrType sort_relation;
541 int completeness_value;
544 int sort_relation_value;
547 attr_init_APT (&structure, zapt, 4);
548 attr_init_APT (&completeness, zapt, 6);
549 attr_init_APT (&relation, zapt, 2);
550 attr_init_APT (&sort_relation, zapt, 7);
551 attr_init_APT (&weight, zapt, 9);
553 completeness_value = attr_find (&completeness, NULL);
554 structure_value = attr_find (&structure, NULL);
555 relation_value = attr_find (&relation, NULL);
556 sort_relation_value = attr_find (&sort_relation, NULL);
557 weight_value = attr_find (&weight, NULL);
559 if (completeness_value == 2 || completeness_value == 3)
565 *sort_flag = (sort_relation_value > 0) ? 1 : 0;
566 *search_type = "phrase";
567 strcpy (rank_type, "void");
568 if (relation_value == 102)
570 if (weight_value == -1)
572 sprintf (rank_type, "rank,%d", weight_value);
578 switch (structure_value)
580 case 6: /* word list */
581 *search_type = "and-list";
583 case 105: /* free-form-text */
584 *search_type = "or-list";
586 case 106: /* document-text */
587 *search_type = "or-list";
592 case 108: /* string */
593 *search_type = "phrase";
595 case 107: /* local-number */
596 *search_type = "local";
599 case 109: /* numeric string */
601 *search_type = "numeric";
605 *search_type = "phrase";
609 *search_type = "phrase";
613 *search_type = "phrase";
617 *search_type = "phrase";
625 int zebra_replace_sub(ZebraMaps zms, unsigned reg_id, const char *ex_list,
626 const char *input_str, int input_len, WRBUF wrbuf);
628 WRBUF zebra_replace(ZebraMaps zms, unsigned reg_id, const char *ex_list,
629 const char *input_str, int input_len)
631 struct zebra_map *zm = zebra_map_get (zms, reg_id);
633 wrbuf_rewind(zms->wrbuf_1);
634 wrbuf_write(zms->wrbuf_1, input_str, input_len);
635 if (!zm || !zm->replace_tokens)
639 logf (LOG_LOG, "in:%.*s:", wrbuf_len(zms->wrbuf_1),
640 wrbuf_buf(zms->wrbuf_1));
644 if (!zebra_replace_sub(zms, reg_id, ex_list, wrbuf_buf(zms->wrbuf_1),
645 wrbuf_len(zms->wrbuf_1), zms->wrbuf_2))
647 if (!zebra_replace_sub(zms, reg_id, ex_list, wrbuf_buf(zms->wrbuf_2),
648 wrbuf_len(zms->wrbuf_2), zms->wrbuf_1))
654 int zebra_replace_sub(ZebraMaps zms, unsigned reg_id, const char *ex_list,
655 const char *input_str, int input_len, WRBUF wrbuf)
659 struct zebra_map *zm = zebra_map_get (zms, reg_id);
662 for (i = -1; i <= input_len; )
664 struct zm_token *token;
665 char replace_string[128];
669 for (token = zm->replace_tokens; !replace_in && token;
673 int replace_done = 0;
678 if (!token->token_from[j])
683 if (ex_list && strchr (ex_list, token->token_from[j]))
685 if (i+j < 0 || j+i >= input_len)
688 c = input_str[j+i] & 255;
689 if (token->token_from[j] == ZEBRA_REPLACE_ANY)
693 replace_string[replace_out++] = c;
697 if (c != token->token_from[j])
703 const char *cp = token->token_to;
705 for (; cp && *cp; cp++)
706 replace_string[replace_out++] = *cp;
713 if (i >= 0 && i < input_len)
714 wrbuf_putc(wrbuf, input_str[i]);
721 wrbuf_write(wrbuf, replace_string, replace_out);
726 logf (LOG_LOG, "out:%.*s:", wrbuf_len(wrbuf), wrbuf_buf(wrbuf));