2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.14 1995-09-28 12:10:32 adam
8 * Bug fixes. Field prefix used in queries.
10 * Revision 1.13 1995/09/18 14:17:50 adam
13 * Revision 1.12 1995/09/15 14:45:21 adam
17 * Revision 1.11 1995/09/14 11:53:27 adam
18 * First work on regular expressions/truncations.
20 * Revision 1.10 1995/09/11 15:23:26 adam
21 * More work on relevance search.
23 * Revision 1.9 1995/09/11 13:09:35 adam
24 * More work on relevance feedback.
26 * Revision 1.8 1995/09/08 14:52:27 adam
27 * Minor changes. Dictionary is lower case now.
29 * Revision 1.7 1995/09/07 13:58:36 adam
30 * New parameter: result-set file descriptor (RSFD) to support multiple
31 * positions within the same result-set.
32 * Boolean operators: and, or, not implemented.
33 * Result-set references.
35 * Revision 1.6 1995/09/06 16:11:18 adam
36 * Option: only one word key per file.
38 * Revision 1.5 1995/09/06 10:33:04 adam
39 * More work on present. Some log messages removed.
41 * Revision 1.4 1995/09/05 15:28:40 adam
42 * More work on search engine.
44 * Revision 1.3 1995/09/04 15:20:22 adam
47 * Revision 1.2 1995/09/04 12:33:43 adam
48 * Various cleanup. YAZ util used instead.
50 * Revision 1.1 1995/09/04 09:10:40 adam
51 * More work on index add/del/update.
52 * Merge sort implemented.
53 * Initial work on z39 server.
69 * attr_print: log attributes
71 static void attr_print (Z_AttributesPlusTerm *t)
74 for (of = 0; of < t->num_attributes; of++)
76 Z_AttributeElement *element;
77 element = t->attributeList[of];
79 switch (element->which)
81 case Z_AttributeValue_numeric:
82 logf (LOG_DEBUG, "attributeType=%d value=%d",
83 *element->attributeType,
84 *element->value.numeric);
86 case Z_AttributeValue_complex:
87 logf (LOG_DEBUG, "attributeType=%d complex",
88 *element->attributeType);
89 for (i = 0; i<element->value.complex->num_list; i++)
91 if (element->value.complex->list[i]->which ==
92 Z_StringOrNumeric_string)
93 logf (LOG_DEBUG, " string: '%s'",
94 element->value.complex->list[i]->u.string);
95 else if (element->value.complex->list[i]->which ==
96 Z_StringOrNumeric_numeric)
97 logf (LOG_DEBUG, " numeric: '%d'",
98 *element->value.complex->list[i]->u.numeric);
111 Z_AttributesPlusTerm *zapt;
114 static int attr_find (AttrType *src)
116 while (src->major < src->zapt->num_attributes)
118 Z_AttributeElement *element;
119 element = src->zapt->attributeList[src->major];
121 if (src->type == *element->attributeType)
123 switch (element->which)
125 case Z_AttributeValue_numeric:
127 return *element->value.numeric;
129 case Z_AttributeValue_complex:
130 if (src->minor >= element->value.complex->num_list ||
131 element->value.complex->list[src->minor]->which !=
132 Z_StringOrNumeric_numeric)
135 return *element->value.complex->list[src->minor-1]->u.numeric;
145 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
158 int (*cmp)(const void *p1, const void *p2);
165 static void heap_swap (struct trunc_info *ti, int i1, int i2)
169 memcpy (ti->swapbuf, ti->heap[i1], ti->keysize);
170 memcpy (ti->heap[i1], ti->heap[i2], ti->keysize);
171 memcpy (ti->heap[i2], ti->swapbuf, ti->keysize);
174 ti->indx[i1] = ti->indx[i2];
178 static void heap_delete (struct trunc_info *ti)
180 int cur = 1, child = 2;
182 assert (ti->heapnum > 0);
183 memcpy (ti->heap[1], ti->heap[ti->heapnum], ti->keysize);
184 ti->indx[1] = ti->indx[ti->heapnum--];
185 while (child <= ti->heapnum) {
186 if (child < ti->heapnum &&
187 (*ti->cmp)(ti->heap[child], ti->heap[1+child]) > 0)
189 if ((*ti->cmp)(ti->heap[cur], ti->heap[child]) > 0)
191 heap_swap (ti, cur, child);
200 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
204 cur = ++(ti->heapnum);
205 memcpy (ti->heap[cur], buf, ti->keysize);
206 ti->indx[cur] = indx;
208 while (parent && (*ti->cmp)(ti->heap[parent], ti->heap[cur]) > 0)
210 heap_swap (ti, cur, parent);
217 struct trunc_info *heap_init (int size, int key_size,
218 int (*cmp)(const void *p1, const void *p2))
220 struct trunc_info *ti = xmalloc (sizeof(*ti));
225 ti->keysize = key_size;
227 ti->indx = xmalloc (size * sizeof(*ti->indx));
228 ti->heap = xmalloc (size * sizeof(*ti->heap));
229 ti->swapbuf = xmalloc (ti->keysize);
230 ti->tmpbuf = xmalloc (ti->keysize);
231 ti->buf = xmalloc (size * ti->keysize);
232 for (i = size; --i >= 0; )
233 ti->heap[i] = ti->buf + ti->keysize * i;
237 static void heap_close (struct trunc_info *ti)
246 static RSET rset_trunc (ISAM isam, ISAM_P *isam_p, int from, int to,
249 logf (LOG_DEBUG, "rset_trunc, range=%d-%d", from, to-1);
250 if (to - from > merge_chunk)
258 struct trunc_info *ti;
261 rset_temp_parms parms;
263 ispt = xmalloc (sizeof(*ispt) * (to-from));
264 parms.key_size = sizeof (struct it_key);
265 result = rset_create (rset_kind_temp, &parms);
266 rsfd = rset_open (result, 1);
268 ti = heap_init (to-from, sizeof(struct it_key),
270 for (i = to-from; --i >= 0; )
272 ispt[i] = is_position (isam, isam_p[from+i]);
273 if (is_readkey (ispt[i], ti->tmpbuf))
274 heap_insert (ti, ti->tmpbuf, i);
280 rset_write (result, rsfd, ti->heap[1]);
282 if (is_readkey (ispt[n], ti->tmpbuf))
283 heap_insert (ti, ti->tmpbuf, n);
285 for (i = to-from; --i >= 0; )
286 is_pt_free (ispt[i]);
287 rset_close (result, rsfd);
294 static ISAM_P *isam_p_buf = NULL;
295 static int isam_p_size = 0;
296 static int isam_p_indx;
298 static void add_isam_p (const char *info)
300 if (isam_p_indx == isam_p_size)
302 ISAM_P *new_isam_p_buf;
304 isam_p_size = 2*isam_p_size + 100;
305 new_isam_p_buf = xmalloc (sizeof(*new_isam_p_buf) *
309 memcpy (new_isam_p_buf, isam_p_buf,
310 isam_p_indx * sizeof(*isam_p_buf));
313 isam_p_buf = new_isam_p_buf;
315 assert (*info == sizeof(*isam_p_buf));
316 memcpy (isam_p_buf + isam_p_indx, info+1, sizeof(*isam_p_buf));
320 static int grep_handle (Dict_char *name, const char *info)
322 logf (LOG_DEBUG, "dict name: %s", name);
327 static int trunc_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
328 const char *term_sub, ISAM_P **isam_ps)
330 char term_dict[2*IT_MAX_WORD+2];
334 int truncation_value;
336 attr_init (&truncation, zapt, 5);
337 truncation_value = attr_find (&truncation);
338 logf (LOG_DEBUG, "truncation value %d", truncation_value);
339 switch (truncation_value)
341 case -1: /* not specified */
342 case 100: /* do not truncate */
343 strcpy (term_dict, term_sub);
344 logf (LOG_DEBUG, "dict_lookup: %s", term_dict);
345 if ((info = dict_lookup (zi->wordDict, term_dict)))
348 case 1: /* right truncation */
349 strcpy (term_dict, term_sub);
350 strcat (term_dict, ".*");
351 dict_lookup_grep (zi->wordDict, term_dict, 0, grep_handle);
353 case 2: /* left truncation */
354 case 3: /* left&right truncation */
357 case 101: /* process # in term */
358 for (j = 0, i = 0; term_sub[i] && i < 2; i++)
359 term_dict[j++] = term_sub[i];
360 for (; term_sub[i]; i++)
361 if (term_sub[i] == '#')
363 term_dict[j++] = '.';
364 term_dict[j++] = '*';
367 term_dict[j++] = term_sub[i];
369 dict_lookup_grep (zi->wordDict, term_dict, 0, grep_handle);
371 case 102: /* regular expression */
372 strcpy (term_dict, term_sub);
373 dict_lookup_grep (zi->wordDict, term_dict, 0, grep_handle);
376 *isam_ps = isam_p_buf;
377 logf (LOG_DEBUG, "%d positions", isam_p_indx);
381 static RSET rpn_search_APT_relevance (ZServerInfo *zi,
382 Z_AttributesPlusTerm *zapt)
384 rset_relevance_parms parms;
385 char termz[IT_MAX_WORD+1];
386 char term_sub[IT_MAX_WORD+1];
387 char *p0 = termz, *p1 = NULL;
388 Z_Term *term = zapt->term;
391 parms.key_size = sizeof(struct it_key);
393 parms.cmp = key_compare;
394 parms.is = zi->wordIsam;
396 if (term->which != Z_Term_general)
401 i = index_word_prefix (termz, 1, 1016);
402 sizez = i + term->u.general->len;
403 if (sizez > IT_MAX_WORD)
405 for ( ; i < sizez; i++)
406 termz[i] = index_char_cvt (term->u.general->buf[i]);
409 isam_p_indx = 0; /* global, set by trunc_term - see below */
412 if ((p1 = strchr (p0, ' ')))
414 memcpy (term_sub, p0, p1-p0);
415 term_sub[p1-p0] = '\0';
418 strcpy (term_sub, p0);
419 if (trunc_term (zi, zapt, term_sub, &parms.isam_positions))
425 parms.no_isam_positions = isam_p_indx;
427 return rset_create (rset_kind_relevance, &parms);
429 return rset_create (rset_kind_null, NULL);
432 static RSET rpn_search_APT_word (ZServerInfo *zi,
433 Z_AttributesPlusTerm *zapt)
435 ISAM_P *isam_positions;
436 rset_isam_parms parms;
438 char termz[IT_MAX_WORD+1];
439 Z_Term *term = zapt->term;
442 if (term->which != Z_Term_general)
447 i = index_word_prefix (termz, 1, 1016);
448 sizez = i + term->u.general->len;
449 if (sizez > IT_MAX_WORD)
451 for ( ; i < sizez; i++)
452 termz[i] = index_char_cvt (term->u.general->buf[i]);
455 isam_p_indx = 0; /* global, set by trunc_term - see below */
456 if (trunc_term (zi, zapt, termz, &isam_positions))
459 return rset_create (rset_kind_null, NULL);
460 else if (isam_p_indx == 1)
462 parms.is = zi->wordIsam;
463 parms.pos = *isam_positions;
464 return rset_create (rset_kind_isam, &parms);
467 return rset_trunc (zi->wordIsam, isam_positions, 0, isam_p_indx, 400);
470 static RSET rpn_search_APT_phrase (ZServerInfo *zi,
471 Z_AttributesPlusTerm *zapt)
473 ISAM_P *isam_positions;
474 rset_isam_parms parms;
476 char termz[IT_MAX_WORD+1];
477 Z_Term *term = zapt->term;
480 if (term->which != Z_Term_general)
485 i = index_word_prefix (termz, 1, 1016);
486 sizez = i + term->u.general->len;
487 if (sizez > IT_MAX_WORD)
489 for ( ; i < sizez; i++)
490 termz[i] = index_char_cvt (term->u.general->buf[i]);
493 isam_p_indx = 0; /* global, set by trunc_term - see below */
494 if (trunc_term (zi, zapt, termz, &isam_positions))
496 if (isam_p_indx != 1)
497 return rset_create (rset_kind_null, NULL);
498 parms.is = zi->wordIsam;
499 parms.pos = *isam_positions;
500 return rset_create (rset_kind_isam, &parms);
503 static RSET rpn_search_APT (ZServerInfo *zi, Z_AttributesPlusTerm *zapt)
507 int relation_value, structure_value;
509 attr_init (&relation, zapt, 2);
510 attr_init (&structure, zapt, 4);
512 relation_value = attr_find (&relation);
513 structure_value = attr_find (&structure);
514 switch (structure_value)
517 if (relation_value == 102) /* relevance relation */
518 return rpn_search_APT_relevance (zi, zapt);
519 return rpn_search_APT_word (zi, zapt);
521 if (relation_value == 102) /* relevance relation */
522 return rpn_search_APT_relevance (zi, zapt);
523 return rpn_search_APT_phrase (zi, zapt);
526 if (relation_value == 102) /* relevance relation */
527 return rpn_search_APT_relevance (zi, zapt);
528 return rpn_search_APT_word (zi, zapt);
533 case 5: /* date - normalized */
535 case 6: /* word list */
536 return rpn_search_APT_relevance (zi, zapt);
537 case 100: /* date - un-normalized */
539 case 101: /* name - normalized */
541 case 102: /* date - un-normalized */
543 case 103: /* structure */
547 case 105: /* free-form-text */
548 return rpn_search_APT_relevance (zi, zapt);
549 case 106: /* document-text */
550 return rpn_search_APT_relevance (zi, zapt);
551 case 107: /* local-number */
553 case 108: /* string */
554 return rpn_search_APT_word (zi, zapt);
555 case 109: /* numeric string */
562 static RSET rpn_search_ref (ZServerInfo *zi, Z_ResultSetId *resultSetId)
566 if (!(s = resultSetGet (zi, resultSetId)))
567 return rset_create (rset_kind_null, NULL);
571 static RSET rpn_search_structure (ZServerInfo *zi, Z_RPNStructure *zs)
574 if (zs->which == Z_RPNStructure_complex)
576 rset_bool_parms bool_parms;
578 bool_parms.rset_l = rpn_search_structure (zi, zs->u.complex->s1);
579 if (bool_parms.rset_l == NULL)
581 bool_parms.rset_r = rpn_search_structure (zi, zs->u.complex->s2);
582 if (bool_parms.rset_r == NULL)
584 rset_delete (bool_parms.rset_l);
587 bool_parms.key_size = sizeof(struct it_key);
588 bool_parms.cmp = key_compare;
590 switch (zs->u.complex->operator->which)
593 r = rset_create (rset_kind_and, &bool_parms);
596 r = rset_create (rset_kind_or, &bool_parms);
598 case Z_Operator_and_not:
599 r = rset_create (rset_kind_not, &bool_parms);
605 else if (zs->which == Z_RPNStructure_simple)
607 if (zs->u.simple->which == Z_Operand_APT)
609 logf (LOG_DEBUG, "rpn_search_APT");
610 r = rpn_search_APT (zi, zs->u.simple->u.attributesPlusTerm);
612 else if (zs->u.simple->which == Z_Operand_resultSetId)
614 logf (LOG_DEBUG, "rpn_search_ref");
615 r = rpn_search_ref (zi, zs->u.simple->u.resultSetId);
629 static void count_set (RSET r, int *count)
635 logf (LOG_DEBUG, "rpn_save_set");
637 rfd = rset_open (r, 0);
638 while (rset_read (r, rfd, &key))
640 if (key.sysno != psysno)
647 logf (LOG_DEBUG, "%d distinct sysnos", *count);
650 int rpn_search (ZServerInfo *zi,
651 Z_RPNQuery *rpn, int num_bases, char **basenames,
652 const char *setname, int *hits)
657 zi->errString = NULL;
658 rset = rpn_search_structure (zi, rpn->RPNStructure);
661 count_set (rset, hits);
662 resultSetAdd (zi, setname, 1, rset);