2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.23 1995-10-06 16:33:37 adam
8 * Use attribute mappings.
10 * Revision 1.22 1995/10/06 15:07:39 adam
11 * Structure 'local-number' handled.
13 * Revision 1.21 1995/10/06 13:52:06 adam
14 * Bug fixes. Handler may abort further scanning.
16 * Revision 1.20 1995/10/06 11:06:33 adam
17 * Scan entries include 'occurrences' now.
19 * Revision 1.19 1995/10/06 10:43:56 adam
20 * Scan added. 'occurrences' in scan entries not set yet.
22 * Revision 1.18 1995/10/04 16:57:20 adam
23 * Key input and merge sort in one pass.
25 * Revision 1.17 1995/10/04 12:55:17 adam
26 * Bug fix in ranked search. Use=Any keys inserted.
28 * Revision 1.16 1995/10/02 16:24:40 adam
29 * Use attribute actually used in search requests.
31 * Revision 1.15 1995/10/02 15:18:52 adam
32 * New member in recRetrieveCtrl: diagnostic.
34 * Revision 1.14 1995/09/28 12:10:32 adam
35 * Bug fixes. Field prefix used in queries.
37 * Revision 1.13 1995/09/18 14:17:50 adam
40 * Revision 1.12 1995/09/15 14:45:21 adam
44 * Revision 1.11 1995/09/14 11:53:27 adam
45 * First work on regular expressions/truncations.
47 * Revision 1.10 1995/09/11 15:23:26 adam
48 * More work on relevance search.
50 * Revision 1.9 1995/09/11 13:09:35 adam
51 * More work on relevance feedback.
53 * Revision 1.8 1995/09/08 14:52:27 adam
54 * Minor changes. Dictionary is lower case now.
56 * Revision 1.7 1995/09/07 13:58:36 adam
57 * New parameter: result-set file descriptor (RSFD) to support multiple
58 * positions within the same result-set.
59 * Boolean operators: and, or, not implemented.
60 * Result-set references.
62 * Revision 1.6 1995/09/06 16:11:18 adam
63 * Option: only one word key per file.
65 * Revision 1.5 1995/09/06 10:33:04 adam
66 * More work on present. Some log messages removed.
68 * Revision 1.4 1995/09/05 15:28:40 adam
69 * More work on search engine.
71 * Revision 1.3 1995/09/04 15:20:22 adam
74 * Revision 1.2 1995/09/04 12:33:43 adam
75 * Various cleanup. YAZ util used instead.
77 * Revision 1.1 1995/09/04 09:10:40 adam
78 * More work on index add/del/update.
79 * Merge sort implemented.
80 * Initial work on z39 server.
88 #include <attribute.h>
96 int index_word_prefix_map (char *string, oid_value attrSet, int attrUse)
100 logf (LOG_DEBUG, "oid_value attrSet = %d", attrSet);
101 logf (LOG_DEBUG, "int attrUse = %d", attrUse);
102 attp = att_getentbyatt (attrSet, attrUse);
104 logf (LOG_DEBUG, "ord=%d", attp->attset_ordinal);
105 return index_word_prefix (string, attp->attset_ordinal,
106 attp->local_attribute);
110 * attr_print: log attributes
112 static void attr_print (Z_AttributesPlusTerm *t)
115 for (of = 0; of < t->num_attributes; of++)
117 Z_AttributeElement *element;
118 element = t->attributeList[of];
120 switch (element->which)
122 case Z_AttributeValue_numeric:
123 logf (LOG_DEBUG, "attributeType=%d value=%d",
124 *element->attributeType,
125 *element->value.numeric);
127 case Z_AttributeValue_complex:
128 logf (LOG_DEBUG, "attributeType=%d complex",
129 *element->attributeType);
130 for (i = 0; i<element->value.complex->num_list; i++)
132 if (element->value.complex->list[i]->which ==
133 Z_StringOrNumeric_string)
134 logf (LOG_DEBUG, " string: '%s'",
135 element->value.complex->list[i]->u.string);
136 else if (element->value.complex->list[i]->which ==
137 Z_StringOrNumeric_numeric)
138 logf (LOG_DEBUG, " numeric: '%d'",
139 *element->value.complex->list[i]->u.numeric);
152 Z_AttributesPlusTerm *zapt;
155 static int attr_find (AttrType *src, oid_value *attributeSetP)
157 while (src->major < src->zapt->num_attributes)
159 Z_AttributeElement *element;
161 element = src->zapt->attributeList[src->major];
162 if (src->type == *element->attributeType)
164 switch (element->which)
166 case Z_AttributeValue_numeric:
168 if (element->attributeSet && attributeSetP)
172 attrset = oid_getentbyoid (element->attributeSet);
173 *attributeSetP = attrset->value;
175 return *element->value.numeric;
177 case Z_AttributeValue_complex:
178 if (src->minor >= element->value.complex->num_list ||
179 element->value.complex->list[src->minor]->which !=
180 Z_StringOrNumeric_numeric)
183 if (element->attributeSet && attributeSetP)
187 attrset = oid_getentbyoid (element->attributeSet);
188 *attributeSetP = attrset->value;
190 return *element->value.complex->list[src->minor-1]->u.numeric;
200 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
213 int (*cmp)(const void *p1, const void *p2);
220 static void heap_swap (struct trunc_info *ti, int i1, int i2)
224 memcpy (ti->swapbuf, ti->heap[i1], ti->keysize);
225 memcpy (ti->heap[i1], ti->heap[i2], ti->keysize);
226 memcpy (ti->heap[i2], ti->swapbuf, ti->keysize);
229 ti->indx[i1] = ti->indx[i2];
233 static void heap_delete (struct trunc_info *ti)
235 int cur = 1, child = 2;
237 assert (ti->heapnum > 0);
238 memcpy (ti->heap[1], ti->heap[ti->heapnum], ti->keysize);
239 ti->indx[1] = ti->indx[ti->heapnum--];
240 while (child <= ti->heapnum) {
241 if (child < ti->heapnum &&
242 (*ti->cmp)(ti->heap[child], ti->heap[1+child]) > 0)
244 if ((*ti->cmp)(ti->heap[cur], ti->heap[child]) > 0)
246 heap_swap (ti, cur, child);
255 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
259 cur = ++(ti->heapnum);
260 memcpy (ti->heap[cur], buf, ti->keysize);
261 ti->indx[cur] = indx;
263 while (parent && (*ti->cmp)(ti->heap[parent], ti->heap[cur]) > 0)
265 heap_swap (ti, cur, parent);
272 struct trunc_info *heap_init (int size, int key_size,
273 int (*cmp)(const void *p1, const void *p2))
275 struct trunc_info *ti = xmalloc (sizeof(*ti));
280 ti->keysize = key_size;
282 ti->indx = xmalloc (size * sizeof(*ti->indx));
283 ti->heap = xmalloc (size * sizeof(*ti->heap));
284 ti->swapbuf = xmalloc (ti->keysize);
285 ti->tmpbuf = xmalloc (ti->keysize);
286 ti->buf = xmalloc (size * ti->keysize);
287 for (i = size; --i >= 0; )
288 ti->heap[i] = ti->buf + ti->keysize * i;
292 static void heap_close (struct trunc_info *ti)
301 static RSET rset_trunc (ISAM isam, ISAM_P *isam_p, int from, int to,
304 logf (LOG_DEBUG, "rset_trunc, range=%d-%d", from, to-1);
305 if (to - from > merge_chunk)
313 struct trunc_info *ti;
316 rset_temp_parms parms;
318 ispt = xmalloc (sizeof(*ispt) * (to-from));
319 parms.key_size = sizeof (struct it_key);
320 result = rset_create (rset_kind_temp, &parms);
321 rsfd = rset_open (result, 1);
323 ti = heap_init (to-from, sizeof(struct it_key),
325 for (i = to-from; --i >= 0; )
327 ispt[i] = is_position (isam, isam_p[from+i]);
328 if (is_readkey (ispt[i], ti->tmpbuf))
329 heap_insert (ti, ti->tmpbuf, i);
335 rset_write (result, rsfd, ti->heap[1]);
337 if (is_readkey (ispt[n], ti->tmpbuf))
338 heap_insert (ti, ti->tmpbuf, n);
340 for (i = to-from; --i >= 0; )
341 is_pt_free (ispt[i]);
342 rset_close (result, rsfd);
349 static ISAM_P *isam_p_buf = NULL;
350 static int isam_p_size = 0;
351 static int isam_p_indx;
353 static void add_isam_p (const char *info)
355 if (isam_p_indx == isam_p_size)
357 ISAM_P *new_isam_p_buf;
359 isam_p_size = 2*isam_p_size + 100;
360 new_isam_p_buf = xmalloc (sizeof(*new_isam_p_buf) *
364 memcpy (new_isam_p_buf, isam_p_buf,
365 isam_p_indx * sizeof(*isam_p_buf));
368 isam_p_buf = new_isam_p_buf;
370 assert (*info == sizeof(*isam_p_buf));
371 memcpy (isam_p_buf + isam_p_indx, info+1, sizeof(*isam_p_buf));
375 static int grep_handle (Dict_char *name, const char *info)
377 logf (LOG_DEBUG, "dict name: %s", name);
382 static int trunc_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
383 const char *term_sub, ISAM_P **isam_ps,
384 oid_value attributeSet)
386 char term_dict[2*IT_MAX_WORD+2];
390 int truncation_value;
393 oid_value curAttributeSet = attributeSet;
395 attr_init (&use, zapt, 1);
396 use_value = attr_find (&use, &curAttributeSet);
397 logf (LOG_DEBUG, "use value %d", use_value);
398 attr_init (&truncation, zapt, 5);
399 truncation_value = attr_find (&truncation, NULL);
400 logf (LOG_DEBUG, "truncation value %d", truncation_value);
404 i = index_word_prefix_map (term_dict, curAttributeSet, use_value);
406 switch (truncation_value)
408 case -1: /* not specified */
409 case 100: /* do not truncate */
410 strcat (term_dict, term_sub);
411 logf (LOG_DEBUG, "dict_lookup: %s", term_dict);
412 if ((info = dict_lookup (zi->wordDict, term_dict)))
415 case 1: /* right truncation */
416 strcat (term_dict, term_sub);
417 strcat (term_dict, ".*");
418 dict_lookup_grep (zi->wordDict, term_dict, 0, grep_handle);
420 case 2: /* left truncation */
421 case 3: /* left&right truncation */
424 case 101: /* process # in term */
425 for (j = strlen(term_dict), i = 0; term_sub[i] && i < 2; i++)
426 term_dict[j++] = term_sub[i];
427 for (; term_sub[i]; i++)
428 if (term_sub[i] == '#')
430 term_dict[j++] = '.';
431 term_dict[j++] = '*';
434 term_dict[j++] = term_sub[i];
436 dict_lookup_grep (zi->wordDict, term_dict, 0, grep_handle);
438 case 102: /* regular expression */
439 strcat (term_dict, term_sub);
440 dict_lookup_grep (zi->wordDict, term_dict, 0, grep_handle);
443 *isam_ps = isam_p_buf;
444 logf (LOG_DEBUG, "%d positions", isam_p_indx);
448 static void trans_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
452 Z_Term *term = zapt->term;
454 sizez = term->u.general->len;
455 if (sizez > IT_MAX_WORD)
457 for (i = 0; i < sizez; i++)
458 termz[i] = index_char_cvt (term->u.general->buf[i]);
462 static RSET rpn_search_APT_relevance (ZServerInfo *zi,
463 Z_AttributesPlusTerm *zapt,
464 oid_value attributeSet)
466 rset_relevance_parms parms;
467 char termz[IT_MAX_WORD+1];
468 char term_sub[IT_MAX_WORD+1];
469 char *p0 = termz, *p1 = NULL;
471 parms.key_size = sizeof(struct it_key);
473 parms.cmp = key_compare;
474 parms.is = zi->wordIsam;
476 if (zapt->term->which != Z_Term_general)
481 trans_term (zi, zapt, termz);
482 isam_p_indx = 0; /* global, set by trunc_term - see below */
485 if ((p1 = strchr (p0, ' ')))
487 memcpy (term_sub, p0, p1-p0);
488 term_sub[p1-p0] = '\0';
491 strcpy (term_sub, p0);
492 if (trunc_term (zi, zapt, term_sub, &parms.isam_positions,
499 parms.no_isam_positions = isam_p_indx;
501 return rset_create (rset_kind_relevance, &parms);
503 return rset_create (rset_kind_null, NULL);
506 static RSET rpn_search_APT_word (ZServerInfo *zi,
507 Z_AttributesPlusTerm *zapt,
508 oid_value attributeSet)
510 ISAM_P *isam_positions;
511 rset_isam_parms parms;
512 char termz[IT_MAX_WORD+1];
514 if (zapt->term->which != Z_Term_general)
519 trans_term (zi, zapt, termz);
520 isam_p_indx = 0; /* global, set by trunc_term - see below */
521 if (trunc_term (zi, zapt, termz, &isam_positions, attributeSet))
524 return rset_create (rset_kind_null, NULL);
525 else if (isam_p_indx == 1)
527 parms.is = zi->wordIsam;
528 parms.pos = *isam_positions;
529 return rset_create (rset_kind_isam, &parms);
532 return rset_trunc (zi->wordIsam, isam_positions, 0, isam_p_indx, 400);
535 static RSET rpn_search_APT_phrase (ZServerInfo *zi,
536 Z_AttributesPlusTerm *zapt,
537 oid_value attributeSet)
539 ISAM_P *isam_positions;
540 rset_isam_parms parms;
541 char termz[IT_MAX_WORD+1];
543 if (zapt->term->which != Z_Term_general)
548 trans_term (zi, zapt, termz);
549 isam_p_indx = 0; /* global, set by trunc_term - see below */
550 if (trunc_term (zi, zapt, termz, &isam_positions, attributeSet))
552 if (isam_p_indx != 1)
553 return rset_create (rset_kind_null, NULL);
554 parms.is = zi->wordIsam;
555 parms.pos = *isam_positions;
556 return rset_create (rset_kind_isam, &parms);
559 static RSET rpn_search_APT_local (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
560 oid_value attributeSet)
565 rset_temp_parms parms;
566 char termz[IT_MAX_WORD+1];
568 if (zapt->term->which != Z_Term_general)
573 parms.key_size = sizeof (struct it_key);
574 result = rset_create (rset_kind_temp, &parms);
575 rsfd = rset_open (result, 1);
577 trans_term (zi, zapt, termz);
578 key.sysno = atoi (termz);
581 rset_write (result, rsfd, &key);
582 rset_close (result, rsfd);
587 static RSET rpn_search_APT (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
588 oid_value attributeSet)
592 int relation_value, structure_value;
594 attr_init (&relation, zapt, 2);
595 attr_init (&structure, zapt, 4);
597 relation_value = attr_find (&relation, NULL);
598 structure_value = attr_find (&structure, NULL);
599 switch (structure_value)
602 if (relation_value == 102) /* relevance relation */
603 return rpn_search_APT_relevance (zi, zapt, attributeSet);
604 return rpn_search_APT_word (zi, zapt, attributeSet);
606 if (relation_value == 102) /* relevance relation */
607 return rpn_search_APT_relevance (zi, zapt, attributeSet);
608 return rpn_search_APT_phrase (zi, zapt, attributeSet);
611 if (relation_value == 102) /* relevance relation */
612 return rpn_search_APT_relevance (zi, zapt, attributeSet);
613 return rpn_search_APT_word (zi, zapt, attributeSet);
618 case 5: /* date - normalized */
620 case 6: /* word list */
621 return rpn_search_APT_relevance (zi, zapt, attributeSet);
622 case 100: /* date - un-normalized */
624 case 101: /* name - normalized */
626 case 102: /* date - un-normalized */
628 case 103: /* structure */
632 case 105: /* free-form-text */
633 return rpn_search_APT_relevance (zi, zapt, attributeSet);
634 case 106: /* document-text */
635 return rpn_search_APT_relevance (zi, zapt, attributeSet);
636 case 107: /* local-number */
637 return rpn_search_APT_local (zi, zapt, attributeSet);
638 case 108: /* string */
639 return rpn_search_APT_word (zi, zapt, attributeSet);
640 case 109: /* numeric string */
647 static RSET rpn_search_ref (ZServerInfo *zi, Z_ResultSetId *resultSetId)
651 if (!(s = resultSetGet (zi, resultSetId)))
652 return rset_create (rset_kind_null, NULL);
656 static RSET rpn_search_structure (ZServerInfo *zi, Z_RPNStructure *zs,
657 oid_value attributeSet)
660 if (zs->which == Z_RPNStructure_complex)
662 rset_bool_parms bool_parms;
664 bool_parms.rset_l = rpn_search_structure (zi, zs->u.complex->s1,
666 if (bool_parms.rset_l == NULL)
668 bool_parms.rset_r = rpn_search_structure (zi, zs->u.complex->s2,
670 if (bool_parms.rset_r == NULL)
672 rset_delete (bool_parms.rset_l);
675 bool_parms.key_size = sizeof(struct it_key);
676 bool_parms.cmp = key_compare;
678 switch (zs->u.complex->operator->which)
681 r = rset_create (rset_kind_and, &bool_parms);
684 r = rset_create (rset_kind_or, &bool_parms);
686 case Z_Operator_and_not:
687 r = rset_create (rset_kind_not, &bool_parms);
693 else if (zs->which == Z_RPNStructure_simple)
695 if (zs->u.simple->which == Z_Operand_APT)
697 logf (LOG_DEBUG, "rpn_search_APT");
698 r = rpn_search_APT (zi, zs->u.simple->u.attributesPlusTerm,
701 else if (zs->u.simple->which == Z_Operand_resultSetId)
703 logf (LOG_DEBUG, "rpn_search_ref");
704 r = rpn_search_ref (zi, zs->u.simple->u.resultSetId);
718 static void count_set (RSET r, int *count)
724 logf (LOG_DEBUG, "rpn_save_set");
726 rfd = rset_open (r, 0);
727 while (rset_read (r, rfd, &key))
729 if (key.sysno != psysno)
736 logf (LOG_DEBUG, "%d distinct sysnos", *count);
739 int rpn_search (ZServerInfo *zi,
740 Z_RPNQuery *rpn, int num_bases, char **basenames,
741 const char *setname, int *hits)
745 oid_value attributeSet;
748 zi->errString = NULL;
750 attrset = oid_getentbyoid (rpn->attributeSetId);
751 attributeSet = attrset->value;
753 rset = rpn_search_structure (zi, rpn->RPNStructure, attributeSet);
756 count_set (rset, hits);
757 resultSetAdd (zi, setname, 1, rset);
759 logf (LOG_DEBUG, "search error: %d", zi->errCode);
763 static struct scan_entry *scan_list;
765 static int scan_before, scan_after;
766 static ISAM scan_isam;
767 static char scan_prefix[20];
769 static int scan_handle (Dict_char *name, const char *info, int pos)
775 rset_isam_parms parms;
777 len_prefix = strlen(scan_prefix);
778 if (memcmp (name, scan_prefix, len_prefix))
781 idx = scan_after - pos + scan_before;
784 scan_list[idx].term = odr_malloc (scan_odr, strlen(name + len_prefix)+1);
785 strcpy (scan_list[idx].term, name + len_prefix);
786 assert (*info == sizeof(isam_p));
787 memcpy (&isam_p, info+1, sizeof(isam_p));
788 parms.is = scan_isam;
791 rset = rset_create (rset_kind_isam, &parms);
792 count_set (rset, &scan_list[idx].occurrences);
795 scan_list[idx].occurrences = 1;
797 logf (LOG_DEBUG, "pos=%3d idx=%3d name=%s", pos, idx, name);
801 int rpn_scan (ZServerInfo *zi, ODR odr, Z_AttributesPlusTerm *zapt,
802 int *position, int *num_entries, struct scan_entry **list)
806 int num = *num_entries;
809 char termz[IT_MAX_WORD+20];
812 Z_Term *term = zapt->term;
814 logf (LOG_DEBUG, "scan, position = %d, num = %d", pos, num);
815 scan_before = before = pos-1;
816 scan_after = after = 1+num-pos;
819 logf (LOG_DEBUG, "scan, before = %d, after = %d", before, after);
821 scan_isam = zi->wordIsam;
822 scan_list = *list = odr_malloc (odr, (before+after)*sizeof(**list));
823 for (j = 0; j<before+after; j++)
824 scan_list[j].term = "------";
825 attr_init (&use, zapt, 1);
826 use_value = attr_find (&use, NULL);
827 logf (LOG_DEBUG, "use value %d", use_value);
831 i = index_word_prefix (termz, 1, use_value);
832 strcpy (scan_prefix, termz);
833 sizez = term->u.general->len;
834 if (sizez > IT_MAX_WORD)
836 for (j = 0; j<sizez; j++)
837 termz[j+i] = index_char_cvt (term->u.general->buf[j]);
840 dict_scan (zi->wordDict, termz, &before, &after, scan_handle);
843 logf (LOG_DEBUG, "scan error: %d", zi->errCode);