2 * Copyright (C) 1994-1996, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.48 1996-05-28 15:15:01 adam
8 * Bug fix: Didn't handle unknown database correctly.
10 * Revision 1.47 1996/05/15 18:36:28 adam
11 * Function trans_term transforms unsearchable characters to blanks.
13 * Revision 1.46 1996/05/15 11:57:56 adam
14 * Fixed bug introduced by set/field mapping in search operations.
16 * Revision 1.45 1996/05/14 11:34:00 adam
17 * Scan support in multiple registers/databases.
19 * Revision 1.44 1996/05/14 06:16:44 adam
20 * Compact use/set bytes used in search service.
22 * Revision 1.43 1996/05/09 09:54:43 adam
23 * Server supports maps from one logical attributes to a list of physical
25 * The extraction process doesn't make space consuming 'any' keys.
27 * Revision 1.42 1996/05/09 07:28:56 quinn
28 * Work towards phrases and multiple registers
30 * Revision 1.41 1996/03/20 09:36:43 adam
31 * Function dict_lookup_grep got extra parameter, init_pos, which marks
32 * from which position in pattern approximate pattern matching should occur.
33 * Approximate pattern matching is used in relevance=re-2.
35 * Revision 1.40 1996/02/02 13:44:44 adam
36 * The public dictionary functions simply use char instead of Dict_char
37 * to represent search strings. Dict_char is used internally only.
39 * Revision 1.39 1996/01/03 16:22:13 quinn
42 * Revision 1.38 1995/12/11 09:12:55 adam
43 * The rec_get function returns NULL if record doesn't exist - will
44 * happen in the server if the result set records have been deleted since
45 * the creation of the set (i.e. the search).
46 * The server saves a result temporarily if it is 'volatile', i.e. the
47 * set is register dependent.
49 * Revision 1.37 1995/12/06 15:05:28 adam
50 * More verbose in count_set.
52 * Revision 1.36 1995/12/06 12:41:27 adam
53 * New command 'stat' for the index program.
54 * Filenames can be read from stdin by specifying '-'.
55 * Bug fix/enhancement of the transformation from terms to regular
56 * expressons in the search engine.
58 * Revision 1.35 1995/11/27 09:29:00 adam
59 * Bug fixes regarding conversion to regular expressions.
61 * Revision 1.34 1995/11/16 17:00:56 adam
62 * Better logging of rpn query.
64 * Revision 1.33 1995/11/01 13:58:28 quinn
65 * Moving data1 to yaz/retrieval
67 * Revision 1.32 1995/10/27 14:00:11 adam
68 * Implemented detection of database availability.
70 * Revision 1.31 1995/10/17 18:02:10 adam
71 * New feature: databases. Implemented as prefix to words in dictionary.
73 * Revision 1.30 1995/10/16 09:32:38 adam
74 * More work on relational op.
76 * Revision 1.29 1995/10/13 16:01:49 adam
79 * Revision 1.28 1995/10/13 12:26:43 adam
80 * Optimization of truncation.
82 * Revision 1.27 1995/10/12 17:07:22 adam
85 * Revision 1.26 1995/10/12 12:40:54 adam
86 * Bug fixes in rpn_prox.
88 * Revision 1.25 1995/10/10 13:59:24 adam
89 * Function rset_open changed its wflag parameter to general flags.
91 * Revision 1.24 1995/10/09 16:18:37 adam
92 * Function dict_lookup_grep got extra client data parameter.
94 * Revision 1.23 1995/10/06 16:33:37 adam
95 * Use attribute mappings.
97 * Revision 1.22 1995/10/06 15:07:39 adam
98 * Structure 'local-number' handled.
100 * Revision 1.21 1995/10/06 13:52:06 adam
101 * Bug fixes. Handler may abort further scanning.
103 * Revision 1.20 1995/10/06 11:06:33 adam
104 * Scan entries include 'occurrences' now.
106 * Revision 1.19 1995/10/06 10:43:56 adam
107 * Scan added. 'occurrences' in scan entries not set yet.
109 * Revision 1.18 1995/10/04 16:57:20 adam
110 * Key input and merge sort in one pass.
112 * Revision 1.17 1995/10/04 12:55:17 adam
113 * Bug fix in ranked search. Use=Any keys inserted.
115 * Revision 1.16 1995/10/02 16:24:40 adam
116 * Use attribute actually used in search requests.
118 * Revision 1.15 1995/10/02 15:18:52 adam
119 * New member in recRetrieveCtrl: diagnostic.
121 * Revision 1.14 1995/09/28 12:10:32 adam
122 * Bug fixes. Field prefix used in queries.
124 * Revision 1.13 1995/09/18 14:17:50 adam
127 * Revision 1.12 1995/09/15 14:45:21 adam
129 * Work on truncation.
131 * Revision 1.11 1995/09/14 11:53:27 adam
132 * First work on regular expressions/truncations.
134 * Revision 1.10 1995/09/11 15:23:26 adam
135 * More work on relevance search.
137 * Revision 1.9 1995/09/11 13:09:35 adam
138 * More work on relevance feedback.
140 * Revision 1.8 1995/09/08 14:52:27 adam
141 * Minor changes. Dictionary is lower case now.
143 * Revision 1.7 1995/09/07 13:58:36 adam
144 * New parameter: result-set file descriptor (RSFD) to support multiple
145 * positions within the same result-set.
146 * Boolean operators: and, or, not implemented.
147 * Result-set references.
149 * Revision 1.6 1995/09/06 16:11:18 adam
150 * Option: only one word key per file.
152 * Revision 1.5 1995/09/06 10:33:04 adam
153 * More work on present. Some log messages removed.
155 * Revision 1.4 1995/09/05 15:28:40 adam
156 * More work on search engine.
158 * Revision 1.3 1995/09/04 15:20:22 adam
161 * Revision 1.2 1995/09/04 12:33:43 adam
162 * Various cleanup. YAZ util used instead.
164 * Revision 1.1 1995/09/04 09:10:40 adam
165 * More work on index add/del/update.
166 * Merge sort implemented.
167 * Initial work on z39 server.
176 #include "attribute.h"
184 int index_word_prefix_map (char *string, oid_value attrSet, int attrUse,
189 logf (LOG_DEBUG, "oid_value attrSet = %d, attrUse = %d", attrSet, attrUse);
190 attp = att_getentbyatt (attrSet, attrUse);
193 logf (LOG_DEBUG, "ord=%d", attp->attset_ordinal);
194 return index_word_prefix (string, attp->attset_ordinal,
195 attp->local_attributes->local, basename);
202 Z_AttributesPlusTerm *zapt;
205 static int attr_find (AttrType *src, oid_value *attributeSetP)
207 while (src->major < src->zapt->num_attributes)
209 Z_AttributeElement *element;
211 element = src->zapt->attributeList[src->major];
212 if (src->type == *element->attributeType)
214 switch (element->which)
216 case Z_AttributeValue_numeric:
218 if (element->attributeSet && attributeSetP)
222 attrset = oid_getentbyoid (element->attributeSet);
223 *attributeSetP = attrset->value;
225 return *element->value.numeric;
227 case Z_AttributeValue_complex:
228 if (src->minor >= element->value.complex->num_list ||
229 element->value.complex->list[src->minor]->which !=
230 Z_StringOrNumeric_numeric)
233 if (element->attributeSet && attributeSetP)
237 attrset = oid_getentbyoid (element->attributeSet);
238 *attributeSetP = attrset->value;
240 return *element->value.complex->list[src->minor-1]->u.numeric;
250 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
264 int (*cmp)(const void *p1, const void *p2);
271 static void heap_swap (struct trunc_info *ti, int i1, int i2)
276 ti->ptr[i1] = ti->ptr[i2];
280 static void heap_delete (struct trunc_info *ti)
282 int cur = 1, child = 2;
284 heap_swap (ti, 1, ti->heapnum--);
285 while (child <= ti->heapnum) {
286 if (child < ti->heapnum &&
287 (*ti->cmp)(ti->heap[ti->ptr[child]],
288 ti->heap[ti->ptr[1+child]]) > 0)
290 if ((*ti->cmp)(ti->heap[ti->ptr[cur]],
291 ti->heap[ti->ptr[child]]) > 0)
293 heap_swap (ti, cur, child);
302 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
306 cur = ++(ti->heapnum);
307 memcpy (ti->heap[ti->ptr[cur]], buf, ti->keysize);
308 ti->indx[ti->ptr[cur]] = indx;
310 while (parent && (*ti->cmp)(ti->heap[ti->ptr[parent]],
311 ti->heap[ti->ptr[cur]]) > 0)
313 heap_swap (ti, cur, parent);
320 struct trunc_info *heap_init (int size, int key_size,
321 int (*cmp)(const void *p1, const void *p2))
323 struct trunc_info *ti = xmalloc (sizeof(*ti));
328 ti->keysize = key_size;
330 ti->indx = xmalloc (size * sizeof(*ti->indx));
331 ti->heap = xmalloc (size * sizeof(*ti->heap));
332 ti->ptr = xmalloc (size * sizeof(*ti->ptr));
333 ti->swapbuf = xmalloc (ti->keysize);
334 ti->tmpbuf = xmalloc (ti->keysize);
335 ti->buf = xmalloc (size * ti->keysize);
336 for (i = size; --i >= 0; )
339 ti->heap[i] = ti->buf + ti->keysize * i;
344 static void heap_close (struct trunc_info *ti)
354 static RSET rset_trunc_r (ISAM isam, ISAM_P *isam_p, int from, int to,
359 rset_temp_parms parms;
361 parms.key_size = sizeof(struct it_key);
362 result = rset_create (rset_kind_temp, &parms);
363 result_rsfd = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
365 if (to - from > merge_chunk)
369 int i, i_add = (to-from)/merge_chunk + 1;
370 struct trunc_info *ti;
372 int rsmax = (to-from)/i_add + 1;
374 rset = xmalloc (sizeof(*rset) * rsmax);
375 rsfd = xmalloc (sizeof(*rsfd) * rsmax);
377 for (i = from; i < to; i += i_add)
380 rset[rscur] = rset_trunc_r (isam, isam_p, i, i+i_add,
383 rset[rscur] = rset_trunc_r (isam, isam_p, i, to,
387 ti = heap_init (rscur, sizeof(struct it_key), key_compare);
388 for (i = rscur; --i >= 0; )
390 rsfd[i] = rset_open (rset[i], RSETF_READ|RSETF_SORT_SYSNO);
391 if (rset_read (rset[i], rsfd[i], ti->tmpbuf))
392 heap_insert (ti, ti->tmpbuf, i);
395 rset_close (rset[i], rsfd[i]);
396 rset_delete (rset[i]);
401 int n = ti->indx[ti->ptr[1]];
403 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
407 if (!rset_read (rset[n], rsfd[n], ti->tmpbuf))
410 rset_close (rset[n], rsfd[n]);
411 rset_delete (rset[n]);
414 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
417 heap_insert (ti, ti->tmpbuf, n);
430 struct trunc_info *ti;
432 ispt = xmalloc (sizeof(*ispt) * (to-from));
434 ti = heap_init (to-from, sizeof(struct it_key),
436 for (i = to-from; --i >= 0; )
438 ispt[i] = is_position (isam, isam_p[from+i]);
439 if (is_readkey (ispt[i], ti->tmpbuf))
440 heap_insert (ti, ti->tmpbuf, i);
442 is_pt_free (ispt[i]);
446 int n = ti->indx[ti->ptr[1]];
448 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
450 /* section that preserve all keys */
452 if (is_readkey (ispt[n], ti->tmpbuf))
453 heap_insert (ti, ti->tmpbuf, n);
455 is_pt_free (ispt[n]);
457 /* section that preserve all keys with unique sysnos */
460 if (!is_readkey (ispt[n], ti->tmpbuf))
463 is_pt_free (ispt[n]);
466 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
469 heap_insert (ti, ti->tmpbuf, n);
478 rset_close (result, result_rsfd);
482 static int isam_trunc_cmp (const void *p1, const void *p2)
484 ISAM_P i1 = *(ISAM_P*) p1;
485 ISAM_P i2 = *(ISAM_P*) p2;
488 d = is_type (i1) - is_type (i2);
491 return is_block (i1) - is_block (i2);
494 static RSET rset_trunc (ISAM isam, ISAM_P *isam_p, int no)
497 qsort (isam_p, no, sizeof(*isam_p), isam_trunc_cmp);
498 return rset_trunc_r (isam, isam_p, 0, no, 100);
507 static void add_isam_p (const char *info, struct grep_info *p)
509 if (p->isam_p_indx == p->isam_p_size)
511 ISAM_P *new_isam_p_buf;
513 p->isam_p_size = 2*p->isam_p_size + 100;
514 new_isam_p_buf = xmalloc (sizeof(*new_isam_p_buf) *
518 memcpy (new_isam_p_buf, p->isam_p_buf,
519 p->isam_p_indx * sizeof(*p->isam_p_buf));
520 xfree (p->isam_p_buf);
522 p->isam_p_buf = new_isam_p_buf;
524 assert (*info == sizeof(*p->isam_p_buf));
525 memcpy (p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
529 static int grep_handle (char *name, const char *info, void *p)
531 logf (LOG_DEBUG, "dict name: %s", name);
532 add_isam_p (info, p);
536 static void gen_regular_rel (char *dst, int val, int islt)
544 sprintf (numstr, "%d", val);
545 for (w = strlen(numstr); --w >= 0; pos++)
564 strcpy (dst + dst_p, numstr);
565 dst_p = strlen(dst) - pos - 1;
593 for (i = 0; i<pos; i++)
606 for (i=1; i<pos; i++)
607 strcat (dst, "[0-9]?");
611 for (i = 0; i <= pos; i++)
612 strcat (dst, "[0-9]");
613 strcat (dst, "[0-9]*");
618 static int relational_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
619 const char *term_sub,
621 oid_value attributeSet,
622 struct grep_info *grep_info,
630 attr_init (&relation, zapt, 2);
631 relation_value = attr_find (&relation, NULL);
632 term_value = atoi (term_sub);
634 switch (relation_value)
639 logf (LOG_DEBUG, "Relation <");
640 gen_regular_rel (term_dict + strlen(term_dict), term_value-1, 1);
645 logf (LOG_DEBUG, "Relation <=");
646 gen_regular_rel (term_dict + strlen(term_dict), term_value, 1);
651 logf (LOG_DEBUG, "Relation >=");
652 gen_regular_rel (term_dict + strlen(term_dict), term_value, 0);
657 logf (LOG_DEBUG, "Relation >");
658 gen_regular_rel (term_dict + strlen(term_dict), term_value+1, 0);
663 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_dict);
664 r = dict_lookup_grep (zi->wordDict, term_dict, 0, grep_info, max_pos,
667 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
668 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
672 static void verbatim_char (int ch, int *indx, char *dst)
675 dst[(*indx)++] = '\\';
679 static int field_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
680 const char *term_sub, int regType,
681 oid_value attributeSet, struct grep_info *grep_info,
682 int num_bases, char **basenames)
684 char term_dict[2*IT_MAX_WORD+2];
685 int i, j, r, base_no;
687 int truncation_value;
690 oid_value curAttributeSet = attributeSet;
692 attr_init (&use, zapt, 1);
693 use_value = attr_find (&use, &curAttributeSet);
694 logf (LOG_DEBUG, "use value %d", use_value);
695 attr_init (&truncation, zapt, 5);
696 truncation_value = attr_find (&truncation, NULL);
697 logf (LOG_DEBUG, "truncation value %d", truncation_value);
702 for (base_no = 0; base_no < num_bases; base_no++)
705 data1_local_attribute *local_attr;
706 int max_pos, prefix_len = 0;
708 attp = att_getentbyatt (curAttributeSet, use_value);
711 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
712 curAttributeSet, use_value);
716 if (zebTargetInfo_curDatabase (zi->zti, basenames[base_no]))
718 zi->errCode = 109; /* Database unavailable */
719 zi->errString = basenames[base_no];
722 for (local_attr = attp->local_attributes; local_attr;
723 local_attr = local_attr->next)
727 ord = zebTargetInfo_lookupSU (zi->zti, attp->attset_ordinal,
732 term_dict[prefix_len++] = '|';
734 term_dict[prefix_len++] = '(';
735 if ((ord >= 'A' && ord <= 'Z') || (ord >= 'a' && ord <= 'z'))
736 term_dict[prefix_len++] = ord;
739 term_dict[prefix_len++] = '\\';
740 term_dict[prefix_len++] = ord;
748 term_dict[prefix_len++] = ')';
749 term_dict[prefix_len++] = regType;
750 term_dict[prefix_len] = '\0';
751 if (!relational_term (zi, zapt, term_sub, term_dict,
752 attributeSet, grep_info, &max_pos))
757 switch (truncation_value)
759 case -1: /* not specified */
760 case 100: /* do not truncate */
761 term_dict[j++] = '(';
762 for (i = 0; term_sub[i]; i++)
763 verbatim_char (term_sub[i], &j, term_dict);
764 strcpy (term_dict+j, ")");
765 r = dict_lookup_grep (zi->wordDict, term_dict, 0, grep_info,
766 &max_pos, 0, grep_handle);
768 logf (LOG_WARN, "dict_lookup_grep err, trunc=none:%d", r);
770 case 1: /* right truncation */
771 term_dict[j++] = '(';
772 for (i = 0; term_sub[i]; i++)
773 verbatim_char (term_sub[i], &j, term_dict);
774 strcpy (term_dict+j, ".*)");
775 dict_lookup_grep (zi->wordDict, term_dict, 0, grep_info,
776 &max_pos, 0, grep_handle);
778 case 2: /* left truncation */
779 case 3: /* left&right truncation */
782 case 101: /* process # in term */
783 term_dict[j++] = '(';
784 for (i=0; term_sub[i]; i++)
785 if (term_sub[i] == '#' && i > 2)
787 term_dict[j++] = '.';
788 term_dict[j++] = '*';
791 verbatim_char (term_sub[i], &j, term_dict);
792 strcpy (term_dict+j, ")");
793 r = dict_lookup_grep (zi->wordDict, term_dict, 0, grep_info,
794 &max_pos, 0, grep_handle);
796 logf (LOG_WARN, "dict_lookup_grep err, trunc=#: %d",
799 case 102: /* regular expression */
800 sprintf (term_dict + j, "(%s)", term_sub);
801 r = dict_lookup_grep (zi->wordDict, term_dict, 0, grep_info,
802 &max_pos, 0, grep_handle);
804 logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
807 case 103: /* regular expression with error correction */
810 if (*cp == '*' && cp[1] && cp[2])
815 sprintf (term_dict + j, "(%s)", cp);
816 r = dict_lookup_grep (zi->wordDict, term_dict, r, grep_info,
817 &max_pos, j, grep_handle);
819 logf (LOG_WARN, "dict_lookup_grep err, trunc=eregular: %d",
825 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
829 static void trans_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
834 Z_Term *term = zapt->term;
836 sizez = term->u.general->len;
837 if (sizez > IT_MAX_WORD-1)
838 sizez = IT_MAX_WORD-1;
839 for (i = 0; i < sizez; i++)
841 if (!isalnum(term->u.general->buf[i]))
847 *termz++ = index_char_cvt (term->u.general->buf[i]);
856 static RSET rpn_search_APT_relevance (ZServerInfo *zi,
857 Z_AttributesPlusTerm *zapt,
858 oid_value attributeSet,
859 int num_bases, char **basenames)
861 rset_relevance_parms parms;
862 char termz[IT_MAX_WORD+1];
863 char term_sub[IT_MAX_WORD+1];
864 struct grep_info grep_info;
865 char *p0 = termz, *p1 = NULL;
868 parms.key_size = sizeof(struct it_key);
870 parms.cmp = key_compare;
871 parms.is = zi->wordIsam;
873 if (zapt->term->which != Z_Term_general)
878 trans_term (zi, zapt, termz);
880 grep_info.isam_p_indx = 0;
881 grep_info.isam_p_size = 0;
882 grep_info.isam_p_buf = NULL;
885 if ((p1 = strchr (p0, ' ')))
887 memcpy (term_sub, p0, p1-p0);
888 term_sub[p1-p0] = '\0';
891 strcpy (term_sub, p0);
892 if (field_term (zi, zapt, term_sub, 'w', attributeSet, &grep_info,
893 num_bases, basenames))
901 parms.isam_positions = grep_info.isam_p_buf;
902 parms.no_isam_positions = grep_info.isam_p_indx;
903 if (grep_info.isam_p_indx > 0)
904 result = rset_create (rset_kind_relevance, &parms);
906 result = rset_create (rset_kind_null, NULL);
907 xfree (grep_info.isam_p_buf);
911 static RSET rpn_search_APT_cphrase (ZServerInfo *zi,
912 Z_AttributesPlusTerm *zapt,
913 oid_value attributeSet,
914 int num_bases, char **basenames)
916 rset_isam_parms parms;
917 char termz[IT_MAX_WORD+1];
918 struct grep_info grep_info;
921 if (zapt->term->which != Z_Term_general)
926 trans_term (zi, zapt, termz);
928 grep_info.isam_p_indx = 0;
929 grep_info.isam_p_size = 0;
930 grep_info.isam_p_buf = NULL;
932 if (field_term (zi, zapt, termz, 'p', attributeSet, &grep_info,
933 num_bases, basenames))
935 if (grep_info.isam_p_indx < 1)
936 result = rset_create (rset_kind_null, NULL);
937 else if (grep_info.isam_p_indx == 1)
939 parms.is = zi->wordIsam;
940 parms.pos = *grep_info.isam_p_buf;
941 result = rset_create (rset_kind_isam, &parms);
944 result = rset_trunc (zi->wordIsam, grep_info.isam_p_buf,
945 grep_info.isam_p_indx);
946 xfree (grep_info.isam_p_buf);
949 static RSET rpn_search_APT_word (ZServerInfo *zi,
950 Z_AttributesPlusTerm *zapt,
951 oid_value attributeSet,
952 int num_bases, char **basenames)
954 rset_isam_parms parms;
955 char termz[IT_MAX_WORD+1];
956 struct grep_info grep_info;
959 if (zapt->term->which != Z_Term_general)
964 trans_term (zi, zapt, termz);
966 grep_info.isam_p_indx = 0;
967 grep_info.isam_p_size = 0;
968 grep_info.isam_p_buf = NULL;
970 if (field_term (zi, zapt, termz, 'w', attributeSet, &grep_info,
971 num_bases, basenames))
973 if (grep_info.isam_p_indx < 1)
974 result = rset_create (rset_kind_null, NULL);
975 else if (grep_info.isam_p_indx == 1)
977 parms.is = zi->wordIsam;
978 parms.pos = *grep_info.isam_p_buf;
979 result = rset_create (rset_kind_isam, &parms);
982 result = rset_trunc (zi->wordIsam, grep_info.isam_p_buf,
983 grep_info.isam_p_indx);
984 xfree (grep_info.isam_p_buf);
988 static RSET rpn_prox (RSET *rset, int rset_no)
996 rset_temp_parms parms;
998 rsfd = xmalloc (sizeof(*rsfd)*rset_no);
999 more = xmalloc (sizeof(*more)*rset_no);
1000 buf = xmalloc (sizeof(*buf)*rset_no);
1002 for (i = 0; i<rset_no; i++)
1004 buf[i] = xmalloc (sizeof(**buf));
1005 rsfd[i] = rset_open (rset[i], RSETF_READ|RSETF_SORT_SYSNO);
1006 if (!(more[i] = rset_read (rset[i], rsfd[i], buf[i])))
1010 rset_close (rset[i], rsfd[i]);
1017 return rset_create (rset_kind_null, NULL);
1020 parms.key_size = sizeof (struct it_key);
1021 result = rset_create (rset_kind_temp, &parms);
1022 rsfd_result = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
1026 for (i = 1; i<rset_no; i++)
1035 cmp = key_compare (buf[i], buf[i-1]);
1038 more[i-1] = rset_read (rset[i-1], rsfd[i-1], buf[i-1]);
1043 if (buf[i-1]->seqno+1 != buf[i]->seqno)
1045 more[i-1] = rset_read (rset[i-1], rsfd[i-1], buf[i-1]);
1051 more[i] = rset_read (rset[i], rsfd[i], buf[i]);
1057 rset_write (result, rsfd_result, buf[0]);
1058 more[0] = rset_read (*rset, *rsfd, *buf);
1062 for (i = 0; i<rset_no; i++)
1064 rset_close (rset[i], rsfd[i]);
1067 rset_close (result, rsfd_result);
1074 static RSET rpn_search_APT_phrase (ZServerInfo *zi,
1075 Z_AttributesPlusTerm *zapt,
1076 oid_value attributeSet,
1077 int num_bases, char **basenames)
1079 char termz[IT_MAX_WORD+1];
1080 char term_sub[IT_MAX_WORD+1];
1081 char *p0 = termz, *p1 = NULL;
1082 RSET rset[60], result;
1084 struct grep_info grep_info;
1086 if (zapt->term->which != Z_Term_general)
1091 trans_term (zi, zapt, termz);
1093 grep_info.isam_p_size = 0;
1094 grep_info.isam_p_buf = NULL;
1098 if ((p1 = strchr (p0, ' ')))
1100 memcpy (term_sub, p0, p1-p0);
1101 term_sub[p1-p0] = '\0';
1104 strcpy (term_sub, p0);
1106 grep_info.isam_p_indx = 0;
1107 if (field_term (zi, zapt, term_sub, 'w', attributeSet, &grep_info,
1108 num_bases, basenames))
1110 if (grep_info.isam_p_indx == 0)
1111 rset[rset_no] = rset_create (rset_kind_null, NULL);
1112 else if (grep_info.isam_p_indx > 1)
1113 rset[rset_no] = rset_trunc (zi->wordIsam,
1114 grep_info.isam_p_buf,
1115 grep_info.isam_p_indx);
1118 rset_isam_parms parms;
1120 parms.is = zi->wordIsam;
1121 parms.pos = *grep_info.isam_p_buf;
1122 rset[rset_no] = rset_create (rset_kind_isam, &parms);
1124 assert (rset[rset_no]);
1125 if (++rset_no >= sizeof(rset)/sizeof(*rset))
1130 while (*++p0 == ' ')
1133 xfree (grep_info.isam_p_buf);
1135 return rset_create (rset_kind_null, NULL);
1136 else if (rset_no == 1)
1139 result = rpn_prox (rset, rset_no);
1140 for (i = 0; i<rset_no; i++)
1141 rset_delete (rset[i]);
1145 static RSET rpn_search_APT_local (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1146 oid_value attributeSet)
1151 rset_temp_parms parms;
1152 char termz[IT_MAX_WORD+1];
1154 if (zapt->term->which != Z_Term_general)
1159 parms.key_size = sizeof (struct it_key);
1160 result = rset_create (rset_kind_temp, &parms);
1161 rsfd = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
1163 trans_term (zi, zapt, termz);
1165 key.sysno = atoi (termz);
1168 rset_write (result, rsfd, &key);
1169 rset_close (result, rsfd);
1173 static RSET rpn_search_APT (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1174 oid_value attributeSet,
1175 int num_bases, char **basenames)
1179 AttrType completeness;
1180 int relation_value, structure_value, completeness_value;
1182 attr_init (&relation, zapt, 2);
1183 attr_init (&structure, zapt, 4);
1184 attr_init (&completeness, zapt, 6);
1186 relation_value = attr_find (&relation, NULL);
1187 structure_value = attr_find (&structure, NULL);
1188 completeness_value = attr_find (&completeness, NULL);
1189 switch (structure_value)
1192 if (relation_value == 102) /* relevance relation */
1193 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1194 num_bases, basenames);
1195 if (completeness_value == 2 || completeness_value == 3)
1196 return rpn_search_APT_cphrase (zi, zapt, attributeSet,
1197 num_bases, basenames);
1198 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1199 num_bases, basenames);
1200 case 1: /* phrase */
1201 if (relation_value == 102) /* relevance relation */
1202 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1203 num_bases, basenames);
1204 if (completeness_value == 2 || completeness_value == 3)
1205 return rpn_search_APT_cphrase (zi, zapt, attributeSet,
1206 num_bases, basenames);
1207 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1208 num_bases, basenames);
1211 if (relation_value == 102) /* relevance relation */
1212 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1213 num_bases, basenames);
1214 if (completeness_value == 2 || completeness_value == 3)
1215 return rpn_search_APT_cphrase (zi, zapt, attributeSet,
1216 num_bases, basenames);
1217 return rpn_search_APT_word (zi, zapt, attributeSet,
1218 num_bases, basenames);
1223 case 5: /* date - normalized */
1225 case 6: /* word list */
1226 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1227 num_bases, basenames);
1228 case 100: /* date - un-normalized */
1230 case 101: /* name - normalized */
1232 case 102: /* date - un-normalized */
1234 case 103: /* structure */
1238 case 105: /* free-form-text */
1239 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1240 num_bases, basenames);
1241 case 106: /* document-text */
1242 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1243 num_bases, basenames);
1244 case 107: /* local-number */
1245 return rpn_search_APT_local (zi, zapt, attributeSet);
1246 case 108: /* string */
1247 return rpn_search_APT_word (zi, zapt, attributeSet,
1248 num_bases, basenames);
1249 case 109: /* numeric string */
1256 static RSET rpn_search_ref (ZServerInfo *zi, Z_ResultSetId *resultSetId)
1260 if (!(s = resultSetGet (zi, resultSetId)))
1261 return rset_create (rset_kind_null, NULL);
1265 static RSET rpn_search_structure (ZServerInfo *zi, Z_RPNStructure *zs,
1266 oid_value attributeSet,
1267 int num_bases, char **basenames)
1270 if (zs->which == Z_RPNStructure_complex)
1272 rset_bool_parms bool_parms;
1275 bool_parms.rset_l = rpn_search_structure (zi, zs->u.complex->s1,
1277 num_bases, basenames);
1278 if (bool_parms.rset_l == NULL)
1280 if (rset_is_ranked(bool_parms.rset_l))
1282 bool_parms.rset_r = rpn_search_structure (zi, zs->u.complex->s2,
1284 num_bases, basenames);
1285 if (bool_parms.rset_r == NULL)
1287 rset_delete (bool_parms.rset_l);
1290 if (rset_is_ranked(bool_parms.rset_r))
1292 bool_parms.key_size = sizeof(struct it_key);
1293 bool_parms.cmp = key_compare;
1295 switch (zs->u.complex->roperator->which)
1297 case Z_Operator_and:
1298 r = rset_create (soft ? rset_kind_sand:rset_kind_and, &bool_parms);
1301 r = rset_create (soft ? rset_kind_sor:rset_kind_or, &bool_parms);
1303 case Z_Operator_and_not:
1304 r = rset_create (soft ? rset_kind_snot:rset_kind_not, &bool_parms);
1310 else if (zs->which == Z_RPNStructure_simple)
1312 if (zs->u.simple->which == Z_Operand_APT)
1314 logf (LOG_DEBUG, "rpn_search_APT");
1315 r = rpn_search_APT (zi, zs->u.simple->u.attributesPlusTerm,
1316 attributeSet, num_bases, basenames);
1318 else if (zs->u.simple->which == Z_Operand_resultSetId)
1320 logf (LOG_DEBUG, "rpn_search_ref");
1321 r = rpn_search_ref (zi, zs->u.simple->u.resultSetId);
1335 void count_set_save (RSET *r, int *count)
1342 rset_temp_parms parms;
1344 logf (LOG_DEBUG, "count_set_save");
1346 parms.key_size = sizeof(struct it_key);
1347 w = rset_create (rset_kind_temp, &parms);
1348 wfd = rset_open (w, RSETF_WRITE|RSETF_SORT_SYSNO);
1349 rfd = rset_open (*r, RSETF_READ|RSETF_SORT_SYSNO);
1350 while (rset_read (*r, rfd, &key))
1352 if (key.sysno != psysno)
1354 rset_write (w, wfd, &key);
1360 rset_close (*r, rfd);
1362 rset_close (w, wfd);
1364 logf (LOG_DEBUG, "%d keys, %d distinct sysnos", kno, *count);
1367 static void count_set (RSET r, int *count)
1374 logf (LOG_DEBUG, "count_set");
1376 rfd = rset_open (r, RSETF_READ|RSETF_SORT_SYSNO);
1377 while (rset_read (r, rfd, &key))
1379 if (key.sysno != psysno)
1386 rset_close (r, rfd);
1387 logf (LOG_DEBUG, "%d keys, %d distinct sysnos", kno, *count);
1390 int rpn_search (ZServerInfo *zi,
1391 Z_RPNQuery *rpn, int num_bases, char **basenames,
1392 const char *setname, int *hits)
1396 oid_value attributeSet;
1401 zi->errString = NULL;
1403 attrset = oid_getentbyoid (rpn->attributeSetId);
1404 attributeSet = attrset->value;
1405 rset = rpn_search_structure (zi, rpn->RPNStructure, attributeSet,
1406 num_bases, basenames);
1409 if (rset_is_volatile(rset))
1410 count_set_save(&rset,hits);
1412 count_set (rset, hits);
1413 resultSetAdd (zi, setname, 1, rset);
1415 logf (LOG_DEBUG, "search error: %d", zi->errCode);
1419 struct scan_info_entry {
1425 struct scan_info_entry *list;
1431 static int scan_handle (char *name, const char *info, int pos, void *client)
1433 int len_prefix, idx;
1434 struct scan_info *scan_info = client;
1436 len_prefix = strlen(scan_info->prefix);
1437 if (memcmp (name, scan_info->prefix, len_prefix))
1440 idx = scan_info->after - pos + scan_info->before;
1443 logf (LOG_DEBUG, "%-3d %s", idx, name+len_prefix);
1444 scan_info->list[idx].term = odr_malloc (scan_info->odr,
1445 strlen(name + len_prefix)+1);
1446 strcpy (scan_info->list[idx].term, name + len_prefix);
1447 assert (*info == sizeof(ISAM_P));
1448 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAM_P));
1453 int rpn_scan (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1454 oid_value attributeset,
1455 int num_bases, char **basenames,
1456 int *position, int *num_entries, struct scan_entry **list,
1460 int pos = *position;
1461 int num = *num_entries;
1465 char termz[IT_MAX_WORD+20];
1468 AttrType completeness;
1469 int completeness_value;
1470 struct scan_info *scan_info_array;
1471 struct scan_entry *glist;
1472 int ords[32], ord_no = 0;
1475 logf (LOG_DEBUG, "scan, position = %d, num = %d", pos, num);
1477 attr_init (&use, zapt, 1);
1478 use_value = attr_find (&use, NULL);
1479 logf (LOG_DEBUG, "use value %d", use_value);
1481 attr_init (&completeness, zapt, 6);
1482 completeness_value = attr_find (&completeness, NULL);
1483 logf (LOG_DEBUG, "completeness value %d", completeness_value);
1485 if (attributeset == VAL_NONE)
1486 attributeset = VAL_BIB1;
1488 if (use_value == -1)
1490 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
1493 data1_local_attribute *local_attr;
1495 attp = att_getentbyatt (attributeset, use_value);
1498 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
1499 attributeset, use_value);
1500 return zi->errCode = 114;
1502 if (zebTargetInfo_curDatabase (zi->zti, basenames[base_no]))
1504 zi->errString = basenames[base_no];
1505 return zi->errCode = 109; /* Database unavailable */
1507 for (local_attr = attp->local_attributes; local_attr && ord_no < 32;
1508 local_attr = local_attr->next)
1512 ord = zebTargetInfo_lookupSU (zi->zti, attp->attset_ordinal,
1515 ords[ord_no++] = ord;
1519 return zi->errCode = 113;
1522 scan_info_array = odr_malloc (zi->odr, ord_no * sizeof(*scan_info_array));
1523 for (i = 0; i < ord_no; i++)
1525 int j, prefix_len = 0;
1526 int before_tmp = before, after_tmp = after;
1527 struct scan_info *scan_info = scan_info_array + i;
1529 scan_info->before = before;
1530 scan_info->after = after;
1531 scan_info->odr = zi->odr;
1533 scan_info->list = odr_malloc (zi->odr, (before+after)*
1534 sizeof(*scan_info->list));
1535 for (j = 0; j<before+after; j++)
1536 scan_info->list[j].term = NULL;
1537 termz[prefix_len++] = ords[i];
1538 termz[prefix_len++] =
1539 (completeness_value==2 || completeness_value==3) ? 'p': 'w';
1540 termz[prefix_len] = 0;
1541 strcpy (scan_info->prefix, termz);
1542 trans_term (zi, zapt, termz+prefix_len);
1543 dict_scan (zi->wordDict, termz, &before_tmp, &after_tmp, scan_info,
1546 glist = odr_malloc (zi->odr, (before+after)*sizeof(*glist));
1547 for (i = 0; i < ord_no; i++)
1550 *status = BEND_SCAN_SUCCESS;
1551 for (i = 0; i<after; i++)
1554 const char *mterm = NULL;
1557 rset_isam_parms parms;
1559 for (j = 0; j < ord_no; j++)
1561 if (ptr[j] < before+after &&
1562 (tst=scan_info_array[j].list[ptr[j]].term) &&
1563 (!mterm || strcmp (tst, mterm) < 0))
1571 glist[i+before].term = odr_malloc (zi->odr, strlen(mterm)+1);
1572 strcpy (glist[i+before].term, mterm);
1574 parms.is = zi->wordIsam;
1575 parms.pos = scan_info_array[j0].list[ptr[j0]].isam_p;
1576 rset = rset_create (rset_kind_isam, &parms);
1579 for (j = j0+1; j<ord_no; j++)
1581 if (ptr[j] < before+after &&
1582 (tst=scan_info_array[j].list[ptr[j]].term) &&
1583 !strcmp (tst, mterm))
1585 rset_isam_parms parms;
1586 rset_bool_parms bool_parms;
1589 parms.is = zi->wordIsam;
1590 parms.pos = scan_info_array[j].list[ptr[j]].isam_p;
1591 rset2 = rset_create (rset_kind_isam, &parms);
1593 bool_parms.key_size = sizeof(struct it_key);
1594 bool_parms.cmp = key_compare;
1595 bool_parms.rset_l = rset;
1596 bool_parms.rset_r = rset2;
1598 rset = rset_create (rset_kind_or, &bool_parms);
1603 count_set (rset, &glist[i+before].occurrences);
1608 *num_entries -= (after-i);
1609 *status = BEND_SCAN_PARTIAL;
1612 for (i = 0; i<ord_no; i++)
1615 for (i = 0; i<before; i++)
1618 const char *mterm = NULL;
1621 rset_isam_parms parms;
1623 for (j = 0; j <ord_no; j++)
1625 if (ptr[j] < before &&
1626 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
1627 (!mterm || strcmp (tst, mterm) > 0))
1635 glist[before-1-i].term = odr_malloc (zi->odr, strlen(mterm)+1);
1636 strcpy (glist[before-1-i].term, mterm);
1638 parms.is = zi->wordIsam;
1639 parms.pos = scan_info_array[j0].list[before-1-ptr[j0]].isam_p;
1640 rset = rset_create (rset_kind_isam, &parms);
1644 for (j = j0+1; j<ord_no; j++)
1646 if (ptr[j] < before &&
1647 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
1648 !strcmp (tst, mterm))
1650 rset_isam_parms parms;
1651 rset_bool_parms bool_parms;
1654 parms.is = zi->wordIsam;
1655 parms.pos = scan_info_array[j].list[before-1-ptr[j]].isam_p;
1656 rset2 = rset_create (rset_kind_isam, &parms);
1658 bool_parms.key_size = sizeof(struct it_key);
1659 bool_parms.cmp = key_compare;
1660 bool_parms.rset_l = rset;
1661 bool_parms.rset_r = rset2;
1663 rset = rset_create (rset_kind_or, &bool_parms);
1668 count_set (rset, &glist[before-1-i].occurrences);
1674 *status = BEND_SCAN_PARTIAL;
1678 *list = glist + i; /* list is set to first 'real' entry */
1680 logf (LOG_DEBUG, "position = %d, num_entries = %d",
1681 *position, *num_entries);
1683 logf (LOG_DEBUG, "scan error: %d", zi->errCode);