2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.22 1995-10-06 15:07:39 adam
8 * Structure 'local-number' handled.
10 * Revision 1.21 1995/10/06 13:52:06 adam
11 * Bug fixes. Handler may abort further scanning.
13 * Revision 1.20 1995/10/06 11:06:33 adam
14 * Scan entries include 'occurrences' now.
16 * Revision 1.19 1995/10/06 10:43:56 adam
17 * Scan added. 'occurrences' in scan entries not set yet.
19 * Revision 1.18 1995/10/04 16:57:20 adam
20 * Key input and merge sort in one pass.
22 * Revision 1.17 1995/10/04 12:55:17 adam
23 * Bug fix in ranked search. Use=Any keys inserted.
25 * Revision 1.16 1995/10/02 16:24:40 adam
26 * Use attribute actually used in search requests.
28 * Revision 1.15 1995/10/02 15:18:52 adam
29 * New member in recRetrieveCtrl: diagnostic.
31 * Revision 1.14 1995/09/28 12:10:32 adam
32 * Bug fixes. Field prefix used in queries.
34 * Revision 1.13 1995/09/18 14:17:50 adam
37 * Revision 1.12 1995/09/15 14:45:21 adam
41 * Revision 1.11 1995/09/14 11:53:27 adam
42 * First work on regular expressions/truncations.
44 * Revision 1.10 1995/09/11 15:23:26 adam
45 * More work on relevance search.
47 * Revision 1.9 1995/09/11 13:09:35 adam
48 * More work on relevance feedback.
50 * Revision 1.8 1995/09/08 14:52:27 adam
51 * Minor changes. Dictionary is lower case now.
53 * Revision 1.7 1995/09/07 13:58:36 adam
54 * New parameter: result-set file descriptor (RSFD) to support multiple
55 * positions within the same result-set.
56 * Boolean operators: and, or, not implemented.
57 * Result-set references.
59 * Revision 1.6 1995/09/06 16:11:18 adam
60 * Option: only one word key per file.
62 * Revision 1.5 1995/09/06 10:33:04 adam
63 * More work on present. Some log messages removed.
65 * Revision 1.4 1995/09/05 15:28:40 adam
66 * More work on search engine.
68 * Revision 1.3 1995/09/04 15:20:22 adam
71 * Revision 1.2 1995/09/04 12:33:43 adam
72 * Various cleanup. YAZ util used instead.
74 * Revision 1.1 1995/09/04 09:10:40 adam
75 * More work on index add/del/update.
76 * Merge sort implemented.
77 * Initial work on z39 server.
93 * attr_print: log attributes
95 static void attr_print (Z_AttributesPlusTerm *t)
98 for (of = 0; of < t->num_attributes; of++)
100 Z_AttributeElement *element;
101 element = t->attributeList[of];
103 switch (element->which)
105 case Z_AttributeValue_numeric:
106 logf (LOG_DEBUG, "attributeType=%d value=%d",
107 *element->attributeType,
108 *element->value.numeric);
110 case Z_AttributeValue_complex:
111 logf (LOG_DEBUG, "attributeType=%d complex",
112 *element->attributeType);
113 for (i = 0; i<element->value.complex->num_list; i++)
115 if (element->value.complex->list[i]->which ==
116 Z_StringOrNumeric_string)
117 logf (LOG_DEBUG, " string: '%s'",
118 element->value.complex->list[i]->u.string);
119 else if (element->value.complex->list[i]->which ==
120 Z_StringOrNumeric_numeric)
121 logf (LOG_DEBUG, " numeric: '%d'",
122 *element->value.complex->list[i]->u.numeric);
135 Z_AttributesPlusTerm *zapt;
138 static int attr_find (AttrType *src)
140 while (src->major < src->zapt->num_attributes)
142 Z_AttributeElement *element;
143 element = src->zapt->attributeList[src->major];
145 if (src->type == *element->attributeType)
147 switch (element->which)
149 case Z_AttributeValue_numeric:
151 return *element->value.numeric;
153 case Z_AttributeValue_complex:
154 if (src->minor >= element->value.complex->num_list ||
155 element->value.complex->list[src->minor]->which !=
156 Z_StringOrNumeric_numeric)
159 return *element->value.complex->list[src->minor-1]->u.numeric;
169 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
182 int (*cmp)(const void *p1, const void *p2);
189 static void heap_swap (struct trunc_info *ti, int i1, int i2)
193 memcpy (ti->swapbuf, ti->heap[i1], ti->keysize);
194 memcpy (ti->heap[i1], ti->heap[i2], ti->keysize);
195 memcpy (ti->heap[i2], ti->swapbuf, ti->keysize);
198 ti->indx[i1] = ti->indx[i2];
202 static void heap_delete (struct trunc_info *ti)
204 int cur = 1, child = 2;
206 assert (ti->heapnum > 0);
207 memcpy (ti->heap[1], ti->heap[ti->heapnum], ti->keysize);
208 ti->indx[1] = ti->indx[ti->heapnum--];
209 while (child <= ti->heapnum) {
210 if (child < ti->heapnum &&
211 (*ti->cmp)(ti->heap[child], ti->heap[1+child]) > 0)
213 if ((*ti->cmp)(ti->heap[cur], ti->heap[child]) > 0)
215 heap_swap (ti, cur, child);
224 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
228 cur = ++(ti->heapnum);
229 memcpy (ti->heap[cur], buf, ti->keysize);
230 ti->indx[cur] = indx;
232 while (parent && (*ti->cmp)(ti->heap[parent], ti->heap[cur]) > 0)
234 heap_swap (ti, cur, parent);
241 struct trunc_info *heap_init (int size, int key_size,
242 int (*cmp)(const void *p1, const void *p2))
244 struct trunc_info *ti = xmalloc (sizeof(*ti));
249 ti->keysize = key_size;
251 ti->indx = xmalloc (size * sizeof(*ti->indx));
252 ti->heap = xmalloc (size * sizeof(*ti->heap));
253 ti->swapbuf = xmalloc (ti->keysize);
254 ti->tmpbuf = xmalloc (ti->keysize);
255 ti->buf = xmalloc (size * ti->keysize);
256 for (i = size; --i >= 0; )
257 ti->heap[i] = ti->buf + ti->keysize * i;
261 static void heap_close (struct trunc_info *ti)
270 static RSET rset_trunc (ISAM isam, ISAM_P *isam_p, int from, int to,
273 logf (LOG_DEBUG, "rset_trunc, range=%d-%d", from, to-1);
274 if (to - from > merge_chunk)
282 struct trunc_info *ti;
285 rset_temp_parms parms;
287 ispt = xmalloc (sizeof(*ispt) * (to-from));
288 parms.key_size = sizeof (struct it_key);
289 result = rset_create (rset_kind_temp, &parms);
290 rsfd = rset_open (result, 1);
292 ti = heap_init (to-from, sizeof(struct it_key),
294 for (i = to-from; --i >= 0; )
296 ispt[i] = is_position (isam, isam_p[from+i]);
297 if (is_readkey (ispt[i], ti->tmpbuf))
298 heap_insert (ti, ti->tmpbuf, i);
304 rset_write (result, rsfd, ti->heap[1]);
306 if (is_readkey (ispt[n], ti->tmpbuf))
307 heap_insert (ti, ti->tmpbuf, n);
309 for (i = to-from; --i >= 0; )
310 is_pt_free (ispt[i]);
311 rset_close (result, rsfd);
318 static ISAM_P *isam_p_buf = NULL;
319 static int isam_p_size = 0;
320 static int isam_p_indx;
322 static void add_isam_p (const char *info)
324 if (isam_p_indx == isam_p_size)
326 ISAM_P *new_isam_p_buf;
328 isam_p_size = 2*isam_p_size + 100;
329 new_isam_p_buf = xmalloc (sizeof(*new_isam_p_buf) *
333 memcpy (new_isam_p_buf, isam_p_buf,
334 isam_p_indx * sizeof(*isam_p_buf));
337 isam_p_buf = new_isam_p_buf;
339 assert (*info == sizeof(*isam_p_buf));
340 memcpy (isam_p_buf + isam_p_indx, info+1, sizeof(*isam_p_buf));
344 static int grep_handle (Dict_char *name, const char *info)
346 logf (LOG_DEBUG, "dict name: %s", name);
351 static int trunc_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
352 const char *term_sub, ISAM_P **isam_ps)
354 char term_dict[2*IT_MAX_WORD+2];
358 int truncation_value;
362 attr_init (&use, zapt, 1);
363 use_value = attr_find (&use);
364 logf (LOG_DEBUG, "use value %d", use_value);
365 attr_init (&truncation, zapt, 5);
366 truncation_value = attr_find (&truncation);
367 logf (LOG_DEBUG, "truncation value %d", truncation_value);
371 i = index_word_prefix (term_dict, 1, use_value);
373 switch (truncation_value)
375 case -1: /* not specified */
376 case 100: /* do not truncate */
377 strcat (term_dict, term_sub);
378 logf (LOG_DEBUG, "dict_lookup: %s", term_dict);
379 if ((info = dict_lookup (zi->wordDict, term_dict)))
382 case 1: /* right truncation */
383 strcat (term_dict, term_sub);
384 strcat (term_dict, ".*");
385 dict_lookup_grep (zi->wordDict, term_dict, 0, grep_handle);
387 case 2: /* left truncation */
388 case 3: /* left&right truncation */
391 case 101: /* process # in term */
392 for (j = strlen(term_dict), i = 0; term_sub[i] && i < 2; i++)
393 term_dict[j++] = term_sub[i];
394 for (; term_sub[i]; i++)
395 if (term_sub[i] == '#')
397 term_dict[j++] = '.';
398 term_dict[j++] = '*';
401 term_dict[j++] = term_sub[i];
403 dict_lookup_grep (zi->wordDict, term_dict, 0, grep_handle);
405 case 102: /* regular expression */
406 strcat (term_dict, term_sub);
407 dict_lookup_grep (zi->wordDict, term_dict, 0, grep_handle);
410 *isam_ps = isam_p_buf;
411 logf (LOG_DEBUG, "%d positions", isam_p_indx);
415 static void trans_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
419 Z_Term *term = zapt->term;
421 sizez = term->u.general->len;
422 if (sizez > IT_MAX_WORD)
424 for (i = 0; i < sizez; i++)
425 termz[i] = index_char_cvt (term->u.general->buf[i]);
429 static RSET rpn_search_APT_relevance (ZServerInfo *zi,
430 Z_AttributesPlusTerm *zapt)
432 rset_relevance_parms parms;
433 char termz[IT_MAX_WORD+1];
434 char term_sub[IT_MAX_WORD+1];
435 char *p0 = termz, *p1 = NULL;
436 Z_Term *term = zapt->term;
438 parms.key_size = sizeof(struct it_key);
440 parms.cmp = key_compare;
441 parms.is = zi->wordIsam;
443 if (term->which != Z_Term_general)
448 trans_term (zi, zapt, termz);
449 isam_p_indx = 0; /* global, set by trunc_term - see below */
452 if ((p1 = strchr (p0, ' ')))
454 memcpy (term_sub, p0, p1-p0);
455 term_sub[p1-p0] = '\0';
458 strcpy (term_sub, p0);
459 if (trunc_term (zi, zapt, term_sub, &parms.isam_positions))
465 parms.no_isam_positions = isam_p_indx;
467 return rset_create (rset_kind_relevance, &parms);
469 return rset_create (rset_kind_null, NULL);
472 static RSET rpn_search_APT_word (ZServerInfo *zi,
473 Z_AttributesPlusTerm *zapt)
475 ISAM_P *isam_positions;
476 rset_isam_parms parms;
478 char termz[IT_MAX_WORD+1];
479 Z_Term *term = zapt->term;
481 if (term->which != Z_Term_general)
486 trans_term (zi, zapt, termz);
487 isam_p_indx = 0; /* global, set by trunc_term - see below */
488 if (trunc_term (zi, zapt, termz, &isam_positions))
491 return rset_create (rset_kind_null, NULL);
492 else if (isam_p_indx == 1)
494 parms.is = zi->wordIsam;
495 parms.pos = *isam_positions;
496 return rset_create (rset_kind_isam, &parms);
499 return rset_trunc (zi->wordIsam, isam_positions, 0, isam_p_indx, 400);
502 static RSET rpn_search_APT_phrase (ZServerInfo *zi,
503 Z_AttributesPlusTerm *zapt)
505 ISAM_P *isam_positions;
506 rset_isam_parms parms;
508 char termz[IT_MAX_WORD+1];
509 Z_Term *term = zapt->term;
511 if (term->which != Z_Term_general)
516 trans_term (zi, zapt, termz);
517 isam_p_indx = 0; /* global, set by trunc_term - see below */
518 if (trunc_term (zi, zapt, termz, &isam_positions))
520 if (isam_p_indx != 1)
521 return rset_create (rset_kind_null, NULL);
522 parms.is = zi->wordIsam;
523 parms.pos = *isam_positions;
524 return rset_create (rset_kind_isam, &parms);
527 static RSET rpn_search_APT_local (ZServerInfo *zi, Z_AttributesPlusTerm *zapt)
532 rset_temp_parms parms;
533 char termz[IT_MAX_WORD+1];
535 if (zapt->term->which != Z_Term_general)
540 parms.key_size = sizeof (struct it_key);
541 result = rset_create (rset_kind_temp, &parms);
542 rsfd = rset_open (result, 1);
544 trans_term (zi, zapt, termz);
545 key.sysno = atoi (termz);
548 rset_write (result, rsfd, &key);
549 rset_close (result, rsfd);
554 static RSET rpn_search_APT (ZServerInfo *zi, Z_AttributesPlusTerm *zapt)
558 int relation_value, structure_value;
560 attr_init (&relation, zapt, 2);
561 attr_init (&structure, zapt, 4);
563 relation_value = attr_find (&relation);
564 structure_value = attr_find (&structure);
565 switch (structure_value)
568 if (relation_value == 102) /* relevance relation */
569 return rpn_search_APT_relevance (zi, zapt);
570 return rpn_search_APT_word (zi, zapt);
572 if (relation_value == 102) /* relevance relation */
573 return rpn_search_APT_relevance (zi, zapt);
574 return rpn_search_APT_phrase (zi, zapt);
577 if (relation_value == 102) /* relevance relation */
578 return rpn_search_APT_relevance (zi, zapt);
579 return rpn_search_APT_word (zi, zapt);
584 case 5: /* date - normalized */
586 case 6: /* word list */
587 return rpn_search_APT_relevance (zi, zapt);
588 case 100: /* date - un-normalized */
590 case 101: /* name - normalized */
592 case 102: /* date - un-normalized */
594 case 103: /* structure */
598 case 105: /* free-form-text */
599 return rpn_search_APT_relevance (zi, zapt);
600 case 106: /* document-text */
601 return rpn_search_APT_relevance (zi, zapt);
602 case 107: /* local-number */
603 return rpn_search_APT_local (zi, zapt);
604 case 108: /* string */
605 return rpn_search_APT_word (zi, zapt);
606 case 109: /* numeric string */
613 static RSET rpn_search_ref (ZServerInfo *zi, Z_ResultSetId *resultSetId)
617 if (!(s = resultSetGet (zi, resultSetId)))
618 return rset_create (rset_kind_null, NULL);
622 static RSET rpn_search_structure (ZServerInfo *zi, Z_RPNStructure *zs)
625 if (zs->which == Z_RPNStructure_complex)
627 rset_bool_parms bool_parms;
629 bool_parms.rset_l = rpn_search_structure (zi, zs->u.complex->s1);
630 if (bool_parms.rset_l == NULL)
632 bool_parms.rset_r = rpn_search_structure (zi, zs->u.complex->s2);
633 if (bool_parms.rset_r == NULL)
635 rset_delete (bool_parms.rset_l);
638 bool_parms.key_size = sizeof(struct it_key);
639 bool_parms.cmp = key_compare;
641 switch (zs->u.complex->operator->which)
644 r = rset_create (rset_kind_and, &bool_parms);
647 r = rset_create (rset_kind_or, &bool_parms);
649 case Z_Operator_and_not:
650 r = rset_create (rset_kind_not, &bool_parms);
656 else if (zs->which == Z_RPNStructure_simple)
658 if (zs->u.simple->which == Z_Operand_APT)
660 logf (LOG_DEBUG, "rpn_search_APT");
661 r = rpn_search_APT (zi, zs->u.simple->u.attributesPlusTerm);
663 else if (zs->u.simple->which == Z_Operand_resultSetId)
665 logf (LOG_DEBUG, "rpn_search_ref");
666 r = rpn_search_ref (zi, zs->u.simple->u.resultSetId);
680 static void count_set (RSET r, int *count)
686 logf (LOG_DEBUG, "rpn_save_set");
688 rfd = rset_open (r, 0);
689 while (rset_read (r, rfd, &key))
691 if (key.sysno != psysno)
698 logf (LOG_DEBUG, "%d distinct sysnos", *count);
701 int rpn_search (ZServerInfo *zi,
702 Z_RPNQuery *rpn, int num_bases, char **basenames,
703 const char *setname, int *hits)
708 zi->errString = NULL;
709 rset = rpn_search_structure (zi, rpn->RPNStructure);
712 count_set (rset, hits);
713 resultSetAdd (zi, setname, 1, rset);
715 logf (LOG_DEBUG, "search error: %d", zi->errCode);
719 static struct scan_entry *scan_list;
721 static int scan_before, scan_after;
722 static ISAM scan_isam;
723 static char scan_prefix[20];
725 static int scan_handle (Dict_char *name, const char *info, int pos)
731 rset_isam_parms parms;
733 len_prefix = strlen(scan_prefix);
734 if (memcmp (name, scan_prefix, len_prefix))
737 idx = scan_after - pos + scan_before;
740 scan_list[idx].term = odr_malloc (scan_odr, strlen(name + len_prefix)+1);
741 strcpy (scan_list[idx].term, name + len_prefix);
742 assert (*info == sizeof(isam_p));
743 memcpy (&isam_p, info+1, sizeof(isam_p));
744 parms.is = scan_isam;
747 rset = rset_create (rset_kind_isam, &parms);
748 count_set (rset, &scan_list[idx].occurrences);
751 scan_list[idx].occurrences = 1;
753 logf (LOG_DEBUG, "pos=%3d idx=%3d name=%s", pos, idx, name);
757 int rpn_scan (ZServerInfo *zi, ODR odr, Z_AttributesPlusTerm *zapt,
758 int *position, int *num_entries, struct scan_entry **list)
762 int num = *num_entries;
765 char termz[IT_MAX_WORD+20];
768 Z_Term *term = zapt->term;
770 logf (LOG_DEBUG, "scan, position = %d, num = %d", pos, num);
771 scan_before = before = pos-1;
772 scan_after = after = 1+num-pos;
775 logf (LOG_DEBUG, "scan, before = %d, after = %d", before, after);
777 scan_isam = zi->wordIsam;
778 scan_list = *list = odr_malloc (odr, (before+after)*sizeof(**list));
779 for (j = 0; j<before+after; j++)
780 scan_list[j].term = "------";
781 attr_init (&use, zapt, 1);
782 use_value = attr_find (&use);
783 logf (LOG_DEBUG, "use value %d", use_value);
787 i = index_word_prefix (termz, 1, use_value);
788 strcpy (scan_prefix, termz);
789 sizez = term->u.general->len;
790 if (sizez > IT_MAX_WORD)
792 for (j = 0; j<sizez; j++)
793 termz[j+i] = index_char_cvt (term->u.general->buf[j]);
796 dict_scan (zi->wordDict, termz, &before, &after, scan_handle);
799 logf (LOG_DEBUG, "search error: %d", zi->errCode);