2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.44 1996-05-14 06:16:44 adam
8 * Compact use/set bytes used in search service.
10 * Revision 1.43 1996/05/09 09:54:43 adam
11 * Server supports maps from one logical attributes to a list of physical
13 * The extraction process doesn't make space consuming 'any' keys.
15 * Revision 1.42 1996/05/09 07:28:56 quinn
16 * Work towards phrases and multiple registers
18 * Revision 1.41 1996/03/20 09:36:43 adam
19 * Function dict_lookup_grep got extra parameter, init_pos, which marks
20 * from which position in pattern approximate pattern matching should occur.
21 * Approximate pattern matching is used in relevance=re-2.
23 * Revision 1.40 1996/02/02 13:44:44 adam
24 * The public dictionary functions simply use char instead of Dict_char
25 * to represent search strings. Dict_char is used internally only.
27 * Revision 1.39 1996/01/03 16:22:13 quinn
30 * Revision 1.38 1995/12/11 09:12:55 adam
31 * The rec_get function returns NULL if record doesn't exist - will
32 * happen in the server if the result set records have been deleted since
33 * the creation of the set (i.e. the search).
34 * The server saves a result temporarily if it is 'volatile', i.e. the
35 * set is register dependent.
37 * Revision 1.37 1995/12/06 15:05:28 adam
38 * More verbose in count_set.
40 * Revision 1.36 1995/12/06 12:41:27 adam
41 * New command 'stat' for the index program.
42 * Filenames can be read from stdin by specifying '-'.
43 * Bug fix/enhancement of the transformation from terms to regular
44 * expressons in the search engine.
46 * Revision 1.35 1995/11/27 09:29:00 adam
47 * Bug fixes regarding conversion to regular expressions.
49 * Revision 1.34 1995/11/16 17:00:56 adam
50 * Better logging of rpn query.
52 * Revision 1.33 1995/11/01 13:58:28 quinn
53 * Moving data1 to yaz/retrieval
55 * Revision 1.32 1995/10/27 14:00:11 adam
56 * Implemented detection of database availability.
58 * Revision 1.31 1995/10/17 18:02:10 adam
59 * New feature: databases. Implemented as prefix to words in dictionary.
61 * Revision 1.30 1995/10/16 09:32:38 adam
62 * More work on relational op.
64 * Revision 1.29 1995/10/13 16:01:49 adam
67 * Revision 1.28 1995/10/13 12:26:43 adam
68 * Optimization of truncation.
70 * Revision 1.27 1995/10/12 17:07:22 adam
73 * Revision 1.26 1995/10/12 12:40:54 adam
74 * Bug fixes in rpn_prox.
76 * Revision 1.25 1995/10/10 13:59:24 adam
77 * Function rset_open changed its wflag parameter to general flags.
79 * Revision 1.24 1995/10/09 16:18:37 adam
80 * Function dict_lookup_grep got extra client data parameter.
82 * Revision 1.23 1995/10/06 16:33:37 adam
83 * Use attribute mappings.
85 * Revision 1.22 1995/10/06 15:07:39 adam
86 * Structure 'local-number' handled.
88 * Revision 1.21 1995/10/06 13:52:06 adam
89 * Bug fixes. Handler may abort further scanning.
91 * Revision 1.20 1995/10/06 11:06:33 adam
92 * Scan entries include 'occurrences' now.
94 * Revision 1.19 1995/10/06 10:43:56 adam
95 * Scan added. 'occurrences' in scan entries not set yet.
97 * Revision 1.18 1995/10/04 16:57:20 adam
98 * Key input and merge sort in one pass.
100 * Revision 1.17 1995/10/04 12:55:17 adam
101 * Bug fix in ranked search. Use=Any keys inserted.
103 * Revision 1.16 1995/10/02 16:24:40 adam
104 * Use attribute actually used in search requests.
106 * Revision 1.15 1995/10/02 15:18:52 adam
107 * New member in recRetrieveCtrl: diagnostic.
109 * Revision 1.14 1995/09/28 12:10:32 adam
110 * Bug fixes. Field prefix used in queries.
112 * Revision 1.13 1995/09/18 14:17:50 adam
115 * Revision 1.12 1995/09/15 14:45:21 adam
117 * Work on truncation.
119 * Revision 1.11 1995/09/14 11:53:27 adam
120 * First work on regular expressions/truncations.
122 * Revision 1.10 1995/09/11 15:23:26 adam
123 * More work on relevance search.
125 * Revision 1.9 1995/09/11 13:09:35 adam
126 * More work on relevance feedback.
128 * Revision 1.8 1995/09/08 14:52:27 adam
129 * Minor changes. Dictionary is lower case now.
131 * Revision 1.7 1995/09/07 13:58:36 adam
132 * New parameter: result-set file descriptor (RSFD) to support multiple
133 * positions within the same result-set.
134 * Boolean operators: and, or, not implemented.
135 * Result-set references.
137 * Revision 1.6 1995/09/06 16:11:18 adam
138 * Option: only one word key per file.
140 * Revision 1.5 1995/09/06 10:33:04 adam
141 * More work on present. Some log messages removed.
143 * Revision 1.4 1995/09/05 15:28:40 adam
144 * More work on search engine.
146 * Revision 1.3 1995/09/04 15:20:22 adam
149 * Revision 1.2 1995/09/04 12:33:43 adam
150 * Various cleanup. YAZ util used instead.
152 * Revision 1.1 1995/09/04 09:10:40 adam
153 * More work on index add/del/update.
154 * Merge sort implemented.
155 * Initial work on z39 server.
164 #include "attribute.h"
172 int index_word_prefix_map (char *string, oid_value attrSet, int attrUse,
177 logf (LOG_DEBUG, "oid_value attrSet = %d, attrUse = %d", attrSet, attrUse);
178 attp = att_getentbyatt (attrSet, attrUse);
181 logf (LOG_DEBUG, "ord=%d", attp->attset_ordinal);
182 return index_word_prefix (string, attp->attset_ordinal,
183 attp->local_attributes->local, basename);
190 Z_AttributesPlusTerm *zapt;
193 static int attr_find (AttrType *src, oid_value *attributeSetP)
195 while (src->major < src->zapt->num_attributes)
197 Z_AttributeElement *element;
199 element = src->zapt->attributeList[src->major];
200 if (src->type == *element->attributeType)
202 switch (element->which)
204 case Z_AttributeValue_numeric:
206 if (element->attributeSet && attributeSetP)
210 attrset = oid_getentbyoid (element->attributeSet);
211 *attributeSetP = attrset->value;
213 return *element->value.numeric;
215 case Z_AttributeValue_complex:
216 if (src->minor >= element->value.complex->num_list ||
217 element->value.complex->list[src->minor]->which !=
218 Z_StringOrNumeric_numeric)
221 if (element->attributeSet && attributeSetP)
225 attrset = oid_getentbyoid (element->attributeSet);
226 *attributeSetP = attrset->value;
228 return *element->value.complex->list[src->minor-1]->u.numeric;
238 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
252 int (*cmp)(const void *p1, const void *p2);
259 static void heap_swap (struct trunc_info *ti, int i1, int i2)
264 ti->ptr[i1] = ti->ptr[i2];
268 static void heap_delete (struct trunc_info *ti)
270 int cur = 1, child = 2;
272 heap_swap (ti, 1, ti->heapnum--);
273 while (child <= ti->heapnum) {
274 if (child < ti->heapnum &&
275 (*ti->cmp)(ti->heap[ti->ptr[child]],
276 ti->heap[ti->ptr[1+child]]) > 0)
278 if ((*ti->cmp)(ti->heap[ti->ptr[cur]],
279 ti->heap[ti->ptr[child]]) > 0)
281 heap_swap (ti, cur, child);
290 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
294 cur = ++(ti->heapnum);
295 memcpy (ti->heap[ti->ptr[cur]], buf, ti->keysize);
296 ti->indx[ti->ptr[cur]] = indx;
298 while (parent && (*ti->cmp)(ti->heap[ti->ptr[parent]],
299 ti->heap[ti->ptr[cur]]) > 0)
301 heap_swap (ti, cur, parent);
308 struct trunc_info *heap_init (int size, int key_size,
309 int (*cmp)(const void *p1, const void *p2))
311 struct trunc_info *ti = xmalloc (sizeof(*ti));
316 ti->keysize = key_size;
318 ti->indx = xmalloc (size * sizeof(*ti->indx));
319 ti->heap = xmalloc (size * sizeof(*ti->heap));
320 ti->ptr = xmalloc (size * sizeof(*ti->ptr));
321 ti->swapbuf = xmalloc (ti->keysize);
322 ti->tmpbuf = xmalloc (ti->keysize);
323 ti->buf = xmalloc (size * ti->keysize);
324 for (i = size; --i >= 0; )
327 ti->heap[i] = ti->buf + ti->keysize * i;
332 static void heap_close (struct trunc_info *ti)
342 static RSET rset_trunc_r (ISAM isam, ISAM_P *isam_p, int from, int to,
347 rset_temp_parms parms;
349 parms.key_size = sizeof(struct it_key);
350 result = rset_create (rset_kind_temp, &parms);
351 result_rsfd = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
353 if (to - from > merge_chunk)
357 int i, i_add = (to-from)/merge_chunk + 1;
358 struct trunc_info *ti;
360 int rsmax = (to-from)/i_add + 1;
362 rset = xmalloc (sizeof(*rset) * rsmax);
363 rsfd = xmalloc (sizeof(*rsfd) * rsmax);
365 for (i = from; i < to; i += i_add)
368 rset[rscur] = rset_trunc_r (isam, isam_p, i, i+i_add,
371 rset[rscur] = rset_trunc_r (isam, isam_p, i, to,
375 ti = heap_init (rscur, sizeof(struct it_key), key_compare);
376 for (i = rscur; --i >= 0; )
378 rsfd[i] = rset_open (rset[i], RSETF_READ|RSETF_SORT_SYSNO);
379 if (rset_read (rset[i], rsfd[i], ti->tmpbuf))
380 heap_insert (ti, ti->tmpbuf, i);
383 rset_close (rset[i], rsfd[i]);
384 rset_delete (rset[i]);
389 int n = ti->indx[ti->ptr[1]];
391 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
395 if (!rset_read (rset[n], rsfd[n], ti->tmpbuf))
398 rset_close (rset[n], rsfd[n]);
399 rset_delete (rset[n]);
402 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
405 heap_insert (ti, ti->tmpbuf, n);
418 struct trunc_info *ti;
420 ispt = xmalloc (sizeof(*ispt) * (to-from));
422 ti = heap_init (to-from, sizeof(struct it_key),
424 for (i = to-from; --i >= 0; )
426 ispt[i] = is_position (isam, isam_p[from+i]);
427 if (is_readkey (ispt[i], ti->tmpbuf))
428 heap_insert (ti, ti->tmpbuf, i);
430 is_pt_free (ispt[i]);
434 int n = ti->indx[ti->ptr[1]];
436 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
438 /* section that preserve all keys */
440 if (is_readkey (ispt[n], ti->tmpbuf))
441 heap_insert (ti, ti->tmpbuf, n);
443 is_pt_free (ispt[n]);
445 /* section that preserve all keys with unique sysnos */
448 if (!is_readkey (ispt[n], ti->tmpbuf))
451 is_pt_free (ispt[n]);
454 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
457 heap_insert (ti, ti->tmpbuf, n);
466 rset_close (result, result_rsfd);
470 static int isam_trunc_cmp (const void *p1, const void *p2)
472 ISAM_P i1 = *(ISAM_P*) p1;
473 ISAM_P i2 = *(ISAM_P*) p2;
476 d = is_type (i1) - is_type (i2);
479 return is_block (i1) - is_block (i2);
482 static RSET rset_trunc (ISAM isam, ISAM_P *isam_p, int no)
485 qsort (isam_p, no, sizeof(*isam_p), isam_trunc_cmp);
486 return rset_trunc_r (isam, isam_p, 0, no, 100);
495 static void add_isam_p (const char *info, struct grep_info *p)
497 if (p->isam_p_indx == p->isam_p_size)
499 ISAM_P *new_isam_p_buf;
501 p->isam_p_size = 2*p->isam_p_size + 100;
502 new_isam_p_buf = xmalloc (sizeof(*new_isam_p_buf) *
506 memcpy (new_isam_p_buf, p->isam_p_buf,
507 p->isam_p_indx * sizeof(*p->isam_p_buf));
508 xfree (p->isam_p_buf);
510 p->isam_p_buf = new_isam_p_buf;
512 assert (*info == sizeof(*p->isam_p_buf));
513 memcpy (p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
517 static int grep_handle (char *name, const char *info, void *p)
519 logf (LOG_DEBUG, "dict name: %s", name);
520 add_isam_p (info, p);
524 static void gen_regular_rel (char *dst, int val, int islt)
532 sprintf (numstr, "%d", val);
533 for (w = strlen(numstr); --w >= 0; pos++)
552 strcpy (dst + dst_p, numstr);
553 dst_p = strlen(dst) - pos - 1;
581 for (i = 0; i<pos; i++)
594 for (i=1; i<pos; i++)
595 strcat (dst, "[0-9]?");
599 for (i = 0; i <= pos; i++)
600 strcat (dst, "[0-9]");
601 strcat (dst, "[0-9]*");
606 static int relational_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
607 const char *term_sub,
609 oid_value attributeSet,
610 struct grep_info *grep_info,
618 attr_init (&relation, zapt, 2);
619 relation_value = attr_find (&relation, NULL);
620 term_value = atoi (term_sub);
622 switch (relation_value)
627 logf (LOG_DEBUG, "Relation <");
628 gen_regular_rel (term_dict + strlen(term_dict), term_value-1, 1);
633 logf (LOG_DEBUG, "Relation <=");
634 gen_regular_rel (term_dict + strlen(term_dict), term_value, 1);
639 logf (LOG_DEBUG, "Relation >=");
640 gen_regular_rel (term_dict + strlen(term_dict), term_value, 0);
645 logf (LOG_DEBUG, "Relation >");
646 gen_regular_rel (term_dict + strlen(term_dict), term_value+1, 0);
651 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_dict);
652 r = dict_lookup_grep (zi->wordDict, term_dict, 0, grep_info, max_pos,
655 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
656 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
660 static void verbatim_char (int ch, int *indx, char *dst)
663 dst[(*indx)++] = '\\';
667 static int field_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
668 const char *term_sub,
669 oid_value attributeSet, struct grep_info *grep_info,
670 int num_bases, char **basenames)
672 char term_dict[2*IT_MAX_WORD+2];
673 int i, j, r, base_no;
675 int truncation_value;
678 oid_value curAttributeSet = attributeSet;
680 attr_init (&use, zapt, 1);
681 use_value = attr_find (&use, &curAttributeSet);
682 logf (LOG_DEBUG, "use value %d", use_value);
683 attr_init (&truncation, zapt, 5);
684 truncation_value = attr_find (&truncation, NULL);
685 logf (LOG_DEBUG, "truncation value %d", truncation_value);
690 for (base_no = 0; base_no < num_bases; base_no++)
694 data1_local_attribute *local_attr;
695 int max_pos, prefix_len = 0;
697 attp = att_getentbyatt (curAttributeSet, use_value);
703 if (zebTargetInfo_curDatabase (zi->zti, basenames[base_no]))
705 zi->errCode = 109; /* Database unavailable */
706 zi->errString = basenames[base_no];
708 for (local_attr = attp->local_attributes; local_attr;
709 local_attr = local_attr->next)
713 ord = zebTargetInfo_lookupSU (zi->zti, attp->attset_ordinal,
718 term_dict[prefix_len++] = '|';
720 term_dict[prefix_len++] = '(';
721 if ((ord >= 'A' && ord <= 'Z') || (ord >= 'a' && ord <= 'z'))
722 term_dict[prefix_len++] = ord;
725 term_dict[prefix_len++] = '\\';
726 term_dict[prefix_len++] = ord;
734 term_dict[prefix_len++] = ')';
735 term_dict[prefix_len] = '\0';
736 if (!relational_term (zi, zapt, term_sub, term_dict,
737 attributeSet, grep_info, &max_pos))
742 switch (truncation_value)
744 case -1: /* not specified */
745 case 100: /* do not truncate */
746 term_dict[j++] = '(';
747 for (i = 0; term_sub[i]; i++)
748 verbatim_char (term_sub[i], &j, term_dict);
749 strcpy (term_dict+j, ")");
750 r = dict_lookup_grep (zi->wordDict, term_dict, 0, grep_info,
751 &max_pos, 0, grep_handle);
753 logf (LOG_WARN, "dict_lookup_grep err, trunc=none:%d", r);
755 case 1: /* right truncation */
756 term_dict[j++] = '(';
757 for (i = 0; term_sub[i]; i++)
758 verbatim_char (term_sub[i], &j, term_dict);
759 strcpy (term_dict+j, ".*)");
760 dict_lookup_grep (zi->wordDict, term_dict, 0, grep_info,
761 &max_pos, 0, grep_handle);
763 case 2: /* left truncation */
764 case 3: /* left&right truncation */
767 case 101: /* process # in term */
768 term_dict[j++] = '(';
769 for (i=0; term_sub[i]; i++)
770 if (term_sub[i] == '#' && i > 2)
772 term_dict[j++] = '.';
773 term_dict[j++] = '*';
776 verbatim_char (term_sub[i], &j, term_dict);
777 strcpy (term_dict+j, ")");
778 r = dict_lookup_grep (zi->wordDict, term_dict, 0, grep_info,
779 &max_pos, 0, grep_handle);
781 logf (LOG_WARN, "dict_lookup_grep err, trunc=#: %d",
784 case 102: /* regular expression */
785 sprintf (term_dict + j, "(%s)", term_sub);
786 r = dict_lookup_grep (zi->wordDict, term_dict, 0, grep_info,
787 &max_pos, 0, grep_handle);
789 logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
792 case 103: /* regular expression with error correction */
795 if (*cp == '*' && cp[1] && cp[2])
800 sprintf (term_dict + j, "(%s)", cp);
801 r = dict_lookup_grep (zi->wordDict, term_dict, r, grep_info,
802 &max_pos, j, grep_handle);
804 logf (LOG_WARN, "dict_lookup_grep err, trunc=eregular: %d",
813 data1_local_attribute *local_attr;
816 attp = att_getentbyatt (curAttributeSet, use_value);
822 for (local_attr = attp->local_attributes; local_attr;
823 local_attr = local_attr->next)
825 prefix_len = index_word_prefix (term_dict, attp->attset_ordinal,
829 if (!relational_term (zi, zapt, term_sub, term_dict,
830 attributeSet, grep_info, &max_pos))
835 switch (truncation_value)
837 case -1: /* not specified */
838 case 100: /* do not truncate */
839 term_dict[j++] = '(';
840 for (i = 0; term_sub[i]; i++)
841 verbatim_char (term_sub[i], &j, term_dict);
842 strcpy (term_dict+j, ")");
843 r = dict_lookup_grep (zi->wordDict, term_dict, 0, grep_info,
844 &max_pos, 0, grep_handle);
846 logf (LOG_WARN, "dict_lookup_grep err, trunc=none:%d", r);
848 case 1: /* right truncation */
849 term_dict[j++] = '(';
850 for (i = 0; term_sub[i]; i++)
851 verbatim_char (term_sub[i], &j, term_dict);
852 strcpy (term_dict+j, ".*)");
853 dict_lookup_grep (zi->wordDict, term_dict, 0, grep_info,
854 &max_pos, 0, grep_handle);
856 case 2: /* left truncation */
857 case 3: /* left&right truncation */
860 case 101: /* process # in term */
861 term_dict[j++] = '(';
862 for (i=0; term_sub[i]; i++)
863 if (term_sub[i] == '#' && i > 2)
865 term_dict[j++] = '.';
866 term_dict[j++] = '*';
869 verbatim_char (term_sub[i], &j, term_dict);
870 strcpy (term_dict+j, ")");
871 r = dict_lookup_grep (zi->wordDict, term_dict, 0, grep_info,
872 &max_pos, 0, grep_handle);
874 logf (LOG_WARN, "dict_lookup_grep err, trunc=#: %d",
877 case 102: /* regular expression */
878 sprintf (term_dict + j, "(%s)", term_sub);
879 r = dict_lookup_grep (zi->wordDict, term_dict, 0, grep_info,
880 &max_pos, 0, grep_handle);
882 logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
885 case 103: /* regular expression with error correction */
888 if (*cp == '*' && cp[1] && cp[2])
893 sprintf (term_dict + j, "(%s)", cp);
894 r = dict_lookup_grep (zi->wordDict, term_dict, r, grep_info,
895 &max_pos, j, grep_handle);
897 logf (LOG_WARN, "dict_lookup_grep err, trunc=eregular: %d",
902 if (max_pos <= strlen(basenames[base_no]))
904 zi->errCode = 109; /* Database unavailable */
905 zi->errString = basenames[base_no];
910 int prefix_len = index_word_prefix_map (term_dict, curAttributeSet,
918 if (!relational_term (zi, zapt, term_sub, term_dict,
919 attributeSet, grep_info, &max_pos))
924 switch (truncation_value)
926 case -1: /* not specified */
927 case 100: /* do not truncate */
928 term_dict[j++] = '(';
929 for (i = 0; term_sub[i]; i++)
930 verbatim_char (term_sub[i], &j, term_dict);
931 strcpy (term_dict+j, ")");
932 r = dict_lookup_grep (zi->wordDict, term_dict, 0, grep_info,
933 &max_pos, 0, grep_handle);
935 logf (LOG_WARN, "dict_lookup_grep err, trunc=none:%d", r);
937 case 1: /* right truncation */
938 term_dict[j++] = '(';
939 for (i = 0; term_sub[i]; i++)
940 verbatim_char (term_sub[i], &j, term_dict);
941 strcpy (term_dict+j, ".*)");
942 dict_lookup_grep (zi->wordDict, term_dict, 0, grep_info,
943 &max_pos, 0, grep_handle);
945 case 2: /* left truncation */
946 case 3: /* left&right truncation */
949 case 101: /* process # in term */
950 term_dict[j++] = '(';
951 for (i=0; term_sub[i]; i++)
952 if (term_sub[i] == '#' && i > 2)
954 term_dict[j++] = '.';
955 term_dict[j++] = '*';
958 verbatim_char (term_sub[i], &j, term_dict);
959 strcpy (term_dict+j, ")");
960 r = dict_lookup_grep (zi->wordDict, term_dict, 0, grep_info,
961 &max_pos, 0, grep_handle);
963 logf (LOG_WARN, "dict_lookup_grep err, trunc=#: %d",
966 case 102: /* regular expression */
967 sprintf (term_dict + j, "(%s)", term_sub);
968 r = dict_lookup_grep (zi->wordDict, term_dict, 0, grep_info,
969 &max_pos, 0, grep_handle);
971 logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
974 case 103: /* regular expression with error correction */
977 if (*cp == '*' && cp[1] && cp[2])
982 sprintf (term_dict + j, "(%s)", cp);
983 r = dict_lookup_grep (zi->wordDict, term_dict, r, grep_info,
984 &max_pos, j, grep_handle);
986 logf (LOG_WARN, "dict_lookup_grep err, trunc=eregular: %d",
991 if (max_pos <= strlen(basenames[base_no]))
993 zi->errCode = 109; /* Database unavailable */
994 zi->errString = basenames[base_no];
1000 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1004 static void trans_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1005 int regType, char *termz)
1008 Z_Term *term = zapt->term;
1010 sizez = term->u.general->len;
1011 if (sizez > IT_MAX_WORD-1)
1012 sizez = IT_MAX_WORD-1;
1014 for (i = 0; i < sizez; i++)
1015 termz[i+1] = index_char_cvt (term->u.general->buf[i]);
1019 static RSET rpn_search_APT_relevance (ZServerInfo *zi,
1020 Z_AttributesPlusTerm *zapt,
1021 oid_value attributeSet,
1022 int num_bases, char **basenames)
1024 rset_relevance_parms parms;
1025 char termz[IT_MAX_WORD+1];
1026 char term_sub[IT_MAX_WORD+1];
1027 struct grep_info grep_info;
1028 char *p0 = termz, *p1 = NULL;
1031 parms.key_size = sizeof(struct it_key);
1032 parms.max_rec = 100;
1033 parms.cmp = key_compare;
1034 parms.is = zi->wordIsam;
1036 if (zapt->term->which != Z_Term_general)
1041 trans_term (zi, zapt, 'w', termz);
1043 grep_info.isam_p_indx = 0;
1044 grep_info.isam_p_size = 0;
1045 grep_info.isam_p_buf = NULL;
1048 if ((p1 = strchr (p0, ' ')))
1050 memcpy (term_sub, p0, p1-p0);
1051 term_sub[p1-p0] = '\0';
1054 strcpy (term_sub, p0);
1055 if (field_term (zi, zapt, term_sub, attributeSet, &grep_info,
1056 num_bases, basenames))
1061 while (*++p0 == ' ')
1064 parms.isam_positions = grep_info.isam_p_buf;
1065 parms.no_isam_positions = grep_info.isam_p_indx;
1066 if (grep_info.isam_p_indx > 0)
1067 result = rset_create (rset_kind_relevance, &parms);
1069 result = rset_create (rset_kind_null, NULL);
1070 xfree (grep_info.isam_p_buf);
1074 static RSET rpn_search_APT_cphrase (ZServerInfo *zi,
1075 Z_AttributesPlusTerm *zapt,
1076 oid_value attributeSet,
1077 int num_bases, char **basenames)
1079 rset_isam_parms parms;
1080 char termz[IT_MAX_WORD+1];
1081 struct grep_info grep_info;
1084 if (zapt->term->which != Z_Term_general)
1089 trans_term (zi, zapt, 'p', termz);
1091 grep_info.isam_p_indx = 0;
1092 grep_info.isam_p_size = 0;
1093 grep_info.isam_p_buf = NULL;
1095 if (field_term (zi, zapt, termz, attributeSet, &grep_info,
1096 num_bases, basenames))
1098 if (grep_info.isam_p_indx < 1)
1099 result = rset_create (rset_kind_null, NULL);
1100 else if (grep_info.isam_p_indx == 1)
1102 parms.is = zi->wordIsam;
1103 parms.pos = *grep_info.isam_p_buf;
1104 result = rset_create (rset_kind_isam, &parms);
1107 result = rset_trunc (zi->wordIsam, grep_info.isam_p_buf,
1108 grep_info.isam_p_indx);
1109 xfree (grep_info.isam_p_buf);
1112 static RSET rpn_search_APT_word (ZServerInfo *zi,
1113 Z_AttributesPlusTerm *zapt,
1114 oid_value attributeSet,
1115 int num_bases, char **basenames)
1117 rset_isam_parms parms;
1118 char termz[IT_MAX_WORD+1];
1119 struct grep_info grep_info;
1122 if (zapt->term->which != Z_Term_general)
1127 trans_term (zi, zapt, 'w', termz);
1129 grep_info.isam_p_indx = 0;
1130 grep_info.isam_p_size = 0;
1131 grep_info.isam_p_buf = NULL;
1133 if (field_term (zi, zapt, termz, attributeSet, &grep_info,
1134 num_bases, basenames))
1136 if (grep_info.isam_p_indx < 1)
1137 result = rset_create (rset_kind_null, NULL);
1138 else if (grep_info.isam_p_indx == 1)
1140 parms.is = zi->wordIsam;
1141 parms.pos = *grep_info.isam_p_buf;
1142 result = rset_create (rset_kind_isam, &parms);
1145 result = rset_trunc (zi->wordIsam, grep_info.isam_p_buf,
1146 grep_info.isam_p_indx);
1147 xfree (grep_info.isam_p_buf);
1151 static RSET rpn_prox (RSET *rset, int rset_no)
1156 struct it_key **buf;
1159 rset_temp_parms parms;
1161 rsfd = xmalloc (sizeof(*rsfd)*rset_no);
1162 more = xmalloc (sizeof(*more)*rset_no);
1163 buf = xmalloc (sizeof(*buf)*rset_no);
1165 for (i = 0; i<rset_no; i++)
1167 buf[i] = xmalloc (sizeof(**buf));
1168 rsfd[i] = rset_open (rset[i], RSETF_READ|RSETF_SORT_SYSNO);
1169 if (!(more[i] = rset_read (rset[i], rsfd[i], buf[i])))
1173 rset_close (rset[i], rsfd[i]);
1180 return rset_create (rset_kind_null, NULL);
1183 parms.key_size = sizeof (struct it_key);
1184 result = rset_create (rset_kind_temp, &parms);
1185 rsfd_result = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
1189 for (i = 1; i<rset_no; i++)
1198 cmp = key_compare (buf[i], buf[i-1]);
1201 more[i-1] = rset_read (rset[i-1], rsfd[i-1], buf[i-1]);
1206 if (buf[i-1]->seqno+1 != buf[i]->seqno)
1208 more[i-1] = rset_read (rset[i-1], rsfd[i-1], buf[i-1]);
1214 more[i] = rset_read (rset[i], rsfd[i], buf[i]);
1220 rset_write (result, rsfd_result, buf[0]);
1221 more[0] = rset_read (*rset, *rsfd, *buf);
1225 for (i = 0; i<rset_no; i++)
1227 rset_close (rset[i], rsfd[i]);
1230 rset_close (result, rsfd_result);
1237 static RSET rpn_search_APT_phrase (ZServerInfo *zi,
1238 Z_AttributesPlusTerm *zapt,
1239 oid_value attributeSet,
1240 int num_bases, char **basenames)
1242 char termz[IT_MAX_WORD+1];
1243 char term_sub[IT_MAX_WORD+1];
1244 char *p0 = termz, *p1 = NULL;
1245 RSET rset[60], result;
1247 struct grep_info grep_info;
1249 if (zapt->term->which != Z_Term_general)
1254 trans_term (zi, zapt, 'w', termz);
1256 grep_info.isam_p_size = 0;
1257 grep_info.isam_p_buf = NULL;
1261 if ((p1 = strchr (p0, ' ')))
1263 memcpy (term_sub, p0, p1-p0);
1264 term_sub[p1-p0] = '\0';
1267 strcpy (term_sub, p0);
1269 grep_info.isam_p_indx = 0;
1270 if (field_term (zi, zapt, term_sub, attributeSet, &grep_info,
1271 num_bases, basenames))
1273 if (grep_info.isam_p_indx == 0)
1274 rset[rset_no] = rset_create (rset_kind_null, NULL);
1275 else if (grep_info.isam_p_indx > 1)
1276 rset[rset_no] = rset_trunc (zi->wordIsam,
1277 grep_info.isam_p_buf,
1278 grep_info.isam_p_indx);
1281 rset_isam_parms parms;
1283 parms.is = zi->wordIsam;
1284 parms.pos = *grep_info.isam_p_buf;
1285 rset[rset_no] = rset_create (rset_kind_isam, &parms);
1287 assert (rset[rset_no]);
1288 if (++rset_no >= sizeof(rset)/sizeof(*rset))
1293 while (*++p0 == ' ')
1296 xfree (grep_info.isam_p_buf);
1298 return rset_create (rset_kind_null, NULL);
1299 else if (rset_no == 1)
1302 result = rpn_prox (rset, rset_no);
1303 for (i = 0; i<rset_no; i++)
1304 rset_delete (rset[i]);
1308 static RSET rpn_search_APT_local (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1309 oid_value attributeSet)
1314 rset_temp_parms parms;
1315 char termz[IT_MAX_WORD+1];
1317 if (zapt->term->which != Z_Term_general)
1322 parms.key_size = sizeof (struct it_key);
1323 result = rset_create (rset_kind_temp, &parms);
1324 rsfd = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
1326 trans_term (zi, zapt, 'w', termz);
1328 key.sysno = atoi (termz+1);
1331 rset_write (result, rsfd, &key);
1332 rset_close (result, rsfd);
1336 static RSET rpn_search_APT (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1337 oid_value attributeSet,
1338 int num_bases, char **basenames)
1342 AttrType completeness;
1343 int relation_value, structure_value, completeness_value;
1345 attr_init (&relation, zapt, 2);
1346 attr_init (&structure, zapt, 4);
1347 attr_init (&completeness, zapt, 6);
1349 relation_value = attr_find (&relation, NULL);
1350 structure_value = attr_find (&structure, NULL);
1351 completeness_value = attr_find (&completeness, NULL);
1352 switch (structure_value)
1355 if (relation_value == 102) /* relevance relation */
1356 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1357 num_bases, basenames);
1358 if (completeness_value == 2 || completeness_value == 3)
1359 return rpn_search_APT_cphrase (zi, zapt, attributeSet,
1360 num_bases, basenames);
1361 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1362 num_bases, basenames);
1363 case 1: /* phrase */
1364 if (relation_value == 102) /* relevance relation */
1365 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1366 num_bases, basenames);
1367 if (completeness_value == 2 || completeness_value == 3)
1368 return rpn_search_APT_cphrase (zi, zapt, attributeSet,
1369 num_bases, basenames);
1370 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1371 num_bases, basenames);
1374 if (relation_value == 102) /* relevance relation */
1375 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1376 num_bases, basenames);
1377 return rpn_search_APT_word (zi, zapt, attributeSet,
1378 num_bases, basenames);
1383 case 5: /* date - normalized */
1385 case 6: /* word list */
1386 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1387 num_bases, basenames);
1388 case 100: /* date - un-normalized */
1390 case 101: /* name - normalized */
1392 case 102: /* date - un-normalized */
1394 case 103: /* structure */
1398 case 105: /* free-form-text */
1399 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1400 num_bases, basenames);
1401 case 106: /* document-text */
1402 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1403 num_bases, basenames);
1404 case 107: /* local-number */
1405 return rpn_search_APT_local (zi, zapt, attributeSet);
1406 case 108: /* string */
1407 return rpn_search_APT_word (zi, zapt, attributeSet,
1408 num_bases, basenames);
1409 case 109: /* numeric string */
1416 static RSET rpn_search_ref (ZServerInfo *zi, Z_ResultSetId *resultSetId)
1420 if (!(s = resultSetGet (zi, resultSetId)))
1421 return rset_create (rset_kind_null, NULL);
1425 static RSET rpn_search_structure (ZServerInfo *zi, Z_RPNStructure *zs,
1426 oid_value attributeSet,
1427 int num_bases, char **basenames)
1430 if (zs->which == Z_RPNStructure_complex)
1432 rset_bool_parms bool_parms;
1435 bool_parms.rset_l = rpn_search_structure (zi, zs->u.complex->s1,
1437 num_bases, basenames);
1438 if (bool_parms.rset_l == NULL)
1440 if (rset_is_ranked(bool_parms.rset_l))
1442 bool_parms.rset_r = rpn_search_structure (zi, zs->u.complex->s2,
1444 num_bases, basenames);
1445 if (bool_parms.rset_r == NULL)
1447 rset_delete (bool_parms.rset_l);
1450 if (rset_is_ranked(bool_parms.rset_r))
1452 bool_parms.key_size = sizeof(struct it_key);
1453 bool_parms.cmp = key_compare;
1455 switch (zs->u.complex->roperator->which)
1457 case Z_Operator_and:
1458 r = rset_create (soft ? rset_kind_sand:rset_kind_and, &bool_parms);
1461 r = rset_create (soft ? rset_kind_sor:rset_kind_or, &bool_parms);
1463 case Z_Operator_and_not:
1464 r = rset_create (soft ? rset_kind_snot:rset_kind_not, &bool_parms);
1470 else if (zs->which == Z_RPNStructure_simple)
1472 if (zs->u.simple->which == Z_Operand_APT)
1474 logf (LOG_DEBUG, "rpn_search_APT");
1475 r = rpn_search_APT (zi, zs->u.simple->u.attributesPlusTerm,
1476 attributeSet, num_bases, basenames);
1478 else if (zs->u.simple->which == Z_Operand_resultSetId)
1480 logf (LOG_DEBUG, "rpn_search_ref");
1481 r = rpn_search_ref (zi, zs->u.simple->u.resultSetId);
1495 void count_set_save (RSET *r, int *count)
1502 rset_temp_parms parms;
1504 logf (LOG_DEBUG, "count_set_save");
1506 parms.key_size = sizeof(struct it_key);
1507 w = rset_create (rset_kind_temp, &parms);
1508 wfd = rset_open (w, RSETF_WRITE|RSETF_SORT_SYSNO);
1509 rfd = rset_open (*r, RSETF_READ|RSETF_SORT_SYSNO);
1510 while (rset_read (*r, rfd, &key))
1512 if (key.sysno != psysno)
1514 rset_write (w, wfd, &key);
1520 rset_close (*r, rfd);
1522 rset_close (w, wfd);
1524 logf (LOG_DEBUG, "%d keys, %d distinct sysnos", kno, *count);
1527 static void count_set (RSET r, int *count)
1534 logf (LOG_DEBUG, "count_set");
1536 rfd = rset_open (r, RSETF_READ|RSETF_SORT_SYSNO);
1537 while (rset_read (r, rfd, &key))
1539 if (key.sysno != psysno)
1546 rset_close (r, rfd);
1547 logf (LOG_DEBUG, "%d keys, %d distinct sysnos", kno, *count);
1550 int rpn_search (ZServerInfo *zi,
1551 Z_RPNQuery *rpn, int num_bases, char **basenames,
1552 const char *setname, int *hits)
1556 oid_value attributeSet;
1561 zi->errString = NULL;
1563 attrset = oid_getentbyoid (rpn->attributeSetId);
1564 attributeSet = attrset->value;
1565 rset = rpn_search_structure (zi, rpn->RPNStructure, attributeSet,
1566 num_bases, basenames);
1569 if (rset_is_volatile(rset))
1570 count_set_save(&rset,hits);
1572 count_set (rset, hits);
1573 resultSetAdd (zi, setname, 1, rset);
1575 logf (LOG_DEBUG, "search error: %d", zi->errCode);
1580 struct scan_entry *list;
1587 static int scan_handle (char *name, const char *info, int pos, void *client)
1589 int len_prefix, idx;
1592 struct scan_info *scan_info = client;
1594 rset_isam_parms parms;
1596 len_prefix = strlen(scan_info->prefix);
1597 if (memcmp (name, scan_info->prefix, len_prefix))
1600 idx = scan_info->after - pos + scan_info->before;
1603 scan_info->list[idx].term = odr_malloc (scan_info->odr,
1604 strlen(name + len_prefix)+1);
1605 strcpy (scan_info->list[idx].term, name + len_prefix);
1606 assert (*info == sizeof(isam_p));
1607 memcpy (&isam_p, info+1, sizeof(isam_p));
1608 parms.is = scan_info->isam;
1611 rset = rset_create (rset_kind_isam, &parms);
1612 count_set (rset, &scan_info->list[idx].occurrences);
1615 scan_info->list[idx].occurrences = 1;
1617 logf (LOG_DEBUG, "pos=%3d idx=%3d name=%s", pos, idx, name);
1622 static int dummy_handle (char *name, const char *info, void *p)
1627 int rpn_scan (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1628 int num_bases, char **basenames,
1629 int *position, int *num_entries, struct scan_entry **list,
1632 int i, j, sizez, max_pos;
1633 int pos = *position;
1634 int num = *num_entries;
1637 char termz[IT_MAX_WORD+20];
1640 Z_Term *term = zapt->term;
1641 struct scan_info scan_info;
1643 logf (LOG_DEBUG, "scan, position = %d, num = %d", pos, num);
1647 scan_info.before = before = pos-1;
1648 scan_info.after = after = 1+num-pos;
1649 scan_info.odr = zi->odr;
1651 logf (LOG_DEBUG, "scan, before = %d, after = %d", before, after);
1653 scan_info.isam = zi->wordIsam;
1654 scan_info.list = odr_malloc (zi->odr, (before+after)*
1655 sizeof(*scan_info.list));
1656 for (j = 0; j<before+after; j++)
1657 scan_info.list[j].term = NULL;
1658 attr_init (&use, zapt, 1);
1659 use_value = attr_find (&use, NULL);
1660 logf (LOG_DEBUG, "use value %d", use_value);
1662 if (use_value == -1)
1664 i = index_word_prefix (termz, 1, use_value, *basenames);
1666 dict_lookup_grep (zi->wordDict, termz, 0, NULL, &max_pos, 0,
1668 if (max_pos <= strlen(*basenames))
1670 zi->errString = *basenames;
1671 return zi->errCode = 109; /* Database unavailable */
1673 strcpy (scan_info.prefix, termz);
1674 sizez = term->u.general->len;
1675 if (sizez > IT_MAX_WORD)
1676 sizez = IT_MAX_WORD;
1677 for (j = 0; j<sizez; j++)
1678 termz[j+i] = index_char_cvt (term->u.general->buf[j]);
1681 dict_scan (zi->wordDict, termz, &before, &after, &scan_info, scan_handle);
1683 *status = BEND_SCAN_SUCCESS;
1685 for (i = 0; i<scan_info.after; i++)
1686 if (scan_info.list[scan_info.before+scan_info.after-i-1].term)
1690 *status = BEND_SCAN_PARTIAL;
1692 for (i = 0; i<scan_info.before; i++)
1693 if (scan_info.list[i].term)
1696 *status = BEND_SCAN_PARTIAL;
1700 *list = scan_info.list+i; /* list is set to first 'real' entry */
1702 if (*num_entries == 0) /* signal 'unsupported use-attribute' */
1703 zi->errCode = 114; /* if no entries was found */
1704 logf (LOG_DEBUG, "position = %d, num_entries = %d",
1705 *position, *num_entries);
1707 logf (LOG_DEBUG, "scan error: %d", zi->errCode);