2 * Copyright (C) 1994-1996, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.54 1996-10-29 14:09:52 adam
8 * Use of cisam system - enabled if setting isamc is 1.
10 * Revision 1.53 1996/06/26 09:21:43 adam
11 * Bug fix: local attribute set wasn't obeyed in scan.
13 * Revision 1.52 1996/06/17 14:26:20 adam
14 * Function gen_regular_rel changed to handle negative numbers.
16 * Revision 1.51 1996/06/11 10:54:15 quinn
19 * Revision 1.50 1996/06/07 08:51:53 adam
20 * Bug fix: Character mapping was broken (introducued by last revision).
22 * Revision 1.49 1996/06/04 10:18:11 adam
23 * Search/scan uses character mapping module.
25 * Revision 1.48 1996/05/28 15:15:01 adam
26 * Bug fix: Didn't handle unknown database correctly.
28 * Revision 1.47 1996/05/15 18:36:28 adam
29 * Function trans_term transforms unsearchable characters to blanks.
31 * Revision 1.46 1996/05/15 11:57:56 adam
32 * Fixed bug introduced by set/field mapping in search operations.
34 * Revision 1.45 1996/05/14 11:34:00 adam
35 * Scan support in multiple registers/databases.
37 * Revision 1.44 1996/05/14 06:16:44 adam
38 * Compact use/set bytes used in search service.
40 * Revision 1.43 1996/05/09 09:54:43 adam
41 * Server supports maps from one logical attributes to a list of physical
43 * The extraction process doesn't make space consuming 'any' keys.
45 * Revision 1.42 1996/05/09 07:28:56 quinn
46 * Work towards phrases and multiple registers
48 * Revision 1.41 1996/03/20 09:36:43 adam
49 * Function dict_lookup_grep got extra parameter, init_pos, which marks
50 * from which position in pattern approximate pattern matching should occur.
51 * Approximate pattern matching is used in relevance=re-2.
53 * Revision 1.40 1996/02/02 13:44:44 adam
54 * The public dictionary functions simply use char instead of Dict_char
55 * to represent search strings. Dict_char is used internally only.
57 * Revision 1.39 1996/01/03 16:22:13 quinn
60 * Revision 1.38 1995/12/11 09:12:55 adam
61 * The rec_get function returns NULL if record doesn't exist - will
62 * happen in the server if the result set records have been deleted since
63 * the creation of the set (i.e. the search).
64 * The server saves a result temporarily if it is 'volatile', i.e. the
65 * set is register dependent.
67 * Revision 1.37 1995/12/06 15:05:28 adam
68 * More verbose in count_set.
70 * Revision 1.36 1995/12/06 12:41:27 adam
71 * New command 'stat' for the index program.
72 * Filenames can be read from stdin by specifying '-'.
73 * Bug fix/enhancement of the transformation from terms to regular
74 * expressons in the search engine.
76 * Revision 1.35 1995/11/27 09:29:00 adam
77 * Bug fixes regarding conversion to regular expressions.
79 * Revision 1.34 1995/11/16 17:00:56 adam
80 * Better logging of rpn query.
82 * Revision 1.33 1995/11/01 13:58:28 quinn
83 * Moving data1 to yaz/retrieval
85 * Revision 1.32 1995/10/27 14:00:11 adam
86 * Implemented detection of database availability.
88 * Revision 1.31 1995/10/17 18:02:10 adam
89 * New feature: databases. Implemented as prefix to words in dictionary.
91 * Revision 1.30 1995/10/16 09:32:38 adam
92 * More work on relational op.
94 * Revision 1.29 1995/10/13 16:01:49 adam
97 * Revision 1.28 1995/10/13 12:26:43 adam
98 * Optimization of truncation.
100 * Revision 1.27 1995/10/12 17:07:22 adam
103 * Revision 1.26 1995/10/12 12:40:54 adam
104 * Bug fixes in rpn_prox.
106 * Revision 1.25 1995/10/10 13:59:24 adam
107 * Function rset_open changed its wflag parameter to general flags.
109 * Revision 1.24 1995/10/09 16:18:37 adam
110 * Function dict_lookup_grep got extra client data parameter.
112 * Revision 1.23 1995/10/06 16:33:37 adam
113 * Use attribute mappings.
115 * Revision 1.22 1995/10/06 15:07:39 adam
116 * Structure 'local-number' handled.
118 * Revision 1.21 1995/10/06 13:52:06 adam
119 * Bug fixes. Handler may abort further scanning.
121 * Revision 1.20 1995/10/06 11:06:33 adam
122 * Scan entries include 'occurrences' now.
124 * Revision 1.19 1995/10/06 10:43:56 adam
125 * Scan added. 'occurrences' in scan entries not set yet.
127 * Revision 1.18 1995/10/04 16:57:20 adam
128 * Key input and merge sort in one pass.
130 * Revision 1.17 1995/10/04 12:55:17 adam
131 * Bug fix in ranked search. Use=Any keys inserted.
133 * Revision 1.16 1995/10/02 16:24:40 adam
134 * Use attribute actually used in search requests.
136 * Revision 1.15 1995/10/02 15:18:52 adam
137 * New member in recRetrieveCtrl: diagnostic.
139 * Revision 1.14 1995/09/28 12:10:32 adam
140 * Bug fixes. Field prefix used in queries.
142 * Revision 1.13 1995/09/18 14:17:50 adam
145 * Revision 1.12 1995/09/15 14:45:21 adam
147 * Work on truncation.
149 * Revision 1.11 1995/09/14 11:53:27 adam
150 * First work on regular expressions/truncations.
152 * Revision 1.10 1995/09/11 15:23:26 adam
153 * More work on relevance search.
155 * Revision 1.9 1995/09/11 13:09:35 adam
156 * More work on relevance feedback.
158 * Revision 1.8 1995/09/08 14:52:27 adam
159 * Minor changes. Dictionary is lower case now.
161 * Revision 1.7 1995/09/07 13:58:36 adam
162 * New parameter: result-set file descriptor (RSFD) to support multiple
163 * positions within the same result-set.
164 * Boolean operators: and, or, not implemented.
165 * Result-set references.
167 * Revision 1.6 1995/09/06 16:11:18 adam
168 * Option: only one word key per file.
170 * Revision 1.5 1995/09/06 10:33:04 adam
171 * More work on present. Some log messages removed.
173 * Revision 1.4 1995/09/05 15:28:40 adam
174 * More work on search engine.
176 * Revision 1.3 1995/09/04 15:20:22 adam
179 * Revision 1.2 1995/09/04 12:33:43 adam
180 * Various cleanup. YAZ util used instead.
182 * Revision 1.1 1995/09/04 09:10:40 adam
183 * More work on index add/del/update.
184 * Merge sort implemented.
185 * Initial work on z39 server.
194 #include "attribute.h"
208 Z_AttributesPlusTerm *zapt;
211 static RSET rset_create_isamx (ZServerInfo *zi, int pos)
215 rset_isam_parms parms;
219 return rset_create (rset_kind_isam, &parms);
223 rset_isamc_parms parms;
226 parms.is = zi->isamc;
227 return rset_create (rset_kind_isamc, &parms);
229 return rset_create (rset_kind_null, NULL);
232 static int attr_find (AttrType *src, oid_value *attributeSetP)
234 while (src->major < src->zapt->num_attributes)
236 Z_AttributeElement *element;
238 element = src->zapt->attributeList[src->major];
239 if (src->type == *element->attributeType)
241 switch (element->which)
243 case Z_AttributeValue_numeric:
245 if (element->attributeSet && attributeSetP)
249 attrset = oid_getentbyoid (element->attributeSet);
250 *attributeSetP = attrset->value;
252 return *element->value.numeric;
254 case Z_AttributeValue_complex:
255 if (src->minor >= element->value.complex->num_list ||
256 element->value.complex->list[src->minor]->which !=
257 Z_StringOrNumeric_numeric)
260 if (element->attributeSet && attributeSetP)
264 attrset = oid_getentbyoid (element->attributeSet);
265 *attributeSetP = attrset->value;
267 return *element->value.complex->list[src->minor-1]->u.numeric;
277 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
291 int (*cmp)(const void *p1, const void *p2);
298 static void heap_swap (struct trunc_info *ti, int i1, int i2)
303 ti->ptr[i1] = ti->ptr[i2];
307 static void heap_delete (struct trunc_info *ti)
309 int cur = 1, child = 2;
311 heap_swap (ti, 1, ti->heapnum--);
312 while (child <= ti->heapnum) {
313 if (child < ti->heapnum &&
314 (*ti->cmp)(ti->heap[ti->ptr[child]],
315 ti->heap[ti->ptr[1+child]]) > 0)
317 if ((*ti->cmp)(ti->heap[ti->ptr[cur]],
318 ti->heap[ti->ptr[child]]) > 0)
320 heap_swap (ti, cur, child);
329 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
333 cur = ++(ti->heapnum);
334 memcpy (ti->heap[ti->ptr[cur]], buf, ti->keysize);
335 ti->indx[ti->ptr[cur]] = indx;
337 while (parent && (*ti->cmp)(ti->heap[ti->ptr[parent]],
338 ti->heap[ti->ptr[cur]]) > 0)
340 heap_swap (ti, cur, parent);
347 struct trunc_info *heap_init (int size, int key_size,
348 int (*cmp)(const void *p1, const void *p2))
350 struct trunc_info *ti = xmalloc (sizeof(*ti));
355 ti->keysize = key_size;
357 ti->indx = xmalloc (size * sizeof(*ti->indx));
358 ti->heap = xmalloc (size * sizeof(*ti->heap));
359 ti->ptr = xmalloc (size * sizeof(*ti->ptr));
360 ti->swapbuf = xmalloc (ti->keysize);
361 ti->tmpbuf = xmalloc (ti->keysize);
362 ti->buf = xmalloc (size * ti->keysize);
363 for (i = size; --i >= 0; )
366 ti->heap[i] = ti->buf + ti->keysize * i;
371 static void heap_close (struct trunc_info *ti)
381 static RSET rset_trunc_r (ISAM isam, ISAM_P *isam_p, int from, int to,
386 rset_temp_parms parms;
388 parms.key_size = sizeof(struct it_key);
389 result = rset_create (rset_kind_temp, &parms);
390 result_rsfd = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
392 if (to - from > merge_chunk)
396 int i, i_add = (to-from)/merge_chunk + 1;
397 struct trunc_info *ti;
399 int rsmax = (to-from)/i_add + 1;
401 rset = xmalloc (sizeof(*rset) * rsmax);
402 rsfd = xmalloc (sizeof(*rsfd) * rsmax);
404 for (i = from; i < to; i += i_add)
407 rset[rscur] = rset_trunc_r (isam, isam_p, i, i+i_add,
410 rset[rscur] = rset_trunc_r (isam, isam_p, i, to,
414 ti = heap_init (rscur, sizeof(struct it_key), key_compare);
415 for (i = rscur; --i >= 0; )
417 rsfd[i] = rset_open (rset[i], RSETF_READ|RSETF_SORT_SYSNO);
418 if (rset_read (rset[i], rsfd[i], ti->tmpbuf))
419 heap_insert (ti, ti->tmpbuf, i);
422 rset_close (rset[i], rsfd[i]);
423 rset_delete (rset[i]);
428 int n = ti->indx[ti->ptr[1]];
430 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
434 if (!rset_read (rset[n], rsfd[n], ti->tmpbuf))
437 rset_close (rset[n], rsfd[n]);
438 rset_delete (rset[n]);
441 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
444 heap_insert (ti, ti->tmpbuf, n);
457 struct trunc_info *ti;
459 ispt = xmalloc (sizeof(*ispt) * (to-from));
461 ti = heap_init (to-from, sizeof(struct it_key),
463 for (i = to-from; --i >= 0; )
465 ispt[i] = is_position (isam, isam_p[from+i]);
466 if (is_readkey (ispt[i], ti->tmpbuf))
467 heap_insert (ti, ti->tmpbuf, i);
469 is_pt_free (ispt[i]);
473 int n = ti->indx[ti->ptr[1]];
475 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
477 /* section that preserve all keys */
479 if (is_readkey (ispt[n], ti->tmpbuf))
480 heap_insert (ti, ti->tmpbuf, n);
482 is_pt_free (ispt[n]);
484 /* section that preserve all keys with unique sysnos */
487 if (!is_readkey (ispt[n], ti->tmpbuf))
490 is_pt_free (ispt[n]);
493 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
496 heap_insert (ti, ti->tmpbuf, n);
505 rset_close (result, result_rsfd);
509 static int isam_trunc_cmp (const void *p1, const void *p2)
511 ISAM_P i1 = *(ISAM_P*) p1;
512 ISAM_P i2 = *(ISAM_P*) p2;
515 d = is_type (i1) - is_type (i2);
518 return is_block (i1) - is_block (i2);
521 static RSET rset_trunc (ISAM isam, ISAM_P *isam_p, int no)
524 qsort (isam_p, no, sizeof(*isam_p), isam_trunc_cmp);
525 return rset_trunc_r (isam, isam_p, 0, no, 100);
539 static void add_isam_p (const char *info, struct grep_info *p)
541 if (p->isam_p_indx == p->isam_p_size)
543 ISAM_P *new_isam_p_buf;
548 p->isam_p_size = 2*p->isam_p_size + 100;
549 new_isam_p_buf = xmalloc (sizeof(*new_isam_p_buf) *
553 memcpy (new_isam_p_buf, p->isam_p_buf,
554 p->isam_p_indx * sizeof(*p->isam_p_buf));
555 xfree (p->isam_p_buf);
557 p->isam_p_buf = new_isam_p_buf;
560 new_term_no = xmalloc (sizeof(*new_term_no) *
564 memcpy (new_term_no, p->isam_p_buf,
565 p->isam_p_indx * sizeof(*p->term_no));
568 p->term_no = new_term_no;
571 assert (*info == sizeof(*p->isam_p_buf));
572 memcpy (p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
576 static int grep_handle (char *name, const char *info, void *p)
578 add_isam_p (info, p);
582 /* gen_regular_rel - generate regular expression from relation
583 * val: border value (inclusive)
584 * islt: 1 if <=; 0 if >=.
586 static void gen_regular_rel (char *dst, int val, int islt)
593 logf (LOG_DEBUG, "gen_regular_rel. val=%d, islt=%d", val, islt);
597 strcpy (dst, "(-[0-9]+|");
605 strcpy (dst, "([0-9]+|-");
606 dst_p = strlen (dst);
616 dst_p = strlen (dst);
617 sprintf (numstr, "%d", val);
618 for (w = strlen(numstr); --w >= 0; pos++)
637 strcpy (dst + dst_p, numstr);
638 dst_p = strlen(dst) - pos - 1;
666 for (i = 0; i<pos; i++)
679 for (i=1; i<pos; i++)
680 strcat (dst, "[0-9]?");
684 for (i = 0; i <= pos; i++)
685 strcat (dst, "[0-9]");
686 strcat (dst, "[0-9]*");
691 static int relational_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
692 const char *term_sub,
694 oid_value attributeSet,
695 struct grep_info *grep_info,
703 attr_init (&relation, zapt, 2);
704 relation_value = attr_find (&relation, NULL);
705 term_value = atoi (term_sub);
707 switch (relation_value)
712 logf (LOG_DEBUG, "Relation <");
713 gen_regular_rel (term_dict + strlen(term_dict), term_value-1, 1);
718 logf (LOG_DEBUG, "Relation <=");
719 gen_regular_rel (term_dict + strlen(term_dict), term_value, 1);
724 logf (LOG_DEBUG, "Relation >=");
725 gen_regular_rel (term_dict + strlen(term_dict), term_value, 0);
730 logf (LOG_DEBUG, "Relation >");
731 gen_regular_rel (term_dict + strlen(term_dict), term_value+1, 0);
736 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_dict);
737 r = dict_lookup_grep (zi->dict, term_dict, 0, grep_info, max_pos,
740 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
741 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
745 static void verbatim_char (int ch, int *indx, char *dst)
748 dst[(*indx)++] = '\\';
752 static int field_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
753 const char *term_sub, int regType,
754 oid_value attributeSet, struct grep_info *grep_info,
755 int num_bases, char **basenames)
757 char term_dict[2*IT_MAX_WORD+2];
758 int i, j, r, base_no;
760 int truncation_value;
763 oid_value curAttributeSet = attributeSet;
765 attr_init (&use, zapt, 1);
766 use_value = attr_find (&use, &curAttributeSet);
767 logf (LOG_DEBUG, "use value %d", use_value);
768 attr_init (&truncation, zapt, 5);
769 truncation_value = attr_find (&truncation, NULL);
770 logf (LOG_DEBUG, "truncation value %d", truncation_value);
775 for (base_no = 0; base_no < num_bases; base_no++)
778 data1_local_attribute *local_attr;
779 int max_pos, prefix_len = 0;
781 attp = att_getentbyatt (curAttributeSet, use_value);
784 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
785 curAttributeSet, use_value);
789 if (zebTargetInfo_curDatabase (zi->zti, basenames[base_no]))
791 zi->errCode = 109; /* Database unavailable */
792 zi->errString = basenames[base_no];
795 for (local_attr = attp->local_attributes; local_attr;
796 local_attr = local_attr->next)
800 ord = zebTargetInfo_lookupSU (zi->zti, attp->attset_ordinal,
805 term_dict[prefix_len++] = '|';
807 term_dict[prefix_len++] = '(';
808 term_dict[prefix_len++] = 1;
809 term_dict[prefix_len++] = ord;
816 term_dict[prefix_len++] = ')';
817 term_dict[prefix_len++] = 1;
818 term_dict[prefix_len++] = regType;
819 term_dict[prefix_len] = '\0';
820 if (!relational_term (zi, zapt, term_sub, term_dict,
821 attributeSet, grep_info, &max_pos))
826 switch (truncation_value)
828 case -1: /* not specified */
829 case 100: /* do not truncate */
830 term_dict[j++] = '(';
831 for (i = 0; term_sub[i]; i++)
832 verbatim_char (term_sub[i], &j, term_dict);
833 strcpy (term_dict+j, ")");
834 r = dict_lookup_grep (zi->dict, term_dict, 0, grep_info,
835 &max_pos, 0, grep_handle);
837 logf (LOG_WARN, "dict_lookup_grep err, trunc=none:%d", r);
839 case 1: /* right truncation */
840 term_dict[j++] = '(';
841 for (i = 0; term_sub[i]; i++)
842 verbatim_char (term_sub[i], &j, term_dict);
843 strcpy (term_dict+j, ".*)");
844 dict_lookup_grep (zi->dict, term_dict, 0, grep_info,
845 &max_pos, 0, grep_handle);
847 case 2: /* left truncation */
848 case 3: /* left&right truncation */
851 case 101: /* process # in term */
852 term_dict[j++] = '(';
853 for (i=0; term_sub[i]; i++)
854 if (term_sub[i] == '#' && i > 2)
856 term_dict[j++] = '.';
857 term_dict[j++] = '*';
860 verbatim_char (term_sub[i], &j, term_dict);
861 strcpy (term_dict+j, ")");
862 r = dict_lookup_grep (zi->dict, term_dict, 0, grep_info,
863 &max_pos, 0, grep_handle);
865 logf (LOG_WARN, "dict_lookup_grep err, trunc=#: %d",
868 case 102: /* regular expression */
869 sprintf (term_dict + j, "(%s)", term_sub);
870 r = dict_lookup_grep (zi->dict, term_dict, 0, grep_info,
871 &max_pos, 0, grep_handle);
873 logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
876 case 103: /* regular expression with error correction */
879 if (*cp == '*' && cp[1] && cp[2])
884 sprintf (term_dict + j, "(%s)", cp);
885 r = dict_lookup_grep (zi->dict, term_dict, r, grep_info,
886 &max_pos, j, grep_handle);
888 logf (LOG_WARN, "dict_lookup_grep err, trunc=eregular: %d",
894 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
898 static void trans_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
902 Z_Term *term = zapt->term;
904 sizez = term->u.general->len;
905 if (sizez > IT_MAX_WORD-1)
906 sizez = IT_MAX_WORD-1;
907 memcpy (termz, term->u.general->buf, sizez);
911 static void trans_scan_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
914 Z_Term *term = zapt->term;
916 const char *cp = (const char *) term->u.general->buf;
917 const char *cp_end = cp + term->u.general->len;
923 while ((len = (cp_end - cp)) > 0)
925 map = map_chrs_input (&cp, len);
926 if (**map == *CHR_SPACE)
934 for (src = *map; *src; src++)
940 static RSET rpn_search_APT_relevance (ZServerInfo *zi,
941 Z_AttributesPlusTerm *zapt,
942 oid_value attributeSet,
943 int num_bases, char **basenames)
945 rset_relevance_parms parms;
946 char termz[IT_MAX_WORD+1];
947 char term_sub[IT_MAX_WORD+1];
948 struct grep_info grep_info;
953 parms.key_size = sizeof(struct it_key);
955 parms.cmp = key_compare;
959 if (zapt->term->which != Z_Term_general)
964 trans_term (zi, zapt, termz);
967 grep_info.term_no = 0;
969 grep_info.isam_p_indx = 0;
970 grep_info.isam_p_size = 0;
971 grep_info.isam_p_buf = NULL;
980 map = map_chrs_input (&p1, strlen(p1));
981 if (**map != *CHR_SPACE)
990 map = map_chrs_input (&p1, strlen(p1));
991 if (**map == *CHR_SPACE)
996 memcpy (term_sub, p0, p2-p0);
997 term_sub[p2-p0] = '\0';
999 if (field_term (zi, zapt, term_sub, 'w', attributeSet, &grep_info,
1000 num_bases, basenames))
1003 for (; term_index < grep_info.isam_p_indx; term_index++)
1004 grep_info.term_no[term_index] = parms.no_terms;
1008 parms.term_no = grep_info.term_no;
1009 parms.isam_positions = grep_info.isam_p_buf;
1010 parms.no_isam_positions = grep_info.isam_p_indx;
1011 if (grep_info.isam_p_indx > 0)
1012 result = rset_create (rset_kind_relevance, &parms);
1014 result = rset_create (rset_kind_null, NULL);
1016 xfree(grep_info.term_no);
1018 xfree (grep_info.isam_p_buf);
1022 static RSET rpn_search_APT_cphrase (ZServerInfo *zi,
1023 Z_AttributesPlusTerm *zapt,
1024 oid_value attributeSet,
1025 int num_bases, char **basenames)
1027 char termz[IT_MAX_WORD+1];
1028 struct grep_info grep_info;
1031 if (zapt->term->which != Z_Term_general)
1036 trans_term (zi, zapt, termz);
1039 grep_info.term_no = 0;
1041 grep_info.isam_p_indx = 0;
1042 grep_info.isam_p_size = 0;
1043 grep_info.isam_p_buf = NULL;
1045 if (field_term (zi, zapt, termz, 'p', attributeSet, &grep_info,
1046 num_bases, basenames))
1048 if (grep_info.isam_p_indx < 1)
1049 result = rset_create (rset_kind_null, NULL);
1050 else if (grep_info.isam_p_indx == 1)
1052 result = rset_create_isamx (zi, *grep_info.isam_p_buf);
1055 result = rset_trunc (zi->isam, grep_info.isam_p_buf,
1056 grep_info.isam_p_indx);
1058 xfree(grep_info.term_no);
1060 xfree (grep_info.isam_p_buf);
1064 static RSET rpn_prox (RSET *rset, int rset_no)
1069 struct it_key **buf;
1072 rset_temp_parms parms;
1074 rsfd = xmalloc (sizeof(*rsfd)*rset_no);
1075 more = xmalloc (sizeof(*more)*rset_no);
1076 buf = xmalloc (sizeof(*buf)*rset_no);
1078 for (i = 0; i<rset_no; i++)
1080 buf[i] = xmalloc (sizeof(**buf));
1081 rsfd[i] = rset_open (rset[i], RSETF_READ|RSETF_SORT_SYSNO);
1082 if (!(more[i] = rset_read (rset[i], rsfd[i], buf[i])))
1086 rset_close (rset[i], rsfd[i]);
1093 return rset_create (rset_kind_null, NULL);
1096 parms.key_size = sizeof (struct it_key);
1097 result = rset_create (rset_kind_temp, &parms);
1098 rsfd_result = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
1102 for (i = 1; i<rset_no; i++)
1111 cmp = key_compare (buf[i], buf[i-1]);
1114 more[i-1] = rset_read (rset[i-1], rsfd[i-1], buf[i-1]);
1119 if (buf[i-1]->seqno+1 != buf[i]->seqno)
1121 more[i-1] = rset_read (rset[i-1], rsfd[i-1], buf[i-1]);
1127 more[i] = rset_read (rset[i], rsfd[i], buf[i]);
1133 rset_write (result, rsfd_result, buf[0]);
1134 more[0] = rset_read (*rset, *rsfd, *buf);
1138 for (i = 0; i<rset_no; i++)
1140 rset_close (rset[i], rsfd[i]);
1143 rset_close (result, rsfd_result);
1150 static RSET rpn_search_APT_phrase (ZServerInfo *zi,
1151 Z_AttributesPlusTerm *zapt,
1152 oid_value attributeSet,
1153 int num_bases, char **basenames)
1155 char termz[IT_MAX_WORD+1];
1156 char term_sub[IT_MAX_WORD+1];
1158 RSET rset[60], result;
1160 struct grep_info grep_info;
1162 if (zapt->term->which != Z_Term_general)
1167 trans_term (zi, zapt, termz);
1170 grep_info.term_no = 0;
1172 grep_info.isam_p_size = 0;
1173 grep_info.isam_p_buf = NULL;
1183 map = map_chrs_input (&p1, strlen(p1));
1184 if (**map != *CHR_SPACE)
1193 map = map_chrs_input (&p1, strlen(p1));
1194 if (**map == *CHR_SPACE)
1200 memcpy (term_sub, p0, p2-p0);
1201 term_sub[p2-p0] = '\0';
1204 grep_info.isam_p_indx = 0;
1205 if (field_term (zi, zapt, term_sub, 'w', attributeSet, &grep_info,
1206 num_bases, basenames))
1208 if (grep_info.isam_p_indx == 0)
1209 rset[rset_no] = rset_create (rset_kind_null, NULL);
1210 else if (grep_info.isam_p_indx > 1)
1211 rset[rset_no] = rset_trunc (zi->isam,
1212 grep_info.isam_p_buf,
1213 grep_info.isam_p_indx);
1216 rset[rset_no] = rset_create_isamx (zi, *grep_info.isam_p_buf);
1218 assert (rset[rset_no]);
1219 if (++rset_no >= sizeof(rset)/sizeof(*rset))
1223 xfree(grep_info.term_no);
1225 xfree (grep_info.isam_p_buf);
1227 return rset_create (rset_kind_null, NULL);
1228 else if (rset_no == 1)
1231 result = rpn_prox (rset, rset_no);
1232 for (i = 0; i<rset_no; i++)
1233 rset_delete (rset[i]);
1237 static RSET rpn_search_APT_local (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1238 oid_value attributeSet)
1243 rset_temp_parms parms;
1244 char termz[IT_MAX_WORD+1];
1246 if (zapt->term->which != Z_Term_general)
1251 parms.key_size = sizeof (struct it_key);
1252 result = rset_create (rset_kind_temp, &parms);
1253 rsfd = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
1255 trans_term (zi, zapt, termz);
1257 key.sysno = atoi (termz);
1260 rset_write (result, rsfd, &key);
1261 rset_close (result, rsfd);
1265 static RSET rpn_search_APT (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1266 oid_value attributeSet,
1267 int num_bases, char **basenames)
1271 AttrType completeness;
1272 int relation_value, structure_value, completeness_value;
1274 attr_init (&relation, zapt, 2);
1275 attr_init (&structure, zapt, 4);
1276 attr_init (&completeness, zapt, 6);
1278 relation_value = attr_find (&relation, NULL);
1279 structure_value = attr_find (&structure, NULL);
1280 completeness_value = attr_find (&completeness, NULL);
1281 switch (structure_value)
1284 if (relation_value == 102) /* relevance relation */
1285 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1286 num_bases, basenames);
1287 if (completeness_value == 2 || completeness_value == 3)
1288 return rpn_search_APT_cphrase (zi, zapt, attributeSet,
1289 num_bases, basenames);
1290 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1291 num_bases, basenames);
1292 case 1: /* phrase */
1293 if (relation_value == 102) /* relevance relation */
1294 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1295 num_bases, basenames);
1296 if (completeness_value == 2 || completeness_value == 3)
1297 return rpn_search_APT_cphrase (zi, zapt, attributeSet,
1298 num_bases, basenames);
1299 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1300 num_bases, basenames);
1303 if (relation_value == 102) /* relevance relation */
1304 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1305 num_bases, basenames);
1306 if (completeness_value == 2 || completeness_value == 3)
1307 return rpn_search_APT_cphrase (zi, zapt, attributeSet,
1308 num_bases, basenames);
1309 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1310 num_bases, basenames);
1315 case 5: /* date - normalized */
1317 case 6: /* word list */
1318 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1319 num_bases, basenames);
1320 case 100: /* date - un-normalized */
1322 case 101: /* name - normalized */
1324 case 102: /* date - un-normalized */
1326 case 103: /* structure */
1330 case 105: /* free-form-text */
1331 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1332 num_bases, basenames);
1333 case 106: /* document-text */
1334 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1335 num_bases, basenames);
1336 case 107: /* local-number */
1337 return rpn_search_APT_local (zi, zapt, attributeSet);
1338 case 108: /* string */
1339 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1340 num_bases, basenames);
1341 case 109: /* numeric string */
1348 static RSET rpn_search_ref (ZServerInfo *zi, Z_ResultSetId *resultSetId)
1352 if (!(s = resultSetGet (zi, resultSetId)))
1353 return rset_create (rset_kind_null, NULL);
1357 static RSET rpn_search_structure (ZServerInfo *zi, Z_RPNStructure *zs,
1358 oid_value attributeSet,
1359 int num_bases, char **basenames)
1362 if (zs->which == Z_RPNStructure_complex)
1364 rset_bool_parms bool_parms;
1367 bool_parms.rset_l = rpn_search_structure (zi, zs->u.complex->s1,
1369 num_bases, basenames);
1370 if (bool_parms.rset_l == NULL)
1372 if (rset_is_ranked(bool_parms.rset_l))
1374 bool_parms.rset_r = rpn_search_structure (zi, zs->u.complex->s2,
1376 num_bases, basenames);
1377 if (bool_parms.rset_r == NULL)
1379 rset_delete (bool_parms.rset_l);
1382 if (rset_is_ranked(bool_parms.rset_r))
1384 bool_parms.key_size = sizeof(struct it_key);
1385 bool_parms.cmp = key_compare;
1387 switch (zs->u.complex->roperator->which)
1389 case Z_Operator_and:
1390 r = rset_create (soft ? rset_kind_sand:rset_kind_and, &bool_parms);
1393 r = rset_create (soft ? rset_kind_sor:rset_kind_or, &bool_parms);
1395 case Z_Operator_and_not:
1396 r = rset_create (soft ? rset_kind_snot:rset_kind_not, &bool_parms);
1402 else if (zs->which == Z_RPNStructure_simple)
1404 if (zs->u.simple->which == Z_Operand_APT)
1406 logf (LOG_DEBUG, "rpn_search_APT");
1407 r = rpn_search_APT (zi, zs->u.simple->u.attributesPlusTerm,
1408 attributeSet, num_bases, basenames);
1410 else if (zs->u.simple->which == Z_Operand_resultSetId)
1412 logf (LOG_DEBUG, "rpn_search_ref");
1413 r = rpn_search_ref (zi, zs->u.simple->u.resultSetId);
1427 void count_set_save (RSET *r, int *count)
1434 rset_temp_parms parms;
1436 logf (LOG_DEBUG, "count_set_save");
1438 parms.key_size = sizeof(struct it_key);
1439 w = rset_create (rset_kind_temp, &parms);
1440 wfd = rset_open (w, RSETF_WRITE|RSETF_SORT_SYSNO);
1441 rfd = rset_open (*r, RSETF_READ|RSETF_SORT_SYSNO);
1442 while (rset_read (*r, rfd, &key))
1444 logf (LOG_DEBUG, "sysno=%-7d seqno=%d", key.sysno, key.seqno);
1445 if (key.sysno != psysno)
1447 rset_write (w, wfd, &key);
1453 rset_close (*r, rfd);
1455 rset_close (w, wfd);
1457 logf (LOG_DEBUG, "%d keys, %d distinct sysnos", kno, *count);
1460 static void count_set (RSET r, int *count)
1467 logf (LOG_DEBUG, "count_set");
1469 rfd = rset_open (r, RSETF_READ|RSETF_SORT_SYSNO);
1470 while (rset_read (r, rfd, &key))
1472 if (key.sysno != psysno)
1479 rset_close (r, rfd);
1480 logf (LOG_DEBUG, "%d keys, %d distinct sysnos", kno, *count);
1483 int rpn_search (ZServerInfo *zi,
1484 Z_RPNQuery *rpn, int num_bases, char **basenames,
1485 const char *setname, int *hits)
1489 oid_value attributeSet;
1491 dict_grep_cmap (zi->dict, map_chrs_input);
1495 zi->errString = NULL;
1497 attrset = oid_getentbyoid (rpn->attributeSetId);
1498 attributeSet = attrset->value;
1499 rset = rpn_search_structure (zi, rpn->RPNStructure, attributeSet,
1500 num_bases, basenames);
1503 if (rset_is_volatile(rset))
1504 count_set_save(&rset,hits);
1506 count_set (rset, hits);
1507 resultSetAdd (zi, setname, 1, rset);
1509 logf (LOG_DEBUG, "search error: %d", zi->errCode);
1513 struct scan_info_entry {
1519 struct scan_info_entry *list;
1525 static int scan_handle (char *name, const char *info, int pos, void *client)
1527 int len_prefix, idx;
1528 struct scan_info *scan_info = client;
1530 len_prefix = strlen(scan_info->prefix);
1531 if (memcmp (name, scan_info->prefix, len_prefix))
1534 idx = scan_info->after - pos + scan_info->before;
1537 logf (LOG_DEBUG, "%-3d %s", idx, name+len_prefix);
1538 scan_info->list[idx].term = odr_malloc (scan_info->odr,
1539 strlen(name + len_prefix)+1);
1540 strcpy (scan_info->list[idx].term, name + len_prefix);
1541 assert (*info == sizeof(ISAM_P));
1542 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAM_P));
1547 static void scan_term_untrans (ODR odr, char **dstp, const char *src)
1549 char *dst = odr_malloc (odr, strlen(src)*2+1);
1554 const char *cp = map_chrs_output (&src);
1561 int rpn_scan (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1562 oid_value attributeset,
1563 int num_bases, char **basenames,
1564 int *position, int *num_entries, struct scan_entry **list,
1568 int pos = *position;
1569 int num = *num_entries;
1573 char termz[IT_MAX_WORD+20];
1576 AttrType completeness;
1577 int completeness_value;
1578 struct scan_info *scan_info_array;
1579 struct scan_entry *glist;
1580 int ords[32], ord_no = 0;
1583 logf (LOG_DEBUG, "scan, position = %d, num = %d", pos, num);
1585 if (attributeset == VAL_NONE)
1586 attributeset = VAL_BIB1;
1588 attr_init (&use, zapt, 1);
1589 use_value = attr_find (&use, &attributeset);
1590 logf (LOG_DEBUG, "use value %d", use_value);
1592 attr_init (&completeness, zapt, 6);
1593 completeness_value = attr_find (&completeness, NULL);
1594 logf (LOG_DEBUG, "completeness value %d", completeness_value);
1596 if (use_value == -1)
1598 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
1601 data1_local_attribute *local_attr;
1603 attp = att_getentbyatt (attributeset, use_value);
1606 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
1607 attributeset, use_value);
1608 return zi->errCode = 114;
1610 if (zebTargetInfo_curDatabase (zi->zti, basenames[base_no]))
1612 zi->errString = basenames[base_no];
1613 return zi->errCode = 109; /* Database unavailable */
1615 for (local_attr = attp->local_attributes; local_attr && ord_no < 32;
1616 local_attr = local_attr->next)
1620 ord = zebTargetInfo_lookupSU (zi->zti, attp->attset_ordinal,
1623 ords[ord_no++] = ord;
1627 return zi->errCode = 113;
1630 scan_info_array = odr_malloc (zi->odr, ord_no * sizeof(*scan_info_array));
1631 for (i = 0; i < ord_no; i++)
1633 int j, prefix_len = 0;
1634 int before_tmp = before, after_tmp = after;
1635 struct scan_info *scan_info = scan_info_array + i;
1637 scan_info->before = before;
1638 scan_info->after = after;
1639 scan_info->odr = zi->odr;
1641 scan_info->list = odr_malloc (zi->odr, (before+after)*
1642 sizeof(*scan_info->list));
1643 for (j = 0; j<before+after; j++)
1644 scan_info->list[j].term = NULL;
1645 termz[prefix_len++] = ords[i];
1646 termz[prefix_len++] =
1647 (completeness_value==2 || completeness_value==3) ? 'p': 'w';
1648 termz[prefix_len] = 0;
1649 strcpy (scan_info->prefix, termz);
1651 trans_scan_term (zi, zapt, termz+prefix_len);
1653 dict_scan (zi->dict, termz, &before_tmp, &after_tmp, scan_info,
1656 glist = odr_malloc (zi->odr, (before+after)*sizeof(*glist));
1657 for (i = 0; i < ord_no; i++)
1660 *status = BEND_SCAN_SUCCESS;
1661 for (i = 0; i<after; i++)
1664 const char *mterm = NULL;
1668 for (j = 0; j < ord_no; j++)
1670 if (ptr[j] < before+after &&
1671 (tst=scan_info_array[j].list[ptr[j]].term) &&
1672 (!mterm || strcmp (tst, mterm) < 0))
1680 scan_term_untrans (zi->odr, &glist[i+before].term, mterm);
1682 rset_create_isamx (zi, scan_info_array[j0].list[ptr[j0]].isam_p);
1685 for (j = j0+1; j<ord_no; j++)
1687 if (ptr[j] < before+after &&
1688 (tst=scan_info_array[j].list[ptr[j]].term) &&
1689 !strcmp (tst, mterm))
1691 rset_bool_parms bool_parms;
1694 rset2 = rset_create_isamx
1695 (zi, scan_info_array[j].list[ptr[j]].isam_p);
1697 bool_parms.key_size = sizeof(struct it_key);
1698 bool_parms.cmp = key_compare;
1699 bool_parms.rset_l = rset;
1700 bool_parms.rset_r = rset2;
1702 rset = rset_create (rset_kind_or, &bool_parms);
1707 count_set (rset, &glist[i+before].occurrences);
1712 *num_entries -= (after-i);
1713 *status = BEND_SCAN_PARTIAL;
1716 for (i = 0; i<ord_no; i++)
1719 for (i = 0; i<before; i++)
1722 const char *mterm = NULL;
1726 for (j = 0; j <ord_no; j++)
1728 if (ptr[j] < before &&
1729 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
1730 (!mterm || strcmp (tst, mterm) > 0))
1739 scan_term_untrans (zi->odr, &glist[before-1-i].term, mterm);
1741 rset = rset_create_isamx
1742 (zi, scan_info_array[j0].list[before-1-ptr[j0]].isam_p);
1746 for (j = j0+1; j<ord_no; j++)
1748 if (ptr[j] < before &&
1749 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
1750 !strcmp (tst, mterm))
1752 rset_bool_parms bool_parms;
1755 rset2 = rset_create_isamx (zi,
1756 scan_info_array[j].list[before-1-ptr[j]].isam_p);
1758 bool_parms.key_size = sizeof(struct it_key);
1759 bool_parms.cmp = key_compare;
1760 bool_parms.rset_l = rset;
1761 bool_parms.rset_r = rset2;
1763 rset = rset_create (rset_kind_or, &bool_parms);
1768 count_set (rset, &glist[before-1-i].occurrences);
1774 *status = BEND_SCAN_PARTIAL;
1778 *list = glist + i; /* list is set to first 'real' entry */
1780 logf (LOG_DEBUG, "position = %d, num_entries = %d",
1781 *position, *num_entries);
1783 logf (LOG_DEBUG, "scan error: %d", zi->errCode);