2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.18 1999-10-15 08:27:46 adam
8 * Fixed replace handler. 8-bit fix.
10 * Revision 1.17 1999/09/08 12:13:21 adam
11 * Fixed minor bug "replace"-mappings. Removed some logging messages.
13 * Revision 1.16 1999/09/07 07:19:21 adam
14 * Work on character mapping. Implemented replace rules.
16 * Revision 1.15 1999/05/26 07:49:14 adam
19 * Revision 1.14 1999/02/19 10:37:40 adam
22 * Revision 1.13 1999/02/18 15:01:04 adam
23 * Structure=key uses register type 0.
25 * Revision 1.12 1999/02/12 13:29:25 adam
26 * Implemented position-flag for registers.
28 * Revision 1.11 1998/10/13 20:09:19 adam
29 * Changed call to readconf_line.
31 * Revision 1.10 1998/06/23 15:33:37 adam
32 * Added feature to specify sort criteria in query (type 7 specifies
35 * Revision 1.9 1998/04/02 14:35:30 adam
36 * First version of Zebra that works with compiled ASN.1.
38 * Revision 1.8 1998/03/05 08:42:44 adam
39 * Minor changes to zebramap data structures. Query mapping rules changed.
41 * Revision 1.7 1998/02/10 12:03:07 adam
44 * Revision 1.6 1998/01/29 13:36:01 adam
45 * Structure word-list, free-form-text and document-text all
46 * trigger ranked search.
48 * Revision 1.5 1997/11/19 10:22:14 adam
49 * Bug fix (introduced by previous commit).
51 * Revision 1.4 1997/11/18 10:05:08 adam
52 * Changed character map facility so that admin can specify character
53 * mapping files for each register type, w, p, etc.
55 * Revision 1.3 1997/11/17 15:35:26 adam
56 * Bug fix. Relation=relevance wasn't observed.
58 * Revision 1.2 1997/10/31 12:39:30 adam
59 * Changed log message.
61 * Revision 1.1 1997/10/27 14:33:06 adam
62 * Moved towards generic character mapping depending on "structure"
63 * field in abstract syntax file. Fixed a few memory leaks. Fixed
64 * bug with negative integers when doing searches with relational
76 #define ZEBRA_MAP_TYPE_SORT 1
77 #define ZEBRA_MAP_TYPE_INDEX 2
79 #define ZEBRA_REPLACE_ANY 300
85 struct zm_token *next;
102 const char *maptab_name;
103 struct zebra_map *next;
104 struct zm_token *replace_tokens;
110 struct zebra_map *map_list;
111 char temp_map_str[2];
112 const char *temp_map_ptr[2];
113 struct zebra_map **lookup_array;
114 WRBUF wrbuf_1, wrbuf_2;
117 void zebra_maps_close (ZebraMaps zms)
119 struct zebra_map *zm = zms->map_list;
123 chrmaptab_destroy (zm->maptab);
126 wrbuf_free (zms->wrbuf_1, 1);
127 wrbuf_free (zms->wrbuf_2, 1);
128 nmem_destroy (zms->nmem);
132 static void zebra_map_read (ZebraMaps zms, const char *name)
139 struct zebra_map **zm = 0, *zp;
141 if (!(f = yaz_path_fopen(zms->tabpath, name, "r")))
143 logf(LOG_WARN|LOG_ERRNO, "%s", name);
146 while ((argc = readconf_line(f, &lineno, line, 512, argv, 10)))
148 if (!yaz_matchstr (argv[0], "index") && argc == 2)
154 *zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(**zm));
155 (*zm)->reg_id = argv[1][0];
156 (*zm)->maptab_name = NULL;
157 (*zm)->maptab = NULL;
158 (*zm)->type = ZEBRA_MAP_TYPE_INDEX;
159 (*zm)->completeness = 0;
160 (*zm)->positioned = 1;
161 (*zm)->replace_tokens = 0;
163 else if (!yaz_matchstr (argv[0], "sort") && argc == 2)
169 *zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(**zm));
170 (*zm)->reg_id = argv[1][0];
171 (*zm)->maptab_name = NULL;
172 (*zm)->type = ZEBRA_MAP_TYPE_SORT;
173 (*zm)->u.sort.entry_size = 80;
174 (*zm)->maptab = NULL;
175 (*zm)->completeness = 0;
176 (*zm)->positioned = 0;
177 (*zm)->replace_tokens = 0;
179 else if (zm && !yaz_matchstr (argv[0], "charmap") && argc == 2)
181 (*zm)->maptab_name = nmem_strdup (zms->nmem, argv[1]);
183 else if (zm && !yaz_matchstr (argv[0], "completeness") && argc == 2)
185 (*zm)->completeness = atoi (argv[1]);
187 else if (zm && !yaz_matchstr (argv[0], "position") && argc == 2)
189 (*zm)->positioned = atoi (argv[1]);
191 else if (zm && !yaz_matchstr (argv[0], "entrysize") && argc == 2)
193 if ((*zm)->type == ZEBRA_MAP_TYPE_SORT)
194 (*zm)->u.sort.entry_size = atoi (argv[1]);
196 else if (zm && !yaz_matchstr (argv[0], "replace") && argc >= 2)
198 struct zm_token *token = nmem_malloc (zms->nmem, sizeof(*token));
199 token->next = (*zm)->replace_tokens;
200 (*zm)->replace_tokens = token;
202 logf (LOG_LOG, "replace %s", argv[1]);
204 token->token_from = 0;
208 int *dp = token->token_from = (int *)
209 nmem_malloc (zms->nmem, (1+strlen(cp))*sizeof(int));
218 *dp++ = ZEBRA_REPLACE_ANY;
223 *dp++ = zebra_prim(&cp);
225 logf (LOG_LOG, " char %2X %c", dp[-1], dp[-1]);
233 char *dp = token->token_to =
234 nmem_malloc (zms->nmem, strlen(cp)+1);
242 *dp++ = zebra_prim(&cp);
253 for (zp = zms->map_list; zp; zp = zp->next)
254 zms->lookup_array[zp->reg_id] = zp;
257 static void zms_map_handle (void *p, const char *name, const char *value)
259 ZebraMaps zms = (ZebraMaps) p;
261 zebra_map_read (zms, value);
264 ZebraMaps zebra_maps_open (Res res)
266 ZebraMaps zms = (ZebraMaps) xmalloc (sizeof(*zms));
269 zms->nmem = nmem_create ();
270 zms->tabpath = nmem_strdup (zms->nmem,
271 res_get_def (res, "profilePath", "."));
272 zms->map_list = NULL;
274 zms->temp_map_str[0] = '\0';
275 zms->temp_map_str[1] = '\0';
277 zms->temp_map_ptr[0] = zms->temp_map_str;
278 zms->temp_map_ptr[1] = NULL;
280 zms->lookup_array = (struct zebra_map**)
281 nmem_malloc (zms->nmem, sizeof(*zms->lookup_array)*256);
282 for (i = 0; i<256; i++)
283 zms->lookup_array[i] = 0;
284 if (!res || !res_trav (res, "index", zms, zms_map_handle))
285 zebra_map_read (zms, "default.idx");
287 zms->wrbuf_1 = wrbuf_alloc();
288 zms->wrbuf_2 = wrbuf_alloc();
292 struct zebra_map *zebra_map_get (ZebraMaps zms, unsigned reg_id)
294 return zms->lookup_array[reg_id];
297 chrmaptab zebra_charmap_get (ZebraMaps zms, unsigned reg_id)
299 struct zebra_map *zm = zebra_map_get (zms, reg_id);
302 zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(*zm));
303 logf (LOG_WARN, "Unknown register type: %c", reg_id);
306 zm->maptab_name = NULL;
308 zm->type = ZEBRA_MAP_TYPE_INDEX;
309 zm->completeness = 0;
310 zm->next = zms->map_list;
311 zms->map_list = zm->next;
313 zms->lookup_array[zm->reg_id & 255] = zm;
317 if (!zm->maptab_name || !yaz_matchstr (zm->maptab_name, "@"))
319 if (!(zm->maptab = chrmaptab_create (zms->tabpath,
320 zm->maptab_name, 0)))
321 logf(LOG_WARN, "Failed to read character table %s",
324 logf(LOG_DEBUG, "Read character table %s", zm->maptab_name);
329 const char **zebra_maps_input (ZebraMaps zms, unsigned reg_id,
330 const char **from, int len)
334 maptab = zebra_charmap_get (zms, reg_id);
336 return chr_map_input(maptab, from, len);
338 zms->temp_map_str[0] = **from;
341 return zms->temp_map_ptr;
345 int zebra_maps_input_tokens (ZebraMaps zms, unsigned reg_id,
346 const char *input_str, int input_len,
349 chrmaptab maptab = zebra_charmap_get (zms, reg_id);
358 wrbuf_write (wrbuf, input_str, input_len);
369 out = chr_map_input (maptab, str, len);
372 while (out && *out && **out == *CHR_SPACE)
373 out = chr_map_input (maptab, str, len);
378 const char *zebra_maps_output(ZebraMaps zms, unsigned reg_id,
381 chrmaptab maptab = zebra_charmap_get (zms, reg_id);
384 return chr_map_output (maptab, from, 1);
388 /* ------------------------------------ */
394 Z_AttributeElement **attributeList;
398 static int attr_find (AttrType *src, oid_value *attributeSetP)
400 while (src->major < src->num_attributes)
402 Z_AttributeElement *element;
404 element = src->attributeList[src->major];
405 if (src->type == *element->attributeType)
407 switch (element->which)
409 case Z_AttributeValue_numeric:
411 if (element->attributeSet && attributeSetP)
415 attrset = oid_getentbyoid (element->attributeSet);
416 *attributeSetP = attrset->value;
418 return *element->value.numeric;
420 case Z_AttributeValue_complex:
421 if (src->minor >= element->value.complex->num_list ||
422 element->value.complex->list[src->minor]->which !=
423 Z_StringOrNumeric_numeric)
426 if (element->attributeSet && attributeSetP)
430 attrset = oid_getentbyoid (element->attributeSet);
431 *attributeSetP = attrset->value;
433 return *element->value.complex->list[src->minor-1]->u.numeric;
443 static void attr_init_APT (AttrType *src, Z_AttributesPlusTerm *zapt, int type)
446 src->attributeList = zapt->attributes->attributes;
447 src->num_attributes = zapt->attributes->num_attributes;
449 src->attributeList = zapt->attributeList;
450 src->num_attributes = zapt->num_attributes;
457 static void attr_init_AttrList (AttrType *src, Z_AttributeList *list, int type)
459 src->attributeList = list->attributes;
460 src->num_attributes = list->num_attributes;
466 /* ------------------------------------ */
468 int zebra_maps_is_complete (ZebraMaps zms, unsigned reg_id)
470 struct zebra_map *zm = zebra_map_get (zms, reg_id);
472 return zm->completeness;
476 int zebra_maps_is_positioned (ZebraMaps zms, unsigned reg_id)
478 struct zebra_map *zm = zebra_map_get (zms, reg_id);
480 return zm->positioned;
484 int zebra_maps_is_sort (ZebraMaps zms, unsigned reg_id)
486 struct zebra_map *zm = zebra_map_get (zms, reg_id);
488 return zm->type == ZEBRA_MAP_TYPE_SORT;
492 int zebra_maps_sort (ZebraMaps zms, Z_SortAttributes *sortAttributes)
495 attr_init_AttrList (&use, sortAttributes->list, 1);
497 return attr_find (&use, NULL);
500 int zebra_maps_attr (ZebraMaps zms, Z_AttributesPlusTerm *zapt,
501 unsigned *reg_id, char **search_type, char **rank_type,
502 int *complete_flag, int *sort_flag)
504 AttrType completeness;
507 AttrType sort_relation;
508 int completeness_value;
511 int sort_relation_value;
513 attr_init_APT (&structure, zapt, 4);
514 attr_init_APT (&completeness, zapt, 6);
515 attr_init_APT (&relation, zapt, 2);
516 attr_init_APT (&sort_relation, zapt, 7);
518 completeness_value = attr_find (&completeness, NULL);
519 structure_value = attr_find (&structure, NULL);
520 relation_value = attr_find (&relation, NULL);
521 sort_relation_value = attr_find (&sort_relation, NULL);
523 if (completeness_value == 2 || completeness_value == 3)
529 *sort_flag = (sort_relation_value > 0) ? 1 : 0;
530 *search_type = "phrase";
532 if (relation_value == 102)
539 switch (structure_value)
541 case 6: /* word list */
542 *search_type = "and-list";
544 case 105: /* free-form-text */
545 *search_type = "or-list";
547 case 106: /* document-text */
548 *search_type = "or-list";
553 case 108: /* string */
554 *search_type = "phrase";
556 case 107: /* local-number */
557 *search_type = "local";
560 case 109: /* numeric string */
562 *search_type = "numeric";
566 *search_type = "phrase";
570 *search_type = "phrase";
578 int zebra_replace_sub(ZebraMaps zms, unsigned reg_id, const char *ex_list,
579 const char *input_str, int input_len, WRBUF wrbuf);
581 WRBUF zebra_replace(ZebraMaps zms, unsigned reg_id, const char *ex_list,
582 const char *input_str, int input_len)
584 struct zebra_map *zm = zebra_map_get (zms, reg_id);
586 wrbuf_rewind(zms->wrbuf_1);
587 wrbuf_write(zms->wrbuf_1, input_str, input_len);
588 if (!zm->replace_tokens)
592 logf (LOG_LOG, "in:%.*s:", wrbuf_len(zms->wrbuf_1),
593 wrbuf_buf(zms->wrbuf_1));
597 if (!zebra_replace_sub(zms, reg_id, ex_list, wrbuf_buf(zms->wrbuf_1),
598 wrbuf_len(zms->wrbuf_1), zms->wrbuf_2))
600 if (!zebra_replace_sub(zms, reg_id, ex_list, wrbuf_buf(zms->wrbuf_2),
601 wrbuf_len(zms->wrbuf_2), zms->wrbuf_1))
607 int zebra_replace_sub(ZebraMaps zms, unsigned reg_id, const char *ex_list,
608 const char *input_str, int input_len, WRBUF wrbuf)
612 struct zebra_map *zm = zebra_map_get (zms, reg_id);
615 for (i = -1; i <= input_len; )
617 struct zm_token *token;
618 char replace_string[128];
622 for (token = zm->replace_tokens; !replace_in && token;
626 int replace_done = 0;
631 if (!token->token_from[j])
636 if (ex_list && strchr (ex_list, token->token_from[j]))
638 if (i+j < 0 || j+i >= input_len)
641 c = input_str[j+i] & 255;
642 if (token->token_from[j] == ZEBRA_REPLACE_ANY)
646 replace_string[replace_out++] = c;
650 if (c != token->token_from[j])
656 const char *cp = token->token_to;
658 for (; cp && *cp; cp++)
659 replace_string[replace_out++] = *cp;
666 if (i >= 0 && i < input_len)
667 wrbuf_putc(wrbuf, input_str[i]);
674 wrbuf_write(wrbuf, replace_string, replace_out);
679 logf (LOG_LOG, "out:%.*s:", wrbuf_len(wrbuf), wrbuf_buf(wrbuf));