2 * Copyright (C) 1994-1996, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.47 1996-05-15 18:36:28 adam
8 * Function trans_term transforms unsearchable characters to blanks.
10 * Revision 1.46 1996/05/15 11:57:56 adam
11 * Fixed bug introduced by set/field mapping in search operations.
13 * Revision 1.45 1996/05/14 11:34:00 adam
14 * Scan support in multiple registers/databases.
16 * Revision 1.44 1996/05/14 06:16:44 adam
17 * Compact use/set bytes used in search service.
19 * Revision 1.43 1996/05/09 09:54:43 adam
20 * Server supports maps from one logical attributes to a list of physical
22 * The extraction process doesn't make space consuming 'any' keys.
24 * Revision 1.42 1996/05/09 07:28:56 quinn
25 * Work towards phrases and multiple registers
27 * Revision 1.41 1996/03/20 09:36:43 adam
28 * Function dict_lookup_grep got extra parameter, init_pos, which marks
29 * from which position in pattern approximate pattern matching should occur.
30 * Approximate pattern matching is used in relevance=re-2.
32 * Revision 1.40 1996/02/02 13:44:44 adam
33 * The public dictionary functions simply use char instead of Dict_char
34 * to represent search strings. Dict_char is used internally only.
36 * Revision 1.39 1996/01/03 16:22:13 quinn
39 * Revision 1.38 1995/12/11 09:12:55 adam
40 * The rec_get function returns NULL if record doesn't exist - will
41 * happen in the server if the result set records have been deleted since
42 * the creation of the set (i.e. the search).
43 * The server saves a result temporarily if it is 'volatile', i.e. the
44 * set is register dependent.
46 * Revision 1.37 1995/12/06 15:05:28 adam
47 * More verbose in count_set.
49 * Revision 1.36 1995/12/06 12:41:27 adam
50 * New command 'stat' for the index program.
51 * Filenames can be read from stdin by specifying '-'.
52 * Bug fix/enhancement of the transformation from terms to regular
53 * expressons in the search engine.
55 * Revision 1.35 1995/11/27 09:29:00 adam
56 * Bug fixes regarding conversion to regular expressions.
58 * Revision 1.34 1995/11/16 17:00:56 adam
59 * Better logging of rpn query.
61 * Revision 1.33 1995/11/01 13:58:28 quinn
62 * Moving data1 to yaz/retrieval
64 * Revision 1.32 1995/10/27 14:00:11 adam
65 * Implemented detection of database availability.
67 * Revision 1.31 1995/10/17 18:02:10 adam
68 * New feature: databases. Implemented as prefix to words in dictionary.
70 * Revision 1.30 1995/10/16 09:32:38 adam
71 * More work on relational op.
73 * Revision 1.29 1995/10/13 16:01:49 adam
76 * Revision 1.28 1995/10/13 12:26:43 adam
77 * Optimization of truncation.
79 * Revision 1.27 1995/10/12 17:07:22 adam
82 * Revision 1.26 1995/10/12 12:40:54 adam
83 * Bug fixes in rpn_prox.
85 * Revision 1.25 1995/10/10 13:59:24 adam
86 * Function rset_open changed its wflag parameter to general flags.
88 * Revision 1.24 1995/10/09 16:18:37 adam
89 * Function dict_lookup_grep got extra client data parameter.
91 * Revision 1.23 1995/10/06 16:33:37 adam
92 * Use attribute mappings.
94 * Revision 1.22 1995/10/06 15:07:39 adam
95 * Structure 'local-number' handled.
97 * Revision 1.21 1995/10/06 13:52:06 adam
98 * Bug fixes. Handler may abort further scanning.
100 * Revision 1.20 1995/10/06 11:06:33 adam
101 * Scan entries include 'occurrences' now.
103 * Revision 1.19 1995/10/06 10:43:56 adam
104 * Scan added. 'occurrences' in scan entries not set yet.
106 * Revision 1.18 1995/10/04 16:57:20 adam
107 * Key input and merge sort in one pass.
109 * Revision 1.17 1995/10/04 12:55:17 adam
110 * Bug fix in ranked search. Use=Any keys inserted.
112 * Revision 1.16 1995/10/02 16:24:40 adam
113 * Use attribute actually used in search requests.
115 * Revision 1.15 1995/10/02 15:18:52 adam
116 * New member in recRetrieveCtrl: diagnostic.
118 * Revision 1.14 1995/09/28 12:10:32 adam
119 * Bug fixes. Field prefix used in queries.
121 * Revision 1.13 1995/09/18 14:17:50 adam
124 * Revision 1.12 1995/09/15 14:45:21 adam
126 * Work on truncation.
128 * Revision 1.11 1995/09/14 11:53:27 adam
129 * First work on regular expressions/truncations.
131 * Revision 1.10 1995/09/11 15:23:26 adam
132 * More work on relevance search.
134 * Revision 1.9 1995/09/11 13:09:35 adam
135 * More work on relevance feedback.
137 * Revision 1.8 1995/09/08 14:52:27 adam
138 * Minor changes. Dictionary is lower case now.
140 * Revision 1.7 1995/09/07 13:58:36 adam
141 * New parameter: result-set file descriptor (RSFD) to support multiple
142 * positions within the same result-set.
143 * Boolean operators: and, or, not implemented.
144 * Result-set references.
146 * Revision 1.6 1995/09/06 16:11:18 adam
147 * Option: only one word key per file.
149 * Revision 1.5 1995/09/06 10:33:04 adam
150 * More work on present. Some log messages removed.
152 * Revision 1.4 1995/09/05 15:28:40 adam
153 * More work on search engine.
155 * Revision 1.3 1995/09/04 15:20:22 adam
158 * Revision 1.2 1995/09/04 12:33:43 adam
159 * Various cleanup. YAZ util used instead.
161 * Revision 1.1 1995/09/04 09:10:40 adam
162 * More work on index add/del/update.
163 * Merge sort implemented.
164 * Initial work on z39 server.
173 #include "attribute.h"
181 int index_word_prefix_map (char *string, oid_value attrSet, int attrUse,
186 logf (LOG_DEBUG, "oid_value attrSet = %d, attrUse = %d", attrSet, attrUse);
187 attp = att_getentbyatt (attrSet, attrUse);
190 logf (LOG_DEBUG, "ord=%d", attp->attset_ordinal);
191 return index_word_prefix (string, attp->attset_ordinal,
192 attp->local_attributes->local, basename);
199 Z_AttributesPlusTerm *zapt;
202 static int attr_find (AttrType *src, oid_value *attributeSetP)
204 while (src->major < src->zapt->num_attributes)
206 Z_AttributeElement *element;
208 element = src->zapt->attributeList[src->major];
209 if (src->type == *element->attributeType)
211 switch (element->which)
213 case Z_AttributeValue_numeric:
215 if (element->attributeSet && attributeSetP)
219 attrset = oid_getentbyoid (element->attributeSet);
220 *attributeSetP = attrset->value;
222 return *element->value.numeric;
224 case Z_AttributeValue_complex:
225 if (src->minor >= element->value.complex->num_list ||
226 element->value.complex->list[src->minor]->which !=
227 Z_StringOrNumeric_numeric)
230 if (element->attributeSet && attributeSetP)
234 attrset = oid_getentbyoid (element->attributeSet);
235 *attributeSetP = attrset->value;
237 return *element->value.complex->list[src->minor-1]->u.numeric;
247 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
261 int (*cmp)(const void *p1, const void *p2);
268 static void heap_swap (struct trunc_info *ti, int i1, int i2)
273 ti->ptr[i1] = ti->ptr[i2];
277 static void heap_delete (struct trunc_info *ti)
279 int cur = 1, child = 2;
281 heap_swap (ti, 1, ti->heapnum--);
282 while (child <= ti->heapnum) {
283 if (child < ti->heapnum &&
284 (*ti->cmp)(ti->heap[ti->ptr[child]],
285 ti->heap[ti->ptr[1+child]]) > 0)
287 if ((*ti->cmp)(ti->heap[ti->ptr[cur]],
288 ti->heap[ti->ptr[child]]) > 0)
290 heap_swap (ti, cur, child);
299 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
303 cur = ++(ti->heapnum);
304 memcpy (ti->heap[ti->ptr[cur]], buf, ti->keysize);
305 ti->indx[ti->ptr[cur]] = indx;
307 while (parent && (*ti->cmp)(ti->heap[ti->ptr[parent]],
308 ti->heap[ti->ptr[cur]]) > 0)
310 heap_swap (ti, cur, parent);
317 struct trunc_info *heap_init (int size, int key_size,
318 int (*cmp)(const void *p1, const void *p2))
320 struct trunc_info *ti = xmalloc (sizeof(*ti));
325 ti->keysize = key_size;
327 ti->indx = xmalloc (size * sizeof(*ti->indx));
328 ti->heap = xmalloc (size * sizeof(*ti->heap));
329 ti->ptr = xmalloc (size * sizeof(*ti->ptr));
330 ti->swapbuf = xmalloc (ti->keysize);
331 ti->tmpbuf = xmalloc (ti->keysize);
332 ti->buf = xmalloc (size * ti->keysize);
333 for (i = size; --i >= 0; )
336 ti->heap[i] = ti->buf + ti->keysize * i;
341 static void heap_close (struct trunc_info *ti)
351 static RSET rset_trunc_r (ISAM isam, ISAM_P *isam_p, int from, int to,
356 rset_temp_parms parms;
358 parms.key_size = sizeof(struct it_key);
359 result = rset_create (rset_kind_temp, &parms);
360 result_rsfd = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
362 if (to - from > merge_chunk)
366 int i, i_add = (to-from)/merge_chunk + 1;
367 struct trunc_info *ti;
369 int rsmax = (to-from)/i_add + 1;
371 rset = xmalloc (sizeof(*rset) * rsmax);
372 rsfd = xmalloc (sizeof(*rsfd) * rsmax);
374 for (i = from; i < to; i += i_add)
377 rset[rscur] = rset_trunc_r (isam, isam_p, i, i+i_add,
380 rset[rscur] = rset_trunc_r (isam, isam_p, i, to,
384 ti = heap_init (rscur, sizeof(struct it_key), key_compare);
385 for (i = rscur; --i >= 0; )
387 rsfd[i] = rset_open (rset[i], RSETF_READ|RSETF_SORT_SYSNO);
388 if (rset_read (rset[i], rsfd[i], ti->tmpbuf))
389 heap_insert (ti, ti->tmpbuf, i);
392 rset_close (rset[i], rsfd[i]);
393 rset_delete (rset[i]);
398 int n = ti->indx[ti->ptr[1]];
400 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
404 if (!rset_read (rset[n], rsfd[n], ti->tmpbuf))
407 rset_close (rset[n], rsfd[n]);
408 rset_delete (rset[n]);
411 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
414 heap_insert (ti, ti->tmpbuf, n);
427 struct trunc_info *ti;
429 ispt = xmalloc (sizeof(*ispt) * (to-from));
431 ti = heap_init (to-from, sizeof(struct it_key),
433 for (i = to-from; --i >= 0; )
435 ispt[i] = is_position (isam, isam_p[from+i]);
436 if (is_readkey (ispt[i], ti->tmpbuf))
437 heap_insert (ti, ti->tmpbuf, i);
439 is_pt_free (ispt[i]);
443 int n = ti->indx[ti->ptr[1]];
445 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
447 /* section that preserve all keys */
449 if (is_readkey (ispt[n], ti->tmpbuf))
450 heap_insert (ti, ti->tmpbuf, n);
452 is_pt_free (ispt[n]);
454 /* section that preserve all keys with unique sysnos */
457 if (!is_readkey (ispt[n], ti->tmpbuf))
460 is_pt_free (ispt[n]);
463 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
466 heap_insert (ti, ti->tmpbuf, n);
475 rset_close (result, result_rsfd);
479 static int isam_trunc_cmp (const void *p1, const void *p2)
481 ISAM_P i1 = *(ISAM_P*) p1;
482 ISAM_P i2 = *(ISAM_P*) p2;
485 d = is_type (i1) - is_type (i2);
488 return is_block (i1) - is_block (i2);
491 static RSET rset_trunc (ISAM isam, ISAM_P *isam_p, int no)
494 qsort (isam_p, no, sizeof(*isam_p), isam_trunc_cmp);
495 return rset_trunc_r (isam, isam_p, 0, no, 100);
504 static void add_isam_p (const char *info, struct grep_info *p)
506 if (p->isam_p_indx == p->isam_p_size)
508 ISAM_P *new_isam_p_buf;
510 p->isam_p_size = 2*p->isam_p_size + 100;
511 new_isam_p_buf = xmalloc (sizeof(*new_isam_p_buf) *
515 memcpy (new_isam_p_buf, p->isam_p_buf,
516 p->isam_p_indx * sizeof(*p->isam_p_buf));
517 xfree (p->isam_p_buf);
519 p->isam_p_buf = new_isam_p_buf;
521 assert (*info == sizeof(*p->isam_p_buf));
522 memcpy (p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
526 static int grep_handle (char *name, const char *info, void *p)
528 logf (LOG_DEBUG, "dict name: %s", name);
529 add_isam_p (info, p);
533 static void gen_regular_rel (char *dst, int val, int islt)
541 sprintf (numstr, "%d", val);
542 for (w = strlen(numstr); --w >= 0; pos++)
561 strcpy (dst + dst_p, numstr);
562 dst_p = strlen(dst) - pos - 1;
590 for (i = 0; i<pos; i++)
603 for (i=1; i<pos; i++)
604 strcat (dst, "[0-9]?");
608 for (i = 0; i <= pos; i++)
609 strcat (dst, "[0-9]");
610 strcat (dst, "[0-9]*");
615 static int relational_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
616 const char *term_sub,
618 oid_value attributeSet,
619 struct grep_info *grep_info,
627 attr_init (&relation, zapt, 2);
628 relation_value = attr_find (&relation, NULL);
629 term_value = atoi (term_sub);
631 switch (relation_value)
636 logf (LOG_DEBUG, "Relation <");
637 gen_regular_rel (term_dict + strlen(term_dict), term_value-1, 1);
642 logf (LOG_DEBUG, "Relation <=");
643 gen_regular_rel (term_dict + strlen(term_dict), term_value, 1);
648 logf (LOG_DEBUG, "Relation >=");
649 gen_regular_rel (term_dict + strlen(term_dict), term_value, 0);
654 logf (LOG_DEBUG, "Relation >");
655 gen_regular_rel (term_dict + strlen(term_dict), term_value+1, 0);
660 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_dict);
661 r = dict_lookup_grep (zi->wordDict, term_dict, 0, grep_info, max_pos,
664 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
665 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
669 static void verbatim_char (int ch, int *indx, char *dst)
672 dst[(*indx)++] = '\\';
676 static int field_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
677 const char *term_sub, int regType,
678 oid_value attributeSet, struct grep_info *grep_info,
679 int num_bases, char **basenames)
681 char term_dict[2*IT_MAX_WORD+2];
682 int i, j, r, base_no;
684 int truncation_value;
687 oid_value curAttributeSet = attributeSet;
689 attr_init (&use, zapt, 1);
690 use_value = attr_find (&use, &curAttributeSet);
691 logf (LOG_DEBUG, "use value %d", use_value);
692 attr_init (&truncation, zapt, 5);
693 truncation_value = attr_find (&truncation, NULL);
694 logf (LOG_DEBUG, "truncation value %d", truncation_value);
699 for (base_no = 0; base_no < num_bases; base_no++)
702 data1_local_attribute *local_attr;
703 int max_pos, prefix_len = 0;
705 attp = att_getentbyatt (curAttributeSet, use_value);
708 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
709 curAttributeSet, use_value);
713 if (zebTargetInfo_curDatabase (zi->zti, basenames[base_no]))
715 zi->errCode = 109; /* Database unavailable */
716 zi->errString = basenames[base_no];
718 for (local_attr = attp->local_attributes; local_attr;
719 local_attr = local_attr->next)
723 ord = zebTargetInfo_lookupSU (zi->zti, attp->attset_ordinal,
728 term_dict[prefix_len++] = '|';
730 term_dict[prefix_len++] = '(';
731 if ((ord >= 'A' && ord <= 'Z') || (ord >= 'a' && ord <= 'z'))
732 term_dict[prefix_len++] = ord;
735 term_dict[prefix_len++] = '\\';
736 term_dict[prefix_len++] = ord;
744 term_dict[prefix_len++] = ')';
745 term_dict[prefix_len++] = regType;
746 term_dict[prefix_len] = '\0';
747 if (!relational_term (zi, zapt, term_sub, term_dict,
748 attributeSet, grep_info, &max_pos))
753 switch (truncation_value)
755 case -1: /* not specified */
756 case 100: /* do not truncate */
757 term_dict[j++] = '(';
758 for (i = 0; term_sub[i]; i++)
759 verbatim_char (term_sub[i], &j, term_dict);
760 strcpy (term_dict+j, ")");
761 r = dict_lookup_grep (zi->wordDict, term_dict, 0, grep_info,
762 &max_pos, 0, grep_handle);
764 logf (LOG_WARN, "dict_lookup_grep err, trunc=none:%d", r);
766 case 1: /* right truncation */
767 term_dict[j++] = '(';
768 for (i = 0; term_sub[i]; i++)
769 verbatim_char (term_sub[i], &j, term_dict);
770 strcpy (term_dict+j, ".*)");
771 dict_lookup_grep (zi->wordDict, term_dict, 0, grep_info,
772 &max_pos, 0, grep_handle);
774 case 2: /* left truncation */
775 case 3: /* left&right truncation */
778 case 101: /* process # in term */
779 term_dict[j++] = '(';
780 for (i=0; term_sub[i]; i++)
781 if (term_sub[i] == '#' && i > 2)
783 term_dict[j++] = '.';
784 term_dict[j++] = '*';
787 verbatim_char (term_sub[i], &j, term_dict);
788 strcpy (term_dict+j, ")");
789 r = dict_lookup_grep (zi->wordDict, term_dict, 0, grep_info,
790 &max_pos, 0, grep_handle);
792 logf (LOG_WARN, "dict_lookup_grep err, trunc=#: %d",
795 case 102: /* regular expression */
796 sprintf (term_dict + j, "(%s)", term_sub);
797 r = dict_lookup_grep (zi->wordDict, term_dict, 0, grep_info,
798 &max_pos, 0, grep_handle);
800 logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
803 case 103: /* regular expression with error correction */
806 if (*cp == '*' && cp[1] && cp[2])
811 sprintf (term_dict + j, "(%s)", cp);
812 r = dict_lookup_grep (zi->wordDict, term_dict, r, grep_info,
813 &max_pos, j, grep_handle);
815 logf (LOG_WARN, "dict_lookup_grep err, trunc=eregular: %d",
821 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
825 static void trans_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
830 Z_Term *term = zapt->term;
832 sizez = term->u.general->len;
833 if (sizez > IT_MAX_WORD-1)
834 sizez = IT_MAX_WORD-1;
835 for (i = 0; i < sizez; i++)
837 if (!isalnum(term->u.general->buf[i]))
843 *termz++ = index_char_cvt (term->u.general->buf[i]);
852 static RSET rpn_search_APT_relevance (ZServerInfo *zi,
853 Z_AttributesPlusTerm *zapt,
854 oid_value attributeSet,
855 int num_bases, char **basenames)
857 rset_relevance_parms parms;
858 char termz[IT_MAX_WORD+1];
859 char term_sub[IT_MAX_WORD+1];
860 struct grep_info grep_info;
861 char *p0 = termz, *p1 = NULL;
864 parms.key_size = sizeof(struct it_key);
866 parms.cmp = key_compare;
867 parms.is = zi->wordIsam;
869 if (zapt->term->which != Z_Term_general)
874 trans_term (zi, zapt, termz);
876 grep_info.isam_p_indx = 0;
877 grep_info.isam_p_size = 0;
878 grep_info.isam_p_buf = NULL;
881 if ((p1 = strchr (p0, ' ')))
883 memcpy (term_sub, p0, p1-p0);
884 term_sub[p1-p0] = '\0';
887 strcpy (term_sub, p0);
888 if (field_term (zi, zapt, term_sub, 'w', attributeSet, &grep_info,
889 num_bases, basenames))
897 parms.isam_positions = grep_info.isam_p_buf;
898 parms.no_isam_positions = grep_info.isam_p_indx;
899 if (grep_info.isam_p_indx > 0)
900 result = rset_create (rset_kind_relevance, &parms);
902 result = rset_create (rset_kind_null, NULL);
903 xfree (grep_info.isam_p_buf);
907 static RSET rpn_search_APT_cphrase (ZServerInfo *zi,
908 Z_AttributesPlusTerm *zapt,
909 oid_value attributeSet,
910 int num_bases, char **basenames)
912 rset_isam_parms parms;
913 char termz[IT_MAX_WORD+1];
914 struct grep_info grep_info;
917 if (zapt->term->which != Z_Term_general)
922 trans_term (zi, zapt, termz);
924 grep_info.isam_p_indx = 0;
925 grep_info.isam_p_size = 0;
926 grep_info.isam_p_buf = NULL;
928 if (field_term (zi, zapt, termz, 'p', attributeSet, &grep_info,
929 num_bases, basenames))
931 if (grep_info.isam_p_indx < 1)
932 result = rset_create (rset_kind_null, NULL);
933 else if (grep_info.isam_p_indx == 1)
935 parms.is = zi->wordIsam;
936 parms.pos = *grep_info.isam_p_buf;
937 result = rset_create (rset_kind_isam, &parms);
940 result = rset_trunc (zi->wordIsam, grep_info.isam_p_buf,
941 grep_info.isam_p_indx);
942 xfree (grep_info.isam_p_buf);
945 static RSET rpn_search_APT_word (ZServerInfo *zi,
946 Z_AttributesPlusTerm *zapt,
947 oid_value attributeSet,
948 int num_bases, char **basenames)
950 rset_isam_parms parms;
951 char termz[IT_MAX_WORD+1];
952 struct grep_info grep_info;
955 if (zapt->term->which != Z_Term_general)
960 trans_term (zi, zapt, termz);
962 grep_info.isam_p_indx = 0;
963 grep_info.isam_p_size = 0;
964 grep_info.isam_p_buf = NULL;
966 if (field_term (zi, zapt, termz, 'w', attributeSet, &grep_info,
967 num_bases, basenames))
969 if (grep_info.isam_p_indx < 1)
970 result = rset_create (rset_kind_null, NULL);
971 else if (grep_info.isam_p_indx == 1)
973 parms.is = zi->wordIsam;
974 parms.pos = *grep_info.isam_p_buf;
975 result = rset_create (rset_kind_isam, &parms);
978 result = rset_trunc (zi->wordIsam, grep_info.isam_p_buf,
979 grep_info.isam_p_indx);
980 xfree (grep_info.isam_p_buf);
984 static RSET rpn_prox (RSET *rset, int rset_no)
992 rset_temp_parms parms;
994 rsfd = xmalloc (sizeof(*rsfd)*rset_no);
995 more = xmalloc (sizeof(*more)*rset_no);
996 buf = xmalloc (sizeof(*buf)*rset_no);
998 for (i = 0; i<rset_no; i++)
1000 buf[i] = xmalloc (sizeof(**buf));
1001 rsfd[i] = rset_open (rset[i], RSETF_READ|RSETF_SORT_SYSNO);
1002 if (!(more[i] = rset_read (rset[i], rsfd[i], buf[i])))
1006 rset_close (rset[i], rsfd[i]);
1013 return rset_create (rset_kind_null, NULL);
1016 parms.key_size = sizeof (struct it_key);
1017 result = rset_create (rset_kind_temp, &parms);
1018 rsfd_result = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
1022 for (i = 1; i<rset_no; i++)
1031 cmp = key_compare (buf[i], buf[i-1]);
1034 more[i-1] = rset_read (rset[i-1], rsfd[i-1], buf[i-1]);
1039 if (buf[i-1]->seqno+1 != buf[i]->seqno)
1041 more[i-1] = rset_read (rset[i-1], rsfd[i-1], buf[i-1]);
1047 more[i] = rset_read (rset[i], rsfd[i], buf[i]);
1053 rset_write (result, rsfd_result, buf[0]);
1054 more[0] = rset_read (*rset, *rsfd, *buf);
1058 for (i = 0; i<rset_no; i++)
1060 rset_close (rset[i], rsfd[i]);
1063 rset_close (result, rsfd_result);
1070 static RSET rpn_search_APT_phrase (ZServerInfo *zi,
1071 Z_AttributesPlusTerm *zapt,
1072 oid_value attributeSet,
1073 int num_bases, char **basenames)
1075 char termz[IT_MAX_WORD+1];
1076 char term_sub[IT_MAX_WORD+1];
1077 char *p0 = termz, *p1 = NULL;
1078 RSET rset[60], result;
1080 struct grep_info grep_info;
1082 if (zapt->term->which != Z_Term_general)
1087 trans_term (zi, zapt, termz);
1089 grep_info.isam_p_size = 0;
1090 grep_info.isam_p_buf = NULL;
1094 if ((p1 = strchr (p0, ' ')))
1096 memcpy (term_sub, p0, p1-p0);
1097 term_sub[p1-p0] = '\0';
1100 strcpy (term_sub, p0);
1102 grep_info.isam_p_indx = 0;
1103 if (field_term (zi, zapt, term_sub, 'w', attributeSet, &grep_info,
1104 num_bases, basenames))
1106 if (grep_info.isam_p_indx == 0)
1107 rset[rset_no] = rset_create (rset_kind_null, NULL);
1108 else if (grep_info.isam_p_indx > 1)
1109 rset[rset_no] = rset_trunc (zi->wordIsam,
1110 grep_info.isam_p_buf,
1111 grep_info.isam_p_indx);
1114 rset_isam_parms parms;
1116 parms.is = zi->wordIsam;
1117 parms.pos = *grep_info.isam_p_buf;
1118 rset[rset_no] = rset_create (rset_kind_isam, &parms);
1120 assert (rset[rset_no]);
1121 if (++rset_no >= sizeof(rset)/sizeof(*rset))
1126 while (*++p0 == ' ')
1129 xfree (grep_info.isam_p_buf);
1131 return rset_create (rset_kind_null, NULL);
1132 else if (rset_no == 1)
1135 result = rpn_prox (rset, rset_no);
1136 for (i = 0; i<rset_no; i++)
1137 rset_delete (rset[i]);
1141 static RSET rpn_search_APT_local (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1142 oid_value attributeSet)
1147 rset_temp_parms parms;
1148 char termz[IT_MAX_WORD+1];
1150 if (zapt->term->which != Z_Term_general)
1155 parms.key_size = sizeof (struct it_key);
1156 result = rset_create (rset_kind_temp, &parms);
1157 rsfd = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
1159 trans_term (zi, zapt, termz);
1161 key.sysno = atoi (termz);
1164 rset_write (result, rsfd, &key);
1165 rset_close (result, rsfd);
1169 static RSET rpn_search_APT (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1170 oid_value attributeSet,
1171 int num_bases, char **basenames)
1175 AttrType completeness;
1176 int relation_value, structure_value, completeness_value;
1178 attr_init (&relation, zapt, 2);
1179 attr_init (&structure, zapt, 4);
1180 attr_init (&completeness, zapt, 6);
1182 relation_value = attr_find (&relation, NULL);
1183 structure_value = attr_find (&structure, NULL);
1184 completeness_value = attr_find (&completeness, NULL);
1185 switch (structure_value)
1188 if (relation_value == 102) /* relevance relation */
1189 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1190 num_bases, basenames);
1191 if (completeness_value == 2 || completeness_value == 3)
1192 return rpn_search_APT_cphrase (zi, zapt, attributeSet,
1193 num_bases, basenames);
1194 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1195 num_bases, basenames);
1196 case 1: /* phrase */
1197 if (relation_value == 102) /* relevance relation */
1198 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1199 num_bases, basenames);
1200 if (completeness_value == 2 || completeness_value == 3)
1201 return rpn_search_APT_cphrase (zi, zapt, attributeSet,
1202 num_bases, basenames);
1203 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1204 num_bases, basenames);
1207 if (relation_value == 102) /* relevance relation */
1208 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1209 num_bases, basenames);
1210 if (completeness_value == 2 || completeness_value == 3)
1211 return rpn_search_APT_cphrase (zi, zapt, attributeSet,
1212 num_bases, basenames);
1213 return rpn_search_APT_word (zi, zapt, attributeSet,
1214 num_bases, basenames);
1219 case 5: /* date - normalized */
1221 case 6: /* word list */
1222 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1223 num_bases, basenames);
1224 case 100: /* date - un-normalized */
1226 case 101: /* name - normalized */
1228 case 102: /* date - un-normalized */
1230 case 103: /* structure */
1234 case 105: /* free-form-text */
1235 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1236 num_bases, basenames);
1237 case 106: /* document-text */
1238 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1239 num_bases, basenames);
1240 case 107: /* local-number */
1241 return rpn_search_APT_local (zi, zapt, attributeSet);
1242 case 108: /* string */
1243 return rpn_search_APT_word (zi, zapt, attributeSet,
1244 num_bases, basenames);
1245 case 109: /* numeric string */
1252 static RSET rpn_search_ref (ZServerInfo *zi, Z_ResultSetId *resultSetId)
1256 if (!(s = resultSetGet (zi, resultSetId)))
1257 return rset_create (rset_kind_null, NULL);
1261 static RSET rpn_search_structure (ZServerInfo *zi, Z_RPNStructure *zs,
1262 oid_value attributeSet,
1263 int num_bases, char **basenames)
1266 if (zs->which == Z_RPNStructure_complex)
1268 rset_bool_parms bool_parms;
1271 bool_parms.rset_l = rpn_search_structure (zi, zs->u.complex->s1,
1273 num_bases, basenames);
1274 if (bool_parms.rset_l == NULL)
1276 if (rset_is_ranked(bool_parms.rset_l))
1278 bool_parms.rset_r = rpn_search_structure (zi, zs->u.complex->s2,
1280 num_bases, basenames);
1281 if (bool_parms.rset_r == NULL)
1283 rset_delete (bool_parms.rset_l);
1286 if (rset_is_ranked(bool_parms.rset_r))
1288 bool_parms.key_size = sizeof(struct it_key);
1289 bool_parms.cmp = key_compare;
1291 switch (zs->u.complex->roperator->which)
1293 case Z_Operator_and:
1294 r = rset_create (soft ? rset_kind_sand:rset_kind_and, &bool_parms);
1297 r = rset_create (soft ? rset_kind_sor:rset_kind_or, &bool_parms);
1299 case Z_Operator_and_not:
1300 r = rset_create (soft ? rset_kind_snot:rset_kind_not, &bool_parms);
1306 else if (zs->which == Z_RPNStructure_simple)
1308 if (zs->u.simple->which == Z_Operand_APT)
1310 logf (LOG_DEBUG, "rpn_search_APT");
1311 r = rpn_search_APT (zi, zs->u.simple->u.attributesPlusTerm,
1312 attributeSet, num_bases, basenames);
1314 else if (zs->u.simple->which == Z_Operand_resultSetId)
1316 logf (LOG_DEBUG, "rpn_search_ref");
1317 r = rpn_search_ref (zi, zs->u.simple->u.resultSetId);
1331 void count_set_save (RSET *r, int *count)
1338 rset_temp_parms parms;
1340 logf (LOG_DEBUG, "count_set_save");
1342 parms.key_size = sizeof(struct it_key);
1343 w = rset_create (rset_kind_temp, &parms);
1344 wfd = rset_open (w, RSETF_WRITE|RSETF_SORT_SYSNO);
1345 rfd = rset_open (*r, RSETF_READ|RSETF_SORT_SYSNO);
1346 while (rset_read (*r, rfd, &key))
1348 if (key.sysno != psysno)
1350 rset_write (w, wfd, &key);
1356 rset_close (*r, rfd);
1358 rset_close (w, wfd);
1360 logf (LOG_DEBUG, "%d keys, %d distinct sysnos", kno, *count);
1363 static void count_set (RSET r, int *count)
1370 logf (LOG_DEBUG, "count_set");
1372 rfd = rset_open (r, RSETF_READ|RSETF_SORT_SYSNO);
1373 while (rset_read (r, rfd, &key))
1375 if (key.sysno != psysno)
1382 rset_close (r, rfd);
1383 logf (LOG_DEBUG, "%d keys, %d distinct sysnos", kno, *count);
1386 int rpn_search (ZServerInfo *zi,
1387 Z_RPNQuery *rpn, int num_bases, char **basenames,
1388 const char *setname, int *hits)
1392 oid_value attributeSet;
1397 zi->errString = NULL;
1399 attrset = oid_getentbyoid (rpn->attributeSetId);
1400 attributeSet = attrset->value;
1401 rset = rpn_search_structure (zi, rpn->RPNStructure, attributeSet,
1402 num_bases, basenames);
1405 if (rset_is_volatile(rset))
1406 count_set_save(&rset,hits);
1408 count_set (rset, hits);
1409 resultSetAdd (zi, setname, 1, rset);
1411 logf (LOG_DEBUG, "search error: %d", zi->errCode);
1415 struct scan_info_entry {
1421 struct scan_info_entry *list;
1427 static int scan_handle (char *name, const char *info, int pos, void *client)
1429 int len_prefix, idx;
1430 struct scan_info *scan_info = client;
1432 len_prefix = strlen(scan_info->prefix);
1433 if (memcmp (name, scan_info->prefix, len_prefix))
1436 idx = scan_info->after - pos + scan_info->before;
1439 logf (LOG_DEBUG, "%-3d %s", idx, name+len_prefix);
1440 scan_info->list[idx].term = odr_malloc (scan_info->odr,
1441 strlen(name + len_prefix)+1);
1442 strcpy (scan_info->list[idx].term, name + len_prefix);
1443 assert (*info == sizeof(ISAM_P));
1444 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAM_P));
1449 int rpn_scan (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1450 oid_value attributeset,
1451 int num_bases, char **basenames,
1452 int *position, int *num_entries, struct scan_entry **list,
1456 int pos = *position;
1457 int num = *num_entries;
1461 char termz[IT_MAX_WORD+20];
1464 AttrType completeness;
1465 int completeness_value;
1466 struct scan_info *scan_info_array;
1467 struct scan_entry *glist;
1468 int ords[32], ord_no = 0;
1471 logf (LOG_DEBUG, "scan, position = %d, num = %d", pos, num);
1473 attr_init (&use, zapt, 1);
1474 use_value = attr_find (&use, NULL);
1475 logf (LOG_DEBUG, "use value %d", use_value);
1477 attr_init (&completeness, zapt, 6);
1478 completeness_value = attr_find (&completeness, NULL);
1479 logf (LOG_DEBUG, "completeness value %d", completeness_value);
1481 if (attributeset == VAL_NONE)
1482 attributeset = VAL_BIB1;
1484 if (use_value == -1)
1486 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
1489 data1_local_attribute *local_attr;
1491 attp = att_getentbyatt (attributeset, use_value);
1494 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
1495 attributeset, use_value);
1496 return zi->errCode = 114;
1498 if (zebTargetInfo_curDatabase (zi->zti, basenames[base_no]))
1500 zi->errString = basenames[base_no];
1501 return zi->errCode = 109; /* Database unavailable */
1503 for (local_attr = attp->local_attributes; local_attr && ord_no < 32;
1504 local_attr = local_attr->next)
1508 ord = zebTargetInfo_lookupSU (zi->zti, attp->attset_ordinal,
1511 ords[ord_no++] = ord;
1515 return zi->errCode = 113;
1518 scan_info_array = odr_malloc (zi->odr, ord_no * sizeof(*scan_info_array));
1519 for (i = 0; i < ord_no; i++)
1521 int j, prefix_len = 0;
1522 int before_tmp = before, after_tmp = after;
1523 struct scan_info *scan_info = scan_info_array + i;
1525 scan_info->before = before;
1526 scan_info->after = after;
1527 scan_info->odr = zi->odr;
1529 scan_info->list = odr_malloc (zi->odr, (before+after)*
1530 sizeof(*scan_info->list));
1531 for (j = 0; j<before+after; j++)
1532 scan_info->list[j].term = NULL;
1533 termz[prefix_len++] = ords[i];
1534 termz[prefix_len++] =
1535 (completeness_value==2 || completeness_value==3) ? 'p': 'w';
1536 termz[prefix_len] = 0;
1537 strcpy (scan_info->prefix, termz);
1538 trans_term (zi, zapt, termz+prefix_len);
1539 dict_scan (zi->wordDict, termz, &before_tmp, &after_tmp, scan_info,
1542 glist = odr_malloc (zi->odr, (before+after)*sizeof(*glist));
1543 for (i = 0; i < ord_no; i++)
1546 *status = BEND_SCAN_SUCCESS;
1547 for (i = 0; i<after; i++)
1550 const char *mterm = NULL;
1553 rset_isam_parms parms;
1555 for (j = 0; j < ord_no; j++)
1557 if (ptr[j] < before+after &&
1558 (tst=scan_info_array[j].list[ptr[j]].term) &&
1559 (!mterm || strcmp (tst, mterm) < 0))
1567 glist[i+before].term = odr_malloc (zi->odr, strlen(mterm)+1);
1568 strcpy (glist[i+before].term, mterm);
1570 parms.is = zi->wordIsam;
1571 parms.pos = scan_info_array[j0].list[ptr[j0]].isam_p;
1572 rset = rset_create (rset_kind_isam, &parms);
1575 for (j = j0+1; j<ord_no; j++)
1577 if (ptr[j] < before+after &&
1578 (tst=scan_info_array[j].list[ptr[j]].term) &&
1579 !strcmp (tst, mterm))
1581 rset_isam_parms parms;
1582 rset_bool_parms bool_parms;
1585 parms.is = zi->wordIsam;
1586 parms.pos = scan_info_array[j].list[ptr[j]].isam_p;
1587 rset2 = rset_create (rset_kind_isam, &parms);
1589 bool_parms.key_size = sizeof(struct it_key);
1590 bool_parms.cmp = key_compare;
1591 bool_parms.rset_l = rset;
1592 bool_parms.rset_r = rset2;
1594 rset = rset_create (rset_kind_or, &bool_parms);
1599 count_set (rset, &glist[i+before].occurrences);
1604 *num_entries -= (after-i);
1605 *status = BEND_SCAN_PARTIAL;
1608 for (i = 0; i<ord_no; i++)
1611 for (i = 0; i<before; i++)
1614 const char *mterm = NULL;
1617 rset_isam_parms parms;
1619 for (j = 0; j <ord_no; j++)
1621 if (ptr[j] < before &&
1622 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
1623 (!mterm || strcmp (tst, mterm) > 0))
1631 glist[before-1-i].term = odr_malloc (zi->odr, strlen(mterm)+1);
1632 strcpy (glist[before-1-i].term, mterm);
1634 parms.is = zi->wordIsam;
1635 parms.pos = scan_info_array[j0].list[before-1-ptr[j0]].isam_p;
1636 rset = rset_create (rset_kind_isam, &parms);
1640 for (j = j0+1; j<ord_no; j++)
1642 if (ptr[j] < before &&
1643 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
1644 !strcmp (tst, mterm))
1646 rset_isam_parms parms;
1647 rset_bool_parms bool_parms;
1650 parms.is = zi->wordIsam;
1651 parms.pos = scan_info_array[j].list[before-1-ptr[j]].isam_p;
1652 rset2 = rset_create (rset_kind_isam, &parms);
1654 bool_parms.key_size = sizeof(struct it_key);
1655 bool_parms.cmp = key_compare;
1656 bool_parms.rset_l = rset;
1657 bool_parms.rset_r = rset2;
1659 rset = rset_create (rset_kind_or, &bool_parms);
1664 count_set (rset, &glist[before-1-i].occurrences);
1670 *status = BEND_SCAN_PARTIAL;
1674 *list = glist + i; /* list is set to first 'real' entry */
1676 logf (LOG_DEBUG, "position = %d, num_entries = %d",
1677 *position, *num_entries);
1679 logf (LOG_DEBUG, "scan error: %d", zi->errCode);