2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.40 1996-02-02 13:44:44 adam
8 * The public dictionary functions simply use char instead of Dict_char
9 * to represent search strings. Dict_char is used internally only.
11 * Revision 1.39 1996/01/03 16:22:13 quinn
14 * Revision 1.38 1995/12/11 09:12:55 adam
15 * The rec_get function returns NULL if record doesn't exist - will
16 * happen in the server if the result set records have been deleted since
17 * the creation of the set (i.e. the search).
18 * The server saves a result temporarily if it is 'volatile', i.e. the
19 * set is register dependent.
21 * Revision 1.37 1995/12/06 15:05:28 adam
22 * More verbose in count_set.
24 * Revision 1.36 1995/12/06 12:41:27 adam
25 * New command 'stat' for the index program.
26 * Filenames can be read from stdin by specifying '-'.
27 * Bug fix/enhancement of the transformation from terms to regular
28 * expressons in the search engine.
30 * Revision 1.35 1995/11/27 09:29:00 adam
31 * Bug fixes regarding conversion to regular expressions.
33 * Revision 1.34 1995/11/16 17:00:56 adam
34 * Better logging of rpn query.
36 * Revision 1.33 1995/11/01 13:58:28 quinn
37 * Moving data1 to yaz/retrieval
39 * Revision 1.32 1995/10/27 14:00:11 adam
40 * Implemented detection of database availability.
42 * Revision 1.31 1995/10/17 18:02:10 adam
43 * New feature: databases. Implemented as prefix to words in dictionary.
45 * Revision 1.30 1995/10/16 09:32:38 adam
46 * More work on relational op.
48 * Revision 1.29 1995/10/13 16:01:49 adam
51 * Revision 1.28 1995/10/13 12:26:43 adam
52 * Optimization of truncation.
54 * Revision 1.27 1995/10/12 17:07:22 adam
57 * Revision 1.26 1995/10/12 12:40:54 adam
58 * Bug fixes in rpn_prox.
60 * Revision 1.25 1995/10/10 13:59:24 adam
61 * Function rset_open changed its wflag parameter to general flags.
63 * Revision 1.24 1995/10/09 16:18:37 adam
64 * Function dict_lookup_grep got extra client data parameter.
66 * Revision 1.23 1995/10/06 16:33:37 adam
67 * Use attribute mappings.
69 * Revision 1.22 1995/10/06 15:07:39 adam
70 * Structure 'local-number' handled.
72 * Revision 1.21 1995/10/06 13:52:06 adam
73 * Bug fixes. Handler may abort further scanning.
75 * Revision 1.20 1995/10/06 11:06:33 adam
76 * Scan entries include 'occurrences' now.
78 * Revision 1.19 1995/10/06 10:43:56 adam
79 * Scan added. 'occurrences' in scan entries not set yet.
81 * Revision 1.18 1995/10/04 16:57:20 adam
82 * Key input and merge sort in one pass.
84 * Revision 1.17 1995/10/04 12:55:17 adam
85 * Bug fix in ranked search. Use=Any keys inserted.
87 * Revision 1.16 1995/10/02 16:24:40 adam
88 * Use attribute actually used in search requests.
90 * Revision 1.15 1995/10/02 15:18:52 adam
91 * New member in recRetrieveCtrl: diagnostic.
93 * Revision 1.14 1995/09/28 12:10:32 adam
94 * Bug fixes. Field prefix used in queries.
96 * Revision 1.13 1995/09/18 14:17:50 adam
99 * Revision 1.12 1995/09/15 14:45:21 adam
101 * Work on truncation.
103 * Revision 1.11 1995/09/14 11:53:27 adam
104 * First work on regular expressions/truncations.
106 * Revision 1.10 1995/09/11 15:23:26 adam
107 * More work on relevance search.
109 * Revision 1.9 1995/09/11 13:09:35 adam
110 * More work on relevance feedback.
112 * Revision 1.8 1995/09/08 14:52:27 adam
113 * Minor changes. Dictionary is lower case now.
115 * Revision 1.7 1995/09/07 13:58:36 adam
116 * New parameter: result-set file descriptor (RSFD) to support multiple
117 * positions within the same result-set.
118 * Boolean operators: and, or, not implemented.
119 * Result-set references.
121 * Revision 1.6 1995/09/06 16:11:18 adam
122 * Option: only one word key per file.
124 * Revision 1.5 1995/09/06 10:33:04 adam
125 * More work on present. Some log messages removed.
127 * Revision 1.4 1995/09/05 15:28:40 adam
128 * More work on search engine.
130 * Revision 1.3 1995/09/04 15:20:22 adam
133 * Revision 1.2 1995/09/04 12:33:43 adam
134 * Various cleanup. YAZ util used instead.
136 * Revision 1.1 1995/09/04 09:10:40 adam
137 * More work on index add/del/update.
138 * Merge sort implemented.
139 * Initial work on z39 server.
148 #include "attribute.h"
156 int index_word_prefix_map (char *string, oid_value attrSet, int attrUse,
161 logf (LOG_DEBUG, "oid_value attrSet = %d, attrUse = %d", attrSet, attrUse);
162 attp = att_getentbyatt (attrSet, attrUse);
165 logf (LOG_DEBUG, "ord=%d", attp->attset_ordinal);
166 return index_word_prefix (string, attp->attset_ordinal,
167 attp->local_attribute, basename);
174 Z_AttributesPlusTerm *zapt;
177 static int attr_find (AttrType *src, oid_value *attributeSetP)
179 while (src->major < src->zapt->num_attributes)
181 Z_AttributeElement *element;
183 element = src->zapt->attributeList[src->major];
184 if (src->type == *element->attributeType)
186 switch (element->which)
188 case Z_AttributeValue_numeric:
190 if (element->attributeSet && attributeSetP)
194 attrset = oid_getentbyoid (element->attributeSet);
195 *attributeSetP = attrset->value;
197 return *element->value.numeric;
199 case Z_AttributeValue_complex:
200 if (src->minor >= element->value.complex->num_list ||
201 element->value.complex->list[src->minor]->which !=
202 Z_StringOrNumeric_numeric)
205 if (element->attributeSet && attributeSetP)
209 attrset = oid_getentbyoid (element->attributeSet);
210 *attributeSetP = attrset->value;
212 return *element->value.complex->list[src->minor-1]->u.numeric;
222 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
236 int (*cmp)(const void *p1, const void *p2);
243 static void heap_swap (struct trunc_info *ti, int i1, int i2)
248 ti->ptr[i1] = ti->ptr[i2];
252 static void heap_delete (struct trunc_info *ti)
254 int cur = 1, child = 2;
256 heap_swap (ti, 1, ti->heapnum--);
257 while (child <= ti->heapnum) {
258 if (child < ti->heapnum &&
259 (*ti->cmp)(ti->heap[ti->ptr[child]],
260 ti->heap[ti->ptr[1+child]]) > 0)
262 if ((*ti->cmp)(ti->heap[ti->ptr[cur]],
263 ti->heap[ti->ptr[child]]) > 0)
265 heap_swap (ti, cur, child);
274 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
278 cur = ++(ti->heapnum);
279 memcpy (ti->heap[ti->ptr[cur]], buf, ti->keysize);
280 ti->indx[ti->ptr[cur]] = indx;
282 while (parent && (*ti->cmp)(ti->heap[ti->ptr[parent]],
283 ti->heap[ti->ptr[cur]]) > 0)
285 heap_swap (ti, cur, parent);
292 struct trunc_info *heap_init (int size, int key_size,
293 int (*cmp)(const void *p1, const void *p2))
295 struct trunc_info *ti = xmalloc (sizeof(*ti));
300 ti->keysize = key_size;
302 ti->indx = xmalloc (size * sizeof(*ti->indx));
303 ti->heap = xmalloc (size * sizeof(*ti->heap));
304 ti->ptr = xmalloc (size * sizeof(*ti->ptr));
305 ti->swapbuf = xmalloc (ti->keysize);
306 ti->tmpbuf = xmalloc (ti->keysize);
307 ti->buf = xmalloc (size * ti->keysize);
308 for (i = size; --i >= 0; )
311 ti->heap[i] = ti->buf + ti->keysize * i;
316 static void heap_close (struct trunc_info *ti)
326 static RSET rset_trunc_r (ISAM isam, ISAM_P *isam_p, int from, int to,
331 rset_temp_parms parms;
333 parms.key_size = sizeof(struct it_key);
334 result = rset_create (rset_kind_temp, &parms);
335 result_rsfd = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
337 if (to - from > merge_chunk)
341 int i, i_add = (to-from)/merge_chunk + 1;
342 struct trunc_info *ti;
344 int rsmax = (to-from)/i_add + 1;
346 rset = xmalloc (sizeof(*rset) * rsmax);
347 rsfd = xmalloc (sizeof(*rsfd) * rsmax);
349 for (i = from; i < to; i += i_add)
352 rset[rscur] = rset_trunc_r (isam, isam_p, i, i+i_add,
355 rset[rscur] = rset_trunc_r (isam, isam_p, i, to,
359 ti = heap_init (rscur, sizeof(struct it_key), key_compare);
360 for (i = rscur; --i >= 0; )
362 rsfd[i] = rset_open (rset[i], RSETF_READ|RSETF_SORT_SYSNO);
363 if (rset_read (rset[i], rsfd[i], ti->tmpbuf))
364 heap_insert (ti, ti->tmpbuf, i);
367 rset_close (rset[i], rsfd[i]);
368 rset_delete (rset[i]);
373 int n = ti->indx[ti->ptr[1]];
375 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
379 if (!rset_read (rset[n], rsfd[n], ti->tmpbuf))
382 rset_close (rset[n], rsfd[n]);
383 rset_delete (rset[n]);
386 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
389 heap_insert (ti, ti->tmpbuf, n);
402 struct trunc_info *ti;
404 ispt = xmalloc (sizeof(*ispt) * (to-from));
406 ti = heap_init (to-from, sizeof(struct it_key),
408 for (i = to-from; --i >= 0; )
410 ispt[i] = is_position (isam, isam_p[from+i]);
411 if (is_readkey (ispt[i], ti->tmpbuf))
412 heap_insert (ti, ti->tmpbuf, i);
414 is_pt_free (ispt[i]);
418 int n = ti->indx[ti->ptr[1]];
420 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
422 /* section that preserve all keys */
424 if (is_readkey (ispt[n], ti->tmpbuf))
425 heap_insert (ti, ti->tmpbuf, n);
427 is_pt_free (ispt[n]);
429 /* section that preserve all keys with unique sysnos */
432 if (!is_readkey (ispt[n], ti->tmpbuf))
435 is_pt_free (ispt[n]);
438 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
441 heap_insert (ti, ti->tmpbuf, n);
450 rset_close (result, result_rsfd);
454 static int isam_trunc_cmp (const void *p1, const void *p2)
456 ISAM_P i1 = *(ISAM_P*) p1;
457 ISAM_P i2 = *(ISAM_P*) p2;
460 d = is_type (i1) - is_type (i2);
463 return is_block (i1) - is_block (i2);
466 static RSET rset_trunc (ISAM isam, ISAM_P *isam_p, int no)
469 qsort (isam_p, no, sizeof(*isam_p), isam_trunc_cmp);
470 return rset_trunc_r (isam, isam_p, 0, no, 100);
479 static void add_isam_p (const char *info, struct grep_info *p)
481 if (p->isam_p_indx == p->isam_p_size)
483 ISAM_P *new_isam_p_buf;
485 p->isam_p_size = 2*p->isam_p_size + 100;
486 new_isam_p_buf = xmalloc (sizeof(*new_isam_p_buf) *
490 memcpy (new_isam_p_buf, p->isam_p_buf,
491 p->isam_p_indx * sizeof(*p->isam_p_buf));
492 xfree (p->isam_p_buf);
494 p->isam_p_buf = new_isam_p_buf;
496 assert (*info == sizeof(*p->isam_p_buf));
497 memcpy (p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
501 static int grep_handle (char *name, const char *info, void *p)
503 logf (LOG_DEBUG, "dict name: %s", name);
504 add_isam_p (info, p);
508 static void gen_regular_rel (char *dst, int val, int islt)
516 sprintf (numstr, "%d", val);
517 for (w = strlen(numstr); --w >= 0; pos++)
536 strcpy (dst + dst_p, numstr);
537 dst_p = strlen(dst) - pos - 1;
565 for (i = 0; i<pos; i++)
578 for (i=1; i<pos; i++)
579 strcat (dst, "[0-9]?");
583 for (i = 0; i <= pos; i++)
584 strcat (dst, "[0-9]");
585 strcat (dst, "[0-9]*");
590 static int relational_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
591 const char *term_sub,
593 oid_value attributeSet,
594 struct grep_info *grep_info,
602 attr_init (&relation, zapt, 2);
603 relation_value = attr_find (&relation, NULL);
604 term_value = atoi (term_sub);
606 switch (relation_value)
611 logf (LOG_DEBUG, "Relation <");
612 gen_regular_rel (term_dict + strlen(term_dict), term_value-1, 1);
617 logf (LOG_DEBUG, "Relation <=");
618 gen_regular_rel (term_dict + strlen(term_dict), term_value, 1);
623 logf (LOG_DEBUG, "Relation >=");
624 gen_regular_rel (term_dict + strlen(term_dict), term_value, 0);
629 logf (LOG_DEBUG, "Relation >");
630 gen_regular_rel (term_dict + strlen(term_dict), term_value+1, 0);
635 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_dict);
636 r = dict_lookup_grep (zi->wordDict, term_dict, 0, grep_info, max_pos,
639 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
640 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
644 static void verbatim_char (int ch, int *indx, char *dst)
647 dst[(*indx)++] = '\\';
651 static int trunc_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
652 const char *term_sub,
653 oid_value attributeSet, struct grep_info *grep_info,
654 int num_bases, char **basenames)
656 char term_dict[2*IT_MAX_WORD+2];
657 int i, j, r, base_no;
659 int truncation_value;
662 oid_value curAttributeSet = attributeSet;
664 attr_init (&use, zapt, 1);
665 use_value = attr_find (&use, &curAttributeSet);
666 logf (LOG_DEBUG, "use value %d", use_value);
667 attr_init (&truncation, zapt, 5);
668 truncation_value = attr_find (&truncation, NULL);
669 logf (LOG_DEBUG, "truncation value %d", truncation_value);
674 for (base_no = 0; base_no < num_bases; base_no++)
677 int prefix_len = index_word_prefix_map (term_dict, curAttributeSet,
685 if (!relational_term (zi, zapt, term_sub, term_dict,
686 attributeSet, grep_info, &max_pos))
688 switch (truncation_value)
690 case -1: /* not specified */
691 case 100: /* do not truncate */
692 j = strlen(term_dict);
693 term_dict[j++] = '(';
694 for (i = 0; term_sub[i]; i++)
695 verbatim_char (term_sub[i], &j, term_dict);
696 strcpy (term_dict+j, ")");
697 r = dict_lookup_grep (zi->wordDict, term_dict, 0, grep_info,
698 &max_pos, grep_handle);
700 logf (LOG_WARN, "dict_lookup_grep err, trunc=none:%d", r);
702 case 1: /* right truncation */
703 j = strlen(term_dict);
704 term_dict[j++] = '(';
705 for (i = 0; term_sub[i]; i++)
706 verbatim_char (term_sub[i], &j, term_dict);
707 strcpy (term_dict+j, ".*)");
708 dict_lookup_grep (zi->wordDict, term_dict, 0, grep_info,
709 &max_pos, grep_handle);
711 case 2: /* left truncation */
712 case 3: /* left&right truncation */
715 case 101: /* process # in term */
716 j = strlen(term_dict);
717 term_dict[j++] = '(';
718 for (i=0; term_sub[i]; i++)
719 if (term_sub[i] == '#' && i > 2)
721 term_dict[j++] = '.';
722 term_dict[j++] = '*';
725 verbatim_char (term_sub[i], &j, term_dict);
726 strcpy (term_dict+j, ")");
727 r = dict_lookup_grep (zi->wordDict, term_dict, 0, grep_info,
728 &max_pos, grep_handle);
730 logf (LOG_WARN, "dict_lookup_grep err, trunc=#: %d",
733 case 102: /* regular expression */
734 sprintf (term_dict + strlen(term_dict), "(%s)", term_sub);
735 r = dict_lookup_grep (zi->wordDict, term_dict, 0, grep_info,
736 &max_pos, grep_handle);
738 logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
743 if (max_pos <= strlen(basenames[base_no]))
745 zi->errCode = 109; /* Database unavailable */
746 zi->errString = basenames[base_no];
750 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
754 static void trans_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
758 Z_Term *term = zapt->term;
760 sizez = term->u.general->len;
761 if (sizez > IT_MAX_WORD)
763 for (i = 0; i < sizez; i++)
764 termz[i] = index_char_cvt (term->u.general->buf[i]);
768 static RSET rpn_search_APT_relevance (ZServerInfo *zi,
769 Z_AttributesPlusTerm *zapt,
770 oid_value attributeSet,
771 int num_bases, char **basenames)
773 rset_relevance_parms parms;
774 char termz[IT_MAX_WORD+1];
775 char term_sub[IT_MAX_WORD+1];
776 struct grep_info grep_info;
777 char *p0 = termz, *p1 = NULL;
780 parms.key_size = sizeof(struct it_key);
782 parms.cmp = key_compare;
783 parms.is = zi->wordIsam;
785 if (zapt->term->which != Z_Term_general)
790 trans_term (zi, zapt, termz);
791 grep_info.isam_p_indx = 0;
792 grep_info.isam_p_size = 0;
793 grep_info.isam_p_buf = NULL;
796 if ((p1 = strchr (p0, ' ')))
798 memcpy (term_sub, p0, p1-p0);
799 term_sub[p1-p0] = '\0';
802 strcpy (term_sub, p0);
803 if (trunc_term (zi, zapt, term_sub, attributeSet, &grep_info,
804 num_bases, basenames))
812 parms.isam_positions = grep_info.isam_p_buf;
813 parms.no_isam_positions = grep_info.isam_p_indx;
814 if (grep_info.isam_p_indx > 0)
815 result = rset_create (rset_kind_relevance, &parms);
817 result = rset_create (rset_kind_null, NULL);
818 xfree (grep_info.isam_p_buf);
822 static RSET rpn_search_APT_word (ZServerInfo *zi,
823 Z_AttributesPlusTerm *zapt,
824 oid_value attributeSet,
825 int num_bases, char **basenames)
827 rset_isam_parms parms;
828 char termz[IT_MAX_WORD+1];
829 struct grep_info grep_info;
832 if (zapt->term->which != Z_Term_general)
837 trans_term (zi, zapt, termz);
839 grep_info.isam_p_indx = 0;
840 grep_info.isam_p_size = 0;
841 grep_info.isam_p_buf = NULL;
843 if (trunc_term (zi, zapt, termz, attributeSet, &grep_info,
844 num_bases, basenames))
846 if (grep_info.isam_p_indx < 1)
847 result = rset_create (rset_kind_null, NULL);
848 else if (grep_info.isam_p_indx == 1)
850 parms.is = zi->wordIsam;
851 parms.pos = *grep_info.isam_p_buf;
852 result = rset_create (rset_kind_isam, &parms);
855 result = rset_trunc (zi->wordIsam, grep_info.isam_p_buf,
856 grep_info.isam_p_indx);
857 xfree (grep_info.isam_p_buf);
861 static RSET rpn_prox (RSET *rset, int rset_no)
869 rset_temp_parms parms;
871 rsfd = xmalloc (sizeof(*rsfd)*rset_no);
872 more = xmalloc (sizeof(*more)*rset_no);
873 buf = xmalloc (sizeof(*buf)*rset_no);
875 for (i = 0; i<rset_no; i++)
877 buf[i] = xmalloc (sizeof(**buf));
878 rsfd[i] = rset_open (rset[i], RSETF_READ|RSETF_SORT_SYSNO);
879 if (!(more[i] = rset_read (rset[i], rsfd[i], buf[i])))
883 rset_close (rset[i], rsfd[i]);
890 return rset_create (rset_kind_null, NULL);
893 parms.key_size = sizeof (struct it_key);
894 result = rset_create (rset_kind_temp, &parms);
895 rsfd_result = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
899 for (i = 1; i<rset_no; i++)
908 cmp = key_compare (buf[i], buf[i-1]);
911 more[i-1] = rset_read (rset[i-1], rsfd[i-1], buf[i-1]);
916 if (buf[i-1]->seqno+1 != buf[i]->seqno)
918 more[i-1] = rset_read (rset[i-1], rsfd[i-1], buf[i-1]);
924 more[i] = rset_read (rset[i], rsfd[i], buf[i]);
930 rset_write (result, rsfd_result, buf[0]);
931 more[0] = rset_read (*rset, *rsfd, *buf);
935 for (i = 0; i<rset_no; i++)
937 rset_close (rset[i], rsfd[i]);
940 rset_close (result, rsfd_result);
947 static RSET rpn_search_APT_phrase (ZServerInfo *zi,
948 Z_AttributesPlusTerm *zapt,
949 oid_value attributeSet,
950 int num_bases, char **basenames)
952 char termz[IT_MAX_WORD+1];
953 char term_sub[IT_MAX_WORD+1];
954 char *p0 = termz, *p1 = NULL;
955 RSET rset[60], result;
957 struct grep_info grep_info;
959 if (zapt->term->which != Z_Term_general)
964 trans_term (zi, zapt, termz);
966 grep_info.isam_p_size = 0;
967 grep_info.isam_p_buf = NULL;
971 if ((p1 = strchr (p0, ' ')))
973 memcpy (term_sub, p0, p1-p0);
974 term_sub[p1-p0] = '\0';
977 strcpy (term_sub, p0);
979 grep_info.isam_p_indx = 0;
980 if (trunc_term (zi, zapt, term_sub, attributeSet, &grep_info,
981 num_bases, basenames))
983 if (grep_info.isam_p_indx == 0)
984 rset[rset_no] = rset_create (rset_kind_null, NULL);
985 else if (grep_info.isam_p_indx > 1)
986 rset[rset_no] = rset_trunc (zi->wordIsam,
987 grep_info.isam_p_buf,
988 grep_info.isam_p_indx);
991 rset_isam_parms parms;
993 parms.is = zi->wordIsam;
994 parms.pos = *grep_info.isam_p_buf;
995 rset[rset_no] = rset_create (rset_kind_isam, &parms);
997 assert (rset[rset_no]);
998 if (++rset_no >= sizeof(rset)/sizeof(*rset))
1003 while (*++p0 == ' ')
1006 xfree (grep_info.isam_p_buf);
1008 return rset_create (rset_kind_null, NULL);
1009 else if (rset_no == 1)
1012 result = rpn_prox (rset, rset_no);
1013 for (i = 0; i<rset_no; i++)
1014 rset_delete (rset[i]);
1018 static RSET rpn_search_APT_local (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1019 oid_value attributeSet)
1024 rset_temp_parms parms;
1025 char termz[IT_MAX_WORD+1];
1027 if (zapt->term->which != Z_Term_general)
1032 parms.key_size = sizeof (struct it_key);
1033 result = rset_create (rset_kind_temp, &parms);
1034 rsfd = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
1036 trans_term (zi, zapt, termz);
1037 key.sysno = atoi (termz);
1040 rset_write (result, rsfd, &key);
1041 rset_close (result, rsfd);
1045 static RSET rpn_search_APT (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1046 oid_value attributeSet,
1047 int num_bases, char **basenames)
1051 int relation_value, structure_value;
1053 attr_init (&relation, zapt, 2);
1054 attr_init (&structure, zapt, 4);
1056 relation_value = attr_find (&relation, NULL);
1057 structure_value = attr_find (&structure, NULL);
1058 switch (structure_value)
1061 if (relation_value == 102) /* relevance relation */
1062 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1063 num_bases, basenames);
1064 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1065 num_bases, basenames);
1066 case 1: /* phrase */
1067 if (relation_value == 102) /* relevance relation */
1068 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1069 num_bases, basenames);
1070 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1071 num_bases, basenames);
1074 if (relation_value == 102) /* relevance relation */
1075 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1076 num_bases, basenames);
1077 return rpn_search_APT_word (zi, zapt, attributeSet,
1078 num_bases, basenames);
1083 case 5: /* date - normalized */
1085 case 6: /* word list */
1086 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1087 num_bases, basenames);
1088 case 100: /* date - un-normalized */
1090 case 101: /* name - normalized */
1092 case 102: /* date - un-normalized */
1094 case 103: /* structure */
1098 case 105: /* free-form-text */
1099 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1100 num_bases, basenames);
1101 case 106: /* document-text */
1102 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1103 num_bases, basenames);
1104 case 107: /* local-number */
1105 return rpn_search_APT_local (zi, zapt, attributeSet);
1106 case 108: /* string */
1107 return rpn_search_APT_word (zi, zapt, attributeSet,
1108 num_bases, basenames);
1109 case 109: /* numeric string */
1116 static RSET rpn_search_ref (ZServerInfo *zi, Z_ResultSetId *resultSetId)
1120 if (!(s = resultSetGet (zi, resultSetId)))
1121 return rset_create (rset_kind_null, NULL);
1125 static RSET rpn_search_structure (ZServerInfo *zi, Z_RPNStructure *zs,
1126 oid_value attributeSet,
1127 int num_bases, char **basenames)
1130 if (zs->which == Z_RPNStructure_complex)
1132 rset_bool_parms bool_parms;
1135 bool_parms.rset_l = rpn_search_structure (zi, zs->u.complex->s1,
1137 num_bases, basenames);
1138 if (bool_parms.rset_l == NULL)
1140 if (rset_is_ranked(bool_parms.rset_l))
1142 bool_parms.rset_r = rpn_search_structure (zi, zs->u.complex->s2,
1144 num_bases, basenames);
1145 if (bool_parms.rset_r == NULL)
1147 rset_delete (bool_parms.rset_l);
1150 if (rset_is_ranked(bool_parms.rset_r))
1152 bool_parms.key_size = sizeof(struct it_key);
1153 bool_parms.cmp = key_compare;
1155 switch (zs->u.complex->roperator->which)
1157 case Z_Operator_and:
1158 r = rset_create (soft ? rset_kind_sand:rset_kind_and, &bool_parms);
1161 r = rset_create (soft ? rset_kind_sor:rset_kind_or, &bool_parms);
1163 case Z_Operator_and_not:
1164 r = rset_create (soft ? rset_kind_snot:rset_kind_not, &bool_parms);
1170 else if (zs->which == Z_RPNStructure_simple)
1172 if (zs->u.simple->which == Z_Operand_APT)
1174 logf (LOG_DEBUG, "rpn_search_APT");
1175 r = rpn_search_APT (zi, zs->u.simple->u.attributesPlusTerm,
1176 attributeSet, num_bases, basenames);
1178 else if (zs->u.simple->which == Z_Operand_resultSetId)
1180 logf (LOG_DEBUG, "rpn_search_ref");
1181 r = rpn_search_ref (zi, zs->u.simple->u.resultSetId);
1195 void count_set_save (RSET *r, int *count)
1202 rset_temp_parms parms;
1204 logf (LOG_DEBUG, "count_set_save");
1206 parms.key_size = sizeof(struct it_key);
1207 w = rset_create (rset_kind_temp, &parms);
1208 wfd = rset_open (w, RSETF_WRITE|RSETF_SORT_SYSNO);
1209 rfd = rset_open (*r, RSETF_READ|RSETF_SORT_SYSNO);
1210 while (rset_read (*r, rfd, &key))
1212 if (key.sysno != psysno)
1214 rset_write (w, wfd, &key);
1220 rset_close (*r, rfd);
1222 rset_close (w, wfd);
1224 logf (LOG_DEBUG, "%d keys, %d distinct sysnos", kno, *count);
1227 static void count_set (RSET r, int *count)
1234 logf (LOG_DEBUG, "count_set");
1236 rfd = rset_open (r, RSETF_READ|RSETF_SORT_SYSNO);
1237 while (rset_read (r, rfd, &key))
1239 if (key.sysno != psysno)
1246 rset_close (r, rfd);
1247 logf (LOG_DEBUG, "%d keys, %d distinct sysnos", kno, *count);
1250 int rpn_search (ZServerInfo *zi,
1251 Z_RPNQuery *rpn, int num_bases, char **basenames,
1252 const char *setname, int *hits)
1256 oid_value attributeSet;
1261 zi->errString = NULL;
1263 attrset = oid_getentbyoid (rpn->attributeSetId);
1264 attributeSet = attrset->value;
1265 rset = rpn_search_structure (zi, rpn->RPNStructure, attributeSet,
1266 num_bases, basenames);
1269 if (rset_is_volatile(rset))
1270 count_set_save(&rset,hits);
1272 count_set (rset, hits);
1273 resultSetAdd (zi, setname, 1, rset);
1275 logf (LOG_DEBUG, "search error: %d", zi->errCode);
1280 struct scan_entry *list;
1287 static int scan_handle (char *name, const char *info, int pos, void *client)
1289 int len_prefix, idx;
1292 struct scan_info *scan_info = client;
1294 rset_isam_parms parms;
1296 len_prefix = strlen(scan_info->prefix);
1297 if (memcmp (name, scan_info->prefix, len_prefix))
1300 idx = scan_info->after - pos + scan_info->before;
1303 scan_info->list[idx].term = odr_malloc (scan_info->odr,
1304 strlen(name + len_prefix)+1);
1305 strcpy (scan_info->list[idx].term, name + len_prefix);
1306 assert (*info == sizeof(isam_p));
1307 memcpy (&isam_p, info+1, sizeof(isam_p));
1308 parms.is = scan_info->isam;
1311 rset = rset_create (rset_kind_isam, &parms);
1312 count_set (rset, &scan_info->list[idx].occurrences);
1315 scan_info->list[idx].occurrences = 1;
1317 logf (LOG_DEBUG, "pos=%3d idx=%3d name=%s", pos, idx, name);
1322 static int dummy_handle (char *name, const char *info, void *p)
1327 int rpn_scan (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1328 int num_bases, char **basenames,
1329 int *position, int *num_entries, struct scan_entry **list,
1332 int i, j, sizez, max_pos;
1333 int pos = *position;
1334 int num = *num_entries;
1337 char termz[IT_MAX_WORD+20];
1340 Z_Term *term = zapt->term;
1341 struct scan_info scan_info;
1343 logf (LOG_DEBUG, "scan, position = %d, num = %d", pos, num);
1347 scan_info.before = before = pos-1;
1348 scan_info.after = after = 1+num-pos;
1349 scan_info.odr = zi->odr;
1351 logf (LOG_DEBUG, "scan, before = %d, after = %d", before, after);
1353 scan_info.isam = zi->wordIsam;
1354 scan_info.list = odr_malloc (zi->odr, (before+after)*
1355 sizeof(*scan_info.list));
1356 for (j = 0; j<before+after; j++)
1357 scan_info.list[j].term = NULL;
1358 attr_init (&use, zapt, 1);
1359 use_value = attr_find (&use, NULL);
1360 logf (LOG_DEBUG, "use value %d", use_value);
1362 if (use_value == -1)
1364 i = index_word_prefix (termz, 1, use_value, *basenames);
1366 dict_lookup_grep (zi->wordDict, termz, 0, NULL, &max_pos,
1368 if (max_pos <= strlen(*basenames))
1370 zi->errString = *basenames;
1371 return zi->errCode = 109; /* Database unavailable */
1373 strcpy (scan_info.prefix, termz);
1374 sizez = term->u.general->len;
1375 if (sizez > IT_MAX_WORD)
1376 sizez = IT_MAX_WORD;
1377 for (j = 0; j<sizez; j++)
1378 termz[j+i] = index_char_cvt (term->u.general->buf[j]);
1381 dict_scan (zi->wordDict, termz, &before, &after, &scan_info, scan_handle);
1383 *status = BEND_SCAN_SUCCESS;
1385 for (i = 0; i<scan_info.after; i++)
1386 if (scan_info.list[scan_info.before+scan_info.after-i-1].term)
1390 *status = BEND_SCAN_PARTIAL;
1392 for (i = 0; i<scan_info.before; i++)
1393 if (scan_info.list[i].term)
1396 *status = BEND_SCAN_PARTIAL;
1400 *list = scan_info.list+i; /* list is set to first 'real' entry */
1402 if (*num_entries == 0) /* signal 'unsupported use-attribute' */
1403 zi->errCode = 114; /* if no entries was found */
1404 logf (LOG_DEBUG, "position = %d, num_entries = %d",
1405 *position, *num_entries);
1407 logf (LOG_DEBUG, "scan error: %d", zi->errCode);