2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.26 2002-08-01 08:53:35 adam
8 * Work on xpath-like queries
10 * Revision 1.25 2002/04/05 12:54:29 adam
13 * Revision 1.24 2002/04/04 20:50:37 adam
14 * Multi register works with record paths and data1 profile path
16 * Revision 1.23 2001/11/15 08:41:24 adam
17 * Fix for weight (bug introduced by previous commit).
19 * Revision 1.22 2001/11/14 22:06:27 adam
20 * Rank-weight may be controlled via query.
22 * Revision 1.21 2001/01/22 10:42:56 adam
23 * Added numerical sort.
25 * Revision 1.20 2000/03/02 14:35:19 adam
26 * Added structure year and date.
28 * Revision 1.19 1999/11/30 13:48:04 adam
29 * Improved installation. Updated for inclusion of YAZ header files.
31 * Revision 1.18 1999/10/15 08:27:46 adam
32 * Fixed replace handler. 8-bit fix.
34 * Revision 1.17 1999/09/08 12:13:21 adam
35 * Fixed minor bug "replace"-mappings. Removed some logging messages.
37 * Revision 1.16 1999/09/07 07:19:21 adam
38 * Work on character mapping. Implemented replace rules.
40 * Revision 1.15 1999/05/26 07:49:14 adam
43 * Revision 1.14 1999/02/19 10:37:40 adam
46 * Revision 1.13 1999/02/18 15:01:04 adam
47 * Structure=key uses register type 0.
49 * Revision 1.12 1999/02/12 13:29:25 adam
50 * Implemented position-flag for registers.
52 * Revision 1.11 1998/10/13 20:09:19 adam
53 * Changed call to readconf_line.
55 * Revision 1.10 1998/06/23 15:33:37 adam
56 * Added feature to specify sort criteria in query (type 7 specifies
59 * Revision 1.9 1998/04/02 14:35:30 adam
60 * First version of Zebra that works with compiled ASN.1.
62 * Revision 1.8 1998/03/05 08:42:44 adam
63 * Minor changes to zebramap data structures. Query mapping rules changed.
65 * Revision 1.7 1998/02/10 12:03:07 adam
68 * Revision 1.6 1998/01/29 13:36:01 adam
69 * Structure word-list, free-form-text and document-text all
70 * trigger ranked search.
72 * Revision 1.5 1997/11/19 10:22:14 adam
73 * Bug fix (introduced by previous commit).
75 * Revision 1.4 1997/11/18 10:05:08 adam
76 * Changed character map facility so that admin can specify character
77 * mapping files for each register type, w, p, etc.
79 * Revision 1.3 1997/11/17 15:35:26 adam
80 * Bug fix. Relation=relevance wasn't observed.
82 * Revision 1.2 1997/10/31 12:39:30 adam
83 * Changed log message.
85 * Revision 1.1 1997/10/27 14:33:06 adam
86 * Moved towards generic character mapping depending on "structure"
87 * field in abstract syntax file. Fixed a few memory leaks. Fixed
88 * bug with negative integers when doing searches with relational
96 #include <yaz/yaz-util.h>
100 #define ZEBRA_MAP_TYPE_SORT 1
101 #define ZEBRA_MAP_TYPE_INDEX 2
103 #define ZEBRA_REPLACE_ANY 300
109 struct zm_token *next;
126 const char *maptab_name;
127 struct zebra_map *next;
128 struct zm_token *replace_tokens;
135 struct zebra_map *map_list;
136 char temp_map_str[2];
137 const char *temp_map_ptr[2];
138 struct zebra_map **lookup_array;
139 WRBUF wrbuf_1, wrbuf_2;
142 void zebra_maps_close (ZebraMaps zms)
144 struct zebra_map *zm = zms->map_list;
148 chrmaptab_destroy (zm->maptab);
151 wrbuf_free (zms->wrbuf_1, 1);
152 wrbuf_free (zms->wrbuf_2, 1);
153 nmem_destroy (zms->nmem);
157 static void zebra_map_read (ZebraMaps zms, const char *name)
164 struct zebra_map **zm = 0, *zp;
166 if (!(f = yaz_fopen(zms->tabpath, name, "r", zms->tabroot)))
168 logf(LOG_WARN|LOG_ERRNO, "%s", name);
171 while ((argc = readconf_line(f, &lineno, line, 512, argv, 10)))
173 if (!yaz_matchstr (argv[0], "index") && argc == 2)
179 *zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(**zm));
180 (*zm)->reg_id = argv[1][0];
181 (*zm)->maptab_name = NULL;
182 (*zm)->maptab = NULL;
183 (*zm)->type = ZEBRA_MAP_TYPE_INDEX;
184 (*zm)->completeness = 0;
185 (*zm)->positioned = 1;
186 (*zm)->replace_tokens = 0;
188 else if (!yaz_matchstr (argv[0], "sort") && argc == 2)
194 *zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(**zm));
195 (*zm)->reg_id = argv[1][0];
196 (*zm)->maptab_name = NULL;
197 (*zm)->type = ZEBRA_MAP_TYPE_SORT;
198 (*zm)->u.sort.entry_size = 80;
199 (*zm)->maptab = NULL;
200 (*zm)->completeness = 0;
201 (*zm)->positioned = 0;
202 (*zm)->replace_tokens = 0;
204 else if (zm && !yaz_matchstr (argv[0], "charmap") && argc == 2)
206 (*zm)->maptab_name = nmem_strdup (zms->nmem, argv[1]);
208 else if (zm && !yaz_matchstr (argv[0], "completeness") && argc == 2)
210 (*zm)->completeness = atoi (argv[1]);
212 else if (zm && !yaz_matchstr (argv[0], "position") && argc == 2)
214 (*zm)->positioned = atoi (argv[1]);
216 else if (zm && !yaz_matchstr (argv[0], "entrysize") && argc == 2)
218 if ((*zm)->type == ZEBRA_MAP_TYPE_SORT)
219 (*zm)->u.sort.entry_size = atoi (argv[1]);
221 else if (zm && !yaz_matchstr (argv[0], "replace") && argc >= 2)
223 struct zm_token *token = nmem_malloc (zms->nmem, sizeof(*token));
224 token->next = (*zm)->replace_tokens;
225 (*zm)->replace_tokens = token;
227 logf (LOG_LOG, "replace %s", argv[1]);
229 token->token_from = 0;
233 int *dp = token->token_from = (int *)
234 nmem_malloc (zms->nmem, (1+strlen(cp))*sizeof(int));
243 *dp++ = ZEBRA_REPLACE_ANY;
248 *dp++ = zebra_prim(&cp);
250 logf (LOG_LOG, " char %2X %c", dp[-1], dp[-1]);
258 char *dp = token->token_to =
259 nmem_malloc (zms->nmem, strlen(cp)+1);
267 *dp++ = zebra_prim(&cp);
278 for (zp = zms->map_list; zp; zp = zp->next)
279 zms->lookup_array[zp->reg_id] = zp;
282 static void zms_map_handle (void *p, const char *name, const char *value)
284 ZebraMaps zms = (ZebraMaps) p;
286 zebra_map_read (zms, value);
289 ZebraMaps zebra_maps_open (Res res, const char *base)
291 ZebraMaps zms = (ZebraMaps) xmalloc (sizeof(*zms));
294 zms->nmem = nmem_create ();
295 zms->tabpath = nmem_strdup (zms->nmem,
296 res_get_def (res, "profilePath", "."));
299 zms->tabroot = nmem_strdup (zms->nmem, base);
300 zms->map_list = NULL;
302 zms->temp_map_str[0] = '\0';
303 zms->temp_map_str[1] = '\0';
305 zms->temp_map_ptr[0] = zms->temp_map_str;
306 zms->temp_map_ptr[1] = NULL;
308 zms->lookup_array = (struct zebra_map**)
309 nmem_malloc (zms->nmem, sizeof(*zms->lookup_array)*256);
310 for (i = 0; i<256; i++)
311 zms->lookup_array[i] = 0;
312 if (!res || !res_trav (res, "index", zms, zms_map_handle))
313 zebra_map_read (zms, "default.idx");
315 zms->wrbuf_1 = wrbuf_alloc();
316 zms->wrbuf_2 = wrbuf_alloc();
321 struct zebra_map *zebra_map_get (ZebraMaps zms, unsigned reg_id)
323 return zms->lookup_array[reg_id];
326 chrmaptab zebra_charmap_get (ZebraMaps zms, unsigned reg_id)
328 struct zebra_map *zm = zebra_map_get (zms, reg_id);
331 zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(*zm));
332 logf (LOG_WARN, "Unknown register type: %c", reg_id);
335 zm->maptab_name = nmem_strdup (zms->nmem, "@");
337 zm->type = ZEBRA_MAP_TYPE_INDEX;
338 zm->completeness = 0;
339 zm->next = zms->map_list;
340 zms->map_list = zm->next;
342 zms->lookup_array[zm->reg_id & 255] = zm;
346 if (!zm->maptab_name || !yaz_matchstr (zm->maptab_name, "@"))
348 if (!(zm->maptab = chrmaptab_create (zms->tabpath,
351 logf(LOG_WARN, "Failed to read character table %s",
354 logf(LOG_DEBUG, "Read character table %s", zm->maptab_name);
359 const char **zebra_maps_input (ZebraMaps zms, unsigned reg_id,
360 const char **from, int len)
364 maptab = zebra_charmap_get (zms, reg_id);
366 return chr_map_input(maptab, from, len);
368 zms->temp_map_str[0] = **from;
371 return zms->temp_map_ptr;
375 int zebra_maps_input_tokens (ZebraMaps zms, unsigned reg_id,
376 const char *input_str, int input_len,
379 chrmaptab maptab = zebra_charmap_get (zms, reg_id);
388 wrbuf_write (wrbuf, input_str, input_len);
399 out = chr_map_input (maptab, str, len);
402 while (out && *out && **out == *CHR_SPACE)
403 out = chr_map_input (maptab, str, len);
408 const char *zebra_maps_output(ZebraMaps zms, unsigned reg_id,
411 chrmaptab maptab = zebra_charmap_get (zms, reg_id);
414 return chr_map_output (maptab, from, 1);
418 /* ------------------------------------ */
424 Z_AttributeElement **attributeList;
428 static int attr_find (AttrType *src, oid_value *attributeSetP)
430 while (src->major < src->num_attributes)
432 Z_AttributeElement *element;
434 element = src->attributeList[src->major];
435 if (src->type == *element->attributeType)
437 switch (element->which)
439 case Z_AttributeValue_numeric:
441 if (element->attributeSet && attributeSetP)
445 attrset = oid_getentbyoid (element->attributeSet);
446 *attributeSetP = attrset->value;
448 return *element->value.numeric;
450 case Z_AttributeValue_complex:
451 if (src->minor >= element->value.complex->num_list ||
452 element->value.complex->list[src->minor]->which !=
453 Z_StringOrNumeric_numeric)
456 if (element->attributeSet && attributeSetP)
460 attrset = oid_getentbyoid (element->attributeSet);
461 *attributeSetP = attrset->value;
463 return *element->value.complex->list[src->minor-1]->u.numeric;
473 static void attr_init_APT (AttrType *src, Z_AttributesPlusTerm *zapt, int type)
476 src->attributeList = zapt->attributes->attributes;
477 src->num_attributes = zapt->attributes->num_attributes;
479 src->attributeList = zapt->attributeList;
480 src->num_attributes = zapt->num_attributes;
487 static void attr_init_AttrList (AttrType *src, Z_AttributeList *list, int type)
489 src->attributeList = list->attributes;
490 src->num_attributes = list->num_attributes;
496 /* ------------------------------------ */
498 int zebra_maps_is_complete (ZebraMaps zms, unsigned reg_id)
500 struct zebra_map *zm = zebra_map_get (zms, reg_id);
502 return zm->completeness;
506 int zebra_maps_is_positioned (ZebraMaps zms, unsigned reg_id)
508 struct zebra_map *zm = zebra_map_get (zms, reg_id);
510 return zm->positioned;
514 int zebra_maps_is_sort (ZebraMaps zms, unsigned reg_id)
516 struct zebra_map *zm = zebra_map_get (zms, reg_id);
518 return zm->type == ZEBRA_MAP_TYPE_SORT;
522 int zebra_maps_sort (ZebraMaps zms, Z_SortAttributes *sortAttributes,
528 attr_init_AttrList (&use, sortAttributes->list, 1);
529 attr_init_AttrList (&structure, sortAttributes->list, 4);
532 structure_value = attr_find (&structure, 0);
533 if (structure_value == 109)
535 return attr_find (&use, NULL);
538 int zebra_maps_attr (ZebraMaps zms, Z_AttributesPlusTerm *zapt,
539 unsigned *reg_id, char **search_type, char *rank_type,
540 int *complete_flag, int *sort_flag)
542 AttrType completeness;
545 AttrType sort_relation;
547 int completeness_value;
550 int sort_relation_value;
553 attr_init_APT (&structure, zapt, 4);
554 attr_init_APT (&completeness, zapt, 6);
555 attr_init_APT (&relation, zapt, 2);
556 attr_init_APT (&sort_relation, zapt, 7);
557 attr_init_APT (&weight, zapt, 9);
559 completeness_value = attr_find (&completeness, NULL);
560 structure_value = attr_find (&structure, NULL);
561 relation_value = attr_find (&relation, NULL);
562 sort_relation_value = attr_find (&sort_relation, NULL);
563 weight_value = attr_find (&weight, NULL);
565 if (completeness_value == 2 || completeness_value == 3)
571 *sort_flag = (sort_relation_value > 0) ? 1 : 0;
572 *search_type = "phrase";
573 strcpy (rank_type, "void");
574 if (relation_value == 102)
576 if (weight_value == -1)
578 sprintf (rank_type, "rank,%d", weight_value);
580 if (relation_value == 103)
582 *search_type = "always";
589 switch (structure_value)
591 case 6: /* word list */
592 *search_type = "and-list";
594 case 105: /* free-form-text */
595 *search_type = "or-list";
597 case 106: /* document-text */
598 *search_type = "or-list";
603 case 108: /* string */
604 *search_type = "phrase";
606 case 107: /* local-number */
607 *search_type = "local";
610 case 109: /* numeric string */
612 *search_type = "numeric";
616 *search_type = "phrase";
620 *search_type = "phrase";
624 *search_type = "phrase";
628 *search_type = "phrase";
636 int zebra_replace_sub(ZebraMaps zms, unsigned reg_id, const char *ex_list,
637 const char *input_str, int input_len, WRBUF wrbuf);
639 WRBUF zebra_replace(ZebraMaps zms, unsigned reg_id, const char *ex_list,
640 const char *input_str, int input_len)
642 struct zebra_map *zm = zebra_map_get (zms, reg_id);
644 wrbuf_rewind(zms->wrbuf_1);
645 wrbuf_write(zms->wrbuf_1, input_str, input_len);
646 if (!zm || !zm->replace_tokens)
650 logf (LOG_LOG, "in:%.*s:", wrbuf_len(zms->wrbuf_1),
651 wrbuf_buf(zms->wrbuf_1));
655 if (!zebra_replace_sub(zms, reg_id, ex_list, wrbuf_buf(zms->wrbuf_1),
656 wrbuf_len(zms->wrbuf_1), zms->wrbuf_2))
658 if (!zebra_replace_sub(zms, reg_id, ex_list, wrbuf_buf(zms->wrbuf_2),
659 wrbuf_len(zms->wrbuf_2), zms->wrbuf_1))
665 int zebra_replace_sub(ZebraMaps zms, unsigned reg_id, const char *ex_list,
666 const char *input_str, int input_len, WRBUF wrbuf)
670 struct zebra_map *zm = zebra_map_get (zms, reg_id);
673 for (i = -1; i <= input_len; )
675 struct zm_token *token;
676 char replace_string[128];
680 for (token = zm->replace_tokens; !replace_in && token;
684 int replace_done = 0;
689 if (!token->token_from[j])
694 if (ex_list && strchr (ex_list, token->token_from[j]))
696 if (i+j < 0 || j+i >= input_len)
699 c = input_str[j+i] & 255;
700 if (token->token_from[j] == ZEBRA_REPLACE_ANY)
704 replace_string[replace_out++] = c;
708 if (c != token->token_from[j])
714 const char *cp = token->token_to;
716 for (; cp && *cp; cp++)
717 replace_string[replace_out++] = *cp;
724 if (i >= 0 && i < input_len)
725 wrbuf_putc(wrbuf, input_str[i]);
732 wrbuf_write(wrbuf, replace_string, replace_out);
737 logf (LOG_LOG, "out:%.*s:", wrbuf_len(wrbuf), wrbuf_buf(wrbuf));