2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.15 1995-10-02 15:18:52 adam
8 * New member in recRetrieveCtrl: diagnostic.
10 * Revision 1.14 1995/09/28 12:10:32 adam
11 * Bug fixes. Field prefix used in queries.
13 * Revision 1.13 1995/09/18 14:17:50 adam
16 * Revision 1.12 1995/09/15 14:45:21 adam
20 * Revision 1.11 1995/09/14 11:53:27 adam
21 * First work on regular expressions/truncations.
23 * Revision 1.10 1995/09/11 15:23:26 adam
24 * More work on relevance search.
26 * Revision 1.9 1995/09/11 13:09:35 adam
27 * More work on relevance feedback.
29 * Revision 1.8 1995/09/08 14:52:27 adam
30 * Minor changes. Dictionary is lower case now.
32 * Revision 1.7 1995/09/07 13:58:36 adam
33 * New parameter: result-set file descriptor (RSFD) to support multiple
34 * positions within the same result-set.
35 * Boolean operators: and, or, not implemented.
36 * Result-set references.
38 * Revision 1.6 1995/09/06 16:11:18 adam
39 * Option: only one word key per file.
41 * Revision 1.5 1995/09/06 10:33:04 adam
42 * More work on present. Some log messages removed.
44 * Revision 1.4 1995/09/05 15:28:40 adam
45 * More work on search engine.
47 * Revision 1.3 1995/09/04 15:20:22 adam
50 * Revision 1.2 1995/09/04 12:33:43 adam
51 * Various cleanup. YAZ util used instead.
53 * Revision 1.1 1995/09/04 09:10:40 adam
54 * More work on index add/del/update.
55 * Merge sort implemented.
56 * Initial work on z39 server.
72 * attr_print: log attributes
74 static void attr_print (Z_AttributesPlusTerm *t)
77 for (of = 0; of < t->num_attributes; of++)
79 Z_AttributeElement *element;
80 element = t->attributeList[of];
82 switch (element->which)
84 case Z_AttributeValue_numeric:
85 logf (LOG_DEBUG, "attributeType=%d value=%d",
86 *element->attributeType,
87 *element->value.numeric);
89 case Z_AttributeValue_complex:
90 logf (LOG_DEBUG, "attributeType=%d complex",
91 *element->attributeType);
92 for (i = 0; i<element->value.complex->num_list; i++)
94 if (element->value.complex->list[i]->which ==
95 Z_StringOrNumeric_string)
96 logf (LOG_DEBUG, " string: '%s'",
97 element->value.complex->list[i]->u.string);
98 else if (element->value.complex->list[i]->which ==
99 Z_StringOrNumeric_numeric)
100 logf (LOG_DEBUG, " numeric: '%d'",
101 *element->value.complex->list[i]->u.numeric);
114 Z_AttributesPlusTerm *zapt;
117 static int attr_find (AttrType *src)
119 while (src->major < src->zapt->num_attributes)
121 Z_AttributeElement *element;
122 element = src->zapt->attributeList[src->major];
124 if (src->type == *element->attributeType)
126 switch (element->which)
128 case Z_AttributeValue_numeric:
130 return *element->value.numeric;
132 case Z_AttributeValue_complex:
133 if (src->minor >= element->value.complex->num_list ||
134 element->value.complex->list[src->minor]->which !=
135 Z_StringOrNumeric_numeric)
138 return *element->value.complex->list[src->minor-1]->u.numeric;
148 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
161 int (*cmp)(const void *p1, const void *p2);
168 static void heap_swap (struct trunc_info *ti, int i1, int i2)
172 memcpy (ti->swapbuf, ti->heap[i1], ti->keysize);
173 memcpy (ti->heap[i1], ti->heap[i2], ti->keysize);
174 memcpy (ti->heap[i2], ti->swapbuf, ti->keysize);
177 ti->indx[i1] = ti->indx[i2];
181 static void heap_delete (struct trunc_info *ti)
183 int cur = 1, child = 2;
185 assert (ti->heapnum > 0);
186 memcpy (ti->heap[1], ti->heap[ti->heapnum], ti->keysize);
187 ti->indx[1] = ti->indx[ti->heapnum--];
188 while (child <= ti->heapnum) {
189 if (child < ti->heapnum &&
190 (*ti->cmp)(ti->heap[child], ti->heap[1+child]) > 0)
192 if ((*ti->cmp)(ti->heap[cur], ti->heap[child]) > 0)
194 heap_swap (ti, cur, child);
203 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
207 cur = ++(ti->heapnum);
208 memcpy (ti->heap[cur], buf, ti->keysize);
209 ti->indx[cur] = indx;
211 while (parent && (*ti->cmp)(ti->heap[parent], ti->heap[cur]) > 0)
213 heap_swap (ti, cur, parent);
220 struct trunc_info *heap_init (int size, int key_size,
221 int (*cmp)(const void *p1, const void *p2))
223 struct trunc_info *ti = xmalloc (sizeof(*ti));
228 ti->keysize = key_size;
230 ti->indx = xmalloc (size * sizeof(*ti->indx));
231 ti->heap = xmalloc (size * sizeof(*ti->heap));
232 ti->swapbuf = xmalloc (ti->keysize);
233 ti->tmpbuf = xmalloc (ti->keysize);
234 ti->buf = xmalloc (size * ti->keysize);
235 for (i = size; --i >= 0; )
236 ti->heap[i] = ti->buf + ti->keysize * i;
240 static void heap_close (struct trunc_info *ti)
249 static RSET rset_trunc (ISAM isam, ISAM_P *isam_p, int from, int to,
252 logf (LOG_DEBUG, "rset_trunc, range=%d-%d", from, to-1);
253 if (to - from > merge_chunk)
261 struct trunc_info *ti;
264 rset_temp_parms parms;
266 ispt = xmalloc (sizeof(*ispt) * (to-from));
267 parms.key_size = sizeof (struct it_key);
268 result = rset_create (rset_kind_temp, &parms);
269 rsfd = rset_open (result, 1);
271 ti = heap_init (to-from, sizeof(struct it_key),
273 for (i = to-from; --i >= 0; )
275 ispt[i] = is_position (isam, isam_p[from+i]);
276 if (is_readkey (ispt[i], ti->tmpbuf))
277 heap_insert (ti, ti->tmpbuf, i);
283 rset_write (result, rsfd, ti->heap[1]);
285 if (is_readkey (ispt[n], ti->tmpbuf))
286 heap_insert (ti, ti->tmpbuf, n);
288 for (i = to-from; --i >= 0; )
289 is_pt_free (ispt[i]);
290 rset_close (result, rsfd);
297 static ISAM_P *isam_p_buf = NULL;
298 static int isam_p_size = 0;
299 static int isam_p_indx;
301 static void add_isam_p (const char *info)
303 if (isam_p_indx == isam_p_size)
305 ISAM_P *new_isam_p_buf;
307 isam_p_size = 2*isam_p_size + 100;
308 new_isam_p_buf = xmalloc (sizeof(*new_isam_p_buf) *
312 memcpy (new_isam_p_buf, isam_p_buf,
313 isam_p_indx * sizeof(*isam_p_buf));
316 isam_p_buf = new_isam_p_buf;
318 assert (*info == sizeof(*isam_p_buf));
319 memcpy (isam_p_buf + isam_p_indx, info+1, sizeof(*isam_p_buf));
323 static int grep_handle (Dict_char *name, const char *info)
325 logf (LOG_DEBUG, "dict name: %s", name);
330 static int trunc_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
331 const char *term_sub, ISAM_P **isam_ps)
333 char term_dict[2*IT_MAX_WORD+2];
337 int truncation_value;
339 attr_init (&truncation, zapt, 5);
340 truncation_value = attr_find (&truncation);
341 logf (LOG_DEBUG, "truncation value %d", truncation_value);
342 switch (truncation_value)
344 case -1: /* not specified */
345 case 100: /* do not truncate */
346 strcpy (term_dict, term_sub);
347 logf (LOG_DEBUG, "dict_lookup: %s", term_dict);
348 if ((info = dict_lookup (zi->wordDict, term_dict)))
351 case 1: /* right truncation */
352 strcpy (term_dict, term_sub);
353 strcat (term_dict, ".*");
354 dict_lookup_grep (zi->wordDict, term_dict, 0, grep_handle);
356 case 2: /* left truncation */
357 case 3: /* left&right truncation */
360 case 101: /* process # in term */
361 for (j = 0, i = 0; term_sub[i] && i < 2; i++)
362 term_dict[j++] = term_sub[i];
363 for (; term_sub[i]; i++)
364 if (term_sub[i] == '#')
366 term_dict[j++] = '.';
367 term_dict[j++] = '*';
370 term_dict[j++] = term_sub[i];
372 dict_lookup_grep (zi->wordDict, term_dict, 0, grep_handle);
374 case 102: /* regular expression */
375 strcpy (term_dict, term_sub);
376 dict_lookup_grep (zi->wordDict, term_dict, 0, grep_handle);
379 *isam_ps = isam_p_buf;
380 logf (LOG_DEBUG, "%d positions", isam_p_indx);
384 static RSET rpn_search_APT_relevance (ZServerInfo *zi,
385 Z_AttributesPlusTerm *zapt)
387 rset_relevance_parms parms;
388 char termz[IT_MAX_WORD+1];
389 char term_sub[IT_MAX_WORD+1];
390 char *p0 = termz, *p1 = NULL;
391 Z_Term *term = zapt->term;
394 parms.key_size = sizeof(struct it_key);
396 parms.cmp = key_compare;
397 parms.is = zi->wordIsam;
399 if (term->which != Z_Term_general)
404 i = index_word_prefix (termz, 1, 1016);
405 sizez = i + term->u.general->len;
406 if (sizez > IT_MAX_WORD)
408 for (j = 0; i < sizez; i++, j++)
409 termz[i] = index_char_cvt (term->u.general->buf[j]);
412 isam_p_indx = 0; /* global, set by trunc_term - see below */
415 if ((p1 = strchr (p0, ' ')))
417 memcpy (term_sub, p0, p1-p0);
418 term_sub[p1-p0] = '\0';
421 strcpy (term_sub, p0);
422 if (trunc_term (zi, zapt, term_sub, &parms.isam_positions))
428 parms.no_isam_positions = isam_p_indx;
430 return rset_create (rset_kind_relevance, &parms);
432 return rset_create (rset_kind_null, NULL);
435 static RSET rpn_search_APT_word (ZServerInfo *zi,
436 Z_AttributesPlusTerm *zapt)
438 ISAM_P *isam_positions;
439 rset_isam_parms parms;
441 char termz[IT_MAX_WORD+1];
442 Z_Term *term = zapt->term;
445 if (term->which != Z_Term_general)
450 i = index_word_prefix (termz, 1, 1016);
451 logf (LOG_DEBUG, "i=%d", i);
452 sizez = i + term->u.general->len;
453 if (sizez > IT_MAX_WORD)
455 for (j = 0; i < sizez; i++, j++)
456 termz[i] = index_char_cvt (term->u.general->buf[j]);
459 isam_p_indx = 0; /* global, set by trunc_term - see below */
460 if (trunc_term (zi, zapt, termz, &isam_positions))
463 return rset_create (rset_kind_null, NULL);
464 else if (isam_p_indx == 1)
466 parms.is = zi->wordIsam;
467 parms.pos = *isam_positions;
468 return rset_create (rset_kind_isam, &parms);
471 return rset_trunc (zi->wordIsam, isam_positions, 0, isam_p_indx, 400);
474 static RSET rpn_search_APT_phrase (ZServerInfo *zi,
475 Z_AttributesPlusTerm *zapt)
477 ISAM_P *isam_positions;
478 rset_isam_parms parms;
480 char termz[IT_MAX_WORD+1];
481 Z_Term *term = zapt->term;
484 if (term->which != Z_Term_general)
489 i = index_word_prefix (termz, 1, 1016);
490 sizez = i + term->u.general->len;
491 if (sizez > IT_MAX_WORD)
493 for (j = 0 ; i < sizez; i++, j++)
494 termz[i] = index_char_cvt (term->u.general->buf[j]);
497 isam_p_indx = 0; /* global, set by trunc_term - see below */
498 if (trunc_term (zi, zapt, termz, &isam_positions))
500 if (isam_p_indx != 1)
501 return rset_create (rset_kind_null, NULL);
502 parms.is = zi->wordIsam;
503 parms.pos = *isam_positions;
504 return rset_create (rset_kind_isam, &parms);
507 static RSET rpn_search_APT (ZServerInfo *zi, Z_AttributesPlusTerm *zapt)
511 int relation_value, structure_value;
513 attr_init (&relation, zapt, 2);
514 attr_init (&structure, zapt, 4);
516 relation_value = attr_find (&relation);
517 structure_value = attr_find (&structure);
518 switch (structure_value)
521 if (relation_value == 102) /* relevance relation */
522 return rpn_search_APT_relevance (zi, zapt);
523 return rpn_search_APT_word (zi, zapt);
525 if (relation_value == 102) /* relevance relation */
526 return rpn_search_APT_relevance (zi, zapt);
527 return rpn_search_APT_phrase (zi, zapt);
530 if (relation_value == 102) /* relevance relation */
531 return rpn_search_APT_relevance (zi, zapt);
532 return rpn_search_APT_word (zi, zapt);
537 case 5: /* date - normalized */
539 case 6: /* word list */
540 return rpn_search_APT_relevance (zi, zapt);
541 case 100: /* date - un-normalized */
543 case 101: /* name - normalized */
545 case 102: /* date - un-normalized */
547 case 103: /* structure */
551 case 105: /* free-form-text */
552 return rpn_search_APT_relevance (zi, zapt);
553 case 106: /* document-text */
554 return rpn_search_APT_relevance (zi, zapt);
555 case 107: /* local-number */
557 case 108: /* string */
558 return rpn_search_APT_word (zi, zapt);
559 case 109: /* numeric string */
566 static RSET rpn_search_ref (ZServerInfo *zi, Z_ResultSetId *resultSetId)
570 if (!(s = resultSetGet (zi, resultSetId)))
571 return rset_create (rset_kind_null, NULL);
575 static RSET rpn_search_structure (ZServerInfo *zi, Z_RPNStructure *zs)
578 if (zs->which == Z_RPNStructure_complex)
580 rset_bool_parms bool_parms;
582 bool_parms.rset_l = rpn_search_structure (zi, zs->u.complex->s1);
583 if (bool_parms.rset_l == NULL)
585 bool_parms.rset_r = rpn_search_structure (zi, zs->u.complex->s2);
586 if (bool_parms.rset_r == NULL)
588 rset_delete (bool_parms.rset_l);
591 bool_parms.key_size = sizeof(struct it_key);
592 bool_parms.cmp = key_compare;
594 switch (zs->u.complex->operator->which)
597 r = rset_create (rset_kind_and, &bool_parms);
600 r = rset_create (rset_kind_or, &bool_parms);
602 case Z_Operator_and_not:
603 r = rset_create (rset_kind_not, &bool_parms);
609 else if (zs->which == Z_RPNStructure_simple)
611 if (zs->u.simple->which == Z_Operand_APT)
613 logf (LOG_DEBUG, "rpn_search_APT");
614 r = rpn_search_APT (zi, zs->u.simple->u.attributesPlusTerm);
616 else if (zs->u.simple->which == Z_Operand_resultSetId)
618 logf (LOG_DEBUG, "rpn_search_ref");
619 r = rpn_search_ref (zi, zs->u.simple->u.resultSetId);
633 static void count_set (RSET r, int *count)
639 logf (LOG_DEBUG, "rpn_save_set");
641 rfd = rset_open (r, 0);
642 while (rset_read (r, rfd, &key))
644 if (key.sysno != psysno)
651 logf (LOG_DEBUG, "%d distinct sysnos", *count);
654 int rpn_search (ZServerInfo *zi,
655 Z_RPNQuery *rpn, int num_bases, char **basenames,
656 const char *setname, int *hits)
661 zi->errString = NULL;
662 rset = rpn_search_structure (zi, rpn->RPNStructure);
665 count_set (rset, hits);
666 resultSetAdd (zi, setname, 1, rset);