2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.25 2002-04-05 12:54:29 adam
10 * Revision 1.24 2002/04/04 20:50:37 adam
11 * Multi register works with record paths and data1 profile path
13 * Revision 1.23 2001/11/15 08:41:24 adam
14 * Fix for weight (bug introduced by previous commit).
16 * Revision 1.22 2001/11/14 22:06:27 adam
17 * Rank-weight may be controlled via query.
19 * Revision 1.21 2001/01/22 10:42:56 adam
20 * Added numerical sort.
22 * Revision 1.20 2000/03/02 14:35:19 adam
23 * Added structure year and date.
25 * Revision 1.19 1999/11/30 13:48:04 adam
26 * Improved installation. Updated for inclusion of YAZ header files.
28 * Revision 1.18 1999/10/15 08:27:46 adam
29 * Fixed replace handler. 8-bit fix.
31 * Revision 1.17 1999/09/08 12:13:21 adam
32 * Fixed minor bug "replace"-mappings. Removed some logging messages.
34 * Revision 1.16 1999/09/07 07:19:21 adam
35 * Work on character mapping. Implemented replace rules.
37 * Revision 1.15 1999/05/26 07:49:14 adam
40 * Revision 1.14 1999/02/19 10:37:40 adam
43 * Revision 1.13 1999/02/18 15:01:04 adam
44 * Structure=key uses register type 0.
46 * Revision 1.12 1999/02/12 13:29:25 adam
47 * Implemented position-flag for registers.
49 * Revision 1.11 1998/10/13 20:09:19 adam
50 * Changed call to readconf_line.
52 * Revision 1.10 1998/06/23 15:33:37 adam
53 * Added feature to specify sort criteria in query (type 7 specifies
56 * Revision 1.9 1998/04/02 14:35:30 adam
57 * First version of Zebra that works with compiled ASN.1.
59 * Revision 1.8 1998/03/05 08:42:44 adam
60 * Minor changes to zebramap data structures. Query mapping rules changed.
62 * Revision 1.7 1998/02/10 12:03:07 adam
65 * Revision 1.6 1998/01/29 13:36:01 adam
66 * Structure word-list, free-form-text and document-text all
67 * trigger ranked search.
69 * Revision 1.5 1997/11/19 10:22:14 adam
70 * Bug fix (introduced by previous commit).
72 * Revision 1.4 1997/11/18 10:05:08 adam
73 * Changed character map facility so that admin can specify character
74 * mapping files for each register type, w, p, etc.
76 * Revision 1.3 1997/11/17 15:35:26 adam
77 * Bug fix. Relation=relevance wasn't observed.
79 * Revision 1.2 1997/10/31 12:39:30 adam
80 * Changed log message.
82 * Revision 1.1 1997/10/27 14:33:06 adam
83 * Moved towards generic character mapping depending on "structure"
84 * field in abstract syntax file. Fixed a few memory leaks. Fixed
85 * bug with negative integers when doing searches with relational
93 #include <yaz/yaz-util.h>
97 #define ZEBRA_MAP_TYPE_SORT 1
98 #define ZEBRA_MAP_TYPE_INDEX 2
100 #define ZEBRA_REPLACE_ANY 300
106 struct zm_token *next;
123 const char *maptab_name;
124 struct zebra_map *next;
125 struct zm_token *replace_tokens;
132 struct zebra_map *map_list;
133 char temp_map_str[2];
134 const char *temp_map_ptr[2];
135 struct zebra_map **lookup_array;
136 WRBUF wrbuf_1, wrbuf_2;
139 void zebra_maps_close (ZebraMaps zms)
141 struct zebra_map *zm = zms->map_list;
145 chrmaptab_destroy (zm->maptab);
148 wrbuf_free (zms->wrbuf_1, 1);
149 wrbuf_free (zms->wrbuf_2, 1);
150 nmem_destroy (zms->nmem);
154 static void zebra_map_read (ZebraMaps zms, const char *name)
161 struct zebra_map **zm = 0, *zp;
163 if (!(f = yaz_fopen(zms->tabpath, name, "r", zms->tabroot)))
165 logf(LOG_WARN|LOG_ERRNO, "%s", name);
168 while ((argc = readconf_line(f, &lineno, line, 512, argv, 10)))
170 if (!yaz_matchstr (argv[0], "index") && argc == 2)
176 *zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(**zm));
177 (*zm)->reg_id = argv[1][0];
178 (*zm)->maptab_name = NULL;
179 (*zm)->maptab = NULL;
180 (*zm)->type = ZEBRA_MAP_TYPE_INDEX;
181 (*zm)->completeness = 0;
182 (*zm)->positioned = 1;
183 (*zm)->replace_tokens = 0;
185 else if (!yaz_matchstr (argv[0], "sort") && argc == 2)
191 *zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(**zm));
192 (*zm)->reg_id = argv[1][0];
193 (*zm)->maptab_name = NULL;
194 (*zm)->type = ZEBRA_MAP_TYPE_SORT;
195 (*zm)->u.sort.entry_size = 80;
196 (*zm)->maptab = NULL;
197 (*zm)->completeness = 0;
198 (*zm)->positioned = 0;
199 (*zm)->replace_tokens = 0;
201 else if (zm && !yaz_matchstr (argv[0], "charmap") && argc == 2)
203 (*zm)->maptab_name = nmem_strdup (zms->nmem, argv[1]);
205 else if (zm && !yaz_matchstr (argv[0], "completeness") && argc == 2)
207 (*zm)->completeness = atoi (argv[1]);
209 else if (zm && !yaz_matchstr (argv[0], "position") && argc == 2)
211 (*zm)->positioned = atoi (argv[1]);
213 else if (zm && !yaz_matchstr (argv[0], "entrysize") && argc == 2)
215 if ((*zm)->type == ZEBRA_MAP_TYPE_SORT)
216 (*zm)->u.sort.entry_size = atoi (argv[1]);
218 else if (zm && !yaz_matchstr (argv[0], "replace") && argc >= 2)
220 struct zm_token *token = nmem_malloc (zms->nmem, sizeof(*token));
221 token->next = (*zm)->replace_tokens;
222 (*zm)->replace_tokens = token;
224 logf (LOG_LOG, "replace %s", argv[1]);
226 token->token_from = 0;
230 int *dp = token->token_from = (int *)
231 nmem_malloc (zms->nmem, (1+strlen(cp))*sizeof(int));
240 *dp++ = ZEBRA_REPLACE_ANY;
245 *dp++ = zebra_prim(&cp);
247 logf (LOG_LOG, " char %2X %c", dp[-1], dp[-1]);
255 char *dp = token->token_to =
256 nmem_malloc (zms->nmem, strlen(cp)+1);
264 *dp++ = zebra_prim(&cp);
275 for (zp = zms->map_list; zp; zp = zp->next)
276 zms->lookup_array[zp->reg_id] = zp;
279 static void zms_map_handle (void *p, const char *name, const char *value)
281 ZebraMaps zms = (ZebraMaps) p;
283 zebra_map_read (zms, value);
286 ZebraMaps zebra_maps_open (Res res, const char *base)
288 ZebraMaps zms = (ZebraMaps) xmalloc (sizeof(*zms));
291 zms->nmem = nmem_create ();
292 zms->tabpath = nmem_strdup (zms->nmem,
293 res_get_def (res, "profilePath", "."));
296 zms->tabroot = nmem_strdup (zms->nmem, base);
297 zms->map_list = NULL;
299 zms->temp_map_str[0] = '\0';
300 zms->temp_map_str[1] = '\0';
302 zms->temp_map_ptr[0] = zms->temp_map_str;
303 zms->temp_map_ptr[1] = NULL;
305 zms->lookup_array = (struct zebra_map**)
306 nmem_malloc (zms->nmem, sizeof(*zms->lookup_array)*256);
307 for (i = 0; i<256; i++)
308 zms->lookup_array[i] = 0;
309 if (!res || !res_trav (res, "index", zms, zms_map_handle))
310 zebra_map_read (zms, "default.idx");
312 zms->wrbuf_1 = wrbuf_alloc();
313 zms->wrbuf_2 = wrbuf_alloc();
318 struct zebra_map *zebra_map_get (ZebraMaps zms, unsigned reg_id)
320 return zms->lookup_array[reg_id];
323 chrmaptab zebra_charmap_get (ZebraMaps zms, unsigned reg_id)
325 struct zebra_map *zm = zebra_map_get (zms, reg_id);
328 zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(*zm));
329 logf (LOG_WARN, "Unknown register type: %c", reg_id);
332 zm->maptab_name = nmem_strdup (zms->nmem, "@");
334 zm->type = ZEBRA_MAP_TYPE_INDEX;
335 zm->completeness = 0;
336 zm->next = zms->map_list;
337 zms->map_list = zm->next;
339 zms->lookup_array[zm->reg_id & 255] = zm;
343 if (!zm->maptab_name || !yaz_matchstr (zm->maptab_name, "@"))
345 if (!(zm->maptab = chrmaptab_create (zms->tabpath,
348 logf(LOG_WARN, "Failed to read character table %s",
351 logf(LOG_DEBUG, "Read character table %s", zm->maptab_name);
356 const char **zebra_maps_input (ZebraMaps zms, unsigned reg_id,
357 const char **from, int len)
361 maptab = zebra_charmap_get (zms, reg_id);
363 return chr_map_input(maptab, from, len);
365 zms->temp_map_str[0] = **from;
368 return zms->temp_map_ptr;
372 int zebra_maps_input_tokens (ZebraMaps zms, unsigned reg_id,
373 const char *input_str, int input_len,
376 chrmaptab maptab = zebra_charmap_get (zms, reg_id);
385 wrbuf_write (wrbuf, input_str, input_len);
396 out = chr_map_input (maptab, str, len);
399 while (out && *out && **out == *CHR_SPACE)
400 out = chr_map_input (maptab, str, len);
405 const char *zebra_maps_output(ZebraMaps zms, unsigned reg_id,
408 chrmaptab maptab = zebra_charmap_get (zms, reg_id);
411 return chr_map_output (maptab, from, 1);
415 /* ------------------------------------ */
421 Z_AttributeElement **attributeList;
425 static int attr_find (AttrType *src, oid_value *attributeSetP)
427 while (src->major < src->num_attributes)
429 Z_AttributeElement *element;
431 element = src->attributeList[src->major];
432 if (src->type == *element->attributeType)
434 switch (element->which)
436 case Z_AttributeValue_numeric:
438 if (element->attributeSet && attributeSetP)
442 attrset = oid_getentbyoid (element->attributeSet);
443 *attributeSetP = attrset->value;
445 return *element->value.numeric;
447 case Z_AttributeValue_complex:
448 if (src->minor >= element->value.complex->num_list ||
449 element->value.complex->list[src->minor]->which !=
450 Z_StringOrNumeric_numeric)
453 if (element->attributeSet && attributeSetP)
457 attrset = oid_getentbyoid (element->attributeSet);
458 *attributeSetP = attrset->value;
460 return *element->value.complex->list[src->minor-1]->u.numeric;
470 static void attr_init_APT (AttrType *src, Z_AttributesPlusTerm *zapt, int type)
473 src->attributeList = zapt->attributes->attributes;
474 src->num_attributes = zapt->attributes->num_attributes;
476 src->attributeList = zapt->attributeList;
477 src->num_attributes = zapt->num_attributes;
484 static void attr_init_AttrList (AttrType *src, Z_AttributeList *list, int type)
486 src->attributeList = list->attributes;
487 src->num_attributes = list->num_attributes;
493 /* ------------------------------------ */
495 int zebra_maps_is_complete (ZebraMaps zms, unsigned reg_id)
497 struct zebra_map *zm = zebra_map_get (zms, reg_id);
499 return zm->completeness;
503 int zebra_maps_is_positioned (ZebraMaps zms, unsigned reg_id)
505 struct zebra_map *zm = zebra_map_get (zms, reg_id);
507 return zm->positioned;
511 int zebra_maps_is_sort (ZebraMaps zms, unsigned reg_id)
513 struct zebra_map *zm = zebra_map_get (zms, reg_id);
515 return zm->type == ZEBRA_MAP_TYPE_SORT;
519 int zebra_maps_sort (ZebraMaps zms, Z_SortAttributes *sortAttributes,
525 attr_init_AttrList (&use, sortAttributes->list, 1);
526 attr_init_AttrList (&structure, sortAttributes->list, 4);
529 structure_value = attr_find (&structure, 0);
530 if (structure_value == 109)
532 return attr_find (&use, NULL);
535 int zebra_maps_attr (ZebraMaps zms, Z_AttributesPlusTerm *zapt,
536 unsigned *reg_id, char **search_type, char *rank_type,
537 int *complete_flag, int *sort_flag)
539 AttrType completeness;
542 AttrType sort_relation;
544 int completeness_value;
547 int sort_relation_value;
550 attr_init_APT (&structure, zapt, 4);
551 attr_init_APT (&completeness, zapt, 6);
552 attr_init_APT (&relation, zapt, 2);
553 attr_init_APT (&sort_relation, zapt, 7);
554 attr_init_APT (&weight, zapt, 9);
556 completeness_value = attr_find (&completeness, NULL);
557 structure_value = attr_find (&structure, NULL);
558 relation_value = attr_find (&relation, NULL);
559 sort_relation_value = attr_find (&sort_relation, NULL);
560 weight_value = attr_find (&weight, NULL);
562 if (completeness_value == 2 || completeness_value == 3)
568 *sort_flag = (sort_relation_value > 0) ? 1 : 0;
569 *search_type = "phrase";
570 strcpy (rank_type, "void");
571 if (relation_value == 102)
573 if (weight_value == -1)
575 sprintf (rank_type, "rank,%d", weight_value);
581 switch (structure_value)
583 case 6: /* word list */
584 *search_type = "and-list";
586 case 105: /* free-form-text */
587 *search_type = "or-list";
589 case 106: /* document-text */
590 *search_type = "or-list";
595 case 108: /* string */
596 *search_type = "phrase";
598 case 107: /* local-number */
599 *search_type = "local";
602 case 109: /* numeric string */
604 *search_type = "numeric";
608 *search_type = "phrase";
612 *search_type = "phrase";
616 *search_type = "phrase";
620 *search_type = "phrase";
628 int zebra_replace_sub(ZebraMaps zms, unsigned reg_id, const char *ex_list,
629 const char *input_str, int input_len, WRBUF wrbuf);
631 WRBUF zebra_replace(ZebraMaps zms, unsigned reg_id, const char *ex_list,
632 const char *input_str, int input_len)
634 struct zebra_map *zm = zebra_map_get (zms, reg_id);
636 wrbuf_rewind(zms->wrbuf_1);
637 wrbuf_write(zms->wrbuf_1, input_str, input_len);
638 if (!zm || !zm->replace_tokens)
642 logf (LOG_LOG, "in:%.*s:", wrbuf_len(zms->wrbuf_1),
643 wrbuf_buf(zms->wrbuf_1));
647 if (!zebra_replace_sub(zms, reg_id, ex_list, wrbuf_buf(zms->wrbuf_1),
648 wrbuf_len(zms->wrbuf_1), zms->wrbuf_2))
650 if (!zebra_replace_sub(zms, reg_id, ex_list, wrbuf_buf(zms->wrbuf_2),
651 wrbuf_len(zms->wrbuf_2), zms->wrbuf_1))
657 int zebra_replace_sub(ZebraMaps zms, unsigned reg_id, const char *ex_list,
658 const char *input_str, int input_len, WRBUF wrbuf)
662 struct zebra_map *zm = zebra_map_get (zms, reg_id);
665 for (i = -1; i <= input_len; )
667 struct zm_token *token;
668 char replace_string[128];
672 for (token = zm->replace_tokens; !replace_in && token;
676 int replace_done = 0;
681 if (!token->token_from[j])
686 if (ex_list && strchr (ex_list, token->token_from[j]))
688 if (i+j < 0 || j+i >= input_len)
691 c = input_str[j+i] & 255;
692 if (token->token_from[j] == ZEBRA_REPLACE_ANY)
696 replace_string[replace_out++] = c;
700 if (c != token->token_from[j])
706 const char *cp = token->token_to;
708 for (; cp && *cp; cp++)
709 replace_string[replace_out++] = *cp;
716 if (i >= 0 && i < input_len)
717 wrbuf_putc(wrbuf, input_str[i]);
724 wrbuf_write(wrbuf, replace_string, replace_out);
729 logf (LOG_LOG, "out:%.*s:", wrbuf_len(wrbuf), wrbuf_buf(wrbuf));