2 * Copyright (C) 1994-1996, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.53 1996-06-26 09:21:43 adam
8 * Bug fix: local attribute set wasn't obeyed in scan.
10 * Revision 1.52 1996/06/17 14:26:20 adam
11 * Function gen_regular_rel changed to handle negative numbers.
13 * Revision 1.51 1996/06/11 10:54:15 quinn
16 * Revision 1.50 1996/06/07 08:51:53 adam
17 * Bug fix: Character mapping was broken (introducued by last revision).
19 * Revision 1.49 1996/06/04 10:18:11 adam
20 * Search/scan uses character mapping module.
22 * Revision 1.48 1996/05/28 15:15:01 adam
23 * Bug fix: Didn't handle unknown database correctly.
25 * Revision 1.47 1996/05/15 18:36:28 adam
26 * Function trans_term transforms unsearchable characters to blanks.
28 * Revision 1.46 1996/05/15 11:57:56 adam
29 * Fixed bug introduced by set/field mapping in search operations.
31 * Revision 1.45 1996/05/14 11:34:00 adam
32 * Scan support in multiple registers/databases.
34 * Revision 1.44 1996/05/14 06:16:44 adam
35 * Compact use/set bytes used in search service.
37 * Revision 1.43 1996/05/09 09:54:43 adam
38 * Server supports maps from one logical attributes to a list of physical
40 * The extraction process doesn't make space consuming 'any' keys.
42 * Revision 1.42 1996/05/09 07:28:56 quinn
43 * Work towards phrases and multiple registers
45 * Revision 1.41 1996/03/20 09:36:43 adam
46 * Function dict_lookup_grep got extra parameter, init_pos, which marks
47 * from which position in pattern approximate pattern matching should occur.
48 * Approximate pattern matching is used in relevance=re-2.
50 * Revision 1.40 1996/02/02 13:44:44 adam
51 * The public dictionary functions simply use char instead of Dict_char
52 * to represent search strings. Dict_char is used internally only.
54 * Revision 1.39 1996/01/03 16:22:13 quinn
57 * Revision 1.38 1995/12/11 09:12:55 adam
58 * The rec_get function returns NULL if record doesn't exist - will
59 * happen in the server if the result set records have been deleted since
60 * the creation of the set (i.e. the search).
61 * The server saves a result temporarily if it is 'volatile', i.e. the
62 * set is register dependent.
64 * Revision 1.37 1995/12/06 15:05:28 adam
65 * More verbose in count_set.
67 * Revision 1.36 1995/12/06 12:41:27 adam
68 * New command 'stat' for the index program.
69 * Filenames can be read from stdin by specifying '-'.
70 * Bug fix/enhancement of the transformation from terms to regular
71 * expressons in the search engine.
73 * Revision 1.35 1995/11/27 09:29:00 adam
74 * Bug fixes regarding conversion to regular expressions.
76 * Revision 1.34 1995/11/16 17:00:56 adam
77 * Better logging of rpn query.
79 * Revision 1.33 1995/11/01 13:58:28 quinn
80 * Moving data1 to yaz/retrieval
82 * Revision 1.32 1995/10/27 14:00:11 adam
83 * Implemented detection of database availability.
85 * Revision 1.31 1995/10/17 18:02:10 adam
86 * New feature: databases. Implemented as prefix to words in dictionary.
88 * Revision 1.30 1995/10/16 09:32:38 adam
89 * More work on relational op.
91 * Revision 1.29 1995/10/13 16:01:49 adam
94 * Revision 1.28 1995/10/13 12:26:43 adam
95 * Optimization of truncation.
97 * Revision 1.27 1995/10/12 17:07:22 adam
100 * Revision 1.26 1995/10/12 12:40:54 adam
101 * Bug fixes in rpn_prox.
103 * Revision 1.25 1995/10/10 13:59:24 adam
104 * Function rset_open changed its wflag parameter to general flags.
106 * Revision 1.24 1995/10/09 16:18:37 adam
107 * Function dict_lookup_grep got extra client data parameter.
109 * Revision 1.23 1995/10/06 16:33:37 adam
110 * Use attribute mappings.
112 * Revision 1.22 1995/10/06 15:07:39 adam
113 * Structure 'local-number' handled.
115 * Revision 1.21 1995/10/06 13:52:06 adam
116 * Bug fixes. Handler may abort further scanning.
118 * Revision 1.20 1995/10/06 11:06:33 adam
119 * Scan entries include 'occurrences' now.
121 * Revision 1.19 1995/10/06 10:43:56 adam
122 * Scan added. 'occurrences' in scan entries not set yet.
124 * Revision 1.18 1995/10/04 16:57:20 adam
125 * Key input and merge sort in one pass.
127 * Revision 1.17 1995/10/04 12:55:17 adam
128 * Bug fix in ranked search. Use=Any keys inserted.
130 * Revision 1.16 1995/10/02 16:24:40 adam
131 * Use attribute actually used in search requests.
133 * Revision 1.15 1995/10/02 15:18:52 adam
134 * New member in recRetrieveCtrl: diagnostic.
136 * Revision 1.14 1995/09/28 12:10:32 adam
137 * Bug fixes. Field prefix used in queries.
139 * Revision 1.13 1995/09/18 14:17:50 adam
142 * Revision 1.12 1995/09/15 14:45:21 adam
144 * Work on truncation.
146 * Revision 1.11 1995/09/14 11:53:27 adam
147 * First work on regular expressions/truncations.
149 * Revision 1.10 1995/09/11 15:23:26 adam
150 * More work on relevance search.
152 * Revision 1.9 1995/09/11 13:09:35 adam
153 * More work on relevance feedback.
155 * Revision 1.8 1995/09/08 14:52:27 adam
156 * Minor changes. Dictionary is lower case now.
158 * Revision 1.7 1995/09/07 13:58:36 adam
159 * New parameter: result-set file descriptor (RSFD) to support multiple
160 * positions within the same result-set.
161 * Boolean operators: and, or, not implemented.
162 * Result-set references.
164 * Revision 1.6 1995/09/06 16:11:18 adam
165 * Option: only one word key per file.
167 * Revision 1.5 1995/09/06 10:33:04 adam
168 * More work on present. Some log messages removed.
170 * Revision 1.4 1995/09/05 15:28:40 adam
171 * More work on search engine.
173 * Revision 1.3 1995/09/04 15:20:22 adam
176 * Revision 1.2 1995/09/04 12:33:43 adam
177 * Various cleanup. YAZ util used instead.
179 * Revision 1.1 1995/09/04 09:10:40 adam
180 * More work on index add/del/update.
181 * Merge sort implemented.
182 * Initial work on z39 server.
191 #include "attribute.h"
204 Z_AttributesPlusTerm *zapt;
207 static int attr_find (AttrType *src, oid_value *attributeSetP)
209 while (src->major < src->zapt->num_attributes)
211 Z_AttributeElement *element;
213 element = src->zapt->attributeList[src->major];
214 if (src->type == *element->attributeType)
216 switch (element->which)
218 case Z_AttributeValue_numeric:
220 if (element->attributeSet && attributeSetP)
224 attrset = oid_getentbyoid (element->attributeSet);
225 *attributeSetP = attrset->value;
227 return *element->value.numeric;
229 case Z_AttributeValue_complex:
230 if (src->minor >= element->value.complex->num_list ||
231 element->value.complex->list[src->minor]->which !=
232 Z_StringOrNumeric_numeric)
235 if (element->attributeSet && attributeSetP)
239 attrset = oid_getentbyoid (element->attributeSet);
240 *attributeSetP = attrset->value;
242 return *element->value.complex->list[src->minor-1]->u.numeric;
252 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
266 int (*cmp)(const void *p1, const void *p2);
273 static void heap_swap (struct trunc_info *ti, int i1, int i2)
278 ti->ptr[i1] = ti->ptr[i2];
282 static void heap_delete (struct trunc_info *ti)
284 int cur = 1, child = 2;
286 heap_swap (ti, 1, ti->heapnum--);
287 while (child <= ti->heapnum) {
288 if (child < ti->heapnum &&
289 (*ti->cmp)(ti->heap[ti->ptr[child]],
290 ti->heap[ti->ptr[1+child]]) > 0)
292 if ((*ti->cmp)(ti->heap[ti->ptr[cur]],
293 ti->heap[ti->ptr[child]]) > 0)
295 heap_swap (ti, cur, child);
304 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
308 cur = ++(ti->heapnum);
309 memcpy (ti->heap[ti->ptr[cur]], buf, ti->keysize);
310 ti->indx[ti->ptr[cur]] = indx;
312 while (parent && (*ti->cmp)(ti->heap[ti->ptr[parent]],
313 ti->heap[ti->ptr[cur]]) > 0)
315 heap_swap (ti, cur, parent);
322 struct trunc_info *heap_init (int size, int key_size,
323 int (*cmp)(const void *p1, const void *p2))
325 struct trunc_info *ti = xmalloc (sizeof(*ti));
330 ti->keysize = key_size;
332 ti->indx = xmalloc (size * sizeof(*ti->indx));
333 ti->heap = xmalloc (size * sizeof(*ti->heap));
334 ti->ptr = xmalloc (size * sizeof(*ti->ptr));
335 ti->swapbuf = xmalloc (ti->keysize);
336 ti->tmpbuf = xmalloc (ti->keysize);
337 ti->buf = xmalloc (size * ti->keysize);
338 for (i = size; --i >= 0; )
341 ti->heap[i] = ti->buf + ti->keysize * i;
346 static void heap_close (struct trunc_info *ti)
356 static RSET rset_trunc_r (ISAM isam, ISAM_P *isam_p, int from, int to,
361 rset_temp_parms parms;
363 parms.key_size = sizeof(struct it_key);
364 result = rset_create (rset_kind_temp, &parms);
365 result_rsfd = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
367 if (to - from > merge_chunk)
371 int i, i_add = (to-from)/merge_chunk + 1;
372 struct trunc_info *ti;
374 int rsmax = (to-from)/i_add + 1;
376 rset = xmalloc (sizeof(*rset) * rsmax);
377 rsfd = xmalloc (sizeof(*rsfd) * rsmax);
379 for (i = from; i < to; i += i_add)
382 rset[rscur] = rset_trunc_r (isam, isam_p, i, i+i_add,
385 rset[rscur] = rset_trunc_r (isam, isam_p, i, to,
389 ti = heap_init (rscur, sizeof(struct it_key), key_compare);
390 for (i = rscur; --i >= 0; )
392 rsfd[i] = rset_open (rset[i], RSETF_READ|RSETF_SORT_SYSNO);
393 if (rset_read (rset[i], rsfd[i], ti->tmpbuf))
394 heap_insert (ti, ti->tmpbuf, i);
397 rset_close (rset[i], rsfd[i]);
398 rset_delete (rset[i]);
403 int n = ti->indx[ti->ptr[1]];
405 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
409 if (!rset_read (rset[n], rsfd[n], ti->tmpbuf))
412 rset_close (rset[n], rsfd[n]);
413 rset_delete (rset[n]);
416 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
419 heap_insert (ti, ti->tmpbuf, n);
432 struct trunc_info *ti;
434 ispt = xmalloc (sizeof(*ispt) * (to-from));
436 ti = heap_init (to-from, sizeof(struct it_key),
438 for (i = to-from; --i >= 0; )
440 ispt[i] = is_position (isam, isam_p[from+i]);
441 if (is_readkey (ispt[i], ti->tmpbuf))
442 heap_insert (ti, ti->tmpbuf, i);
444 is_pt_free (ispt[i]);
448 int n = ti->indx[ti->ptr[1]];
450 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
452 /* section that preserve all keys */
454 if (is_readkey (ispt[n], ti->tmpbuf))
455 heap_insert (ti, ti->tmpbuf, n);
457 is_pt_free (ispt[n]);
459 /* section that preserve all keys with unique sysnos */
462 if (!is_readkey (ispt[n], ti->tmpbuf))
465 is_pt_free (ispt[n]);
468 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
471 heap_insert (ti, ti->tmpbuf, n);
480 rset_close (result, result_rsfd);
484 static int isam_trunc_cmp (const void *p1, const void *p2)
486 ISAM_P i1 = *(ISAM_P*) p1;
487 ISAM_P i2 = *(ISAM_P*) p2;
490 d = is_type (i1) - is_type (i2);
493 return is_block (i1) - is_block (i2);
496 static RSET rset_trunc (ISAM isam, ISAM_P *isam_p, int no)
499 qsort (isam_p, no, sizeof(*isam_p), isam_trunc_cmp);
500 return rset_trunc_r (isam, isam_p, 0, no, 100);
514 static void add_isam_p (const char *info, struct grep_info *p)
516 if (p->isam_p_indx == p->isam_p_size)
518 ISAM_P *new_isam_p_buf;
523 p->isam_p_size = 2*p->isam_p_size + 100;
524 new_isam_p_buf = xmalloc (sizeof(*new_isam_p_buf) *
528 memcpy (new_isam_p_buf, p->isam_p_buf,
529 p->isam_p_indx * sizeof(*p->isam_p_buf));
530 xfree (p->isam_p_buf);
532 p->isam_p_buf = new_isam_p_buf;
535 new_term_no = xmalloc (sizeof(*new_term_no) *
539 memcpy (new_term_no, p->isam_p_buf,
540 p->isam_p_indx * sizeof(*p->term_no));
543 p->term_no = new_term_no;
546 assert (*info == sizeof(*p->isam_p_buf));
547 memcpy (p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
551 static int grep_handle (char *name, const char *info, void *p)
553 add_isam_p (info, p);
557 /* gen_regular_rel - generate regular expression from relation
558 * val: border value (inclusive)
559 * islt: 1 if <=; 0 if >=.
561 static void gen_regular_rel (char *dst, int val, int islt)
568 logf (LOG_DEBUG, "gen_regular_rel. val=%d, islt=%d", val, islt);
572 strcpy (dst, "(-[0-9]+|");
580 strcpy (dst, "([0-9]+|-");
581 dst_p = strlen (dst);
591 dst_p = strlen (dst);
592 sprintf (numstr, "%d", val);
593 for (w = strlen(numstr); --w >= 0; pos++)
612 strcpy (dst + dst_p, numstr);
613 dst_p = strlen(dst) - pos - 1;
641 for (i = 0; i<pos; i++)
654 for (i=1; i<pos; i++)
655 strcat (dst, "[0-9]?");
659 for (i = 0; i <= pos; i++)
660 strcat (dst, "[0-9]");
661 strcat (dst, "[0-9]*");
666 static int relational_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
667 const char *term_sub,
669 oid_value attributeSet,
670 struct grep_info *grep_info,
678 attr_init (&relation, zapt, 2);
679 relation_value = attr_find (&relation, NULL);
680 term_value = atoi (term_sub);
682 switch (relation_value)
687 logf (LOG_DEBUG, "Relation <");
688 gen_regular_rel (term_dict + strlen(term_dict), term_value-1, 1);
693 logf (LOG_DEBUG, "Relation <=");
694 gen_regular_rel (term_dict + strlen(term_dict), term_value, 1);
699 logf (LOG_DEBUG, "Relation >=");
700 gen_regular_rel (term_dict + strlen(term_dict), term_value, 0);
705 logf (LOG_DEBUG, "Relation >");
706 gen_regular_rel (term_dict + strlen(term_dict), term_value+1, 0);
711 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_dict);
712 r = dict_lookup_grep (zi->wordDict, term_dict, 0, grep_info, max_pos,
715 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
716 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
720 static void verbatim_char (int ch, int *indx, char *dst)
723 dst[(*indx)++] = '\\';
727 static int field_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
728 const char *term_sub, int regType,
729 oid_value attributeSet, struct grep_info *grep_info,
730 int num_bases, char **basenames)
732 char term_dict[2*IT_MAX_WORD+2];
733 int i, j, r, base_no;
735 int truncation_value;
738 oid_value curAttributeSet = attributeSet;
740 attr_init (&use, zapt, 1);
741 use_value = attr_find (&use, &curAttributeSet);
742 logf (LOG_DEBUG, "use value %d", use_value);
743 attr_init (&truncation, zapt, 5);
744 truncation_value = attr_find (&truncation, NULL);
745 logf (LOG_DEBUG, "truncation value %d", truncation_value);
750 for (base_no = 0; base_no < num_bases; base_no++)
753 data1_local_attribute *local_attr;
754 int max_pos, prefix_len = 0;
756 attp = att_getentbyatt (curAttributeSet, use_value);
759 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
760 curAttributeSet, use_value);
764 if (zebTargetInfo_curDatabase (zi->zti, basenames[base_no]))
766 zi->errCode = 109; /* Database unavailable */
767 zi->errString = basenames[base_no];
770 for (local_attr = attp->local_attributes; local_attr;
771 local_attr = local_attr->next)
775 ord = zebTargetInfo_lookupSU (zi->zti, attp->attset_ordinal,
780 term_dict[prefix_len++] = '|';
782 term_dict[prefix_len++] = '(';
783 term_dict[prefix_len++] = 1;
784 term_dict[prefix_len++] = ord;
791 term_dict[prefix_len++] = ')';
792 term_dict[prefix_len++] = 1;
793 term_dict[prefix_len++] = regType;
794 term_dict[prefix_len] = '\0';
795 if (!relational_term (zi, zapt, term_sub, term_dict,
796 attributeSet, grep_info, &max_pos))
801 switch (truncation_value)
803 case -1: /* not specified */
804 case 100: /* do not truncate */
805 term_dict[j++] = '(';
806 for (i = 0; term_sub[i]; i++)
807 verbatim_char (term_sub[i], &j, term_dict);
808 strcpy (term_dict+j, ")");
809 r = dict_lookup_grep (zi->wordDict, term_dict, 0, grep_info,
810 &max_pos, 0, grep_handle);
812 logf (LOG_WARN, "dict_lookup_grep err, trunc=none:%d", r);
814 case 1: /* right truncation */
815 term_dict[j++] = '(';
816 for (i = 0; term_sub[i]; i++)
817 verbatim_char (term_sub[i], &j, term_dict);
818 strcpy (term_dict+j, ".*)");
819 dict_lookup_grep (zi->wordDict, term_dict, 0, grep_info,
820 &max_pos, 0, grep_handle);
822 case 2: /* left truncation */
823 case 3: /* left&right truncation */
826 case 101: /* process # in term */
827 term_dict[j++] = '(';
828 for (i=0; term_sub[i]; i++)
829 if (term_sub[i] == '#' && i > 2)
831 term_dict[j++] = '.';
832 term_dict[j++] = '*';
835 verbatim_char (term_sub[i], &j, term_dict);
836 strcpy (term_dict+j, ")");
837 r = dict_lookup_grep (zi->wordDict, term_dict, 0, grep_info,
838 &max_pos, 0, grep_handle);
840 logf (LOG_WARN, "dict_lookup_grep err, trunc=#: %d",
843 case 102: /* regular expression */
844 sprintf (term_dict + j, "(%s)", term_sub);
845 r = dict_lookup_grep (zi->wordDict, term_dict, 0, grep_info,
846 &max_pos, 0, grep_handle);
848 logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
851 case 103: /* regular expression with error correction */
854 if (*cp == '*' && cp[1] && cp[2])
859 sprintf (term_dict + j, "(%s)", cp);
860 r = dict_lookup_grep (zi->wordDict, term_dict, r, grep_info,
861 &max_pos, j, grep_handle);
863 logf (LOG_WARN, "dict_lookup_grep err, trunc=eregular: %d",
869 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
873 static void trans_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
877 Z_Term *term = zapt->term;
879 sizez = term->u.general->len;
880 if (sizez > IT_MAX_WORD-1)
881 sizez = IT_MAX_WORD-1;
882 memcpy (termz, term->u.general->buf, sizez);
886 static void trans_scan_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
889 Z_Term *term = zapt->term;
891 const char *cp = (const char *) term->u.general->buf;
892 const char *cp_end = cp + term->u.general->len;
898 while ((len = (cp_end - cp)) > 0)
900 map = map_chrs_input (&cp, len);
901 if (**map == *CHR_SPACE)
909 for (src = *map; *src; src++)
915 static RSET rpn_search_APT_relevance (ZServerInfo *zi,
916 Z_AttributesPlusTerm *zapt,
917 oid_value attributeSet,
918 int num_bases, char **basenames)
920 rset_relevance_parms parms;
921 char termz[IT_MAX_WORD+1];
922 char term_sub[IT_MAX_WORD+1];
923 struct grep_info grep_info;
928 parms.key_size = sizeof(struct it_key);
930 parms.cmp = key_compare;
931 parms.is = zi->wordIsam;
934 if (zapt->term->which != Z_Term_general)
939 trans_term (zi, zapt, termz);
942 grep_info.term_no = 0;
944 grep_info.isam_p_indx = 0;
945 grep_info.isam_p_size = 0;
946 grep_info.isam_p_buf = NULL;
955 map = map_chrs_input (&p1, strlen(p1));
956 if (**map != *CHR_SPACE)
965 map = map_chrs_input (&p1, strlen(p1));
966 if (**map == *CHR_SPACE)
971 memcpy (term_sub, p0, p2-p0);
972 term_sub[p2-p0] = '\0';
974 if (field_term (zi, zapt, term_sub, 'w', attributeSet, &grep_info,
975 num_bases, basenames))
978 for (; term_index < grep_info.isam_p_indx; term_index++)
979 grep_info.term_no[term_index] = parms.no_terms;
983 parms.term_no = grep_info.term_no;
984 parms.isam_positions = grep_info.isam_p_buf;
985 parms.no_isam_positions = grep_info.isam_p_indx;
986 if (grep_info.isam_p_indx > 0)
987 result = rset_create (rset_kind_relevance, &parms);
989 result = rset_create (rset_kind_null, NULL);
991 xfree(grep_info.term_no);
993 xfree (grep_info.isam_p_buf);
997 static RSET rpn_search_APT_cphrase (ZServerInfo *zi,
998 Z_AttributesPlusTerm *zapt,
999 oid_value attributeSet,
1000 int num_bases, char **basenames)
1002 rset_isam_parms parms;
1003 char termz[IT_MAX_WORD+1];
1004 struct grep_info grep_info;
1007 if (zapt->term->which != Z_Term_general)
1012 trans_term (zi, zapt, termz);
1015 grep_info.term_no = 0;
1017 grep_info.isam_p_indx = 0;
1018 grep_info.isam_p_size = 0;
1019 grep_info.isam_p_buf = NULL;
1021 if (field_term (zi, zapt, termz, 'p', attributeSet, &grep_info,
1022 num_bases, basenames))
1024 if (grep_info.isam_p_indx < 1)
1025 result = rset_create (rset_kind_null, NULL);
1026 else if (grep_info.isam_p_indx == 1)
1028 parms.is = zi->wordIsam;
1029 parms.pos = *grep_info.isam_p_buf;
1030 result = rset_create (rset_kind_isam, &parms);
1033 result = rset_trunc (zi->wordIsam, grep_info.isam_p_buf,
1034 grep_info.isam_p_indx);
1036 xfree(grep_info.term_no);
1038 xfree (grep_info.isam_p_buf);
1042 static RSET rpn_prox (RSET *rset, int rset_no)
1047 struct it_key **buf;
1050 rset_temp_parms parms;
1052 rsfd = xmalloc (sizeof(*rsfd)*rset_no);
1053 more = xmalloc (sizeof(*more)*rset_no);
1054 buf = xmalloc (sizeof(*buf)*rset_no);
1056 for (i = 0; i<rset_no; i++)
1058 buf[i] = xmalloc (sizeof(**buf));
1059 rsfd[i] = rset_open (rset[i], RSETF_READ|RSETF_SORT_SYSNO);
1060 if (!(more[i] = rset_read (rset[i], rsfd[i], buf[i])))
1064 rset_close (rset[i], rsfd[i]);
1071 return rset_create (rset_kind_null, NULL);
1074 parms.key_size = sizeof (struct it_key);
1075 result = rset_create (rset_kind_temp, &parms);
1076 rsfd_result = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
1080 for (i = 1; i<rset_no; i++)
1089 cmp = key_compare (buf[i], buf[i-1]);
1092 more[i-1] = rset_read (rset[i-1], rsfd[i-1], buf[i-1]);
1097 if (buf[i-1]->seqno+1 != buf[i]->seqno)
1099 more[i-1] = rset_read (rset[i-1], rsfd[i-1], buf[i-1]);
1105 more[i] = rset_read (rset[i], rsfd[i], buf[i]);
1111 rset_write (result, rsfd_result, buf[0]);
1112 more[0] = rset_read (*rset, *rsfd, *buf);
1116 for (i = 0; i<rset_no; i++)
1118 rset_close (rset[i], rsfd[i]);
1121 rset_close (result, rsfd_result);
1128 static RSET rpn_search_APT_phrase (ZServerInfo *zi,
1129 Z_AttributesPlusTerm *zapt,
1130 oid_value attributeSet,
1131 int num_bases, char **basenames)
1133 char termz[IT_MAX_WORD+1];
1134 char term_sub[IT_MAX_WORD+1];
1136 RSET rset[60], result;
1138 struct grep_info grep_info;
1140 if (zapt->term->which != Z_Term_general)
1145 trans_term (zi, zapt, termz);
1148 grep_info.term_no = 0;
1150 grep_info.isam_p_size = 0;
1151 grep_info.isam_p_buf = NULL;
1161 map = map_chrs_input (&p1, strlen(p1));
1162 if (**map != *CHR_SPACE)
1171 map = map_chrs_input (&p1, strlen(p1));
1172 if (**map == *CHR_SPACE)
1178 memcpy (term_sub, p0, p2-p0);
1179 term_sub[p2-p0] = '\0';
1182 grep_info.isam_p_indx = 0;
1183 if (field_term (zi, zapt, term_sub, 'w', attributeSet, &grep_info,
1184 num_bases, basenames))
1186 if (grep_info.isam_p_indx == 0)
1187 rset[rset_no] = rset_create (rset_kind_null, NULL);
1188 else if (grep_info.isam_p_indx > 1)
1189 rset[rset_no] = rset_trunc (zi->wordIsam,
1190 grep_info.isam_p_buf,
1191 grep_info.isam_p_indx);
1194 rset_isam_parms parms;
1196 parms.is = zi->wordIsam;
1197 parms.pos = *grep_info.isam_p_buf;
1198 rset[rset_no] = rset_create (rset_kind_isam, &parms);
1200 assert (rset[rset_no]);
1201 if (++rset_no >= sizeof(rset)/sizeof(*rset))
1205 xfree(grep_info.term_no);
1207 xfree (grep_info.isam_p_buf);
1209 return rset_create (rset_kind_null, NULL);
1210 else if (rset_no == 1)
1213 result = rpn_prox (rset, rset_no);
1214 for (i = 0; i<rset_no; i++)
1215 rset_delete (rset[i]);
1219 static RSET rpn_search_APT_local (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1220 oid_value attributeSet)
1225 rset_temp_parms parms;
1226 char termz[IT_MAX_WORD+1];
1228 if (zapt->term->which != Z_Term_general)
1233 parms.key_size = sizeof (struct it_key);
1234 result = rset_create (rset_kind_temp, &parms);
1235 rsfd = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
1237 trans_term (zi, zapt, termz);
1239 key.sysno = atoi (termz);
1242 rset_write (result, rsfd, &key);
1243 rset_close (result, rsfd);
1247 static RSET rpn_search_APT (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1248 oid_value attributeSet,
1249 int num_bases, char **basenames)
1253 AttrType completeness;
1254 int relation_value, structure_value, completeness_value;
1256 attr_init (&relation, zapt, 2);
1257 attr_init (&structure, zapt, 4);
1258 attr_init (&completeness, zapt, 6);
1260 relation_value = attr_find (&relation, NULL);
1261 structure_value = attr_find (&structure, NULL);
1262 completeness_value = attr_find (&completeness, NULL);
1263 switch (structure_value)
1266 if (relation_value == 102) /* relevance relation */
1267 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1268 num_bases, basenames);
1269 if (completeness_value == 2 || completeness_value == 3)
1270 return rpn_search_APT_cphrase (zi, zapt, attributeSet,
1271 num_bases, basenames);
1272 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1273 num_bases, basenames);
1274 case 1: /* phrase */
1275 if (relation_value == 102) /* relevance relation */
1276 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1277 num_bases, basenames);
1278 if (completeness_value == 2 || completeness_value == 3)
1279 return rpn_search_APT_cphrase (zi, zapt, attributeSet,
1280 num_bases, basenames);
1281 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1282 num_bases, basenames);
1285 if (relation_value == 102) /* relevance relation */
1286 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1287 num_bases, basenames);
1288 if (completeness_value == 2 || completeness_value == 3)
1289 return rpn_search_APT_cphrase (zi, zapt, attributeSet,
1290 num_bases, basenames);
1291 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1292 num_bases, basenames);
1297 case 5: /* date - normalized */
1299 case 6: /* word list */
1300 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1301 num_bases, basenames);
1302 case 100: /* date - un-normalized */
1304 case 101: /* name - normalized */
1306 case 102: /* date - un-normalized */
1308 case 103: /* structure */
1312 case 105: /* free-form-text */
1313 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1314 num_bases, basenames);
1315 case 106: /* document-text */
1316 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1317 num_bases, basenames);
1318 case 107: /* local-number */
1319 return rpn_search_APT_local (zi, zapt, attributeSet);
1320 case 108: /* string */
1321 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1322 num_bases, basenames);
1323 case 109: /* numeric string */
1330 static RSET rpn_search_ref (ZServerInfo *zi, Z_ResultSetId *resultSetId)
1334 if (!(s = resultSetGet (zi, resultSetId)))
1335 return rset_create (rset_kind_null, NULL);
1339 static RSET rpn_search_structure (ZServerInfo *zi, Z_RPNStructure *zs,
1340 oid_value attributeSet,
1341 int num_bases, char **basenames)
1344 if (zs->which == Z_RPNStructure_complex)
1346 rset_bool_parms bool_parms;
1349 bool_parms.rset_l = rpn_search_structure (zi, zs->u.complex->s1,
1351 num_bases, basenames);
1352 if (bool_parms.rset_l == NULL)
1354 if (rset_is_ranked(bool_parms.rset_l))
1356 bool_parms.rset_r = rpn_search_structure (zi, zs->u.complex->s2,
1358 num_bases, basenames);
1359 if (bool_parms.rset_r == NULL)
1361 rset_delete (bool_parms.rset_l);
1364 if (rset_is_ranked(bool_parms.rset_r))
1366 bool_parms.key_size = sizeof(struct it_key);
1367 bool_parms.cmp = key_compare;
1369 switch (zs->u.complex->roperator->which)
1371 case Z_Operator_and:
1372 r = rset_create (soft ? rset_kind_sand:rset_kind_and, &bool_parms);
1375 r = rset_create (soft ? rset_kind_sor:rset_kind_or, &bool_parms);
1377 case Z_Operator_and_not:
1378 r = rset_create (soft ? rset_kind_snot:rset_kind_not, &bool_parms);
1384 else if (zs->which == Z_RPNStructure_simple)
1386 if (zs->u.simple->which == Z_Operand_APT)
1388 logf (LOG_DEBUG, "rpn_search_APT");
1389 r = rpn_search_APT (zi, zs->u.simple->u.attributesPlusTerm,
1390 attributeSet, num_bases, basenames);
1392 else if (zs->u.simple->which == Z_Operand_resultSetId)
1394 logf (LOG_DEBUG, "rpn_search_ref");
1395 r = rpn_search_ref (zi, zs->u.simple->u.resultSetId);
1409 void count_set_save (RSET *r, int *count)
1416 rset_temp_parms parms;
1418 logf (LOG_DEBUG, "count_set_save");
1420 parms.key_size = sizeof(struct it_key);
1421 w = rset_create (rset_kind_temp, &parms);
1422 wfd = rset_open (w, RSETF_WRITE|RSETF_SORT_SYSNO);
1423 rfd = rset_open (*r, RSETF_READ|RSETF_SORT_SYSNO);
1424 while (rset_read (*r, rfd, &key))
1426 if (key.sysno != psysno)
1428 rset_write (w, wfd, &key);
1434 rset_close (*r, rfd);
1436 rset_close (w, wfd);
1438 logf (LOG_DEBUG, "%d keys, %d distinct sysnos", kno, *count);
1441 static void count_set (RSET r, int *count)
1448 logf (LOG_DEBUG, "count_set");
1450 rfd = rset_open (r, RSETF_READ|RSETF_SORT_SYSNO);
1451 while (rset_read (r, rfd, &key))
1453 if (key.sysno != psysno)
1460 rset_close (r, rfd);
1461 logf (LOG_DEBUG, "%d keys, %d distinct sysnos", kno, *count);
1464 int rpn_search (ZServerInfo *zi,
1465 Z_RPNQuery *rpn, int num_bases, char **basenames,
1466 const char *setname, int *hits)
1470 oid_value attributeSet;
1472 dict_grep_cmap (zi->wordDict, map_chrs_input);
1476 zi->errString = NULL;
1478 attrset = oid_getentbyoid (rpn->attributeSetId);
1479 attributeSet = attrset->value;
1480 rset = rpn_search_structure (zi, rpn->RPNStructure, attributeSet,
1481 num_bases, basenames);
1484 if (rset_is_volatile(rset))
1485 count_set_save(&rset,hits);
1487 count_set (rset, hits);
1488 resultSetAdd (zi, setname, 1, rset);
1490 logf (LOG_DEBUG, "search error: %d", zi->errCode);
1494 struct scan_info_entry {
1500 struct scan_info_entry *list;
1506 static int scan_handle (char *name, const char *info, int pos, void *client)
1508 int len_prefix, idx;
1509 struct scan_info *scan_info = client;
1511 len_prefix = strlen(scan_info->prefix);
1512 if (memcmp (name, scan_info->prefix, len_prefix))
1515 idx = scan_info->after - pos + scan_info->before;
1518 logf (LOG_DEBUG, "%-3d %s", idx, name+len_prefix);
1519 scan_info->list[idx].term = odr_malloc (scan_info->odr,
1520 strlen(name + len_prefix)+1);
1521 strcpy (scan_info->list[idx].term, name + len_prefix);
1522 assert (*info == sizeof(ISAM_P));
1523 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAM_P));
1528 static void scan_term_untrans (ODR odr, char **dstp, const char *src)
1530 char *dst = odr_malloc (odr, strlen(src)*2+1);
1535 const char *cp = map_chrs_output (&src);
1542 int rpn_scan (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1543 oid_value attributeset,
1544 int num_bases, char **basenames,
1545 int *position, int *num_entries, struct scan_entry **list,
1549 int pos = *position;
1550 int num = *num_entries;
1554 char termz[IT_MAX_WORD+20];
1557 AttrType completeness;
1558 int completeness_value;
1559 struct scan_info *scan_info_array;
1560 struct scan_entry *glist;
1561 int ords[32], ord_no = 0;
1564 logf (LOG_DEBUG, "scan, position = %d, num = %d", pos, num);
1566 if (attributeset == VAL_NONE)
1567 attributeset = VAL_BIB1;
1569 attr_init (&use, zapt, 1);
1570 use_value = attr_find (&use, &attributeset);
1571 logf (LOG_DEBUG, "use value %d", use_value);
1573 attr_init (&completeness, zapt, 6);
1574 completeness_value = attr_find (&completeness, NULL);
1575 logf (LOG_DEBUG, "completeness value %d", completeness_value);
1577 if (use_value == -1)
1579 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
1582 data1_local_attribute *local_attr;
1584 attp = att_getentbyatt (attributeset, use_value);
1587 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
1588 attributeset, use_value);
1589 return zi->errCode = 114;
1591 if (zebTargetInfo_curDatabase (zi->zti, basenames[base_no]))
1593 zi->errString = basenames[base_no];
1594 return zi->errCode = 109; /* Database unavailable */
1596 for (local_attr = attp->local_attributes; local_attr && ord_no < 32;
1597 local_attr = local_attr->next)
1601 ord = zebTargetInfo_lookupSU (zi->zti, attp->attset_ordinal,
1604 ords[ord_no++] = ord;
1608 return zi->errCode = 113;
1611 scan_info_array = odr_malloc (zi->odr, ord_no * sizeof(*scan_info_array));
1612 for (i = 0; i < ord_no; i++)
1614 int j, prefix_len = 0;
1615 int before_tmp = before, after_tmp = after;
1616 struct scan_info *scan_info = scan_info_array + i;
1618 scan_info->before = before;
1619 scan_info->after = after;
1620 scan_info->odr = zi->odr;
1622 scan_info->list = odr_malloc (zi->odr, (before+after)*
1623 sizeof(*scan_info->list));
1624 for (j = 0; j<before+after; j++)
1625 scan_info->list[j].term = NULL;
1626 termz[prefix_len++] = ords[i];
1627 termz[prefix_len++] =
1628 (completeness_value==2 || completeness_value==3) ? 'p': 'w';
1629 termz[prefix_len] = 0;
1630 strcpy (scan_info->prefix, termz);
1632 trans_scan_term (zi, zapt, termz+prefix_len);
1634 dict_scan (zi->wordDict, termz, &before_tmp, &after_tmp, scan_info,
1637 glist = odr_malloc (zi->odr, (before+after)*sizeof(*glist));
1638 for (i = 0; i < ord_no; i++)
1641 *status = BEND_SCAN_SUCCESS;
1642 for (i = 0; i<after; i++)
1645 const char *mterm = NULL;
1648 rset_isam_parms parms;
1650 for (j = 0; j < ord_no; j++)
1652 if (ptr[j] < before+after &&
1653 (tst=scan_info_array[j].list[ptr[j]].term) &&
1654 (!mterm || strcmp (tst, mterm) < 0))
1662 scan_term_untrans (zi->odr, &glist[i+before].term, mterm);
1663 parms.is = zi->wordIsam;
1664 parms.pos = scan_info_array[j0].list[ptr[j0]].isam_p;
1665 rset = rset_create (rset_kind_isam, &parms);
1668 for (j = j0+1; j<ord_no; j++)
1670 if (ptr[j] < before+after &&
1671 (tst=scan_info_array[j].list[ptr[j]].term) &&
1672 !strcmp (tst, mterm))
1674 rset_isam_parms parms;
1675 rset_bool_parms bool_parms;
1678 parms.is = zi->wordIsam;
1679 parms.pos = scan_info_array[j].list[ptr[j]].isam_p;
1680 rset2 = rset_create (rset_kind_isam, &parms);
1682 bool_parms.key_size = sizeof(struct it_key);
1683 bool_parms.cmp = key_compare;
1684 bool_parms.rset_l = rset;
1685 bool_parms.rset_r = rset2;
1687 rset = rset_create (rset_kind_or, &bool_parms);
1692 count_set (rset, &glist[i+before].occurrences);
1697 *num_entries -= (after-i);
1698 *status = BEND_SCAN_PARTIAL;
1701 for (i = 0; i<ord_no; i++)
1704 for (i = 0; i<before; i++)
1707 const char *mterm = NULL;
1710 rset_isam_parms parms;
1712 for (j = 0; j <ord_no; j++)
1714 if (ptr[j] < before &&
1715 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
1716 (!mterm || strcmp (tst, mterm) > 0))
1725 scan_term_untrans (zi->odr, &glist[before-1-i].term, mterm);
1727 parms.is = zi->wordIsam;
1728 parms.pos = scan_info_array[j0].list[before-1-ptr[j0]].isam_p;
1729 rset = rset_create (rset_kind_isam, &parms);
1733 for (j = j0+1; j<ord_no; j++)
1735 if (ptr[j] < before &&
1736 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
1737 !strcmp (tst, mterm))
1739 rset_isam_parms parms;
1740 rset_bool_parms bool_parms;
1743 parms.is = zi->wordIsam;
1744 parms.pos = scan_info_array[j].list[before-1-ptr[j]].isam_p;
1745 rset2 = rset_create (rset_kind_isam, &parms);
1747 bool_parms.key_size = sizeof(struct it_key);
1748 bool_parms.cmp = key_compare;
1749 bool_parms.rset_l = rset;
1750 bool_parms.rset_r = rset2;
1752 rset = rset_create (rset_kind_or, &bool_parms);
1757 count_set (rset, &glist[before-1-i].occurrences);
1763 *status = BEND_SCAN_PARTIAL;
1767 *list = glist + i; /* list is set to first 'real' entry */
1769 logf (LOG_DEBUG, "position = %d, num_entries = %d",
1770 *position, *num_entries);
1772 logf (LOG_DEBUG, "scan error: %d", zi->errCode);