2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.21 1995-10-06 13:52:06 adam
8 * Bug fixes. Handler may abort further scanning.
10 * Revision 1.20 1995/10/06 11:06:33 adam
11 * Scan entries include 'occurrences' now.
13 * Revision 1.19 1995/10/06 10:43:56 adam
14 * Scan added. 'occurrences' in scan entries not set yet.
16 * Revision 1.18 1995/10/04 16:57:20 adam
17 * Key input and merge sort in one pass.
19 * Revision 1.17 1995/10/04 12:55:17 adam
20 * Bug fix in ranked search. Use=Any keys inserted.
22 * Revision 1.16 1995/10/02 16:24:40 adam
23 * Use attribute actually used in search requests.
25 * Revision 1.15 1995/10/02 15:18:52 adam
26 * New member in recRetrieveCtrl: diagnostic.
28 * Revision 1.14 1995/09/28 12:10:32 adam
29 * Bug fixes. Field prefix used in queries.
31 * Revision 1.13 1995/09/18 14:17:50 adam
34 * Revision 1.12 1995/09/15 14:45:21 adam
38 * Revision 1.11 1995/09/14 11:53:27 adam
39 * First work on regular expressions/truncations.
41 * Revision 1.10 1995/09/11 15:23:26 adam
42 * More work on relevance search.
44 * Revision 1.9 1995/09/11 13:09:35 adam
45 * More work on relevance feedback.
47 * Revision 1.8 1995/09/08 14:52:27 adam
48 * Minor changes. Dictionary is lower case now.
50 * Revision 1.7 1995/09/07 13:58:36 adam
51 * New parameter: result-set file descriptor (RSFD) to support multiple
52 * positions within the same result-set.
53 * Boolean operators: and, or, not implemented.
54 * Result-set references.
56 * Revision 1.6 1995/09/06 16:11:18 adam
57 * Option: only one word key per file.
59 * Revision 1.5 1995/09/06 10:33:04 adam
60 * More work on present. Some log messages removed.
62 * Revision 1.4 1995/09/05 15:28:40 adam
63 * More work on search engine.
65 * Revision 1.3 1995/09/04 15:20:22 adam
68 * Revision 1.2 1995/09/04 12:33:43 adam
69 * Various cleanup. YAZ util used instead.
71 * Revision 1.1 1995/09/04 09:10:40 adam
72 * More work on index add/del/update.
73 * Merge sort implemented.
74 * Initial work on z39 server.
90 * attr_print: log attributes
92 static void attr_print (Z_AttributesPlusTerm *t)
95 for (of = 0; of < t->num_attributes; of++)
97 Z_AttributeElement *element;
98 element = t->attributeList[of];
100 switch (element->which)
102 case Z_AttributeValue_numeric:
103 logf (LOG_DEBUG, "attributeType=%d value=%d",
104 *element->attributeType,
105 *element->value.numeric);
107 case Z_AttributeValue_complex:
108 logf (LOG_DEBUG, "attributeType=%d complex",
109 *element->attributeType);
110 for (i = 0; i<element->value.complex->num_list; i++)
112 if (element->value.complex->list[i]->which ==
113 Z_StringOrNumeric_string)
114 logf (LOG_DEBUG, " string: '%s'",
115 element->value.complex->list[i]->u.string);
116 else if (element->value.complex->list[i]->which ==
117 Z_StringOrNumeric_numeric)
118 logf (LOG_DEBUG, " numeric: '%d'",
119 *element->value.complex->list[i]->u.numeric);
132 Z_AttributesPlusTerm *zapt;
135 static int attr_find (AttrType *src)
137 while (src->major < src->zapt->num_attributes)
139 Z_AttributeElement *element;
140 element = src->zapt->attributeList[src->major];
142 if (src->type == *element->attributeType)
144 switch (element->which)
146 case Z_AttributeValue_numeric:
148 return *element->value.numeric;
150 case Z_AttributeValue_complex:
151 if (src->minor >= element->value.complex->num_list ||
152 element->value.complex->list[src->minor]->which !=
153 Z_StringOrNumeric_numeric)
156 return *element->value.complex->list[src->minor-1]->u.numeric;
166 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
179 int (*cmp)(const void *p1, const void *p2);
186 static void heap_swap (struct trunc_info *ti, int i1, int i2)
190 memcpy (ti->swapbuf, ti->heap[i1], ti->keysize);
191 memcpy (ti->heap[i1], ti->heap[i2], ti->keysize);
192 memcpy (ti->heap[i2], ti->swapbuf, ti->keysize);
195 ti->indx[i1] = ti->indx[i2];
199 static void heap_delete (struct trunc_info *ti)
201 int cur = 1, child = 2;
203 assert (ti->heapnum > 0);
204 memcpy (ti->heap[1], ti->heap[ti->heapnum], ti->keysize);
205 ti->indx[1] = ti->indx[ti->heapnum--];
206 while (child <= ti->heapnum) {
207 if (child < ti->heapnum &&
208 (*ti->cmp)(ti->heap[child], ti->heap[1+child]) > 0)
210 if ((*ti->cmp)(ti->heap[cur], ti->heap[child]) > 0)
212 heap_swap (ti, cur, child);
221 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
225 cur = ++(ti->heapnum);
226 memcpy (ti->heap[cur], buf, ti->keysize);
227 ti->indx[cur] = indx;
229 while (parent && (*ti->cmp)(ti->heap[parent], ti->heap[cur]) > 0)
231 heap_swap (ti, cur, parent);
238 struct trunc_info *heap_init (int size, int key_size,
239 int (*cmp)(const void *p1, const void *p2))
241 struct trunc_info *ti = xmalloc (sizeof(*ti));
246 ti->keysize = key_size;
248 ti->indx = xmalloc (size * sizeof(*ti->indx));
249 ti->heap = xmalloc (size * sizeof(*ti->heap));
250 ti->swapbuf = xmalloc (ti->keysize);
251 ti->tmpbuf = xmalloc (ti->keysize);
252 ti->buf = xmalloc (size * ti->keysize);
253 for (i = size; --i >= 0; )
254 ti->heap[i] = ti->buf + ti->keysize * i;
258 static void heap_close (struct trunc_info *ti)
267 static RSET rset_trunc (ISAM isam, ISAM_P *isam_p, int from, int to,
270 logf (LOG_DEBUG, "rset_trunc, range=%d-%d", from, to-1);
271 if (to - from > merge_chunk)
279 struct trunc_info *ti;
282 rset_temp_parms parms;
284 ispt = xmalloc (sizeof(*ispt) * (to-from));
285 parms.key_size = sizeof (struct it_key);
286 result = rset_create (rset_kind_temp, &parms);
287 rsfd = rset_open (result, 1);
289 ti = heap_init (to-from, sizeof(struct it_key),
291 for (i = to-from; --i >= 0; )
293 ispt[i] = is_position (isam, isam_p[from+i]);
294 if (is_readkey (ispt[i], ti->tmpbuf))
295 heap_insert (ti, ti->tmpbuf, i);
301 rset_write (result, rsfd, ti->heap[1]);
303 if (is_readkey (ispt[n], ti->tmpbuf))
304 heap_insert (ti, ti->tmpbuf, n);
306 for (i = to-from; --i >= 0; )
307 is_pt_free (ispt[i]);
308 rset_close (result, rsfd);
315 static ISAM_P *isam_p_buf = NULL;
316 static int isam_p_size = 0;
317 static int isam_p_indx;
319 static void add_isam_p (const char *info)
321 if (isam_p_indx == isam_p_size)
323 ISAM_P *new_isam_p_buf;
325 isam_p_size = 2*isam_p_size + 100;
326 new_isam_p_buf = xmalloc (sizeof(*new_isam_p_buf) *
330 memcpy (new_isam_p_buf, isam_p_buf,
331 isam_p_indx * sizeof(*isam_p_buf));
334 isam_p_buf = new_isam_p_buf;
336 assert (*info == sizeof(*isam_p_buf));
337 memcpy (isam_p_buf + isam_p_indx, info+1, sizeof(*isam_p_buf));
341 static int grep_handle (Dict_char *name, const char *info)
343 logf (LOG_DEBUG, "dict name: %s", name);
348 static int trunc_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
349 const char *term_sub, ISAM_P **isam_ps)
351 char term_dict[2*IT_MAX_WORD+2];
355 int truncation_value;
359 attr_init (&use, zapt, 1);
360 use_value = attr_find (&use);
361 logf (LOG_DEBUG, "use value %d", use_value);
362 attr_init (&truncation, zapt, 5);
363 truncation_value = attr_find (&truncation);
364 logf (LOG_DEBUG, "truncation value %d", truncation_value);
368 i = index_word_prefix (term_dict, 1, use_value);
370 switch (truncation_value)
372 case -1: /* not specified */
373 case 100: /* do not truncate */
374 strcat (term_dict, term_sub);
375 logf (LOG_DEBUG, "dict_lookup: %s", term_dict);
376 if ((info = dict_lookup (zi->wordDict, term_dict)))
379 case 1: /* right truncation */
380 strcat (term_dict, term_sub);
381 strcat (term_dict, ".*");
382 dict_lookup_grep (zi->wordDict, term_dict, 0, grep_handle);
384 case 2: /* left truncation */
385 case 3: /* left&right truncation */
388 case 101: /* process # in term */
389 for (j = strlen(term_dict), i = 0; term_sub[i] && i < 2; i++)
390 term_dict[j++] = term_sub[i];
391 for (; term_sub[i]; i++)
392 if (term_sub[i] == '#')
394 term_dict[j++] = '.';
395 term_dict[j++] = '*';
398 term_dict[j++] = term_sub[i];
400 dict_lookup_grep (zi->wordDict, term_dict, 0, grep_handle);
402 case 102: /* regular expression */
403 strcat (term_dict, term_sub);
404 dict_lookup_grep (zi->wordDict, term_dict, 0, grep_handle);
407 *isam_ps = isam_p_buf;
408 logf (LOG_DEBUG, "%d positions", isam_p_indx);
412 static void trans_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
416 Z_Term *term = zapt->term;
418 sizez = term->u.general->len;
419 if (sizez > IT_MAX_WORD)
421 for (i = 0; i < sizez; i++)
422 termz[i] = index_char_cvt (term->u.general->buf[i]);
426 static RSET rpn_search_APT_relevance (ZServerInfo *zi,
427 Z_AttributesPlusTerm *zapt)
429 rset_relevance_parms parms;
430 char termz[IT_MAX_WORD+1];
431 char term_sub[IT_MAX_WORD+1];
432 char *p0 = termz, *p1 = NULL;
433 Z_Term *term = zapt->term;
435 parms.key_size = sizeof(struct it_key);
437 parms.cmp = key_compare;
438 parms.is = zi->wordIsam;
440 if (term->which != Z_Term_general)
445 trans_term (zi, zapt, termz);
446 isam_p_indx = 0; /* global, set by trunc_term - see below */
449 if ((p1 = strchr (p0, ' ')))
451 memcpy (term_sub, p0, p1-p0);
452 term_sub[p1-p0] = '\0';
455 strcpy (term_sub, p0);
456 if (trunc_term (zi, zapt, term_sub, &parms.isam_positions))
462 parms.no_isam_positions = isam_p_indx;
464 return rset_create (rset_kind_relevance, &parms);
466 return rset_create (rset_kind_null, NULL);
469 static RSET rpn_search_APT_word (ZServerInfo *zi,
470 Z_AttributesPlusTerm *zapt)
472 ISAM_P *isam_positions;
473 rset_isam_parms parms;
475 char termz[IT_MAX_WORD+1];
476 Z_Term *term = zapt->term;
478 if (term->which != Z_Term_general)
483 trans_term (zi, zapt, termz);
484 isam_p_indx = 0; /* global, set by trunc_term - see below */
485 if (trunc_term (zi, zapt, termz, &isam_positions))
488 return rset_create (rset_kind_null, NULL);
489 else if (isam_p_indx == 1)
491 parms.is = zi->wordIsam;
492 parms.pos = *isam_positions;
493 return rset_create (rset_kind_isam, &parms);
496 return rset_trunc (zi->wordIsam, isam_positions, 0, isam_p_indx, 400);
499 static RSET rpn_search_APT_phrase (ZServerInfo *zi,
500 Z_AttributesPlusTerm *zapt)
502 ISAM_P *isam_positions;
503 rset_isam_parms parms;
505 char termz[IT_MAX_WORD+1];
506 Z_Term *term = zapt->term;
508 if (term->which != Z_Term_general)
513 trans_term (zi, zapt, termz);
514 isam_p_indx = 0; /* global, set by trunc_term - see below */
515 if (trunc_term (zi, zapt, termz, &isam_positions))
517 if (isam_p_indx != 1)
518 return rset_create (rset_kind_null, NULL);
519 parms.is = zi->wordIsam;
520 parms.pos = *isam_positions;
521 return rset_create (rset_kind_isam, &parms);
524 static RSET rpn_search_APT (ZServerInfo *zi, Z_AttributesPlusTerm *zapt)
528 int relation_value, structure_value;
530 attr_init (&relation, zapt, 2);
531 attr_init (&structure, zapt, 4);
533 relation_value = attr_find (&relation);
534 structure_value = attr_find (&structure);
535 switch (structure_value)
538 if (relation_value == 102) /* relevance relation */
539 return rpn_search_APT_relevance (zi, zapt);
540 return rpn_search_APT_word (zi, zapt);
542 if (relation_value == 102) /* relevance relation */
543 return rpn_search_APT_relevance (zi, zapt);
544 return rpn_search_APT_phrase (zi, zapt);
547 if (relation_value == 102) /* relevance relation */
548 return rpn_search_APT_relevance (zi, zapt);
549 return rpn_search_APT_word (zi, zapt);
554 case 5: /* date - normalized */
556 case 6: /* word list */
557 return rpn_search_APT_relevance (zi, zapt);
558 case 100: /* date - un-normalized */
560 case 101: /* name - normalized */
562 case 102: /* date - un-normalized */
564 case 103: /* structure */
568 case 105: /* free-form-text */
569 return rpn_search_APT_relevance (zi, zapt);
570 case 106: /* document-text */
571 return rpn_search_APT_relevance (zi, zapt);
572 case 107: /* local-number */
574 case 108: /* string */
575 return rpn_search_APT_word (zi, zapt);
576 case 109: /* numeric string */
583 static RSET rpn_search_ref (ZServerInfo *zi, Z_ResultSetId *resultSetId)
587 if (!(s = resultSetGet (zi, resultSetId)))
588 return rset_create (rset_kind_null, NULL);
592 static RSET rpn_search_structure (ZServerInfo *zi, Z_RPNStructure *zs)
595 if (zs->which == Z_RPNStructure_complex)
597 rset_bool_parms bool_parms;
599 bool_parms.rset_l = rpn_search_structure (zi, zs->u.complex->s1);
600 if (bool_parms.rset_l == NULL)
602 bool_parms.rset_r = rpn_search_structure (zi, zs->u.complex->s2);
603 if (bool_parms.rset_r == NULL)
605 rset_delete (bool_parms.rset_l);
608 bool_parms.key_size = sizeof(struct it_key);
609 bool_parms.cmp = key_compare;
611 switch (zs->u.complex->operator->which)
614 r = rset_create (rset_kind_and, &bool_parms);
617 r = rset_create (rset_kind_or, &bool_parms);
619 case Z_Operator_and_not:
620 r = rset_create (rset_kind_not, &bool_parms);
626 else if (zs->which == Z_RPNStructure_simple)
628 if (zs->u.simple->which == Z_Operand_APT)
630 logf (LOG_DEBUG, "rpn_search_APT");
631 r = rpn_search_APT (zi, zs->u.simple->u.attributesPlusTerm);
633 else if (zs->u.simple->which == Z_Operand_resultSetId)
635 logf (LOG_DEBUG, "rpn_search_ref");
636 r = rpn_search_ref (zi, zs->u.simple->u.resultSetId);
650 static void count_set (RSET r, int *count)
656 logf (LOG_DEBUG, "rpn_save_set");
658 rfd = rset_open (r, 0);
659 while (rset_read (r, rfd, &key))
661 if (key.sysno != psysno)
668 logf (LOG_DEBUG, "%d distinct sysnos", *count);
671 int rpn_search (ZServerInfo *zi,
672 Z_RPNQuery *rpn, int num_bases, char **basenames,
673 const char *setname, int *hits)
678 zi->errString = NULL;
679 rset = rpn_search_structure (zi, rpn->RPNStructure);
682 count_set (rset, hits);
683 resultSetAdd (zi, setname, 1, rset);
685 logf (LOG_DEBUG, "search error: %d", zi->errCode);
689 static struct scan_entry *scan_list;
691 static int scan_before, scan_after;
692 static ISAM scan_isam;
693 static char scan_prefix[20];
695 static int scan_handle (Dict_char *name, const char *info, int pos)
701 rset_isam_parms parms;
703 len_prefix = strlen(scan_prefix);
704 if (memcmp (name, scan_prefix, len_prefix))
707 idx = scan_after - pos + scan_before;
710 scan_list[idx].term = odr_malloc (scan_odr, strlen(name + len_prefix)+1);
711 strcpy (scan_list[idx].term, name + len_prefix);
712 assert (*info == sizeof(isam_p));
713 memcpy (&isam_p, info+1, sizeof(isam_p));
714 parms.is = scan_isam;
717 rset = rset_create (rset_kind_isam, &parms);
718 count_set (rset, &scan_list[idx].occurrences);
721 scan_list[idx].occurrences = 1;
723 logf (LOG_DEBUG, "pos=%3d idx=%3d name=%s", pos, idx, name);
727 int rpn_scan (ZServerInfo *zi, ODR odr, Z_AttributesPlusTerm *zapt,
728 int *position, int *num_entries, struct scan_entry **list)
732 int num = *num_entries;
735 char termz[IT_MAX_WORD+20];
738 Z_Term *term = zapt->term;
740 logf (LOG_DEBUG, "scan, position = %d, num = %d", pos, num);
741 scan_before = before = pos-1;
742 scan_after = after = 1+num-pos;
745 logf (LOG_DEBUG, "scan, before = %d, after = %d", before, after);
747 scan_isam = zi->wordIsam;
748 scan_list = *list = odr_malloc (odr, (before+after)*sizeof(**list));
749 for (j = 0; j<before+after; j++)
750 scan_list[j].term = "------";
751 attr_init (&use, zapt, 1);
752 use_value = attr_find (&use);
753 logf (LOG_DEBUG, "use value %d", use_value);
757 i = index_word_prefix (termz, 1, use_value);
758 strcpy (scan_prefix, termz);
759 sizez = term->u.general->len;
760 if (sizez > IT_MAX_WORD)
762 for (j = 0; j<sizez; j++)
763 termz[j+i] = index_char_cvt (term->u.general->buf[j]);
766 dict_scan (zi->wordDict, termz, &before, &after, scan_handle);
769 logf (LOG_DEBUG, "search error: %d", zi->errCode);