2 * Copyright (C) 1994-1996, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.52 1996-06-17 14:26:20 adam
8 * Function gen_regular_rel changed to handle negative numbers.
10 * Revision 1.51 1996/06/11 10:54:15 quinn
13 * Revision 1.50 1996/06/07 08:51:53 adam
14 * Bug fix: Character mapping was broken (introducued by last revision).
16 * Revision 1.49 1996/06/04 10:18:11 adam
17 * Search/scan uses character mapping module.
19 * Revision 1.48 1996/05/28 15:15:01 adam
20 * Bug fix: Didn't handle unknown database correctly.
22 * Revision 1.47 1996/05/15 18:36:28 adam
23 * Function trans_term transforms unsearchable characters to blanks.
25 * Revision 1.46 1996/05/15 11:57:56 adam
26 * Fixed bug introduced by set/field mapping in search operations.
28 * Revision 1.45 1996/05/14 11:34:00 adam
29 * Scan support in multiple registers/databases.
31 * Revision 1.44 1996/05/14 06:16:44 adam
32 * Compact use/set bytes used in search service.
34 * Revision 1.43 1996/05/09 09:54:43 adam
35 * Server supports maps from one logical attributes to a list of physical
37 * The extraction process doesn't make space consuming 'any' keys.
39 * Revision 1.42 1996/05/09 07:28:56 quinn
40 * Work towards phrases and multiple registers
42 * Revision 1.41 1996/03/20 09:36:43 adam
43 * Function dict_lookup_grep got extra parameter, init_pos, which marks
44 * from which position in pattern approximate pattern matching should occur.
45 * Approximate pattern matching is used in relevance=re-2.
47 * Revision 1.40 1996/02/02 13:44:44 adam
48 * The public dictionary functions simply use char instead of Dict_char
49 * to represent search strings. Dict_char is used internally only.
51 * Revision 1.39 1996/01/03 16:22:13 quinn
54 * Revision 1.38 1995/12/11 09:12:55 adam
55 * The rec_get function returns NULL if record doesn't exist - will
56 * happen in the server if the result set records have been deleted since
57 * the creation of the set (i.e. the search).
58 * The server saves a result temporarily if it is 'volatile', i.e. the
59 * set is register dependent.
61 * Revision 1.37 1995/12/06 15:05:28 adam
62 * More verbose in count_set.
64 * Revision 1.36 1995/12/06 12:41:27 adam
65 * New command 'stat' for the index program.
66 * Filenames can be read from stdin by specifying '-'.
67 * Bug fix/enhancement of the transformation from terms to regular
68 * expressons in the search engine.
70 * Revision 1.35 1995/11/27 09:29:00 adam
71 * Bug fixes regarding conversion to regular expressions.
73 * Revision 1.34 1995/11/16 17:00:56 adam
74 * Better logging of rpn query.
76 * Revision 1.33 1995/11/01 13:58:28 quinn
77 * Moving data1 to yaz/retrieval
79 * Revision 1.32 1995/10/27 14:00:11 adam
80 * Implemented detection of database availability.
82 * Revision 1.31 1995/10/17 18:02:10 adam
83 * New feature: databases. Implemented as prefix to words in dictionary.
85 * Revision 1.30 1995/10/16 09:32:38 adam
86 * More work on relational op.
88 * Revision 1.29 1995/10/13 16:01:49 adam
91 * Revision 1.28 1995/10/13 12:26:43 adam
92 * Optimization of truncation.
94 * Revision 1.27 1995/10/12 17:07:22 adam
97 * Revision 1.26 1995/10/12 12:40:54 adam
98 * Bug fixes in rpn_prox.
100 * Revision 1.25 1995/10/10 13:59:24 adam
101 * Function rset_open changed its wflag parameter to general flags.
103 * Revision 1.24 1995/10/09 16:18:37 adam
104 * Function dict_lookup_grep got extra client data parameter.
106 * Revision 1.23 1995/10/06 16:33:37 adam
107 * Use attribute mappings.
109 * Revision 1.22 1995/10/06 15:07:39 adam
110 * Structure 'local-number' handled.
112 * Revision 1.21 1995/10/06 13:52:06 adam
113 * Bug fixes. Handler may abort further scanning.
115 * Revision 1.20 1995/10/06 11:06:33 adam
116 * Scan entries include 'occurrences' now.
118 * Revision 1.19 1995/10/06 10:43:56 adam
119 * Scan added. 'occurrences' in scan entries not set yet.
121 * Revision 1.18 1995/10/04 16:57:20 adam
122 * Key input and merge sort in one pass.
124 * Revision 1.17 1995/10/04 12:55:17 adam
125 * Bug fix in ranked search. Use=Any keys inserted.
127 * Revision 1.16 1995/10/02 16:24:40 adam
128 * Use attribute actually used in search requests.
130 * Revision 1.15 1995/10/02 15:18:52 adam
131 * New member in recRetrieveCtrl: diagnostic.
133 * Revision 1.14 1995/09/28 12:10:32 adam
134 * Bug fixes. Field prefix used in queries.
136 * Revision 1.13 1995/09/18 14:17:50 adam
139 * Revision 1.12 1995/09/15 14:45:21 adam
141 * Work on truncation.
143 * Revision 1.11 1995/09/14 11:53:27 adam
144 * First work on regular expressions/truncations.
146 * Revision 1.10 1995/09/11 15:23:26 adam
147 * More work on relevance search.
149 * Revision 1.9 1995/09/11 13:09:35 adam
150 * More work on relevance feedback.
152 * Revision 1.8 1995/09/08 14:52:27 adam
153 * Minor changes. Dictionary is lower case now.
155 * Revision 1.7 1995/09/07 13:58:36 adam
156 * New parameter: result-set file descriptor (RSFD) to support multiple
157 * positions within the same result-set.
158 * Boolean operators: and, or, not implemented.
159 * Result-set references.
161 * Revision 1.6 1995/09/06 16:11:18 adam
162 * Option: only one word key per file.
164 * Revision 1.5 1995/09/06 10:33:04 adam
165 * More work on present. Some log messages removed.
167 * Revision 1.4 1995/09/05 15:28:40 adam
168 * More work on search engine.
170 * Revision 1.3 1995/09/04 15:20:22 adam
173 * Revision 1.2 1995/09/04 12:33:43 adam
174 * Various cleanup. YAZ util used instead.
176 * Revision 1.1 1995/09/04 09:10:40 adam
177 * More work on index add/del/update.
178 * Merge sort implemented.
179 * Initial work on z39 server.
188 #include "attribute.h"
201 Z_AttributesPlusTerm *zapt;
204 static int attr_find (AttrType *src, oid_value *attributeSetP)
206 while (src->major < src->zapt->num_attributes)
208 Z_AttributeElement *element;
210 element = src->zapt->attributeList[src->major];
211 if (src->type == *element->attributeType)
213 switch (element->which)
215 case Z_AttributeValue_numeric:
217 if (element->attributeSet && attributeSetP)
221 attrset = oid_getentbyoid (element->attributeSet);
222 *attributeSetP = attrset->value;
224 return *element->value.numeric;
226 case Z_AttributeValue_complex:
227 if (src->minor >= element->value.complex->num_list ||
228 element->value.complex->list[src->minor]->which !=
229 Z_StringOrNumeric_numeric)
232 if (element->attributeSet && attributeSetP)
236 attrset = oid_getentbyoid (element->attributeSet);
237 *attributeSetP = attrset->value;
239 return *element->value.complex->list[src->minor-1]->u.numeric;
249 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
263 int (*cmp)(const void *p1, const void *p2);
270 static void heap_swap (struct trunc_info *ti, int i1, int i2)
275 ti->ptr[i1] = ti->ptr[i2];
279 static void heap_delete (struct trunc_info *ti)
281 int cur = 1, child = 2;
283 heap_swap (ti, 1, ti->heapnum--);
284 while (child <= ti->heapnum) {
285 if (child < ti->heapnum &&
286 (*ti->cmp)(ti->heap[ti->ptr[child]],
287 ti->heap[ti->ptr[1+child]]) > 0)
289 if ((*ti->cmp)(ti->heap[ti->ptr[cur]],
290 ti->heap[ti->ptr[child]]) > 0)
292 heap_swap (ti, cur, child);
301 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
305 cur = ++(ti->heapnum);
306 memcpy (ti->heap[ti->ptr[cur]], buf, ti->keysize);
307 ti->indx[ti->ptr[cur]] = indx;
309 while (parent && (*ti->cmp)(ti->heap[ti->ptr[parent]],
310 ti->heap[ti->ptr[cur]]) > 0)
312 heap_swap (ti, cur, parent);
319 struct trunc_info *heap_init (int size, int key_size,
320 int (*cmp)(const void *p1, const void *p2))
322 struct trunc_info *ti = xmalloc (sizeof(*ti));
327 ti->keysize = key_size;
329 ti->indx = xmalloc (size * sizeof(*ti->indx));
330 ti->heap = xmalloc (size * sizeof(*ti->heap));
331 ti->ptr = xmalloc (size * sizeof(*ti->ptr));
332 ti->swapbuf = xmalloc (ti->keysize);
333 ti->tmpbuf = xmalloc (ti->keysize);
334 ti->buf = xmalloc (size * ti->keysize);
335 for (i = size; --i >= 0; )
338 ti->heap[i] = ti->buf + ti->keysize * i;
343 static void heap_close (struct trunc_info *ti)
353 static RSET rset_trunc_r (ISAM isam, ISAM_P *isam_p, int from, int to,
358 rset_temp_parms parms;
360 parms.key_size = sizeof(struct it_key);
361 result = rset_create (rset_kind_temp, &parms);
362 result_rsfd = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
364 if (to - from > merge_chunk)
368 int i, i_add = (to-from)/merge_chunk + 1;
369 struct trunc_info *ti;
371 int rsmax = (to-from)/i_add + 1;
373 rset = xmalloc (sizeof(*rset) * rsmax);
374 rsfd = xmalloc (sizeof(*rsfd) * rsmax);
376 for (i = from; i < to; i += i_add)
379 rset[rscur] = rset_trunc_r (isam, isam_p, i, i+i_add,
382 rset[rscur] = rset_trunc_r (isam, isam_p, i, to,
386 ti = heap_init (rscur, sizeof(struct it_key), key_compare);
387 for (i = rscur; --i >= 0; )
389 rsfd[i] = rset_open (rset[i], RSETF_READ|RSETF_SORT_SYSNO);
390 if (rset_read (rset[i], rsfd[i], ti->tmpbuf))
391 heap_insert (ti, ti->tmpbuf, i);
394 rset_close (rset[i], rsfd[i]);
395 rset_delete (rset[i]);
400 int n = ti->indx[ti->ptr[1]];
402 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
406 if (!rset_read (rset[n], rsfd[n], ti->tmpbuf))
409 rset_close (rset[n], rsfd[n]);
410 rset_delete (rset[n]);
413 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
416 heap_insert (ti, ti->tmpbuf, n);
429 struct trunc_info *ti;
431 ispt = xmalloc (sizeof(*ispt) * (to-from));
433 ti = heap_init (to-from, sizeof(struct it_key),
435 for (i = to-from; --i >= 0; )
437 ispt[i] = is_position (isam, isam_p[from+i]);
438 if (is_readkey (ispt[i], ti->tmpbuf))
439 heap_insert (ti, ti->tmpbuf, i);
441 is_pt_free (ispt[i]);
445 int n = ti->indx[ti->ptr[1]];
447 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
449 /* section that preserve all keys */
451 if (is_readkey (ispt[n], ti->tmpbuf))
452 heap_insert (ti, ti->tmpbuf, n);
454 is_pt_free (ispt[n]);
456 /* section that preserve all keys with unique sysnos */
459 if (!is_readkey (ispt[n], ti->tmpbuf))
462 is_pt_free (ispt[n]);
465 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
468 heap_insert (ti, ti->tmpbuf, n);
477 rset_close (result, result_rsfd);
481 static int isam_trunc_cmp (const void *p1, const void *p2)
483 ISAM_P i1 = *(ISAM_P*) p1;
484 ISAM_P i2 = *(ISAM_P*) p2;
487 d = is_type (i1) - is_type (i2);
490 return is_block (i1) - is_block (i2);
493 static RSET rset_trunc (ISAM isam, ISAM_P *isam_p, int no)
496 qsort (isam_p, no, sizeof(*isam_p), isam_trunc_cmp);
497 return rset_trunc_r (isam, isam_p, 0, no, 100);
511 static void add_isam_p (const char *info, struct grep_info *p)
513 if (p->isam_p_indx == p->isam_p_size)
515 ISAM_P *new_isam_p_buf;
520 p->isam_p_size = 2*p->isam_p_size + 100;
521 new_isam_p_buf = xmalloc (sizeof(*new_isam_p_buf) *
525 memcpy (new_isam_p_buf, p->isam_p_buf,
526 p->isam_p_indx * sizeof(*p->isam_p_buf));
527 xfree (p->isam_p_buf);
529 p->isam_p_buf = new_isam_p_buf;
532 new_term_no = xmalloc (sizeof(*new_term_no) *
536 memcpy (new_term_no, p->isam_p_buf,
537 p->isam_p_indx * sizeof(*p->term_no));
540 p->term_no = new_term_no;
543 assert (*info == sizeof(*p->isam_p_buf));
544 memcpy (p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
548 static int grep_handle (char *name, const char *info, void *p)
550 add_isam_p (info, p);
554 /* gen_regular_rel - generate regular expression from relation
555 * val: border value (inclusive)
556 * islt: 1 if <=; 0 if >=.
558 static void gen_regular_rel (char *dst, int val, int islt)
565 logf (LOG_DEBUG, "gen_regular_rel. val=%d, islt=%d", val, islt);
569 strcpy (dst, "(-[0-9]+|");
577 strcpy (dst, "([0-9]+|-");
578 dst_p = strlen (dst);
588 dst_p = strlen (dst);
589 sprintf (numstr, "%d", val);
590 for (w = strlen(numstr); --w >= 0; pos++)
609 strcpy (dst + dst_p, numstr);
610 dst_p = strlen(dst) - pos - 1;
638 for (i = 0; i<pos; i++)
651 for (i=1; i<pos; i++)
652 strcat (dst, "[0-9]?");
656 for (i = 0; i <= pos; i++)
657 strcat (dst, "[0-9]");
658 strcat (dst, "[0-9]*");
663 static int relational_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
664 const char *term_sub,
666 oid_value attributeSet,
667 struct grep_info *grep_info,
675 attr_init (&relation, zapt, 2);
676 relation_value = attr_find (&relation, NULL);
677 term_value = atoi (term_sub);
679 switch (relation_value)
684 logf (LOG_DEBUG, "Relation <");
685 gen_regular_rel (term_dict + strlen(term_dict), term_value-1, 1);
690 logf (LOG_DEBUG, "Relation <=");
691 gen_regular_rel (term_dict + strlen(term_dict), term_value, 1);
696 logf (LOG_DEBUG, "Relation >=");
697 gen_regular_rel (term_dict + strlen(term_dict), term_value, 0);
702 logf (LOG_DEBUG, "Relation >");
703 gen_regular_rel (term_dict + strlen(term_dict), term_value+1, 0);
708 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_dict);
709 r = dict_lookup_grep (zi->wordDict, term_dict, 0, grep_info, max_pos,
712 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
713 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
717 static void verbatim_char (int ch, int *indx, char *dst)
720 dst[(*indx)++] = '\\';
724 static int field_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
725 const char *term_sub, int regType,
726 oid_value attributeSet, struct grep_info *grep_info,
727 int num_bases, char **basenames)
729 char term_dict[2*IT_MAX_WORD+2];
730 int i, j, r, base_no;
732 int truncation_value;
735 oid_value curAttributeSet = attributeSet;
737 attr_init (&use, zapt, 1);
738 use_value = attr_find (&use, &curAttributeSet);
739 logf (LOG_DEBUG, "use value %d", use_value);
740 attr_init (&truncation, zapt, 5);
741 truncation_value = attr_find (&truncation, NULL);
742 logf (LOG_DEBUG, "truncation value %d", truncation_value);
747 for (base_no = 0; base_no < num_bases; base_no++)
750 data1_local_attribute *local_attr;
751 int max_pos, prefix_len = 0;
753 attp = att_getentbyatt (curAttributeSet, use_value);
756 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
757 curAttributeSet, use_value);
761 if (zebTargetInfo_curDatabase (zi->zti, basenames[base_no]))
763 zi->errCode = 109; /* Database unavailable */
764 zi->errString = basenames[base_no];
767 for (local_attr = attp->local_attributes; local_attr;
768 local_attr = local_attr->next)
772 ord = zebTargetInfo_lookupSU (zi->zti, attp->attset_ordinal,
777 term_dict[prefix_len++] = '|';
779 term_dict[prefix_len++] = '(';
780 term_dict[prefix_len++] = 1;
781 term_dict[prefix_len++] = ord;
788 term_dict[prefix_len++] = ')';
789 term_dict[prefix_len++] = 1;
790 term_dict[prefix_len++] = regType;
791 term_dict[prefix_len] = '\0';
792 if (!relational_term (zi, zapt, term_sub, term_dict,
793 attributeSet, grep_info, &max_pos))
798 switch (truncation_value)
800 case -1: /* not specified */
801 case 100: /* do not truncate */
802 term_dict[j++] = '(';
803 for (i = 0; term_sub[i]; i++)
804 verbatim_char (term_sub[i], &j, term_dict);
805 strcpy (term_dict+j, ")");
806 r = dict_lookup_grep (zi->wordDict, term_dict, 0, grep_info,
807 &max_pos, 0, grep_handle);
809 logf (LOG_WARN, "dict_lookup_grep err, trunc=none:%d", r);
811 case 1: /* right truncation */
812 term_dict[j++] = '(';
813 for (i = 0; term_sub[i]; i++)
814 verbatim_char (term_sub[i], &j, term_dict);
815 strcpy (term_dict+j, ".*)");
816 dict_lookup_grep (zi->wordDict, term_dict, 0, grep_info,
817 &max_pos, 0, grep_handle);
819 case 2: /* left truncation */
820 case 3: /* left&right truncation */
823 case 101: /* process # in term */
824 term_dict[j++] = '(';
825 for (i=0; term_sub[i]; i++)
826 if (term_sub[i] == '#' && i > 2)
828 term_dict[j++] = '.';
829 term_dict[j++] = '*';
832 verbatim_char (term_sub[i], &j, term_dict);
833 strcpy (term_dict+j, ")");
834 r = dict_lookup_grep (zi->wordDict, term_dict, 0, grep_info,
835 &max_pos, 0, grep_handle);
837 logf (LOG_WARN, "dict_lookup_grep err, trunc=#: %d",
840 case 102: /* regular expression */
841 sprintf (term_dict + j, "(%s)", term_sub);
842 r = dict_lookup_grep (zi->wordDict, term_dict, 0, grep_info,
843 &max_pos, 0, grep_handle);
845 logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
848 case 103: /* regular expression with error correction */
851 if (*cp == '*' && cp[1] && cp[2])
856 sprintf (term_dict + j, "(%s)", cp);
857 r = dict_lookup_grep (zi->wordDict, term_dict, r, grep_info,
858 &max_pos, j, grep_handle);
860 logf (LOG_WARN, "dict_lookup_grep err, trunc=eregular: %d",
866 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
870 static void trans_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
874 Z_Term *term = zapt->term;
876 sizez = term->u.general->len;
877 if (sizez > IT_MAX_WORD-1)
878 sizez = IT_MAX_WORD-1;
879 memcpy (termz, term->u.general->buf, sizez);
883 static void trans_scan_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
886 Z_Term *term = zapt->term;
888 const char *cp = (const char *) term->u.general->buf;
889 const char *cp_end = cp + term->u.general->len;
895 while ((len = (cp_end - cp)) > 0)
897 map = map_chrs_input (&cp, len);
898 if (**map == *CHR_SPACE)
906 for (src = *map; *src; src++)
912 static RSET rpn_search_APT_relevance (ZServerInfo *zi,
913 Z_AttributesPlusTerm *zapt,
914 oid_value attributeSet,
915 int num_bases, char **basenames)
917 rset_relevance_parms parms;
918 char termz[IT_MAX_WORD+1];
919 char term_sub[IT_MAX_WORD+1];
920 struct grep_info grep_info;
925 parms.key_size = sizeof(struct it_key);
927 parms.cmp = key_compare;
928 parms.is = zi->wordIsam;
931 if (zapt->term->which != Z_Term_general)
936 trans_term (zi, zapt, termz);
939 grep_info.term_no = 0;
941 grep_info.isam_p_indx = 0;
942 grep_info.isam_p_size = 0;
943 grep_info.isam_p_buf = NULL;
952 map = map_chrs_input (&p1, strlen(p1));
953 if (**map != *CHR_SPACE)
962 map = map_chrs_input (&p1, strlen(p1));
963 if (**map == *CHR_SPACE)
968 memcpy (term_sub, p0, p2-p0);
969 term_sub[p2-p0] = '\0';
971 if (field_term (zi, zapt, term_sub, 'w', attributeSet, &grep_info,
972 num_bases, basenames))
975 for (; term_index < grep_info.isam_p_indx; term_index++)
976 grep_info.term_no[term_index] = parms.no_terms;
980 parms.term_no = grep_info.term_no;
981 parms.isam_positions = grep_info.isam_p_buf;
982 parms.no_isam_positions = grep_info.isam_p_indx;
983 if (grep_info.isam_p_indx > 0)
984 result = rset_create (rset_kind_relevance, &parms);
986 result = rset_create (rset_kind_null, NULL);
988 xfree(grep_info.term_no);
990 xfree (grep_info.isam_p_buf);
994 static RSET rpn_search_APT_cphrase (ZServerInfo *zi,
995 Z_AttributesPlusTerm *zapt,
996 oid_value attributeSet,
997 int num_bases, char **basenames)
999 rset_isam_parms parms;
1000 char termz[IT_MAX_WORD+1];
1001 struct grep_info grep_info;
1004 if (zapt->term->which != Z_Term_general)
1009 trans_term (zi, zapt, termz);
1012 grep_info.term_no = 0;
1014 grep_info.isam_p_indx = 0;
1015 grep_info.isam_p_size = 0;
1016 grep_info.isam_p_buf = NULL;
1018 if (field_term (zi, zapt, termz, 'p', attributeSet, &grep_info,
1019 num_bases, basenames))
1021 if (grep_info.isam_p_indx < 1)
1022 result = rset_create (rset_kind_null, NULL);
1023 else if (grep_info.isam_p_indx == 1)
1025 parms.is = zi->wordIsam;
1026 parms.pos = *grep_info.isam_p_buf;
1027 result = rset_create (rset_kind_isam, &parms);
1030 result = rset_trunc (zi->wordIsam, grep_info.isam_p_buf,
1031 grep_info.isam_p_indx);
1033 xfree(grep_info.term_no);
1035 xfree (grep_info.isam_p_buf);
1039 static RSET rpn_prox (RSET *rset, int rset_no)
1044 struct it_key **buf;
1047 rset_temp_parms parms;
1049 rsfd = xmalloc (sizeof(*rsfd)*rset_no);
1050 more = xmalloc (sizeof(*more)*rset_no);
1051 buf = xmalloc (sizeof(*buf)*rset_no);
1053 for (i = 0; i<rset_no; i++)
1055 buf[i] = xmalloc (sizeof(**buf));
1056 rsfd[i] = rset_open (rset[i], RSETF_READ|RSETF_SORT_SYSNO);
1057 if (!(more[i] = rset_read (rset[i], rsfd[i], buf[i])))
1061 rset_close (rset[i], rsfd[i]);
1068 return rset_create (rset_kind_null, NULL);
1071 parms.key_size = sizeof (struct it_key);
1072 result = rset_create (rset_kind_temp, &parms);
1073 rsfd_result = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
1077 for (i = 1; i<rset_no; i++)
1086 cmp = key_compare (buf[i], buf[i-1]);
1089 more[i-1] = rset_read (rset[i-1], rsfd[i-1], buf[i-1]);
1094 if (buf[i-1]->seqno+1 != buf[i]->seqno)
1096 more[i-1] = rset_read (rset[i-1], rsfd[i-1], buf[i-1]);
1102 more[i] = rset_read (rset[i], rsfd[i], buf[i]);
1108 rset_write (result, rsfd_result, buf[0]);
1109 more[0] = rset_read (*rset, *rsfd, *buf);
1113 for (i = 0; i<rset_no; i++)
1115 rset_close (rset[i], rsfd[i]);
1118 rset_close (result, rsfd_result);
1125 static RSET rpn_search_APT_phrase (ZServerInfo *zi,
1126 Z_AttributesPlusTerm *zapt,
1127 oid_value attributeSet,
1128 int num_bases, char **basenames)
1130 char termz[IT_MAX_WORD+1];
1131 char term_sub[IT_MAX_WORD+1];
1133 RSET rset[60], result;
1135 struct grep_info grep_info;
1137 if (zapt->term->which != Z_Term_general)
1142 trans_term (zi, zapt, termz);
1145 grep_info.term_no = 0;
1147 grep_info.isam_p_size = 0;
1148 grep_info.isam_p_buf = NULL;
1158 map = map_chrs_input (&p1, strlen(p1));
1159 if (**map != *CHR_SPACE)
1168 map = map_chrs_input (&p1, strlen(p1));
1169 if (**map == *CHR_SPACE)
1175 memcpy (term_sub, p0, p2-p0);
1176 term_sub[p2-p0] = '\0';
1179 grep_info.isam_p_indx = 0;
1180 if (field_term (zi, zapt, term_sub, 'w', attributeSet, &grep_info,
1181 num_bases, basenames))
1183 if (grep_info.isam_p_indx == 0)
1184 rset[rset_no] = rset_create (rset_kind_null, NULL);
1185 else if (grep_info.isam_p_indx > 1)
1186 rset[rset_no] = rset_trunc (zi->wordIsam,
1187 grep_info.isam_p_buf,
1188 grep_info.isam_p_indx);
1191 rset_isam_parms parms;
1193 parms.is = zi->wordIsam;
1194 parms.pos = *grep_info.isam_p_buf;
1195 rset[rset_no] = rset_create (rset_kind_isam, &parms);
1197 assert (rset[rset_no]);
1198 if (++rset_no >= sizeof(rset)/sizeof(*rset))
1202 xfree(grep_info.term_no);
1204 xfree (grep_info.isam_p_buf);
1206 return rset_create (rset_kind_null, NULL);
1207 else if (rset_no == 1)
1210 result = rpn_prox (rset, rset_no);
1211 for (i = 0; i<rset_no; i++)
1212 rset_delete (rset[i]);
1216 static RSET rpn_search_APT_local (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1217 oid_value attributeSet)
1222 rset_temp_parms parms;
1223 char termz[IT_MAX_WORD+1];
1225 if (zapt->term->which != Z_Term_general)
1230 parms.key_size = sizeof (struct it_key);
1231 result = rset_create (rset_kind_temp, &parms);
1232 rsfd = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
1234 trans_term (zi, zapt, termz);
1236 key.sysno = atoi (termz);
1239 rset_write (result, rsfd, &key);
1240 rset_close (result, rsfd);
1244 static RSET rpn_search_APT (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1245 oid_value attributeSet,
1246 int num_bases, char **basenames)
1250 AttrType completeness;
1251 int relation_value, structure_value, completeness_value;
1253 attr_init (&relation, zapt, 2);
1254 attr_init (&structure, zapt, 4);
1255 attr_init (&completeness, zapt, 6);
1257 relation_value = attr_find (&relation, NULL);
1258 structure_value = attr_find (&structure, NULL);
1259 completeness_value = attr_find (&completeness, NULL);
1260 switch (structure_value)
1263 if (relation_value == 102) /* relevance relation */
1264 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1265 num_bases, basenames);
1266 if (completeness_value == 2 || completeness_value == 3)
1267 return rpn_search_APT_cphrase (zi, zapt, attributeSet,
1268 num_bases, basenames);
1269 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1270 num_bases, basenames);
1271 case 1: /* phrase */
1272 if (relation_value == 102) /* relevance relation */
1273 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1274 num_bases, basenames);
1275 if (completeness_value == 2 || completeness_value == 3)
1276 return rpn_search_APT_cphrase (zi, zapt, attributeSet,
1277 num_bases, basenames);
1278 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1279 num_bases, basenames);
1282 if (relation_value == 102) /* relevance relation */
1283 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1284 num_bases, basenames);
1285 if (completeness_value == 2 || completeness_value == 3)
1286 return rpn_search_APT_cphrase (zi, zapt, attributeSet,
1287 num_bases, basenames);
1288 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1289 num_bases, basenames);
1294 case 5: /* date - normalized */
1296 case 6: /* word list */
1297 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1298 num_bases, basenames);
1299 case 100: /* date - un-normalized */
1301 case 101: /* name - normalized */
1303 case 102: /* date - un-normalized */
1305 case 103: /* structure */
1309 case 105: /* free-form-text */
1310 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1311 num_bases, basenames);
1312 case 106: /* document-text */
1313 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1314 num_bases, basenames);
1315 case 107: /* local-number */
1316 return rpn_search_APT_local (zi, zapt, attributeSet);
1317 case 108: /* string */
1318 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1319 num_bases, basenames);
1320 case 109: /* numeric string */
1327 static RSET rpn_search_ref (ZServerInfo *zi, Z_ResultSetId *resultSetId)
1331 if (!(s = resultSetGet (zi, resultSetId)))
1332 return rset_create (rset_kind_null, NULL);
1336 static RSET rpn_search_structure (ZServerInfo *zi, Z_RPNStructure *zs,
1337 oid_value attributeSet,
1338 int num_bases, char **basenames)
1341 if (zs->which == Z_RPNStructure_complex)
1343 rset_bool_parms bool_parms;
1346 bool_parms.rset_l = rpn_search_structure (zi, zs->u.complex->s1,
1348 num_bases, basenames);
1349 if (bool_parms.rset_l == NULL)
1351 if (rset_is_ranked(bool_parms.rset_l))
1353 bool_parms.rset_r = rpn_search_structure (zi, zs->u.complex->s2,
1355 num_bases, basenames);
1356 if (bool_parms.rset_r == NULL)
1358 rset_delete (bool_parms.rset_l);
1361 if (rset_is_ranked(bool_parms.rset_r))
1363 bool_parms.key_size = sizeof(struct it_key);
1364 bool_parms.cmp = key_compare;
1366 switch (zs->u.complex->roperator->which)
1368 case Z_Operator_and:
1369 r = rset_create (soft ? rset_kind_sand:rset_kind_and, &bool_parms);
1372 r = rset_create (soft ? rset_kind_sor:rset_kind_or, &bool_parms);
1374 case Z_Operator_and_not:
1375 r = rset_create (soft ? rset_kind_snot:rset_kind_not, &bool_parms);
1381 else if (zs->which == Z_RPNStructure_simple)
1383 if (zs->u.simple->which == Z_Operand_APT)
1385 logf (LOG_DEBUG, "rpn_search_APT");
1386 r = rpn_search_APT (zi, zs->u.simple->u.attributesPlusTerm,
1387 attributeSet, num_bases, basenames);
1389 else if (zs->u.simple->which == Z_Operand_resultSetId)
1391 logf (LOG_DEBUG, "rpn_search_ref");
1392 r = rpn_search_ref (zi, zs->u.simple->u.resultSetId);
1406 void count_set_save (RSET *r, int *count)
1413 rset_temp_parms parms;
1415 logf (LOG_DEBUG, "count_set_save");
1417 parms.key_size = sizeof(struct it_key);
1418 w = rset_create (rset_kind_temp, &parms);
1419 wfd = rset_open (w, RSETF_WRITE|RSETF_SORT_SYSNO);
1420 rfd = rset_open (*r, RSETF_READ|RSETF_SORT_SYSNO);
1421 while (rset_read (*r, rfd, &key))
1423 if (key.sysno != psysno)
1425 rset_write (w, wfd, &key);
1431 rset_close (*r, rfd);
1433 rset_close (w, wfd);
1435 logf (LOG_DEBUG, "%d keys, %d distinct sysnos", kno, *count);
1438 static void count_set (RSET r, int *count)
1445 logf (LOG_DEBUG, "count_set");
1447 rfd = rset_open (r, RSETF_READ|RSETF_SORT_SYSNO);
1448 while (rset_read (r, rfd, &key))
1450 if (key.sysno != psysno)
1457 rset_close (r, rfd);
1458 logf (LOG_DEBUG, "%d keys, %d distinct sysnos", kno, *count);
1461 int rpn_search (ZServerInfo *zi,
1462 Z_RPNQuery *rpn, int num_bases, char **basenames,
1463 const char *setname, int *hits)
1467 oid_value attributeSet;
1469 dict_grep_cmap (zi->wordDict, map_chrs_input);
1473 zi->errString = NULL;
1475 attrset = oid_getentbyoid (rpn->attributeSetId);
1476 attributeSet = attrset->value;
1477 rset = rpn_search_structure (zi, rpn->RPNStructure, attributeSet,
1478 num_bases, basenames);
1481 if (rset_is_volatile(rset))
1482 count_set_save(&rset,hits);
1484 count_set (rset, hits);
1485 resultSetAdd (zi, setname, 1, rset);
1487 logf (LOG_DEBUG, "search error: %d", zi->errCode);
1491 struct scan_info_entry {
1497 struct scan_info_entry *list;
1503 static int scan_handle (char *name, const char *info, int pos, void *client)
1505 int len_prefix, idx;
1506 struct scan_info *scan_info = client;
1508 len_prefix = strlen(scan_info->prefix);
1509 if (memcmp (name, scan_info->prefix, len_prefix))
1512 idx = scan_info->after - pos + scan_info->before;
1515 logf (LOG_DEBUG, "%-3d %s", idx, name+len_prefix);
1516 scan_info->list[idx].term = odr_malloc (scan_info->odr,
1517 strlen(name + len_prefix)+1);
1518 strcpy (scan_info->list[idx].term, name + len_prefix);
1519 assert (*info == sizeof(ISAM_P));
1520 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAM_P));
1525 static void scan_term_untrans (ODR odr, char **dstp, const char *src)
1527 char *dst = odr_malloc (odr, strlen(src)*2+1);
1532 const char *cp = map_chrs_output (&src);
1539 int rpn_scan (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1540 oid_value attributeset,
1541 int num_bases, char **basenames,
1542 int *position, int *num_entries, struct scan_entry **list,
1546 int pos = *position;
1547 int num = *num_entries;
1551 char termz[IT_MAX_WORD+20];
1554 AttrType completeness;
1555 int completeness_value;
1556 struct scan_info *scan_info_array;
1557 struct scan_entry *glist;
1558 int ords[32], ord_no = 0;
1561 logf (LOG_DEBUG, "scan, position = %d, num = %d", pos, num);
1563 attr_init (&use, zapt, 1);
1564 use_value = attr_find (&use, NULL);
1565 logf (LOG_DEBUG, "use value %d", use_value);
1567 attr_init (&completeness, zapt, 6);
1568 completeness_value = attr_find (&completeness, NULL);
1569 logf (LOG_DEBUG, "completeness value %d", completeness_value);
1571 if (attributeset == VAL_NONE)
1572 attributeset = VAL_BIB1;
1574 if (use_value == -1)
1576 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
1579 data1_local_attribute *local_attr;
1581 attp = att_getentbyatt (attributeset, use_value);
1584 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
1585 attributeset, use_value);
1586 return zi->errCode = 114;
1588 if (zebTargetInfo_curDatabase (zi->zti, basenames[base_no]))
1590 zi->errString = basenames[base_no];
1591 return zi->errCode = 109; /* Database unavailable */
1593 for (local_attr = attp->local_attributes; local_attr && ord_no < 32;
1594 local_attr = local_attr->next)
1598 ord = zebTargetInfo_lookupSU (zi->zti, attp->attset_ordinal,
1601 ords[ord_no++] = ord;
1605 return zi->errCode = 113;
1608 scan_info_array = odr_malloc (zi->odr, ord_no * sizeof(*scan_info_array));
1609 for (i = 0; i < ord_no; i++)
1611 int j, prefix_len = 0;
1612 int before_tmp = before, after_tmp = after;
1613 struct scan_info *scan_info = scan_info_array + i;
1615 scan_info->before = before;
1616 scan_info->after = after;
1617 scan_info->odr = zi->odr;
1619 scan_info->list = odr_malloc (zi->odr, (before+after)*
1620 sizeof(*scan_info->list));
1621 for (j = 0; j<before+after; j++)
1622 scan_info->list[j].term = NULL;
1623 termz[prefix_len++] = ords[i];
1624 termz[prefix_len++] =
1625 (completeness_value==2 || completeness_value==3) ? 'p': 'w';
1626 termz[prefix_len] = 0;
1627 strcpy (scan_info->prefix, termz);
1629 trans_scan_term (zi, zapt, termz+prefix_len);
1631 dict_scan (zi->wordDict, termz, &before_tmp, &after_tmp, scan_info,
1634 glist = odr_malloc (zi->odr, (before+after)*sizeof(*glist));
1635 for (i = 0; i < ord_no; i++)
1638 *status = BEND_SCAN_SUCCESS;
1639 for (i = 0; i<after; i++)
1642 const char *mterm = NULL;
1645 rset_isam_parms parms;
1647 for (j = 0; j < ord_no; j++)
1649 if (ptr[j] < before+after &&
1650 (tst=scan_info_array[j].list[ptr[j]].term) &&
1651 (!mterm || strcmp (tst, mterm) < 0))
1659 scan_term_untrans (zi->odr, &glist[i+before].term, mterm);
1660 parms.is = zi->wordIsam;
1661 parms.pos = scan_info_array[j0].list[ptr[j0]].isam_p;
1662 rset = rset_create (rset_kind_isam, &parms);
1665 for (j = j0+1; j<ord_no; j++)
1667 if (ptr[j] < before+after &&
1668 (tst=scan_info_array[j].list[ptr[j]].term) &&
1669 !strcmp (tst, mterm))
1671 rset_isam_parms parms;
1672 rset_bool_parms bool_parms;
1675 parms.is = zi->wordIsam;
1676 parms.pos = scan_info_array[j].list[ptr[j]].isam_p;
1677 rset2 = rset_create (rset_kind_isam, &parms);
1679 bool_parms.key_size = sizeof(struct it_key);
1680 bool_parms.cmp = key_compare;
1681 bool_parms.rset_l = rset;
1682 bool_parms.rset_r = rset2;
1684 rset = rset_create (rset_kind_or, &bool_parms);
1689 count_set (rset, &glist[i+before].occurrences);
1694 *num_entries -= (after-i);
1695 *status = BEND_SCAN_PARTIAL;
1698 for (i = 0; i<ord_no; i++)
1701 for (i = 0; i<before; i++)
1704 const char *mterm = NULL;
1707 rset_isam_parms parms;
1709 for (j = 0; j <ord_no; j++)
1711 if (ptr[j] < before &&
1712 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
1713 (!mterm || strcmp (tst, mterm) > 0))
1722 scan_term_untrans (zi->odr, &glist[before-1-i].term, mterm);
1724 parms.is = zi->wordIsam;
1725 parms.pos = scan_info_array[j0].list[before-1-ptr[j0]].isam_p;
1726 rset = rset_create (rset_kind_isam, &parms);
1730 for (j = j0+1; j<ord_no; j++)
1732 if (ptr[j] < before &&
1733 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
1734 !strcmp (tst, mterm))
1736 rset_isam_parms parms;
1737 rset_bool_parms bool_parms;
1740 parms.is = zi->wordIsam;
1741 parms.pos = scan_info_array[j].list[before-1-ptr[j]].isam_p;
1742 rset2 = rset_create (rset_kind_isam, &parms);
1744 bool_parms.key_size = sizeof(struct it_key);
1745 bool_parms.cmp = key_compare;
1746 bool_parms.rset_l = rset;
1747 bool_parms.rset_r = rset2;
1749 rset = rset_create (rset_kind_or, &bool_parms);
1754 count_set (rset, &glist[before-1-i].occurrences);
1760 *status = BEND_SCAN_PARTIAL;
1764 *list = glist + i; /* list is set to first 'real' entry */
1766 logf (LOG_DEBUG, "position = %d, num_entries = %d",
1767 *position, *num_entries);
1769 logf (LOG_DEBUG, "scan error: %d", zi->errCode);