2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.32 1995-10-27 14:00:11 adam
8 * Implemented detection of database availability.
10 * Revision 1.31 1995/10/17 18:02:10 adam
11 * New feature: databases. Implemented as prefix to words in dictionary.
13 * Revision 1.30 1995/10/16 09:32:38 adam
14 * More work on relational op.
16 * Revision 1.29 1995/10/13 16:01:49 adam
19 * Revision 1.28 1995/10/13 12:26:43 adam
20 * Optimization of truncation.
22 * Revision 1.27 1995/10/12 17:07:22 adam
25 * Revision 1.26 1995/10/12 12:40:54 adam
26 * Bug fixes in rpn_prox.
28 * Revision 1.25 1995/10/10 13:59:24 adam
29 * Function rset_open changed its wflag parameter to general flags.
31 * Revision 1.24 1995/10/09 16:18:37 adam
32 * Function dict_lookup_grep got extra client data parameter.
34 * Revision 1.23 1995/10/06 16:33:37 adam
35 * Use attribute mappings.
37 * Revision 1.22 1995/10/06 15:07:39 adam
38 * Structure 'local-number' handled.
40 * Revision 1.21 1995/10/06 13:52:06 adam
41 * Bug fixes. Handler may abort further scanning.
43 * Revision 1.20 1995/10/06 11:06:33 adam
44 * Scan entries include 'occurrences' now.
46 * Revision 1.19 1995/10/06 10:43:56 adam
47 * Scan added. 'occurrences' in scan entries not set yet.
49 * Revision 1.18 1995/10/04 16:57:20 adam
50 * Key input and merge sort in one pass.
52 * Revision 1.17 1995/10/04 12:55:17 adam
53 * Bug fix in ranked search. Use=Any keys inserted.
55 * Revision 1.16 1995/10/02 16:24:40 adam
56 * Use attribute actually used in search requests.
58 * Revision 1.15 1995/10/02 15:18:52 adam
59 * New member in recRetrieveCtrl: diagnostic.
61 * Revision 1.14 1995/09/28 12:10:32 adam
62 * Bug fixes. Field prefix used in queries.
64 * Revision 1.13 1995/09/18 14:17:50 adam
67 * Revision 1.12 1995/09/15 14:45:21 adam
71 * Revision 1.11 1995/09/14 11:53:27 adam
72 * First work on regular expressions/truncations.
74 * Revision 1.10 1995/09/11 15:23:26 adam
75 * More work on relevance search.
77 * Revision 1.9 1995/09/11 13:09:35 adam
78 * More work on relevance feedback.
80 * Revision 1.8 1995/09/08 14:52:27 adam
81 * Minor changes. Dictionary is lower case now.
83 * Revision 1.7 1995/09/07 13:58:36 adam
84 * New parameter: result-set file descriptor (RSFD) to support multiple
85 * positions within the same result-set.
86 * Boolean operators: and, or, not implemented.
87 * Result-set references.
89 * Revision 1.6 1995/09/06 16:11:18 adam
90 * Option: only one word key per file.
92 * Revision 1.5 1995/09/06 10:33:04 adam
93 * More work on present. Some log messages removed.
95 * Revision 1.4 1995/09/05 15:28:40 adam
96 * More work on search engine.
98 * Revision 1.3 1995/09/04 15:20:22 adam
101 * Revision 1.2 1995/09/04 12:33:43 adam
102 * Various cleanup. YAZ util used instead.
104 * Revision 1.1 1995/09/04 09:10:40 adam
105 * More work on index add/del/update.
106 * Merge sort implemented.
107 * Initial work on z39 server.
115 #include <attribute.h>
123 int index_word_prefix_map (char *string, oid_value attrSet, int attrUse,
128 logf (LOG_DEBUG, "oid_value attrSet = %d, attrUse = %d", attrSet, attrUse);
129 attp = att_getentbyatt (attrSet, attrUse);
132 logf (LOG_DEBUG, "ord=%d", attp->attset_ordinal);
133 return index_word_prefix (string, attp->attset_ordinal,
134 attp->local_attribute, basename);
138 * attr_print: log attributes
140 static void attr_print (Z_AttributesPlusTerm *t)
143 for (of = 0; of < t->num_attributes; of++)
145 Z_AttributeElement *element;
146 element = t->attributeList[of];
148 switch (element->which)
150 case Z_AttributeValue_numeric:
151 logf (LOG_DEBUG, "attributeType=%d value=%d",
152 *element->attributeType,
153 *element->value.numeric);
155 case Z_AttributeValue_complex:
156 logf (LOG_DEBUG, "attributeType=%d complex",
157 *element->attributeType);
158 for (i = 0; i<element->value.complex->num_list; i++)
160 if (element->value.complex->list[i]->which ==
161 Z_StringOrNumeric_string)
162 logf (LOG_DEBUG, " string: '%s'",
163 element->value.complex->list[i]->u.string);
164 else if (element->value.complex->list[i]->which ==
165 Z_StringOrNumeric_numeric)
166 logf (LOG_DEBUG, " numeric: '%d'",
167 *element->value.complex->list[i]->u.numeric);
180 Z_AttributesPlusTerm *zapt;
183 static int attr_find (AttrType *src, oid_value *attributeSetP)
185 while (src->major < src->zapt->num_attributes)
187 Z_AttributeElement *element;
189 element = src->zapt->attributeList[src->major];
190 if (src->type == *element->attributeType)
192 switch (element->which)
194 case Z_AttributeValue_numeric:
196 if (element->attributeSet && attributeSetP)
200 attrset = oid_getentbyoid (element->attributeSet);
201 *attributeSetP = attrset->value;
203 return *element->value.numeric;
205 case Z_AttributeValue_complex:
206 if (src->minor >= element->value.complex->num_list ||
207 element->value.complex->list[src->minor]->which !=
208 Z_StringOrNumeric_numeric)
211 if (element->attributeSet && attributeSetP)
215 attrset = oid_getentbyoid (element->attributeSet);
216 *attributeSetP = attrset->value;
218 return *element->value.complex->list[src->minor-1]->u.numeric;
228 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
242 int (*cmp)(const void *p1, const void *p2);
249 static void heap_swap (struct trunc_info *ti, int i1, int i2)
254 ti->ptr[i1] = ti->ptr[i2];
258 static void heap_delete (struct trunc_info *ti)
260 int cur = 1, child = 2;
262 heap_swap (ti, 1, ti->heapnum--);
263 while (child <= ti->heapnum) {
264 if (child < ti->heapnum &&
265 (*ti->cmp)(ti->heap[ti->ptr[child]],
266 ti->heap[ti->ptr[1+child]]) > 0)
268 if ((*ti->cmp)(ti->heap[ti->ptr[cur]],
269 ti->heap[ti->ptr[child]]) > 0)
271 heap_swap (ti, cur, child);
280 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
284 cur = ++(ti->heapnum);
285 memcpy (ti->heap[ti->ptr[cur]], buf, ti->keysize);
286 ti->indx[ti->ptr[cur]] = indx;
288 while (parent && (*ti->cmp)(ti->heap[ti->ptr[parent]],
289 ti->heap[ti->ptr[cur]]) > 0)
291 heap_swap (ti, cur, parent);
298 struct trunc_info *heap_init (int size, int key_size,
299 int (*cmp)(const void *p1, const void *p2))
301 struct trunc_info *ti = xmalloc (sizeof(*ti));
306 ti->keysize = key_size;
308 ti->indx = xmalloc (size * sizeof(*ti->indx));
309 ti->heap = xmalloc (size * sizeof(*ti->heap));
310 ti->ptr = xmalloc (size * sizeof(*ti->ptr));
311 ti->swapbuf = xmalloc (ti->keysize);
312 ti->tmpbuf = xmalloc (ti->keysize);
313 ti->buf = xmalloc (size * ti->keysize);
314 for (i = size; --i >= 0; )
317 ti->heap[i] = ti->buf + ti->keysize * i;
322 static void heap_close (struct trunc_info *ti)
332 static RSET rset_trunc_r (ISAM isam, ISAM_P *isam_p, int from, int to,
337 rset_temp_parms parms;
339 parms.key_size = sizeof(struct it_key);
340 result = rset_create (rset_kind_temp, &parms);
341 result_rsfd = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
343 if (to - from > merge_chunk)
347 int i, i_add = (to-from)/merge_chunk + 1;
348 struct trunc_info *ti;
350 int rsmax = (to-from)/i_add + 1;
352 rset = xmalloc (sizeof(*rset) * rsmax);
353 rsfd = xmalloc (sizeof(*rsfd) * rsmax);
355 for (i = from; i < to; i += i_add)
358 rset[rscur] = rset_trunc_r (isam, isam_p, i, i+i_add,
361 rset[rscur] = rset_trunc_r (isam, isam_p, i, to,
365 ti = heap_init (rscur, sizeof(struct it_key), key_compare);
366 for (i = rscur; --i >= 0; )
368 rsfd[i] = rset_open (rset[i], RSETF_READ|RSETF_SORT_SYSNO);
369 if (rset_read (rset[i], rsfd[i], ti->tmpbuf))
370 heap_insert (ti, ti->tmpbuf, i);
373 rset_close (rset[i], rsfd[i]);
374 rset_delete (rset[i]);
379 int n = ti->indx[ti->ptr[1]];
381 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
385 if (!rset_read (rset[n], rsfd[n], ti->tmpbuf))
388 rset_close (rset[n], rsfd[n]);
389 rset_delete (rset[n]);
392 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
395 heap_insert (ti, ti->tmpbuf, n);
408 struct trunc_info *ti;
410 ispt = xmalloc (sizeof(*ispt) * (to-from));
412 ti = heap_init (to-from, sizeof(struct it_key),
414 for (i = to-from; --i >= 0; )
416 ispt[i] = is_position (isam, isam_p[from+i]);
417 if (is_readkey (ispt[i], ti->tmpbuf))
418 heap_insert (ti, ti->tmpbuf, i);
420 is_pt_free (ispt[i]);
424 int n = ti->indx[ti->ptr[1]];
426 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
428 /* section that preserve all keys */
430 if (is_readkey (ispt[n], ti->tmpbuf))
431 heap_insert (ti, ti->tmpbuf, n);
433 is_pt_free (ispt[n]);
435 /* section that preserve all keys with unique sysnos */
438 if (!is_readkey (ispt[n], ti->tmpbuf))
441 is_pt_free (ispt[n]);
444 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
447 heap_insert (ti, ti->tmpbuf, n);
456 rset_close (result, result_rsfd);
460 static int isam_trunc_cmp (const void *p1, const void *p2)
462 ISAM_P i1 = *(ISAM_P*) p1;
463 ISAM_P i2 = *(ISAM_P*) p2;
466 d = is_type (i1) - is_type (i2);
469 return is_block (i1) - is_block (i2);
472 static RSET rset_trunc (ISAM isam, ISAM_P *isam_p, int no)
475 qsort (isam_p, no, sizeof(*isam_p), isam_trunc_cmp);
476 return rset_trunc_r (isam, isam_p, 0, no, 100);
485 static void add_isam_p (const char *info, struct grep_info *p)
487 if (p->isam_p_indx == p->isam_p_size)
489 ISAM_P *new_isam_p_buf;
491 p->isam_p_size = 2*p->isam_p_size + 100;
492 new_isam_p_buf = xmalloc (sizeof(*new_isam_p_buf) *
496 memcpy (new_isam_p_buf, p->isam_p_buf,
497 p->isam_p_indx * sizeof(*p->isam_p_buf));
498 xfree (p->isam_p_buf);
500 p->isam_p_buf = new_isam_p_buf;
502 assert (*info == sizeof(*p->isam_p_buf));
503 memcpy (p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
507 static int grep_handle (Dict_char *name, const char *info, void *p)
509 logf (LOG_DEBUG, "dict name: %s", name);
510 add_isam_p (info, p);
514 static void gen_regular_rel (char *dst, int val, int islt)
522 sprintf (numstr, "%d", val);
523 for (w = strlen(numstr); --w >= 0; pos++)
542 strcpy (dst + dst_p, numstr);
543 dst_p = strlen(dst) - pos - 1;
571 for (i = 0; i<pos; i++)
584 for (i=1; i<pos; i++)
585 strcat (dst, "[0-9]?");
589 for (i = 0; i <= pos; i++)
590 strcat (dst, "[0-9]");
591 strcat (dst, "[0-9]*");
596 static int relational_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
597 const char *term_sub,
599 oid_value attributeSet,
600 struct grep_info *grep_info,
608 attr_init (&relation, zapt, 2);
609 relation_value = attr_find (&relation, NULL);
610 term_value = atoi (term_sub);
612 switch (relation_value)
617 logf (LOG_DEBUG, "Relation <");
618 gen_regular_rel (term_dict + strlen(term_dict), term_value-1, 1);
623 logf (LOG_DEBUG, "Relation <=");
624 gen_regular_rel (term_dict + strlen(term_dict), term_value, 1);
629 logf (LOG_DEBUG, "Relation >=");
630 gen_regular_rel (term_dict + strlen(term_dict), term_value, 0);
635 logf (LOG_DEBUG, "Relation >");
636 gen_regular_rel (term_dict + strlen(term_dict), term_value+1, 0);
641 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_dict);
642 r = dict_lookup_grep (zi->wordDict, term_dict, 0, grep_info, max_pos,
645 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
646 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
650 static int trunc_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
651 const char *term_sub,
652 oid_value attributeSet, struct grep_info *grep_info,
653 int num_bases, char **basenames)
655 char term_dict[2*IT_MAX_WORD+2];
656 int i, j, r, base_no;
658 int truncation_value;
661 oid_value curAttributeSet = attributeSet;
663 attr_init (&use, zapt, 1);
664 use_value = attr_find (&use, &curAttributeSet);
665 logf (LOG_DEBUG, "use value %d", use_value);
666 attr_init (&truncation, zapt, 5);
667 truncation_value = attr_find (&truncation, NULL);
668 logf (LOG_DEBUG, "truncation value %d", truncation_value);
673 for (base_no = 0; base_no < num_bases; base_no++)
676 int prefix_len = index_word_prefix_map (term_dict, curAttributeSet,
684 if (!relational_term (zi, zapt, term_sub, term_dict,
685 attributeSet, grep_info, &max_pos))
687 switch (truncation_value)
689 case -1: /* not specified */
690 case 100: /* do not truncate */
691 strcat (term_dict, "(");
692 strcat (term_dict, term_sub);
693 strcat (term_dict, ")");
694 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_dict);
695 r = dict_lookup_grep (zi->wordDict, term_dict, 0, grep_info,
696 &max_pos, grep_handle);
698 logf (LOG_WARN, "dict_lookup_grep err, trunc=none:%d", r);
700 case 1: /* right truncation */
701 strcat (term_dict, term_sub);
702 strcat (term_dict, ".*");
703 dict_lookup_grep (zi->wordDict, term_dict, 0, grep_info,
704 &max_pos, grep_handle);
706 case 2: /* left truncation */
707 case 3: /* left&right truncation */
710 case 101: /* process # in term */
711 for (j = strlen(term_dict), i = 0; term_sub[i] && i < 2; i++)
712 term_dict[j++] = term_sub[i];
713 for (; term_sub[i]; i++)
714 if (term_sub[i] == '#')
716 term_dict[j++] = '.';
717 term_dict[j++] = '*';
720 term_dict[j++] = term_sub[i];
722 r = dict_lookup_grep (zi->wordDict, term_dict, 0, grep_info,
723 &max_pos, grep_handle);
725 logf (LOG_WARN, "dict_lookup_grep err, trunc=#: %d",
728 case 102: /* regular expression */
729 strcat (term_dict, "(");
730 strcat (term_dict, term_sub);
731 strcat (term_dict, ")");
732 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_dict);
733 r = dict_lookup_grep (zi->wordDict, term_dict, 0, grep_info,
734 &max_pos, grep_handle);
736 logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
741 logf (LOG_DEBUG, "max_pos = %d", max_pos);
742 if (max_pos <= strlen(basenames[base_no]))
744 zi->errCode = 109; /* Database unavailable */
745 zi->errString = basenames[base_no];
749 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
753 static void trans_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
757 Z_Term *term = zapt->term;
759 sizez = term->u.general->len;
760 if (sizez > IT_MAX_WORD)
762 for (i = 0; i < sizez; i++)
763 termz[i] = index_char_cvt (term->u.general->buf[i]);
767 static RSET rpn_search_APT_relevance (ZServerInfo *zi,
768 Z_AttributesPlusTerm *zapt,
769 oid_value attributeSet,
770 int num_bases, char **basenames)
772 rset_relevance_parms parms;
773 char termz[IT_MAX_WORD+1];
774 char term_sub[IT_MAX_WORD+1];
775 struct grep_info grep_info;
776 char *p0 = termz, *p1 = NULL;
779 parms.key_size = sizeof(struct it_key);
781 parms.cmp = key_compare;
782 parms.is = zi->wordIsam;
784 if (zapt->term->which != Z_Term_general)
789 trans_term (zi, zapt, termz);
790 grep_info.isam_p_indx = 0;
791 grep_info.isam_p_size = 0;
792 grep_info.isam_p_buf = NULL;
795 if ((p1 = strchr (p0, ' ')))
797 memcpy (term_sub, p0, p1-p0);
798 term_sub[p1-p0] = '\0';
801 strcpy (term_sub, p0);
802 if (trunc_term (zi, zapt, term_sub, attributeSet, &grep_info,
803 num_bases, basenames))
811 parms.isam_positions = grep_info.isam_p_buf;
812 parms.no_isam_positions = grep_info.isam_p_indx;
813 if (grep_info.isam_p_indx > 0)
814 result = rset_create (rset_kind_relevance, &parms);
816 result = rset_create (rset_kind_null, NULL);
817 xfree (grep_info.isam_p_buf);
821 static RSET rpn_search_APT_word (ZServerInfo *zi,
822 Z_AttributesPlusTerm *zapt,
823 oid_value attributeSet,
824 int num_bases, char **basenames)
826 rset_isam_parms parms;
827 char termz[IT_MAX_WORD+1];
828 struct grep_info grep_info;
831 if (zapt->term->which != Z_Term_general)
836 trans_term (zi, zapt, termz);
838 grep_info.isam_p_indx = 0;
839 grep_info.isam_p_size = 0;
840 grep_info.isam_p_buf = NULL;
842 if (trunc_term (zi, zapt, termz, attributeSet, &grep_info,
843 num_bases, basenames))
845 if (grep_info.isam_p_indx < 1)
846 result = rset_create (rset_kind_null, NULL);
847 else if (grep_info.isam_p_indx == 1)
849 parms.is = zi->wordIsam;
850 parms.pos = *grep_info.isam_p_buf;
851 result = rset_create (rset_kind_isam, &parms);
854 result = rset_trunc (zi->wordIsam, grep_info.isam_p_buf,
855 grep_info.isam_p_indx);
856 xfree (grep_info.isam_p_buf);
860 static RSET rpn_prox (RSET *rset, int rset_no)
868 rset_temp_parms parms;
870 rsfd = xmalloc (sizeof(*rsfd)*rset_no);
871 more = xmalloc (sizeof(*more)*rset_no);
872 buf = xmalloc (sizeof(*buf)*rset_no);
874 for (i = 0; i<rset_no; i++)
876 buf[i] = xmalloc (sizeof(**buf));
877 rsfd[i] = rset_open (rset[i], RSETF_READ|RSETF_SORT_SYSNO);
878 if (!(more[i] = rset_read (rset[i], rsfd[i], buf[i])))
882 rset_close (rset[i], rsfd[i]);
889 return rset_create (rset_kind_null, NULL);
892 parms.key_size = sizeof (struct it_key);
893 result = rset_create (rset_kind_temp, &parms);
894 rsfd_result = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
898 for (i = 1; i<rset_no; i++)
907 cmp = key_compare (buf[i], buf[i-1]);
910 more[i-1] = rset_read (rset[i-1], rsfd[i-1], buf[i-1]);
915 if (buf[i-1]->seqno+1 != buf[i]->seqno)
917 more[i-1] = rset_read (rset[i-1], rsfd[i-1], buf[i-1]);
923 more[i] = rset_read (rset[i], rsfd[i], buf[i]);
929 rset_write (result, rsfd_result, buf[0]);
930 more[0] = rset_read (*rset, *rsfd, *buf);
934 for (i = 0; i<rset_no; i++)
936 rset_close (rset[i], rsfd[i]);
939 rset_close (result, rsfd_result);
946 static RSET rpn_search_APT_phrase (ZServerInfo *zi,
947 Z_AttributesPlusTerm *zapt,
948 oid_value attributeSet,
949 int num_bases, char **basenames)
951 char termz[IT_MAX_WORD+1];
952 char term_sub[IT_MAX_WORD+1];
953 char *p0 = termz, *p1 = NULL;
954 RSET rset[60], result;
956 struct grep_info grep_info;
958 if (zapt->term->which != Z_Term_general)
963 trans_term (zi, zapt, termz);
965 grep_info.isam_p_size = 0;
966 grep_info.isam_p_buf = NULL;
970 if ((p1 = strchr (p0, ' ')))
972 memcpy (term_sub, p0, p1-p0);
973 term_sub[p1-p0] = '\0';
976 strcpy (term_sub, p0);
978 grep_info.isam_p_indx = 0;
979 if (trunc_term (zi, zapt, term_sub, attributeSet, &grep_info,
980 num_bases, basenames))
982 if (grep_info.isam_p_indx == 0)
983 rset[rset_no] = rset_create (rset_kind_null, NULL);
984 else if (grep_info.isam_p_indx > 1)
985 rset[rset_no] = rset_trunc (zi->wordIsam,
986 grep_info.isam_p_buf,
987 grep_info.isam_p_indx);
990 rset_isam_parms parms;
992 parms.is = zi->wordIsam;
993 parms.pos = *grep_info.isam_p_buf;
994 rset[rset_no] = rset_create (rset_kind_isam, &parms);
996 assert (rset[rset_no]);
997 if (++rset_no >= sizeof(rset)/sizeof(*rset))
1002 while (*++p0 == ' ')
1005 xfree (grep_info.isam_p_buf);
1007 return rset_create (rset_kind_null, NULL);
1008 else if (rset_no == 1)
1011 result = rpn_prox (rset, rset_no);
1012 for (i = 0; i<rset_no; i++)
1013 rset_delete (rset[i]);
1017 static RSET rpn_search_APT_local (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1018 oid_value attributeSet)
1023 rset_temp_parms parms;
1024 char termz[IT_MAX_WORD+1];
1026 if (zapt->term->which != Z_Term_general)
1031 parms.key_size = sizeof (struct it_key);
1032 result = rset_create (rset_kind_temp, &parms);
1033 rsfd = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
1035 trans_term (zi, zapt, termz);
1036 key.sysno = atoi (termz);
1039 rset_write (result, rsfd, &key);
1040 rset_close (result, rsfd);
1044 static RSET rpn_search_APT (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1045 oid_value attributeSet,
1046 int num_bases, char **basenames)
1050 int relation_value, structure_value;
1052 attr_init (&relation, zapt, 2);
1053 attr_init (&structure, zapt, 4);
1055 relation_value = attr_find (&relation, NULL);
1056 structure_value = attr_find (&structure, NULL);
1057 switch (structure_value)
1060 if (relation_value == 102) /* relevance relation */
1061 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1062 num_bases, basenames);
1063 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1064 num_bases, basenames);
1065 case 1: /* phrase */
1066 if (relation_value == 102) /* relevance relation */
1067 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1068 num_bases, basenames);
1069 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1070 num_bases, basenames);
1073 if (relation_value == 102) /* relevance relation */
1074 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1075 num_bases, basenames);
1076 return rpn_search_APT_word (zi, zapt, attributeSet,
1077 num_bases, basenames);
1082 case 5: /* date - normalized */
1084 case 6: /* word list */
1085 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1086 num_bases, basenames);
1087 case 100: /* date - un-normalized */
1089 case 101: /* name - normalized */
1091 case 102: /* date - un-normalized */
1093 case 103: /* structure */
1097 case 105: /* free-form-text */
1098 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1099 num_bases, basenames);
1100 case 106: /* document-text */
1101 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1102 num_bases, basenames);
1103 case 107: /* local-number */
1104 return rpn_search_APT_local (zi, zapt, attributeSet);
1105 case 108: /* string */
1106 return rpn_search_APT_word (zi, zapt, attributeSet,
1107 num_bases, basenames);
1108 case 109: /* numeric string */
1115 static RSET rpn_search_ref (ZServerInfo *zi, Z_ResultSetId *resultSetId)
1119 if (!(s = resultSetGet (zi, resultSetId)))
1120 return rset_create (rset_kind_null, NULL);
1124 static RSET rpn_search_structure (ZServerInfo *zi, Z_RPNStructure *zs,
1125 oid_value attributeSet,
1126 int num_bases, char **basenames)
1129 if (zs->which == Z_RPNStructure_complex)
1131 rset_bool_parms bool_parms;
1133 bool_parms.rset_l = rpn_search_structure (zi, zs->u.complex->s1,
1135 num_bases, basenames);
1136 if (bool_parms.rset_l == NULL)
1138 bool_parms.rset_r = rpn_search_structure (zi, zs->u.complex->s2,
1140 num_bases, basenames);
1141 if (bool_parms.rset_r == NULL)
1143 rset_delete (bool_parms.rset_l);
1146 bool_parms.key_size = sizeof(struct it_key);
1147 bool_parms.cmp = key_compare;
1149 switch (zs->u.complex->operator->which)
1151 case Z_Operator_and:
1152 r = rset_create (rset_kind_and, &bool_parms);
1155 r = rset_create (rset_kind_or, &bool_parms);
1157 case Z_Operator_and_not:
1158 r = rset_create (rset_kind_not, &bool_parms);
1164 else if (zs->which == Z_RPNStructure_simple)
1166 if (zs->u.simple->which == Z_Operand_APT)
1168 logf (LOG_DEBUG, "rpn_search_APT");
1169 r = rpn_search_APT (zi, zs->u.simple->u.attributesPlusTerm,
1170 attributeSet, num_bases, basenames);
1172 else if (zs->u.simple->which == Z_Operand_resultSetId)
1174 logf (LOG_DEBUG, "rpn_search_ref");
1175 r = rpn_search_ref (zi, zs->u.simple->u.resultSetId);
1189 static void count_set (RSET r, int *count)
1195 logf (LOG_DEBUG, "rpn_save_set");
1197 rfd = rset_open (r, RSETF_READ|RSETF_SORT_SYSNO);
1198 while (rset_read (r, rfd, &key))
1200 if (key.sysno != psysno)
1206 rset_close (r, rfd);
1207 logf (LOG_DEBUG, "%d distinct sysnos", *count);
1210 int rpn_search (ZServerInfo *zi,
1211 Z_RPNQuery *rpn, int num_bases, char **basenames,
1212 const char *setname, int *hits)
1216 oid_value attributeSet;
1219 zi->errString = NULL;
1221 attrset = oid_getentbyoid (rpn->attributeSetId);
1222 attributeSet = attrset->value;
1224 rset = rpn_search_structure (zi, rpn->RPNStructure, attributeSet,
1225 num_bases, basenames);
1228 count_set (rset, hits);
1229 resultSetAdd (zi, setname, 1, rset);
1231 logf (LOG_DEBUG, "search error: %d", zi->errCode);
1236 struct scan_entry *list;
1243 static int scan_handle (Dict_char *name, const char *info, int pos,
1246 int len_prefix, idx;
1249 struct scan_info *scan_info = client;
1251 rset_isam_parms parms;
1253 len_prefix = strlen(scan_info->prefix);
1254 if (memcmp (name, scan_info->prefix, len_prefix))
1257 idx = scan_info->after - pos + scan_info->before;
1260 scan_info->list[idx].term = odr_malloc (scan_info->odr,
1261 strlen(name + len_prefix)+1);
1262 strcpy (scan_info->list[idx].term, name + len_prefix);
1263 assert (*info == sizeof(isam_p));
1264 memcpy (&isam_p, info+1, sizeof(isam_p));
1265 parms.is = scan_info->isam;
1268 rset = rset_create (rset_kind_isam, &parms);
1269 count_set (rset, &scan_info->list[idx].occurrences);
1272 scan_info->list[idx].occurrences = 1;
1274 logf (LOG_DEBUG, "pos=%3d idx=%3d name=%s", pos, idx, name);
1279 static int dummy_handle (Dict_char *name, const char *info, void *p)
1284 int rpn_scan (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1285 int num_bases, char **basenames,
1286 int *position, int *num_entries, struct scan_entry **list,
1289 int i, j, sizez, max_pos;
1290 int pos = *position;
1291 int num = *num_entries;
1294 char termz[IT_MAX_WORD+20];
1297 Z_Term *term = zapt->term;
1298 struct scan_info scan_info;
1300 logf (LOG_DEBUG, "scan, position = %d, num = %d", pos, num);
1304 scan_info.before = before = pos-1;
1305 scan_info.after = after = 1+num-pos;
1306 scan_info.odr = zi->odr;
1308 logf (LOG_DEBUG, "scan, before = %d, after = %d", before, after);
1310 scan_info.isam = zi->wordIsam;
1311 scan_info.list = odr_malloc (zi->odr, (before+after)*
1312 sizeof(*scan_info.list));
1313 for (j = 0; j<before+after; j++)
1314 scan_info.list[j].term = NULL;
1315 attr_init (&use, zapt, 1);
1316 use_value = attr_find (&use, NULL);
1317 logf (LOG_DEBUG, "use value %d", use_value);
1319 if (use_value == -1)
1321 i = index_word_prefix (termz, 1, use_value, *basenames);
1323 dict_lookup_grep (zi->wordDict, termz, 0, NULL, &max_pos,
1325 if (max_pos <= strlen(*basenames))
1327 zi->errString = *basenames;
1328 return zi->errCode = 109; /* Database unavailable */
1330 strcpy (scan_info.prefix, termz);
1331 sizez = term->u.general->len;
1332 if (sizez > IT_MAX_WORD)
1333 sizez = IT_MAX_WORD;
1334 for (j = 0; j<sizez; j++)
1335 termz[j+i] = index_char_cvt (term->u.general->buf[j]);
1338 dict_scan (zi->wordDict, termz, &before, &after, &scan_info, scan_handle);
1340 *status = BEND_SCAN_SUCCESS;
1342 for (i = 0; i<scan_info.after; i++)
1343 if (scan_info.list[scan_info.before+scan_info.after-i-1].term)
1347 *status = BEND_SCAN_PARTIAL;
1349 for (i = 0; i<scan_info.before; i++)
1350 if (scan_info.list[i].term)
1353 *status = BEND_SCAN_PARTIAL;
1357 *list = scan_info.list+i; /* list is set to first 'real' entry */
1359 if (*num_entries == 0) /* signal 'unsupported use-attribute' */
1360 zi->errCode = 114; /* if no entries was found */
1361 logf (LOG_DEBUG, "position = %d, num_entries = %d",
1362 *position, *num_entries);
1364 logf (LOG_DEBUG, "scan error: %d", zi->errCode);