2 * Copyright (C) 1994-1996, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.46 1996-05-15 11:57:56 adam
8 * Fixed bug introduced by set/field mapping in search operations.
10 * Revision 1.45 1996/05/14 11:34:00 adam
11 * Scan support in multiple registers/databases.
13 * Revision 1.44 1996/05/14 06:16:44 adam
14 * Compact use/set bytes used in search service.
16 * Revision 1.43 1996/05/09 09:54:43 adam
17 * Server supports maps from one logical attributes to a list of physical
19 * The extraction process doesn't make space consuming 'any' keys.
21 * Revision 1.42 1996/05/09 07:28:56 quinn
22 * Work towards phrases and multiple registers
24 * Revision 1.41 1996/03/20 09:36:43 adam
25 * Function dict_lookup_grep got extra parameter, init_pos, which marks
26 * from which position in pattern approximate pattern matching should occur.
27 * Approximate pattern matching is used in relevance=re-2.
29 * Revision 1.40 1996/02/02 13:44:44 adam
30 * The public dictionary functions simply use char instead of Dict_char
31 * to represent search strings. Dict_char is used internally only.
33 * Revision 1.39 1996/01/03 16:22:13 quinn
36 * Revision 1.38 1995/12/11 09:12:55 adam
37 * The rec_get function returns NULL if record doesn't exist - will
38 * happen in the server if the result set records have been deleted since
39 * the creation of the set (i.e. the search).
40 * The server saves a result temporarily if it is 'volatile', i.e. the
41 * set is register dependent.
43 * Revision 1.37 1995/12/06 15:05:28 adam
44 * More verbose in count_set.
46 * Revision 1.36 1995/12/06 12:41:27 adam
47 * New command 'stat' for the index program.
48 * Filenames can be read from stdin by specifying '-'.
49 * Bug fix/enhancement of the transformation from terms to regular
50 * expressons in the search engine.
52 * Revision 1.35 1995/11/27 09:29:00 adam
53 * Bug fixes regarding conversion to regular expressions.
55 * Revision 1.34 1995/11/16 17:00:56 adam
56 * Better logging of rpn query.
58 * Revision 1.33 1995/11/01 13:58:28 quinn
59 * Moving data1 to yaz/retrieval
61 * Revision 1.32 1995/10/27 14:00:11 adam
62 * Implemented detection of database availability.
64 * Revision 1.31 1995/10/17 18:02:10 adam
65 * New feature: databases. Implemented as prefix to words in dictionary.
67 * Revision 1.30 1995/10/16 09:32:38 adam
68 * More work on relational op.
70 * Revision 1.29 1995/10/13 16:01:49 adam
73 * Revision 1.28 1995/10/13 12:26:43 adam
74 * Optimization of truncation.
76 * Revision 1.27 1995/10/12 17:07:22 adam
79 * Revision 1.26 1995/10/12 12:40:54 adam
80 * Bug fixes in rpn_prox.
82 * Revision 1.25 1995/10/10 13:59:24 adam
83 * Function rset_open changed its wflag parameter to general flags.
85 * Revision 1.24 1995/10/09 16:18:37 adam
86 * Function dict_lookup_grep got extra client data parameter.
88 * Revision 1.23 1995/10/06 16:33:37 adam
89 * Use attribute mappings.
91 * Revision 1.22 1995/10/06 15:07:39 adam
92 * Structure 'local-number' handled.
94 * Revision 1.21 1995/10/06 13:52:06 adam
95 * Bug fixes. Handler may abort further scanning.
97 * Revision 1.20 1995/10/06 11:06:33 adam
98 * Scan entries include 'occurrences' now.
100 * Revision 1.19 1995/10/06 10:43:56 adam
101 * Scan added. 'occurrences' in scan entries not set yet.
103 * Revision 1.18 1995/10/04 16:57:20 adam
104 * Key input and merge sort in one pass.
106 * Revision 1.17 1995/10/04 12:55:17 adam
107 * Bug fix in ranked search. Use=Any keys inserted.
109 * Revision 1.16 1995/10/02 16:24:40 adam
110 * Use attribute actually used in search requests.
112 * Revision 1.15 1995/10/02 15:18:52 adam
113 * New member in recRetrieveCtrl: diagnostic.
115 * Revision 1.14 1995/09/28 12:10:32 adam
116 * Bug fixes. Field prefix used in queries.
118 * Revision 1.13 1995/09/18 14:17:50 adam
121 * Revision 1.12 1995/09/15 14:45:21 adam
123 * Work on truncation.
125 * Revision 1.11 1995/09/14 11:53:27 adam
126 * First work on regular expressions/truncations.
128 * Revision 1.10 1995/09/11 15:23:26 adam
129 * More work on relevance search.
131 * Revision 1.9 1995/09/11 13:09:35 adam
132 * More work on relevance feedback.
134 * Revision 1.8 1995/09/08 14:52:27 adam
135 * Minor changes. Dictionary is lower case now.
137 * Revision 1.7 1995/09/07 13:58:36 adam
138 * New parameter: result-set file descriptor (RSFD) to support multiple
139 * positions within the same result-set.
140 * Boolean operators: and, or, not implemented.
141 * Result-set references.
143 * Revision 1.6 1995/09/06 16:11:18 adam
144 * Option: only one word key per file.
146 * Revision 1.5 1995/09/06 10:33:04 adam
147 * More work on present. Some log messages removed.
149 * Revision 1.4 1995/09/05 15:28:40 adam
150 * More work on search engine.
152 * Revision 1.3 1995/09/04 15:20:22 adam
155 * Revision 1.2 1995/09/04 12:33:43 adam
156 * Various cleanup. YAZ util used instead.
158 * Revision 1.1 1995/09/04 09:10:40 adam
159 * More work on index add/del/update.
160 * Merge sort implemented.
161 * Initial work on z39 server.
170 #include "attribute.h"
178 int index_word_prefix_map (char *string, oid_value attrSet, int attrUse,
183 logf (LOG_DEBUG, "oid_value attrSet = %d, attrUse = %d", attrSet, attrUse);
184 attp = att_getentbyatt (attrSet, attrUse);
187 logf (LOG_DEBUG, "ord=%d", attp->attset_ordinal);
188 return index_word_prefix (string, attp->attset_ordinal,
189 attp->local_attributes->local, basename);
196 Z_AttributesPlusTerm *zapt;
199 static int attr_find (AttrType *src, oid_value *attributeSetP)
201 while (src->major < src->zapt->num_attributes)
203 Z_AttributeElement *element;
205 element = src->zapt->attributeList[src->major];
206 if (src->type == *element->attributeType)
208 switch (element->which)
210 case Z_AttributeValue_numeric:
212 if (element->attributeSet && attributeSetP)
216 attrset = oid_getentbyoid (element->attributeSet);
217 *attributeSetP = attrset->value;
219 return *element->value.numeric;
221 case Z_AttributeValue_complex:
222 if (src->minor >= element->value.complex->num_list ||
223 element->value.complex->list[src->minor]->which !=
224 Z_StringOrNumeric_numeric)
227 if (element->attributeSet && attributeSetP)
231 attrset = oid_getentbyoid (element->attributeSet);
232 *attributeSetP = attrset->value;
234 return *element->value.complex->list[src->minor-1]->u.numeric;
244 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
258 int (*cmp)(const void *p1, const void *p2);
265 static void heap_swap (struct trunc_info *ti, int i1, int i2)
270 ti->ptr[i1] = ti->ptr[i2];
274 static void heap_delete (struct trunc_info *ti)
276 int cur = 1, child = 2;
278 heap_swap (ti, 1, ti->heapnum--);
279 while (child <= ti->heapnum) {
280 if (child < ti->heapnum &&
281 (*ti->cmp)(ti->heap[ti->ptr[child]],
282 ti->heap[ti->ptr[1+child]]) > 0)
284 if ((*ti->cmp)(ti->heap[ti->ptr[cur]],
285 ti->heap[ti->ptr[child]]) > 0)
287 heap_swap (ti, cur, child);
296 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
300 cur = ++(ti->heapnum);
301 memcpy (ti->heap[ti->ptr[cur]], buf, ti->keysize);
302 ti->indx[ti->ptr[cur]] = indx;
304 while (parent && (*ti->cmp)(ti->heap[ti->ptr[parent]],
305 ti->heap[ti->ptr[cur]]) > 0)
307 heap_swap (ti, cur, parent);
314 struct trunc_info *heap_init (int size, int key_size,
315 int (*cmp)(const void *p1, const void *p2))
317 struct trunc_info *ti = xmalloc (sizeof(*ti));
322 ti->keysize = key_size;
324 ti->indx = xmalloc (size * sizeof(*ti->indx));
325 ti->heap = xmalloc (size * sizeof(*ti->heap));
326 ti->ptr = xmalloc (size * sizeof(*ti->ptr));
327 ti->swapbuf = xmalloc (ti->keysize);
328 ti->tmpbuf = xmalloc (ti->keysize);
329 ti->buf = xmalloc (size * ti->keysize);
330 for (i = size; --i >= 0; )
333 ti->heap[i] = ti->buf + ti->keysize * i;
338 static void heap_close (struct trunc_info *ti)
348 static RSET rset_trunc_r (ISAM isam, ISAM_P *isam_p, int from, int to,
353 rset_temp_parms parms;
355 parms.key_size = sizeof(struct it_key);
356 result = rset_create (rset_kind_temp, &parms);
357 result_rsfd = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
359 if (to - from > merge_chunk)
363 int i, i_add = (to-from)/merge_chunk + 1;
364 struct trunc_info *ti;
366 int rsmax = (to-from)/i_add + 1;
368 rset = xmalloc (sizeof(*rset) * rsmax);
369 rsfd = xmalloc (sizeof(*rsfd) * rsmax);
371 for (i = from; i < to; i += i_add)
374 rset[rscur] = rset_trunc_r (isam, isam_p, i, i+i_add,
377 rset[rscur] = rset_trunc_r (isam, isam_p, i, to,
381 ti = heap_init (rscur, sizeof(struct it_key), key_compare);
382 for (i = rscur; --i >= 0; )
384 rsfd[i] = rset_open (rset[i], RSETF_READ|RSETF_SORT_SYSNO);
385 if (rset_read (rset[i], rsfd[i], ti->tmpbuf))
386 heap_insert (ti, ti->tmpbuf, i);
389 rset_close (rset[i], rsfd[i]);
390 rset_delete (rset[i]);
395 int n = ti->indx[ti->ptr[1]];
397 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
401 if (!rset_read (rset[n], rsfd[n], ti->tmpbuf))
404 rset_close (rset[n], rsfd[n]);
405 rset_delete (rset[n]);
408 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
411 heap_insert (ti, ti->tmpbuf, n);
424 struct trunc_info *ti;
426 ispt = xmalloc (sizeof(*ispt) * (to-from));
428 ti = heap_init (to-from, sizeof(struct it_key),
430 for (i = to-from; --i >= 0; )
432 ispt[i] = is_position (isam, isam_p[from+i]);
433 if (is_readkey (ispt[i], ti->tmpbuf))
434 heap_insert (ti, ti->tmpbuf, i);
436 is_pt_free (ispt[i]);
440 int n = ti->indx[ti->ptr[1]];
442 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
444 /* section that preserve all keys */
446 if (is_readkey (ispt[n], ti->tmpbuf))
447 heap_insert (ti, ti->tmpbuf, n);
449 is_pt_free (ispt[n]);
451 /* section that preserve all keys with unique sysnos */
454 if (!is_readkey (ispt[n], ti->tmpbuf))
457 is_pt_free (ispt[n]);
460 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
463 heap_insert (ti, ti->tmpbuf, n);
472 rset_close (result, result_rsfd);
476 static int isam_trunc_cmp (const void *p1, const void *p2)
478 ISAM_P i1 = *(ISAM_P*) p1;
479 ISAM_P i2 = *(ISAM_P*) p2;
482 d = is_type (i1) - is_type (i2);
485 return is_block (i1) - is_block (i2);
488 static RSET rset_trunc (ISAM isam, ISAM_P *isam_p, int no)
491 qsort (isam_p, no, sizeof(*isam_p), isam_trunc_cmp);
492 return rset_trunc_r (isam, isam_p, 0, no, 100);
501 static void add_isam_p (const char *info, struct grep_info *p)
503 if (p->isam_p_indx == p->isam_p_size)
505 ISAM_P *new_isam_p_buf;
507 p->isam_p_size = 2*p->isam_p_size + 100;
508 new_isam_p_buf = xmalloc (sizeof(*new_isam_p_buf) *
512 memcpy (new_isam_p_buf, p->isam_p_buf,
513 p->isam_p_indx * sizeof(*p->isam_p_buf));
514 xfree (p->isam_p_buf);
516 p->isam_p_buf = new_isam_p_buf;
518 assert (*info == sizeof(*p->isam_p_buf));
519 memcpy (p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
523 static int grep_handle (char *name, const char *info, void *p)
525 logf (LOG_DEBUG, "dict name: %s", name);
526 add_isam_p (info, p);
530 static void gen_regular_rel (char *dst, int val, int islt)
538 sprintf (numstr, "%d", val);
539 for (w = strlen(numstr); --w >= 0; pos++)
558 strcpy (dst + dst_p, numstr);
559 dst_p = strlen(dst) - pos - 1;
587 for (i = 0; i<pos; i++)
600 for (i=1; i<pos; i++)
601 strcat (dst, "[0-9]?");
605 for (i = 0; i <= pos; i++)
606 strcat (dst, "[0-9]");
607 strcat (dst, "[0-9]*");
612 static int relational_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
613 const char *term_sub,
615 oid_value attributeSet,
616 struct grep_info *grep_info,
624 attr_init (&relation, zapt, 2);
625 relation_value = attr_find (&relation, NULL);
626 term_value = atoi (term_sub);
628 switch (relation_value)
633 logf (LOG_DEBUG, "Relation <");
634 gen_regular_rel (term_dict + strlen(term_dict), term_value-1, 1);
639 logf (LOG_DEBUG, "Relation <=");
640 gen_regular_rel (term_dict + strlen(term_dict), term_value, 1);
645 logf (LOG_DEBUG, "Relation >=");
646 gen_regular_rel (term_dict + strlen(term_dict), term_value, 0);
651 logf (LOG_DEBUG, "Relation >");
652 gen_regular_rel (term_dict + strlen(term_dict), term_value+1, 0);
657 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_dict);
658 r = dict_lookup_grep (zi->wordDict, term_dict, 0, grep_info, max_pos,
661 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
662 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
666 static void verbatim_char (int ch, int *indx, char *dst)
669 dst[(*indx)++] = '\\';
673 static int field_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
674 const char *term_sub, int regType,
675 oid_value attributeSet, struct grep_info *grep_info,
676 int num_bases, char **basenames)
678 char term_dict[2*IT_MAX_WORD+2];
679 int i, j, r, base_no;
681 int truncation_value;
684 oid_value curAttributeSet = attributeSet;
686 attr_init (&use, zapt, 1);
687 use_value = attr_find (&use, &curAttributeSet);
688 logf (LOG_DEBUG, "use value %d", use_value);
689 attr_init (&truncation, zapt, 5);
690 truncation_value = attr_find (&truncation, NULL);
691 logf (LOG_DEBUG, "truncation value %d", truncation_value);
696 for (base_no = 0; base_no < num_bases; base_no++)
699 data1_local_attribute *local_attr;
700 int max_pos, prefix_len = 0;
702 attp = att_getentbyatt (curAttributeSet, use_value);
705 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
706 curAttributeSet, use_value);
710 if (zebTargetInfo_curDatabase (zi->zti, basenames[base_no]))
712 zi->errCode = 109; /* Database unavailable */
713 zi->errString = basenames[base_no];
715 for (local_attr = attp->local_attributes; local_attr;
716 local_attr = local_attr->next)
720 ord = zebTargetInfo_lookupSU (zi->zti, attp->attset_ordinal,
725 term_dict[prefix_len++] = '|';
727 term_dict[prefix_len++] = '(';
728 if ((ord >= 'A' && ord <= 'Z') || (ord >= 'a' && ord <= 'z'))
729 term_dict[prefix_len++] = ord;
732 term_dict[prefix_len++] = '\\';
733 term_dict[prefix_len++] = ord;
741 term_dict[prefix_len++] = ')';
742 term_dict[prefix_len++] = regType;
743 term_dict[prefix_len] = '\0';
744 if (!relational_term (zi, zapt, term_sub, term_dict,
745 attributeSet, grep_info, &max_pos))
750 switch (truncation_value)
752 case -1: /* not specified */
753 case 100: /* do not truncate */
754 term_dict[j++] = '(';
755 for (i = 0; term_sub[i]; i++)
756 verbatim_char (term_sub[i], &j, term_dict);
757 strcpy (term_dict+j, ")");
758 r = dict_lookup_grep (zi->wordDict, term_dict, 0, grep_info,
759 &max_pos, 0, grep_handle);
761 logf (LOG_WARN, "dict_lookup_grep err, trunc=none:%d", r);
763 case 1: /* right truncation */
764 term_dict[j++] = '(';
765 for (i = 0; term_sub[i]; i++)
766 verbatim_char (term_sub[i], &j, term_dict);
767 strcpy (term_dict+j, ".*)");
768 dict_lookup_grep (zi->wordDict, term_dict, 0, grep_info,
769 &max_pos, 0, grep_handle);
771 case 2: /* left truncation */
772 case 3: /* left&right truncation */
775 case 101: /* process # in term */
776 term_dict[j++] = '(';
777 for (i=0; term_sub[i]; i++)
778 if (term_sub[i] == '#' && i > 2)
780 term_dict[j++] = '.';
781 term_dict[j++] = '*';
784 verbatim_char (term_sub[i], &j, term_dict);
785 strcpy (term_dict+j, ")");
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=#: %d",
792 case 102: /* regular expression */
793 sprintf (term_dict + j, "(%s)", term_sub);
794 r = dict_lookup_grep (zi->wordDict, term_dict, 0, grep_info,
795 &max_pos, 0, grep_handle);
797 logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
800 case 103: /* regular expression with error correction */
803 if (*cp == '*' && cp[1] && cp[2])
808 sprintf (term_dict + j, "(%s)", cp);
809 r = dict_lookup_grep (zi->wordDict, term_dict, r, grep_info,
810 &max_pos, j, grep_handle);
812 logf (LOG_WARN, "dict_lookup_grep err, trunc=eregular: %d",
818 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
822 static void trans_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
826 Z_Term *term = zapt->term;
828 sizez = term->u.general->len;
829 if (sizez > IT_MAX_WORD-1)
830 sizez = IT_MAX_WORD-1;
831 for (i = 0; i < sizez; i++)
832 termz[i] = index_char_cvt (term->u.general->buf[i]);
836 static RSET rpn_search_APT_relevance (ZServerInfo *zi,
837 Z_AttributesPlusTerm *zapt,
838 oid_value attributeSet,
839 int num_bases, char **basenames)
841 rset_relevance_parms parms;
842 char termz[IT_MAX_WORD+1];
843 char term_sub[IT_MAX_WORD+1];
844 struct grep_info grep_info;
845 char *p0 = termz, *p1 = NULL;
848 parms.key_size = sizeof(struct it_key);
850 parms.cmp = key_compare;
851 parms.is = zi->wordIsam;
853 if (zapt->term->which != Z_Term_general)
858 trans_term (zi, zapt, termz);
860 grep_info.isam_p_indx = 0;
861 grep_info.isam_p_size = 0;
862 grep_info.isam_p_buf = NULL;
865 if ((p1 = strchr (p0, ' ')))
867 memcpy (term_sub, p0, p1-p0);
868 term_sub[p1-p0] = '\0';
871 strcpy (term_sub, p0);
872 if (field_term (zi, zapt, term_sub, 'w', attributeSet, &grep_info,
873 num_bases, basenames))
881 parms.isam_positions = grep_info.isam_p_buf;
882 parms.no_isam_positions = grep_info.isam_p_indx;
883 if (grep_info.isam_p_indx > 0)
884 result = rset_create (rset_kind_relevance, &parms);
886 result = rset_create (rset_kind_null, NULL);
887 xfree (grep_info.isam_p_buf);
891 static RSET rpn_search_APT_cphrase (ZServerInfo *zi,
892 Z_AttributesPlusTerm *zapt,
893 oid_value attributeSet,
894 int num_bases, char **basenames)
896 rset_isam_parms parms;
897 char termz[IT_MAX_WORD+1];
898 struct grep_info grep_info;
901 if (zapt->term->which != Z_Term_general)
906 trans_term (zi, zapt, termz);
908 grep_info.isam_p_indx = 0;
909 grep_info.isam_p_size = 0;
910 grep_info.isam_p_buf = NULL;
912 if (field_term (zi, zapt, termz, 'p', attributeSet, &grep_info,
913 num_bases, basenames))
915 if (grep_info.isam_p_indx < 1)
916 result = rset_create (rset_kind_null, NULL);
917 else if (grep_info.isam_p_indx == 1)
919 parms.is = zi->wordIsam;
920 parms.pos = *grep_info.isam_p_buf;
921 result = rset_create (rset_kind_isam, &parms);
924 result = rset_trunc (zi->wordIsam, grep_info.isam_p_buf,
925 grep_info.isam_p_indx);
926 xfree (grep_info.isam_p_buf);
929 static RSET rpn_search_APT_word (ZServerInfo *zi,
930 Z_AttributesPlusTerm *zapt,
931 oid_value attributeSet,
932 int num_bases, char **basenames)
934 rset_isam_parms parms;
935 char termz[IT_MAX_WORD+1];
936 struct grep_info grep_info;
939 if (zapt->term->which != Z_Term_general)
944 trans_term (zi, zapt, termz);
946 grep_info.isam_p_indx = 0;
947 grep_info.isam_p_size = 0;
948 grep_info.isam_p_buf = NULL;
950 if (field_term (zi, zapt, termz, 'w', attributeSet, &grep_info,
951 num_bases, basenames))
953 if (grep_info.isam_p_indx < 1)
954 result = rset_create (rset_kind_null, NULL);
955 else if (grep_info.isam_p_indx == 1)
957 parms.is = zi->wordIsam;
958 parms.pos = *grep_info.isam_p_buf;
959 result = rset_create (rset_kind_isam, &parms);
962 result = rset_trunc (zi->wordIsam, grep_info.isam_p_buf,
963 grep_info.isam_p_indx);
964 xfree (grep_info.isam_p_buf);
968 static RSET rpn_prox (RSET *rset, int rset_no)
976 rset_temp_parms parms;
978 rsfd = xmalloc (sizeof(*rsfd)*rset_no);
979 more = xmalloc (sizeof(*more)*rset_no);
980 buf = xmalloc (sizeof(*buf)*rset_no);
982 for (i = 0; i<rset_no; i++)
984 buf[i] = xmalloc (sizeof(**buf));
985 rsfd[i] = rset_open (rset[i], RSETF_READ|RSETF_SORT_SYSNO);
986 if (!(more[i] = rset_read (rset[i], rsfd[i], buf[i])))
990 rset_close (rset[i], rsfd[i]);
997 return rset_create (rset_kind_null, NULL);
1000 parms.key_size = sizeof (struct it_key);
1001 result = rset_create (rset_kind_temp, &parms);
1002 rsfd_result = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
1006 for (i = 1; i<rset_no; i++)
1015 cmp = key_compare (buf[i], buf[i-1]);
1018 more[i-1] = rset_read (rset[i-1], rsfd[i-1], buf[i-1]);
1023 if (buf[i-1]->seqno+1 != buf[i]->seqno)
1025 more[i-1] = rset_read (rset[i-1], rsfd[i-1], buf[i-1]);
1031 more[i] = rset_read (rset[i], rsfd[i], buf[i]);
1037 rset_write (result, rsfd_result, buf[0]);
1038 more[0] = rset_read (*rset, *rsfd, *buf);
1042 for (i = 0; i<rset_no; i++)
1044 rset_close (rset[i], rsfd[i]);
1047 rset_close (result, rsfd_result);
1054 static RSET rpn_search_APT_phrase (ZServerInfo *zi,
1055 Z_AttributesPlusTerm *zapt,
1056 oid_value attributeSet,
1057 int num_bases, char **basenames)
1059 char termz[IT_MAX_WORD+1];
1060 char term_sub[IT_MAX_WORD+1];
1061 char *p0 = termz, *p1 = NULL;
1062 RSET rset[60], result;
1064 struct grep_info grep_info;
1066 if (zapt->term->which != Z_Term_general)
1071 trans_term (zi, zapt, termz);
1073 grep_info.isam_p_size = 0;
1074 grep_info.isam_p_buf = NULL;
1078 if ((p1 = strchr (p0, ' ')))
1080 memcpy (term_sub, p0, p1-p0);
1081 term_sub[p1-p0] = '\0';
1084 strcpy (term_sub, p0);
1086 grep_info.isam_p_indx = 0;
1087 if (field_term (zi, zapt, term_sub, 'w', attributeSet, &grep_info,
1088 num_bases, basenames))
1090 if (grep_info.isam_p_indx == 0)
1091 rset[rset_no] = rset_create (rset_kind_null, NULL);
1092 else if (grep_info.isam_p_indx > 1)
1093 rset[rset_no] = rset_trunc (zi->wordIsam,
1094 grep_info.isam_p_buf,
1095 grep_info.isam_p_indx);
1098 rset_isam_parms parms;
1100 parms.is = zi->wordIsam;
1101 parms.pos = *grep_info.isam_p_buf;
1102 rset[rset_no] = rset_create (rset_kind_isam, &parms);
1104 assert (rset[rset_no]);
1105 if (++rset_no >= sizeof(rset)/sizeof(*rset))
1110 while (*++p0 == ' ')
1113 xfree (grep_info.isam_p_buf);
1115 return rset_create (rset_kind_null, NULL);
1116 else if (rset_no == 1)
1119 result = rpn_prox (rset, rset_no);
1120 for (i = 0; i<rset_no; i++)
1121 rset_delete (rset[i]);
1125 static RSET rpn_search_APT_local (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1126 oid_value attributeSet)
1131 rset_temp_parms parms;
1132 char termz[IT_MAX_WORD+1];
1134 if (zapt->term->which != Z_Term_general)
1139 parms.key_size = sizeof (struct it_key);
1140 result = rset_create (rset_kind_temp, &parms);
1141 rsfd = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
1143 trans_term (zi, zapt, termz);
1145 key.sysno = atoi (termz);
1148 rset_write (result, rsfd, &key);
1149 rset_close (result, rsfd);
1153 static RSET rpn_search_APT (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1154 oid_value attributeSet,
1155 int num_bases, char **basenames)
1159 AttrType completeness;
1160 int relation_value, structure_value, completeness_value;
1162 attr_init (&relation, zapt, 2);
1163 attr_init (&structure, zapt, 4);
1164 attr_init (&completeness, zapt, 6);
1166 relation_value = attr_find (&relation, NULL);
1167 structure_value = attr_find (&structure, NULL);
1168 completeness_value = attr_find (&completeness, NULL);
1169 switch (structure_value)
1172 if (relation_value == 102) /* relevance relation */
1173 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1174 num_bases, basenames);
1175 if (completeness_value == 2 || completeness_value == 3)
1176 return rpn_search_APT_cphrase (zi, zapt, attributeSet,
1177 num_bases, basenames);
1178 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1179 num_bases, basenames);
1180 case 1: /* phrase */
1181 if (relation_value == 102) /* relevance relation */
1182 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1183 num_bases, basenames);
1184 if (completeness_value == 2 || completeness_value == 3)
1185 return rpn_search_APT_cphrase (zi, zapt, attributeSet,
1186 num_bases, basenames);
1187 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1188 num_bases, basenames);
1191 if (relation_value == 102) /* relevance relation */
1192 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1193 num_bases, basenames);
1194 if (completeness_value == 2 || completeness_value == 3)
1195 return rpn_search_APT_cphrase (zi, zapt, attributeSet,
1196 num_bases, basenames);
1197 return rpn_search_APT_word (zi, zapt, attributeSet,
1198 num_bases, basenames);
1203 case 5: /* date - normalized */
1205 case 6: /* word list */
1206 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1207 num_bases, basenames);
1208 case 100: /* date - un-normalized */
1210 case 101: /* name - normalized */
1212 case 102: /* date - un-normalized */
1214 case 103: /* structure */
1218 case 105: /* free-form-text */
1219 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1220 num_bases, basenames);
1221 case 106: /* document-text */
1222 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1223 num_bases, basenames);
1224 case 107: /* local-number */
1225 return rpn_search_APT_local (zi, zapt, attributeSet);
1226 case 108: /* string */
1227 return rpn_search_APT_word (zi, zapt, attributeSet,
1228 num_bases, basenames);
1229 case 109: /* numeric string */
1236 static RSET rpn_search_ref (ZServerInfo *zi, Z_ResultSetId *resultSetId)
1240 if (!(s = resultSetGet (zi, resultSetId)))
1241 return rset_create (rset_kind_null, NULL);
1245 static RSET rpn_search_structure (ZServerInfo *zi, Z_RPNStructure *zs,
1246 oid_value attributeSet,
1247 int num_bases, char **basenames)
1250 if (zs->which == Z_RPNStructure_complex)
1252 rset_bool_parms bool_parms;
1255 bool_parms.rset_l = rpn_search_structure (zi, zs->u.complex->s1,
1257 num_bases, basenames);
1258 if (bool_parms.rset_l == NULL)
1260 if (rset_is_ranked(bool_parms.rset_l))
1262 bool_parms.rset_r = rpn_search_structure (zi, zs->u.complex->s2,
1264 num_bases, basenames);
1265 if (bool_parms.rset_r == NULL)
1267 rset_delete (bool_parms.rset_l);
1270 if (rset_is_ranked(bool_parms.rset_r))
1272 bool_parms.key_size = sizeof(struct it_key);
1273 bool_parms.cmp = key_compare;
1275 switch (zs->u.complex->roperator->which)
1277 case Z_Operator_and:
1278 r = rset_create (soft ? rset_kind_sand:rset_kind_and, &bool_parms);
1281 r = rset_create (soft ? rset_kind_sor:rset_kind_or, &bool_parms);
1283 case Z_Operator_and_not:
1284 r = rset_create (soft ? rset_kind_snot:rset_kind_not, &bool_parms);
1290 else if (zs->which == Z_RPNStructure_simple)
1292 if (zs->u.simple->which == Z_Operand_APT)
1294 logf (LOG_DEBUG, "rpn_search_APT");
1295 r = rpn_search_APT (zi, zs->u.simple->u.attributesPlusTerm,
1296 attributeSet, num_bases, basenames);
1298 else if (zs->u.simple->which == Z_Operand_resultSetId)
1300 logf (LOG_DEBUG, "rpn_search_ref");
1301 r = rpn_search_ref (zi, zs->u.simple->u.resultSetId);
1315 void count_set_save (RSET *r, int *count)
1322 rset_temp_parms parms;
1324 logf (LOG_DEBUG, "count_set_save");
1326 parms.key_size = sizeof(struct it_key);
1327 w = rset_create (rset_kind_temp, &parms);
1328 wfd = rset_open (w, RSETF_WRITE|RSETF_SORT_SYSNO);
1329 rfd = rset_open (*r, RSETF_READ|RSETF_SORT_SYSNO);
1330 while (rset_read (*r, rfd, &key))
1332 if (key.sysno != psysno)
1334 rset_write (w, wfd, &key);
1340 rset_close (*r, rfd);
1342 rset_close (w, wfd);
1344 logf (LOG_DEBUG, "%d keys, %d distinct sysnos", kno, *count);
1347 static void count_set (RSET r, int *count)
1354 logf (LOG_DEBUG, "count_set");
1356 rfd = rset_open (r, RSETF_READ|RSETF_SORT_SYSNO);
1357 while (rset_read (r, rfd, &key))
1359 if (key.sysno != psysno)
1366 rset_close (r, rfd);
1367 logf (LOG_DEBUG, "%d keys, %d distinct sysnos", kno, *count);
1370 int rpn_search (ZServerInfo *zi,
1371 Z_RPNQuery *rpn, int num_bases, char **basenames,
1372 const char *setname, int *hits)
1376 oid_value attributeSet;
1381 zi->errString = NULL;
1383 attrset = oid_getentbyoid (rpn->attributeSetId);
1384 attributeSet = attrset->value;
1385 rset = rpn_search_structure (zi, rpn->RPNStructure, attributeSet,
1386 num_bases, basenames);
1389 if (rset_is_volatile(rset))
1390 count_set_save(&rset,hits);
1392 count_set (rset, hits);
1393 resultSetAdd (zi, setname, 1, rset);
1395 logf (LOG_DEBUG, "search error: %d", zi->errCode);
1399 struct scan_info_entry {
1405 struct scan_info_entry *list;
1411 static int scan_handle (char *name, const char *info, int pos, void *client)
1413 int len_prefix, idx;
1414 struct scan_info *scan_info = client;
1416 len_prefix = strlen(scan_info->prefix);
1417 if (memcmp (name, scan_info->prefix, len_prefix))
1420 idx = scan_info->after - pos + scan_info->before;
1423 logf (LOG_DEBUG, "%-3d %s", idx, name+len_prefix);
1424 scan_info->list[idx].term = odr_malloc (scan_info->odr,
1425 strlen(name + len_prefix)+1);
1426 strcpy (scan_info->list[idx].term, name + len_prefix);
1427 assert (*info == sizeof(ISAM_P));
1428 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAM_P));
1433 int rpn_scan (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1434 oid_value attributeset,
1435 int num_bases, char **basenames,
1436 int *position, int *num_entries, struct scan_entry **list,
1440 int pos = *position;
1441 int num = *num_entries;
1445 char termz[IT_MAX_WORD+20];
1448 AttrType completeness;
1449 int completeness_value;
1450 struct scan_info *scan_info_array;
1451 struct scan_entry *glist;
1452 int ords[32], ord_no = 0;
1455 logf (LOG_DEBUG, "scan, position = %d, num = %d", pos, num);
1457 attr_init (&use, zapt, 1);
1458 use_value = attr_find (&use, NULL);
1459 logf (LOG_DEBUG, "use value %d", use_value);
1461 attr_init (&completeness, zapt, 6);
1462 completeness_value = attr_find (&completeness, NULL);
1463 logf (LOG_DEBUG, "completeness value %d", completeness_value);
1465 if (attributeset == VAL_NONE)
1466 attributeset = VAL_BIB1;
1468 if (use_value == -1)
1470 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
1473 data1_local_attribute *local_attr;
1475 attp = att_getentbyatt (attributeset, use_value);
1478 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
1479 attributeset, use_value);
1480 return zi->errCode = 114;
1482 if (zebTargetInfo_curDatabase (zi->zti, basenames[base_no]))
1484 zi->errString = basenames[base_no];
1485 return zi->errCode = 109; /* Database unavailable */
1487 for (local_attr = attp->local_attributes; local_attr && ord_no < 32;
1488 local_attr = local_attr->next)
1492 ord = zebTargetInfo_lookupSU (zi->zti, attp->attset_ordinal,
1495 ords[ord_no++] = ord;
1499 return zi->errCode = 113;
1502 scan_info_array = odr_malloc (zi->odr, ord_no * sizeof(*scan_info_array));
1503 for (i = 0; i < ord_no; i++)
1505 int j, prefix_len = 0;
1506 int before_tmp = before, after_tmp = after;
1507 struct scan_info *scan_info = scan_info_array + i;
1509 scan_info->before = before;
1510 scan_info->after = after;
1511 scan_info->odr = zi->odr;
1513 scan_info->list = odr_malloc (zi->odr, (before+after)*
1514 sizeof(*scan_info->list));
1515 for (j = 0; j<before+after; j++)
1516 scan_info->list[j].term = NULL;
1517 termz[prefix_len++] = ords[i];
1518 termz[prefix_len++] =
1519 (completeness_value==2 || completeness_value==3) ? 'p': 'w';
1520 termz[prefix_len] = 0;
1521 strcpy (scan_info->prefix, termz);
1522 trans_term (zi, zapt, termz+prefix_len);
1523 dict_scan (zi->wordDict, termz, &before_tmp, &after_tmp, scan_info,
1526 glist = odr_malloc (zi->odr, (before+after)*sizeof(*glist));
1527 for (i = 0; i < ord_no; i++)
1530 *status = BEND_SCAN_SUCCESS;
1531 for (i = 0; i<after; i++)
1534 const char *mterm = NULL;
1537 rset_isam_parms parms;
1539 for (j = 0; j < ord_no; j++)
1541 if (ptr[j] < before+after &&
1542 (tst=scan_info_array[j].list[ptr[j]].term) &&
1543 (!mterm || strcmp (tst, mterm) < 0))
1551 glist[i+before].term = odr_malloc (zi->odr, strlen(mterm)+1);
1552 strcpy (glist[i+before].term, mterm);
1554 parms.is = zi->wordIsam;
1555 parms.pos = scan_info_array[j0].list[ptr[j0]].isam_p;
1556 rset = rset_create (rset_kind_isam, &parms);
1559 for (j = j0+1; j<ord_no; j++)
1561 if (ptr[j] < before+after &&
1562 (tst=scan_info_array[j].list[ptr[j]].term) &&
1563 !strcmp (tst, mterm))
1565 rset_isam_parms parms;
1566 rset_bool_parms bool_parms;
1569 parms.is = zi->wordIsam;
1570 parms.pos = scan_info_array[j].list[ptr[j]].isam_p;
1571 rset2 = rset_create (rset_kind_isam, &parms);
1573 bool_parms.key_size = sizeof(struct it_key);
1574 bool_parms.cmp = key_compare;
1575 bool_parms.rset_l = rset;
1576 bool_parms.rset_r = rset2;
1578 rset = rset_create (rset_kind_or, &bool_parms);
1583 count_set (rset, &glist[i+before].occurrences);
1588 *num_entries -= (after-i);
1589 *status = BEND_SCAN_PARTIAL;
1592 for (i = 0; i<ord_no; i++)
1595 for (i = 0; i<before; i++)
1598 const char *mterm = NULL;
1601 rset_isam_parms parms;
1603 for (j = 0; j <ord_no; j++)
1605 if (ptr[j] < before &&
1606 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
1607 (!mterm || strcmp (tst, mterm) > 0))
1615 glist[before-1-i].term = odr_malloc (zi->odr, strlen(mterm)+1);
1616 strcpy (glist[before-1-i].term, mterm);
1618 parms.is = zi->wordIsam;
1619 parms.pos = scan_info_array[j0].list[before-1-ptr[j0]].isam_p;
1620 rset = rset_create (rset_kind_isam, &parms);
1624 for (j = j0+1; j<ord_no; j++)
1626 if (ptr[j] < before &&
1627 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
1628 !strcmp (tst, mterm))
1630 rset_isam_parms parms;
1631 rset_bool_parms bool_parms;
1634 parms.is = zi->wordIsam;
1635 parms.pos = scan_info_array[j].list[before-1-ptr[j]].isam_p;
1636 rset2 = rset_create (rset_kind_isam, &parms);
1638 bool_parms.key_size = sizeof(struct it_key);
1639 bool_parms.cmp = key_compare;
1640 bool_parms.rset_l = rset;
1641 bool_parms.rset_r = rset2;
1643 rset = rset_create (rset_kind_or, &bool_parms);
1648 count_set (rset, &glist[before-1-i].occurrences);
1654 *status = BEND_SCAN_PARTIAL;
1658 *list = glist + i; /* list is set to first 'real' entry */
1660 logf (LOG_DEBUG, "position = %d, num_entries = %d",
1661 *position, *num_entries);
1663 logf (LOG_DEBUG, "scan error: %d", zi->errCode);