2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.18 1995-10-04 16:57:20 adam
8 * Key input and merge sort in one pass.
10 * Revision 1.17 1995/10/04 12:55:17 adam
11 * Bug fix in ranked search. Use=Any keys inserted.
13 * Revision 1.16 1995/10/02 16:24:40 adam
14 * Use attribute actually used in search requests.
16 * Revision 1.15 1995/10/02 15:18:52 adam
17 * New member in recRetrieveCtrl: diagnostic.
19 * Revision 1.14 1995/09/28 12:10:32 adam
20 * Bug fixes. Field prefix used in queries.
22 * Revision 1.13 1995/09/18 14:17:50 adam
25 * Revision 1.12 1995/09/15 14:45:21 adam
29 * Revision 1.11 1995/09/14 11:53:27 adam
30 * First work on regular expressions/truncations.
32 * Revision 1.10 1995/09/11 15:23:26 adam
33 * More work on relevance search.
35 * Revision 1.9 1995/09/11 13:09:35 adam
36 * More work on relevance feedback.
38 * Revision 1.8 1995/09/08 14:52:27 adam
39 * Minor changes. Dictionary is lower case now.
41 * Revision 1.7 1995/09/07 13:58:36 adam
42 * New parameter: result-set file descriptor (RSFD) to support multiple
43 * positions within the same result-set.
44 * Boolean operators: and, or, not implemented.
45 * Result-set references.
47 * Revision 1.6 1995/09/06 16:11:18 adam
48 * Option: only one word key per file.
50 * Revision 1.5 1995/09/06 10:33:04 adam
51 * More work on present. Some log messages removed.
53 * Revision 1.4 1995/09/05 15:28:40 adam
54 * More work on search engine.
56 * Revision 1.3 1995/09/04 15:20:22 adam
59 * Revision 1.2 1995/09/04 12:33:43 adam
60 * Various cleanup. YAZ util used instead.
62 * Revision 1.1 1995/09/04 09:10:40 adam
63 * More work on index add/del/update.
64 * Merge sort implemented.
65 * Initial work on z39 server.
81 * attr_print: log attributes
83 static void attr_print (Z_AttributesPlusTerm *t)
86 for (of = 0; of < t->num_attributes; of++)
88 Z_AttributeElement *element;
89 element = t->attributeList[of];
91 switch (element->which)
93 case Z_AttributeValue_numeric:
94 logf (LOG_DEBUG, "attributeType=%d value=%d",
95 *element->attributeType,
96 *element->value.numeric);
98 case Z_AttributeValue_complex:
99 logf (LOG_DEBUG, "attributeType=%d complex",
100 *element->attributeType);
101 for (i = 0; i<element->value.complex->num_list; i++)
103 if (element->value.complex->list[i]->which ==
104 Z_StringOrNumeric_string)
105 logf (LOG_DEBUG, " string: '%s'",
106 element->value.complex->list[i]->u.string);
107 else if (element->value.complex->list[i]->which ==
108 Z_StringOrNumeric_numeric)
109 logf (LOG_DEBUG, " numeric: '%d'",
110 *element->value.complex->list[i]->u.numeric);
123 Z_AttributesPlusTerm *zapt;
126 static int attr_find (AttrType *src)
128 while (src->major < src->zapt->num_attributes)
130 Z_AttributeElement *element;
131 element = src->zapt->attributeList[src->major];
133 if (src->type == *element->attributeType)
135 switch (element->which)
137 case Z_AttributeValue_numeric:
139 return *element->value.numeric;
141 case Z_AttributeValue_complex:
142 if (src->minor >= element->value.complex->num_list ||
143 element->value.complex->list[src->minor]->which !=
144 Z_StringOrNumeric_numeric)
147 return *element->value.complex->list[src->minor-1]->u.numeric;
157 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
170 int (*cmp)(const void *p1, const void *p2);
177 static void heap_swap (struct trunc_info *ti, int i1, int i2)
181 memcpy (ti->swapbuf, ti->heap[i1], ti->keysize);
182 memcpy (ti->heap[i1], ti->heap[i2], ti->keysize);
183 memcpy (ti->heap[i2], ti->swapbuf, ti->keysize);
186 ti->indx[i1] = ti->indx[i2];
190 static void heap_delete (struct trunc_info *ti)
192 int cur = 1, child = 2;
194 assert (ti->heapnum > 0);
195 memcpy (ti->heap[1], ti->heap[ti->heapnum], ti->keysize);
196 ti->indx[1] = ti->indx[ti->heapnum--];
197 while (child <= ti->heapnum) {
198 if (child < ti->heapnum &&
199 (*ti->cmp)(ti->heap[child], ti->heap[1+child]) > 0)
201 if ((*ti->cmp)(ti->heap[cur], ti->heap[child]) > 0)
203 heap_swap (ti, cur, child);
212 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
216 cur = ++(ti->heapnum);
217 memcpy (ti->heap[cur], buf, ti->keysize);
218 ti->indx[cur] = indx;
220 while (parent && (*ti->cmp)(ti->heap[parent], ti->heap[cur]) > 0)
222 heap_swap (ti, cur, parent);
229 struct trunc_info *heap_init (int size, int key_size,
230 int (*cmp)(const void *p1, const void *p2))
232 struct trunc_info *ti = xmalloc (sizeof(*ti));
237 ti->keysize = key_size;
239 ti->indx = xmalloc (size * sizeof(*ti->indx));
240 ti->heap = xmalloc (size * sizeof(*ti->heap));
241 ti->swapbuf = xmalloc (ti->keysize);
242 ti->tmpbuf = xmalloc (ti->keysize);
243 ti->buf = xmalloc (size * ti->keysize);
244 for (i = size; --i >= 0; )
245 ti->heap[i] = ti->buf + ti->keysize * i;
249 static void heap_close (struct trunc_info *ti)
258 static RSET rset_trunc (ISAM isam, ISAM_P *isam_p, int from, int to,
261 logf (LOG_DEBUG, "rset_trunc, range=%d-%d", from, to-1);
262 if (to - from > merge_chunk)
270 struct trunc_info *ti;
273 rset_temp_parms parms;
275 ispt = xmalloc (sizeof(*ispt) * (to-from));
276 parms.key_size = sizeof (struct it_key);
277 result = rset_create (rset_kind_temp, &parms);
278 rsfd = rset_open (result, 1);
280 ti = heap_init (to-from, sizeof(struct it_key),
282 for (i = to-from; --i >= 0; )
284 ispt[i] = is_position (isam, isam_p[from+i]);
285 if (is_readkey (ispt[i], ti->tmpbuf))
286 heap_insert (ti, ti->tmpbuf, i);
292 rset_write (result, rsfd, ti->heap[1]);
294 if (is_readkey (ispt[n], ti->tmpbuf))
295 heap_insert (ti, ti->tmpbuf, n);
297 for (i = to-from; --i >= 0; )
298 is_pt_free (ispt[i]);
299 rset_close (result, rsfd);
306 static ISAM_P *isam_p_buf = NULL;
307 static int isam_p_size = 0;
308 static int isam_p_indx;
310 static void add_isam_p (const char *info)
312 if (isam_p_indx == isam_p_size)
314 ISAM_P *new_isam_p_buf;
316 isam_p_size = 2*isam_p_size + 100;
317 new_isam_p_buf = xmalloc (sizeof(*new_isam_p_buf) *
321 memcpy (new_isam_p_buf, isam_p_buf,
322 isam_p_indx * sizeof(*isam_p_buf));
325 isam_p_buf = new_isam_p_buf;
327 assert (*info == sizeof(*isam_p_buf));
328 memcpy (isam_p_buf + isam_p_indx, info+1, sizeof(*isam_p_buf));
332 static int grep_handle (Dict_char *name, const char *info)
334 logf (LOG_DEBUG, "dict name: %s", name);
339 static int trunc_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
340 const char *term_sub, ISAM_P **isam_ps)
342 char term_dict[2*IT_MAX_WORD+2];
346 int truncation_value;
350 attr_init (&use, zapt, 1);
351 use_value = attr_find (&use);
352 logf (LOG_DEBUG, "use value %d", truncation_value);
353 attr_init (&truncation, zapt, 5);
354 truncation_value = attr_find (&truncation);
355 logf (LOG_DEBUG, "truncation value %d", truncation_value);
359 i = index_word_prefix (term_dict, 1, use_value);
361 switch (truncation_value)
363 case -1: /* not specified */
364 case 100: /* do not truncate */
365 strcat (term_dict, term_sub);
366 logf (LOG_DEBUG, "dict_lookup: %s", term_dict);
367 if ((info = dict_lookup (zi->wordDict, term_dict)))
370 case 1: /* right truncation */
371 strcat (term_dict, term_sub);
372 strcat (term_dict, ".*");
373 dict_lookup_grep (zi->wordDict, term_dict, 0, grep_handle);
375 case 2: /* left truncation */
376 case 3: /* left&right truncation */
379 case 101: /* process # in term */
380 for (j = strlen(term_dict), i = 0; term_sub[i] && i < 2; i++)
381 term_dict[j++] = term_sub[i];
382 for (; term_sub[i]; i++)
383 if (term_sub[i] == '#')
385 term_dict[j++] = '.';
386 term_dict[j++] = '*';
389 term_dict[j++] = term_sub[i];
391 dict_lookup_grep (zi->wordDict, term_dict, 0, grep_handle);
393 case 102: /* regular expression */
394 strcat (term_dict, term_sub);
395 dict_lookup_grep (zi->wordDict, term_dict, 0, grep_handle);
398 *isam_ps = isam_p_buf;
399 logf (LOG_DEBUG, "%d positions", isam_p_indx);
403 static void trans_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
407 Z_Term *term = zapt->term;
409 sizez = term->u.general->len;
410 if (sizez > IT_MAX_WORD)
412 for (i = 0; i < sizez; i++)
413 termz[i] = index_char_cvt (term->u.general->buf[i]);
417 static RSET rpn_search_APT_relevance (ZServerInfo *zi,
418 Z_AttributesPlusTerm *zapt)
420 rset_relevance_parms parms;
421 char termz[IT_MAX_WORD+1];
422 char term_sub[IT_MAX_WORD+1];
423 char *p0 = termz, *p1 = NULL;
424 Z_Term *term = zapt->term;
426 parms.key_size = sizeof(struct it_key);
428 parms.cmp = key_compare;
429 parms.is = zi->wordIsam;
431 if (term->which != Z_Term_general)
436 trans_term (zi, zapt, termz);
437 isam_p_indx = 0; /* global, set by trunc_term - see below */
440 if ((p1 = strchr (p0, ' ')))
442 memcpy (term_sub, p0, p1-p0);
443 term_sub[p1-p0] = '\0';
446 strcpy (term_sub, p0);
447 if (trunc_term (zi, zapt, term_sub, &parms.isam_positions))
453 parms.no_isam_positions = isam_p_indx;
455 return rset_create (rset_kind_relevance, &parms);
457 return rset_create (rset_kind_null, NULL);
460 static RSET rpn_search_APT_word (ZServerInfo *zi,
461 Z_AttributesPlusTerm *zapt)
463 ISAM_P *isam_positions;
464 rset_isam_parms parms;
466 char termz[IT_MAX_WORD+1];
467 Z_Term *term = zapt->term;
469 if (term->which != Z_Term_general)
474 trans_term (zi, zapt, termz);
475 isam_p_indx = 0; /* global, set by trunc_term - see below */
476 if (trunc_term (zi, zapt, termz, &isam_positions))
479 return rset_create (rset_kind_null, NULL);
480 else if (isam_p_indx == 1)
482 parms.is = zi->wordIsam;
483 parms.pos = *isam_positions;
484 return rset_create (rset_kind_isam, &parms);
487 return rset_trunc (zi->wordIsam, isam_positions, 0, isam_p_indx, 400);
490 static RSET rpn_search_APT_phrase (ZServerInfo *zi,
491 Z_AttributesPlusTerm *zapt)
493 ISAM_P *isam_positions;
494 rset_isam_parms parms;
496 char termz[IT_MAX_WORD+1];
497 Z_Term *term = zapt->term;
499 if (term->which != Z_Term_general)
504 trans_term (zi, zapt, termz);
505 isam_p_indx = 0; /* global, set by trunc_term - see below */
506 if (trunc_term (zi, zapt, termz, &isam_positions))
508 if (isam_p_indx != 1)
509 return rset_create (rset_kind_null, NULL);
510 parms.is = zi->wordIsam;
511 parms.pos = *isam_positions;
512 return rset_create (rset_kind_isam, &parms);
515 static RSET rpn_search_APT (ZServerInfo *zi, Z_AttributesPlusTerm *zapt)
519 int relation_value, structure_value;
521 attr_init (&relation, zapt, 2);
522 attr_init (&structure, zapt, 4);
524 relation_value = attr_find (&relation);
525 structure_value = attr_find (&structure);
526 switch (structure_value)
529 if (relation_value == 102) /* relevance relation */
530 return rpn_search_APT_relevance (zi, zapt);
531 return rpn_search_APT_word (zi, zapt);
533 if (relation_value == 102) /* relevance relation */
534 return rpn_search_APT_relevance (zi, zapt);
535 return rpn_search_APT_phrase (zi, zapt);
538 if (relation_value == 102) /* relevance relation */
539 return rpn_search_APT_relevance (zi, zapt);
540 return rpn_search_APT_word (zi, zapt);
545 case 5: /* date - normalized */
547 case 6: /* word list */
548 return rpn_search_APT_relevance (zi, zapt);
549 case 100: /* date - un-normalized */
551 case 101: /* name - normalized */
553 case 102: /* date - un-normalized */
555 case 103: /* structure */
559 case 105: /* free-form-text */
560 return rpn_search_APT_relevance (zi, zapt);
561 case 106: /* document-text */
562 return rpn_search_APT_relevance (zi, zapt);
563 case 107: /* local-number */
565 case 108: /* string */
566 return rpn_search_APT_word (zi, zapt);
567 case 109: /* numeric string */
574 static RSET rpn_search_ref (ZServerInfo *zi, Z_ResultSetId *resultSetId)
578 if (!(s = resultSetGet (zi, resultSetId)))
579 return rset_create (rset_kind_null, NULL);
583 static RSET rpn_search_structure (ZServerInfo *zi, Z_RPNStructure *zs)
586 if (zs->which == Z_RPNStructure_complex)
588 rset_bool_parms bool_parms;
590 bool_parms.rset_l = rpn_search_structure (zi, zs->u.complex->s1);
591 if (bool_parms.rset_l == NULL)
593 bool_parms.rset_r = rpn_search_structure (zi, zs->u.complex->s2);
594 if (bool_parms.rset_r == NULL)
596 rset_delete (bool_parms.rset_l);
599 bool_parms.key_size = sizeof(struct it_key);
600 bool_parms.cmp = key_compare;
602 switch (zs->u.complex->operator->which)
605 r = rset_create (rset_kind_and, &bool_parms);
608 r = rset_create (rset_kind_or, &bool_parms);
610 case Z_Operator_and_not:
611 r = rset_create (rset_kind_not, &bool_parms);
617 else if (zs->which == Z_RPNStructure_simple)
619 if (zs->u.simple->which == Z_Operand_APT)
621 logf (LOG_DEBUG, "rpn_search_APT");
622 r = rpn_search_APT (zi, zs->u.simple->u.attributesPlusTerm);
624 else if (zs->u.simple->which == Z_Operand_resultSetId)
626 logf (LOG_DEBUG, "rpn_search_ref");
627 r = rpn_search_ref (zi, zs->u.simple->u.resultSetId);
641 static void count_set (RSET r, int *count)
647 logf (LOG_DEBUG, "rpn_save_set");
649 rfd = rset_open (r, 0);
650 while (rset_read (r, rfd, &key))
652 if (key.sysno != psysno)
659 logf (LOG_DEBUG, "%d distinct sysnos", *count);
662 int rpn_search (ZServerInfo *zi,
663 Z_RPNQuery *rpn, int num_bases, char **basenames,
664 const char *setname, int *hits)
669 zi->errString = NULL;
670 rset = rpn_search_structure (zi, rpn->RPNStructure);
673 count_set (rset, hits);
674 resultSetAdd (zi, setname, 1, rset);