2 * Copyright (C) 1995-1998, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.77 1998-05-20 10:12:22 adam
8 * Implemented automatic EXPLAIN database maintenance.
9 * Modified Zebra to work with ASN.1 compiled version of YAZ.
11 * Revision 1.76 1998/04/02 14:35:29 adam
12 * First version of Zebra that works with compiled ASN.1.
14 * Revision 1.75 1998/03/05 08:45:13 adam
15 * New result set model and modular ranking system. Moved towards
16 * descent server API. System information stored as "SGML" records.
18 * Revision 1.74 1998/02/10 12:03:06 adam
21 * Revision 1.73 1998/01/29 13:40:11 adam
22 * Better logging for scan service.
24 * Revision 1.72 1998/01/07 13:53:41 adam
25 * Queries using simple ranked operands returns right number of hits.
27 * Revision 1.71 1997/12/18 10:54:24 adam
28 * New method result set method rs_hits that returns the number of
29 * hits in result-set (if known). The ranked result set returns real
30 * number of hits but only when not combined with other operands.
32 * Revision 1.70 1997/10/31 12:34:43 adam
33 * Changed a few log statements.
35 * Revision 1.69 1997/10/29 12:05:02 adam
36 * Server produces diagnostic "Unsupported Attribute Set" when appropriate.
38 * Revision 1.68 1997/10/27 14:33:06 adam
39 * Moved towards generic character mapping depending on "structure"
40 * field in abstract syntax file. Fixed a few memory leaks. Fixed
41 * bug with negative integers when doing searches with relational
44 * Revision 1.67 1997/09/29 09:06:10 adam
45 * Removed one static var in order to make this module thread safe.
47 * Revision 1.66 1997/09/25 14:58:03 adam
50 * Revision 1.65 1997/09/22 12:39:06 adam
51 * Added get_pos method for the ranked result sets.
53 * Revision 1.64 1997/09/18 08:59:20 adam
54 * Extra generic handle for the character mapping routines.
56 * Revision 1.63 1997/09/17 12:19:18 adam
57 * Zebra version corresponds to YAZ version 1.4.
58 * Changed Zebra server so that it doesn't depend on global common_resource.
60 * Revision 1.62 1997/09/05 15:30:09 adam
61 * Changed prototype for chr_map_input - added const.
62 * Added support for C++, headers uses extern "C" for public definitions.
64 * Revision 1.61 1997/02/10 10:21:14 adam
65 * Bug fix: in search terms character (^) wasn't observed.
67 * Revision 1.60 1997/01/31 11:10:34 adam
68 * Bug fix: Leading and trailing white space weren't removed in scan tokens.
70 * Revision 1.59 1997/01/17 11:31:46 adam
71 * Bug fix: complete phrase search didn't work.
73 * Revision 1.58 1996/12/23 15:30:45 adam
75 * Bug fix: result sets weren't deleted after server shut down.
77 * Revision 1.57 1996/11/11 13:38:02 adam
78 * Added proximity support in search.
80 * Revision 1.56 1996/11/08 11:10:32 adam
81 * Buffers used during file match got bigger.
82 * Compressed ISAM support everywhere.
83 * Bug fixes regarding masking characters in queries.
84 * Redesigned Regexp-2 queries.
86 * Revision 1.55 1996/11/04 14:07:44 adam
87 * Moved truncation code to trunc.c.
89 * Revision 1.54 1996/10/29 14:09:52 adam
90 * Use of cisam system - enabled if setting isamc is 1.
92 * Revision 1.53 1996/06/26 09:21:43 adam
93 * Bug fix: local attribute set wasn't obeyed in scan.
95 * Revision 1.52 1996/06/17 14:26:20 adam
96 * Function gen_regular_rel changed to handle negative numbers.
98 * Revision 1.51 1996/06/11 10:54:15 quinn
101 * Revision 1.50 1996/06/07 08:51:53 adam
102 * Bug fix: Character mapping was broken (introducued by last revision).
104 * Revision 1.49 1996/06/04 10:18:11 adam
105 * Search/scan uses character mapping module.
107 * Revision 1.48 1996/05/28 15:15:01 adam
108 * Bug fix: Didn't handle unknown database correctly.
110 * Revision 1.47 1996/05/15 18:36:28 adam
111 * Function trans_term transforms unsearchable characters to blanks.
113 * Revision 1.46 1996/05/15 11:57:56 adam
114 * Fixed bug introduced by set/field mapping in search operations.
116 * Revision 1.45 1996/05/14 11:34:00 adam
117 * Scan support in multiple registers/databases.
119 * Revision 1.44 1996/05/14 06:16:44 adam
120 * Compact use/set bytes used in search service.
122 * Revision 1.43 1996/05/09 09:54:43 adam
123 * Server supports maps from one logical attributes to a list of physical
125 * The extraction process doesn't make space consuming 'any' keys.
127 * Revision 1.42 1996/05/09 07:28:56 quinn
128 * Work towards phrases and multiple registers
130 * Revision 1.41 1996/03/20 09:36:43 adam
131 * Function dict_lookup_grep got extra parameter, init_pos, which marks
132 * from which position in pattern approximate pattern matching should occur.
133 * Approximate pattern matching is used in relevance=re-2.
135 * Revision 1.40 1996/02/02 13:44:44 adam
136 * The public dictionary functions simply use char instead of Dict_char
137 * to represent search strings. Dict_char is used internally only.
139 * Revision 1.39 1996/01/03 16:22:13 quinn
140 * operator->roperator
142 * Revision 1.38 1995/12/11 09:12:55 adam
143 * The rec_get function returns NULL if record doesn't exist - will
144 * happen in the server if the result set records have been deleted since
145 * the creation of the set (i.e. the search).
146 * The server saves a result temporarily if it is 'volatile', i.e. the
147 * set is register dependent.
149 * Revision 1.37 1995/12/06 15:05:28 adam
150 * More verbose in count_set.
152 * Revision 1.36 1995/12/06 12:41:27 adam
153 * New command 'stat' for the index program.
154 * Filenames can be read from stdin by specifying '-'.
155 * Bug fix/enhancement of the transformation from terms to regular
156 * expressons in the search engine.
158 * Revision 1.35 1995/11/27 09:29:00 adam
159 * Bug fixes regarding conversion to regular expressions.
161 * Revision 1.34 1995/11/16 17:00:56 adam
162 * Better logging of rpn query.
164 * Revision 1.33 1995/11/01 13:58:28 quinn
165 * Moving data1 to yaz/retrieval
167 * Revision 1.32 1995/10/27 14:00:11 adam
168 * Implemented detection of database availability.
170 * Revision 1.31 1995/10/17 18:02:10 adam
171 * New feature: databases. Implemented as prefix to words in dictionary.
173 * Revision 1.30 1995/10/16 09:32:38 adam
174 * More work on relational op.
176 * Revision 1.29 1995/10/13 16:01:49 adam
179 * Revision 1.28 1995/10/13 12:26:43 adam
180 * Optimization of truncation.
182 * Revision 1.27 1995/10/12 17:07:22 adam
185 * Revision 1.26 1995/10/12 12:40:54 adam
186 * Bug fixes in rpn_prox.
188 * Revision 1.25 1995/10/10 13:59:24 adam
189 * Function rset_open changed its wflag parameter to general flags.
191 * Revision 1.24 1995/10/09 16:18:37 adam
192 * Function dict_lookup_grep got extra client data parameter.
194 * Revision 1.23 1995/10/06 16:33:37 adam
195 * Use attribute mappings.
197 * Revision 1.22 1995/10/06 15:07:39 adam
198 * Structure 'local-number' handled.
200 * Revision 1.21 1995/10/06 13:52:06 adam
201 * Bug fixes. Handler may abort further scanning.
203 * Revision 1.20 1995/10/06 11:06:33 adam
204 * Scan entries include 'occurrences' now.
206 * Revision 1.19 1995/10/06 10:43:56 adam
207 * Scan added. 'occurrences' in scan entries not set yet.
209 * Revision 1.18 1995/10/04 16:57:20 adam
210 * Key input and merge sort in one pass.
212 * Revision 1.17 1995/10/04 12:55:17 adam
213 * Bug fix in ranked search. Use=Any keys inserted.
215 * Revision 1.16 1995/10/02 16:24:40 adam
216 * Use attribute actually used in search requests.
218 * Revision 1.15 1995/10/02 15:18:52 adam
219 * New member in recRetrieveCtrl: diagnostic.
221 * Revision 1.14 1995/09/28 12:10:32 adam
222 * Bug fixes. Field prefix used in queries.
224 * Revision 1.13 1995/09/18 14:17:50 adam
227 * Revision 1.12 1995/09/15 14:45:21 adam
229 * Work on truncation.
231 * Revision 1.11 1995/09/14 11:53:27 adam
232 * First work on regular expressions/truncations.
234 * Revision 1.10 1995/09/11 15:23:26 adam
235 * More work on relevance search.
237 * Revision 1.9 1995/09/11 13:09:35 adam
238 * More work on relevance feedback.
240 * Revision 1.8 1995/09/08 14:52:27 adam
241 * Minor changes. Dictionary is lower case now.
243 * Revision 1.7 1995/09/07 13:58:36 adam
244 * New parameter: result-set file descriptor (RSFD) to support multiple
245 * positions within the same result-set.
246 * Boolean operators: and, or, not implemented.
247 * Result-set references.
249 * Revision 1.6 1995/09/06 16:11:18 adam
250 * Option: only one word key per file.
252 * Revision 1.5 1995/09/06 10:33:04 adam
253 * More work on present. Some log messages removed.
255 * Revision 1.4 1995/09/05 15:28:40 adam
256 * More work on search engine.
258 * Revision 1.3 1995/09/04 15:20:22 adam
261 * Revision 1.2 1995/09/04 12:33:43 adam
262 * Various cleanup. YAZ util used instead.
264 * Revision 1.1 1995/09/04 09:10:40 adam
265 * More work on index add/del/update.
266 * Merge sort implemented.
267 * Initial work on z39 server.
286 struct rpn_char_map_info {
291 static const char **rpn_char_map_handler (void *vp, const char **from, int len)
293 struct rpn_char_map_info *p = vp;
294 return zebra_maps_input (p->zm, p->reg_type, from, len);
297 static void rpn_char_map_prepare (ZebraHandle zh, int reg_type,
298 struct rpn_char_map_info *map_info)
300 map_info->zm = zh->zebra_maps;
301 map_info->reg_type = reg_type;
302 dict_grep_cmap (zh->dict, map_info, rpn_char_map_handler);
309 Z_AttributesPlusTerm *zapt;
312 static int attr_find (AttrType *src, oid_value *attributeSetP)
317 num_attributes = src->zapt->attributes->num_attributes;
319 num_attributes = src->zapt->num_attributes;
321 while (src->major < num_attributes)
323 Z_AttributeElement *element;
326 element = src->zapt->attributes->attributes[src->major];
328 element = src->zapt->attributeList[src->major];
330 if (src->type == *element->attributeType)
332 switch (element->which)
334 case Z_AttributeValue_numeric:
336 if (element->attributeSet && attributeSetP)
340 attrset = oid_getentbyoid (element->attributeSet);
341 *attributeSetP = attrset->value;
343 return *element->value.numeric;
345 case Z_AttributeValue_complex:
346 if (src->minor >= element->value.complex->num_list ||
347 element->value.complex->list[src->minor]->which !=
348 Z_StringOrNumeric_numeric)
351 if (element->attributeSet && attributeSetP)
355 attrset = oid_getentbyoid (element->attributeSet);
356 *attributeSetP = attrset->value;
358 return *element->value.complex->list[src->minor-1]->u.numeric;
368 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
390 static void term_untrans (ZebraHandle zh, int reg_type,
391 char *dst, const char *src)
395 const char *cp = zebra_maps_output (zh->zebra_maps, reg_type, &src);
402 static void add_isam_p (const char *name, const char *info,
406 if (p->isam_p_indx == p->isam_p_size)
408 ISAM_P *new_isam_p_buf;
412 p->isam_p_size = 2*p->isam_p_size + 100;
413 new_isam_p_buf = xmalloc (sizeof(*new_isam_p_buf) *
417 memcpy (new_isam_p_buf, p->isam_p_buf,
418 p->isam_p_indx * sizeof(*p->isam_p_buf));
419 xfree (p->isam_p_buf);
421 p->isam_p_buf = new_isam_p_buf;
424 new_term_no = xmalloc (sizeof(*new_term_no) *
428 memcpy (new_term_no, p->isam_p_buf,
429 p->isam_p_indx * sizeof(*p->term_no));
432 p->term_no = new_term_no;
435 assert (*info == sizeof(*p->isam_p_buf));
436 memcpy (p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
438 term_untrans (p->zh, p->reg_type, term_tmp, name+2);
439 logf (LOG_DEBUG, "grep: %s", term_tmp);
444 static int grep_handle (char *name, const char *info, void *p)
446 add_isam_p (name, info, p);
450 static int term_pre (ZebraMaps zebra_maps, int reg_type, const char **src,
451 const char *ct1, const char *ct2)
453 const char *s1, *s0 = *src;
456 /* skip white space */
459 if (ct1 && strchr (ct1, *s0))
461 if (ct2 && strchr (ct2, *s0))
464 map = zebra_maps_input (zebra_maps, reg_type, &s1, strlen(s1));
465 if (**map != *CHR_SPACE)
473 static int term_100 (ZebraMaps zebra_maps, int reg_type,
474 const char **src, char *dst, int space_split,
482 if (!term_pre (zebra_maps, reg_type, src, NULL, NULL))
488 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
489 if (space_split && **map == *CHR_SPACE)
493 if (!isalnum (*s1) && *s1 != '-')
505 static int term_101 (ZebraMaps zebra_maps, int reg_type,
506 const char **src, char *dst, int space_split,
514 if (!term_pre (zebra_maps, reg_type, src, "#", "#"))
523 dst_term[j++] = *s0++;
528 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
529 if (space_split && **map == *CHR_SPACE)
541 dst_term[j++] = '\0';
547 static int term_103 (ZebraMaps zebra_maps, int reg_type, const char **src,
548 char *dst, int *errors, int space_split,
556 if (!term_pre (zebra_maps, reg_type, src, "^\\()[].*+?|", "("))
559 if (errors && *s0 == '+' && s0[1] && s0[2] == '+' && s0[3] &&
562 *errors = s0[1] - '0';
569 if (strchr ("^\\()[].*+?|-", *s0))
577 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
578 if (**map == *CHR_SPACE)
595 static int term_102 (ZebraMaps zebra_maps, int reg_type, const char **src,
596 char *dst, int space_split, char *dst_term)
598 return term_103 (zebra_maps, reg_type, src, dst, NULL, space_split,
602 /* gen_regular_rel - generate regular expression from relation
603 * val: border value (inclusive)
604 * islt: 1 if <=; 0 if >=.
606 static void gen_regular_rel (char *dst, int val, int islt)
613 logf (LOG_DEBUG, "gen_regular_rel. val=%d, islt=%d", val, islt);
617 strcpy (dst, "(-[0-9]+|(");
625 strcpy (dst, "([0-9]+|-(");
626 dst_p = strlen (dst);
636 dst_p = strlen (dst);
637 sprintf (numstr, "%d", val);
638 for (w = strlen(numstr); --w >= 0; pos++)
657 strcpy (dst + dst_p, numstr);
658 dst_p = strlen(dst) - pos - 1;
686 for (i = 0; i<pos; i++)
699 for (i=1; i<pos; i++)
700 strcat (dst, "[0-9]?");
704 for (i = 0; i <= pos; i++)
705 strcat (dst, "[0-9]");
706 strcat (dst, "[0-9]*");
711 static int relational_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
712 const char **term_sub,
714 oid_value attributeSet,
715 struct grep_info *grep_info,
724 char *term_tmp = term_dict + strlen(term_dict);
726 attr_init (&relation, zapt, 2);
727 relation_value = attr_find (&relation, NULL);
729 logf (LOG_DEBUG, "relation value=%d", relation_value);
730 switch (relation_value)
733 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_tmp, 1,
736 term_value = atoi (term_tmp);
737 logf (LOG_DEBUG, "Relation <");
738 gen_regular_rel (term_tmp, term_value-1, 1);
741 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_tmp, 1,
744 term_value = atoi (term_tmp);
745 logf (LOG_DEBUG, "Relation <=");
746 gen_regular_rel (term_tmp, term_value, 1);
749 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_tmp, 1,
752 term_value = atoi (term_tmp);
753 logf (LOG_DEBUG, "Relation >=");
754 gen_regular_rel (term_tmp, term_value, 0);
757 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_tmp, 1,
760 term_value = atoi (term_tmp);
761 logf (LOG_DEBUG, "Relation >");
762 gen_regular_rel (term_tmp, term_value+1, 0);
767 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_tmp);
768 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info, max_pos,
771 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
772 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
776 static int field_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
777 const char **term_sub,
778 oid_value attributeSet, struct grep_info *grep_info,
779 int reg_type, int complete_flag,
780 int num_bases, char **basenames,
783 char term_dict[2*IT_MAX_WORD+2];
786 int truncation_value;
789 oid_value curAttributeSet = attributeSet;
791 struct rpn_char_map_info rcmi;
792 int space_split = complete_flag ? 0 : 1;
794 rpn_char_map_prepare (zh, reg_type, &rcmi);
795 attr_init (&use, zapt, 1);
796 use_value = attr_find (&use, &curAttributeSet);
797 logf (LOG_DEBUG, "field_term, use value %d", use_value);
798 attr_init (&truncation, zapt, 5);
799 truncation_value = attr_find (&truncation, NULL);
800 logf (LOG_DEBUG, "truncation value %d", truncation_value);
805 for (base_no = 0; base_no < num_bases; base_no++)
808 data1_local_attribute *local_attr;
809 int max_pos, prefix_len = 0;
812 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
814 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
815 curAttributeSet, use_value, r);
822 if (zebraExplain_curDatabase (zh->zei, basenames[base_no]))
824 zh->errCode = 109; /* Database unavailable */
825 zh->errString = basenames[base_no];
828 for (local_attr = attp.local_attributes; local_attr;
829 local_attr = local_attr->next)
833 ord = zebraExplain_lookupSU (zh->zei, attp.attset_ordinal,
838 term_dict[prefix_len++] = '|';
840 term_dict[prefix_len++] = '(';
841 term_dict[prefix_len++] = 1;
842 term_dict[prefix_len++] = ord;
849 term_dict[prefix_len++] = ')';
850 term_dict[prefix_len++] = 1;
851 term_dict[prefix_len++] = reg_type;
852 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
853 term_dict[prefix_len] = '\0';
854 if (!relational_term (zh, zapt, &termp, term_dict,
855 attributeSet, grep_info, &max_pos, reg_type,
859 switch (truncation_value)
861 case -1: /* not specified */
862 case 100: /* do not truncate */
863 term_dict[j++] = '(';
864 if (!term_100 (zh->zebra_maps, reg_type,
865 &termp, term_dict + j, space_split, term_dst))
867 strcat (term_dict, ")");
868 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
869 &max_pos, 0, grep_handle);
871 logf (LOG_WARN, "dict_lookup_grep err, trunc=none:%d", r);
873 case 1: /* right truncation */
874 term_dict[j++] = '(';
875 if (!term_100 (zh->zebra_maps, reg_type,
876 &termp, term_dict + j, space_split, term_dst))
878 strcat (term_dict, ".*)");
879 dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
880 &max_pos, 0, grep_handle);
882 case 2: /* left truncation */
883 case 3: /* left&right truncation */
886 case 101: /* process # in term */
887 term_dict[j++] = '(';
888 if (!term_101 (zh->zebra_maps, reg_type,
889 &termp, term_dict + j, space_split, term_dst))
891 strcat (term_dict, ")");
892 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
893 &max_pos, 0, grep_handle);
895 logf (LOG_WARN, "dict_lookup_grep err, trunc=#: %d", r);
897 case 102: /* Regexp-1 */
898 term_dict[j++] = '(';
899 if (!term_102 (zh->zebra_maps, reg_type,
900 &termp, term_dict + j, space_split, term_dst))
902 strcat (term_dict, ")");
903 logf (LOG_DEBUG, "Regexp-1 tolerance=%d", r);
904 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
905 &max_pos, 0, grep_handle);
907 logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
910 case 103: /* Regexp-2 */
912 term_dict[j++] = '(';
913 if (!term_103 (zh->zebra_maps, reg_type,
914 &termp, term_dict + j, &r, space_split, term_dst))
916 strcat (term_dict, ")");
917 logf (LOG_DEBUG, "Regexp-2 tolerance=%d", r);
918 r = dict_lookup_grep (zh->dict, term_dict, r, grep_info,
919 &max_pos, 2, grep_handle);
921 logf (LOG_WARN, "dict_lookup_grep err, trunc=eregular: %d",
928 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
932 static void trans_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
936 Z_Term *term = zapt->term;
938 sizez = term->u.general->len;
939 if (sizez > IT_MAX_WORD-1)
940 sizez = IT_MAX_WORD-1;
941 memcpy (termz, term->u.general->buf, sizez);
945 static void trans_scan_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
946 char *termz, int reg_type)
948 Z_Term *term = zapt->term;
950 const char *cp = (const char *) term->u.general->buf;
951 const char *cp_end = cp + term->u.general->len;
954 const char *space_map = NULL;
957 while ((len = (cp_end - cp)) > 0)
959 map = zebra_maps_input (zh->zebra_maps, reg_type, &cp, len);
960 if (**map == *CHR_SPACE)
965 for (src = space_map; *src; src++)
968 for (src = *map; *src; src++)
975 static RSET rpn_proximity (ZebraHandle zh, RSET rset1, RSET rset2,
977 int exclusion, int relation, int distance)
982 struct it_key buf1, buf2;
985 rset_temp_parms parms;
988 rsfd1 = rset_open (rset1, RSETF_READ);
989 more1 = rset_read (rset1, rsfd1, &buf1, &term_index);
991 rsfd2 = rset_open (rset2, RSETF_READ);
992 more2 = rset_read (rset2, rsfd2, &buf2, &term_index);
994 parms.key_size = sizeof (struct it_key);
995 parms.temp_path = res_get (zh->res, "setTmpDir");
996 result = rset_create (rset_kind_temp, &parms);
997 rsfd_result = rset_open (result, RSETF_WRITE);
999 logf (LOG_DEBUG, "rpn_proximity excl=%d ord=%d rel=%d dis=%d",
1000 exclusion, ordered, relation, distance);
1001 while (more1 && more2)
1003 int cmp = key_compare_it (&buf1, &buf2);
1005 more1 = rset_read (rset1, rsfd1, &buf1, &term_index);
1007 more2 = rset_read (rset2, rsfd2, &buf2, &term_index);
1010 int sysno = buf1.sysno;
1014 seqno[n++] = buf1.seqno;
1015 while ((more1 = rset_read (rset1, rsfd1, &buf1, &term_index)) &&
1016 sysno == buf1.sysno)
1018 seqno[n++] = buf1.seqno;
1021 for (i = 0; i<n; i++)
1023 int diff = buf2.seqno - seqno[i];
1024 int excl = exclusion;
1025 if (!ordered && diff < 0)
1030 if (diff < distance)
1034 if (diff <= distance)
1038 if (diff == distance)
1042 if (diff >= distance)
1046 if (diff > distance)
1050 if (diff != distance)
1055 rset_write (result, rsfd_result, &buf2);
1057 } while ((more2 = rset_read (rset2, rsfd2, &buf2, &term_index)) &&
1058 sysno == buf2.sysno);
1061 rset_close (result, rsfd_result);
1062 rset_close (rset1, rsfd1);
1063 rset_close (rset2, rsfd2);
1067 static RSET rpn_prox (ZebraHandle zh, RSET *rset, int rset_no)
1072 struct it_key **buf;
1074 char prox_term[1024];
1075 int length_prox_term = 0;
1076 int min_nn = 10000000;
1078 const char *flags = NULL;
1080 rsfd = xmalloc (sizeof(*rsfd)*rset_no);
1081 more = xmalloc (sizeof(*more)*rset_no);
1082 buf = xmalloc (sizeof(*buf)*rset_no);
1084 for (i = 0; i<rset_no; i++)
1087 buf[i] = xmalloc (sizeof(**buf));
1088 rsfd[i] = rset_open (rset[i], RSETF_READ);
1089 if (!(more[i] = rset_read (rset[i], rsfd[i], buf[i], &term_index)))
1091 for (j = 0; j<rset[i]->no_rset_terms; j++)
1093 const char *nflags = rset[i]->rset_terms[j]->flags;
1094 char *term = rset[i]->rset_terms[j]->name;
1095 int lterm = strlen(term);
1096 if (length_prox_term)
1097 prox_term[length_prox_term++] = ' ';
1098 strcpy (prox_term + length_prox_term, term);
1099 length_prox_term += lterm;
1100 if (min_nn > rset[i]->rset_terms[j]->nn)
1101 min_nn = rset[i]->rset_terms[j]->nn;
1107 rset_null_parms parms;
1111 rset_close (rset[i], rsfd[i]);
1115 parms.rset_term = rset_term_create (prox_term, -1, flags);
1116 parms.rset_term->nn = 0;
1117 result = rset_create (rset_kind_null, &parms);
1121 rset_temp_parms parms;
1124 parms.rset_term = rset_term_create (prox_term, -1, flags);
1125 parms.rset_term->nn = min_nn;
1126 parms.key_size = sizeof (struct it_key);
1127 parms.temp_path = res_get (zh->res, "setTmpDir");
1128 result = rset_create (rset_kind_temp, &parms);
1129 rsfd_result = rset_open (result, RSETF_WRITE);
1133 for (i = 1; i<rset_no; i++)
1142 cmp = key_compare_it (buf[i], buf[i-1]);
1145 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1146 buf[i-1], &term_index);
1151 if (buf[i-1]->seqno+1 != buf[i]->seqno)
1153 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1154 buf[i-1], &term_index);
1160 more[i] = rset_read (rset[i], rsfd[i], buf[i],
1167 rset_write (result, rsfd_result, buf[0]);
1168 more[0] = rset_read (*rset, *rsfd, *buf, &term_index);
1172 for (i = 0; i<rset_no; i++)
1174 rset_close (rset[i], rsfd[i]);
1177 rset_close (result, rsfd_result);
1185 static RSET rpn_search_APT_phrase (ZebraHandle zh,
1186 Z_AttributesPlusTerm *zapt,
1188 oid_value attributeSet,
1189 int reg_type, int complete_flag,
1190 const char *rank_type,
1191 int num_bases, char **basenames)
1193 char term_dst[IT_MAX_WORD+1];
1194 const char *termp = termz;
1195 RSET rset[60], result;
1196 int i, r, rset_no = 0;
1197 struct grep_info grep_info;
1200 grep_info.term_no = 0;
1202 grep_info.isam_p_size = 0;
1203 grep_info.isam_p_buf = NULL;
1205 grep_info.reg_type = reg_type;
1209 logf (LOG_DEBUG, "APT_phrase termp=%s", termp);
1210 grep_info.isam_p_indx = 0;
1211 r = field_term (zh, zapt, &termp, attributeSet, &grep_info,
1212 reg_type, complete_flag, num_bases, basenames,
1216 logf (LOG_DEBUG, "term: %s", term_dst);
1217 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1218 grep_info.isam_p_indx, term_dst,
1219 strlen(term_dst), rank_type);
1220 assert (rset[rset_no]);
1221 if (++rset_no >= sizeof(rset)/sizeof(*rset))
1225 xfree(grep_info.term_no);
1227 xfree (grep_info.isam_p_buf);
1230 rset_null_parms parms;
1232 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1233 return rset_create (rset_kind_null, &parms);
1235 else if (rset_no == 1)
1237 result = rpn_prox (zh, rset, rset_no);
1238 for (i = 0; i<rset_no; i++)
1239 rset_delete (rset[i]);
1243 static RSET rpn_search_APT_or_list (ZebraHandle zh,
1244 Z_AttributesPlusTerm *zapt,
1246 oid_value attributeSet,
1247 int reg_type, int complete_flag,
1248 const char *rank_type,
1249 int num_bases, char **basenames)
1251 char term_dst[IT_MAX_WORD+1];
1252 const char *termp = termz;
1253 RSET rset[60], result;
1254 int i, r, rset_no = 0;
1255 struct grep_info grep_info;
1258 grep_info.term_no = 0;
1260 grep_info.isam_p_size = 0;
1261 grep_info.isam_p_buf = NULL;
1263 grep_info.reg_type = reg_type;
1267 logf (LOG_DEBUG, "APT_or_list termp=%s", termp);
1268 grep_info.isam_p_indx = 0;
1269 r = field_term (zh, zapt, &termp, attributeSet, &grep_info,
1270 reg_type, complete_flag, num_bases, basenames,
1274 logf (LOG_DEBUG, "term: %s", term_dst);
1275 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1276 grep_info.isam_p_indx, term_dst,
1277 strlen(term_dst), rank_type);
1278 assert (rset[rset_no]);
1279 if (++rset_no >= sizeof(rset)/sizeof(*rset))
1283 xfree(grep_info.term_no);
1285 xfree (grep_info.isam_p_buf);
1288 rset_null_parms parms;
1290 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1291 return rset_create (rset_kind_null, &parms);
1294 for (i = 1; i<rset_no; i++)
1296 rset_bool_parms bool_parms;
1298 bool_parms.rset_l = result;
1299 bool_parms.rset_r = rset[i];
1300 bool_parms.key_size = sizeof(struct it_key);
1301 bool_parms.cmp = key_compare_it;
1302 result = rset_create (rset_kind_or, &bool_parms);
1307 static RSET rpn_search_APT_and_list (ZebraHandle zh,
1308 Z_AttributesPlusTerm *zapt,
1310 oid_value attributeSet,
1311 int reg_type, int complete_flag,
1312 const char *rank_type,
1313 int num_bases, char **basenames)
1315 char term_dst[IT_MAX_WORD+1];
1316 const char *termp = termz;
1317 RSET rset[60], result;
1318 int i, r, rset_no = 0;
1319 struct grep_info grep_info;
1322 grep_info.term_no = 0;
1324 grep_info.isam_p_size = 0;
1325 grep_info.isam_p_buf = NULL;
1327 grep_info.reg_type = reg_type;
1331 logf (LOG_DEBUG, "APT_and_list termp=%s", termp);
1332 grep_info.isam_p_indx = 0;
1333 r = field_term (zh, zapt, &termp, attributeSet, &grep_info,
1334 reg_type, complete_flag, num_bases, basenames,
1338 logf (LOG_DEBUG, "term: %s", term_dst);
1339 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1340 grep_info.isam_p_indx, term_dst,
1341 strlen(term_dst), rank_type);
1342 assert (rset[rset_no]);
1343 if (++rset_no >= sizeof(rset)/sizeof(*rset))
1347 xfree(grep_info.term_no);
1349 xfree (grep_info.isam_p_buf);
1352 rset_null_parms parms;
1354 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1355 return rset_create (rset_kind_null, &parms);
1358 for (i = 1; i<rset_no; i++)
1360 rset_bool_parms bool_parms;
1362 bool_parms.rset_l = result;
1363 bool_parms.rset_r = rset[i];
1364 bool_parms.key_size = sizeof(struct it_key);
1365 bool_parms.cmp = key_compare_it;
1366 result = rset_create (rset_kind_and, &bool_parms);
1372 static RSET rpn_search_APT_local (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1374 oid_value attributeSet,
1375 const char *rank_type)
1380 rset_temp_parms parms;
1382 parms.rset_term = rset_term_create (termz, -1, rank_type);
1383 parms.key_size = sizeof (struct it_key);
1384 parms.temp_path = res_get (zh->res, "setTmpDir");
1385 result = rset_create (rset_kind_temp, &parms);
1386 rsfd = rset_open (result, RSETF_WRITE);
1388 key.sysno = atoi (termz);
1392 rset_write (result, rsfd, &key);
1393 rset_close (result, rsfd);
1397 static RSET rpn_search_APT (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1398 oid_value attributeSet,
1399 int num_bases, char **basenames)
1402 char *search_type = NULL;
1403 char *rank_type = NULL;
1405 char termz[IT_MAX_WORD+1];
1407 zebra_maps_attr (zh->zebra_maps, zapt, ®_id, &search_type,
1408 &rank_type, &complete_flag);
1410 logf (LOG_DEBUG, "reg_id=%c", reg_id);
1411 logf (LOG_DEBUG, "complete_flag=%d", complete_flag);
1412 logf (LOG_DEBUG, "search_type=%s", search_type);
1413 logf (LOG_DEBUG, "rank_type=%s", rank_type);
1415 if (zapt->term->which != Z_Term_general)
1420 trans_term (zh, zapt, termz);
1422 if (!strcmp (search_type, "phrase"))
1424 return rpn_search_APT_phrase (zh, zapt, termz, attributeSet,
1425 reg_id, complete_flag, rank_type,
1426 num_bases, basenames);
1428 else if (!strcmp (search_type, "and-list"))
1430 return rpn_search_APT_and_list (zh, zapt, termz, attributeSet,
1431 reg_id, complete_flag, rank_type,
1432 num_bases, basenames);
1434 else if (!strcmp (search_type, "or-list"))
1436 return rpn_search_APT_or_list (zh, zapt, termz, attributeSet,
1437 reg_id, complete_flag, rank_type,
1438 num_bases, basenames);
1440 else if (!strcmp (search_type, "local"))
1442 return rpn_search_APT_local (zh, zapt, termz, attributeSet,
1449 static RSET rpn_search_structure (ZebraHandle zh, Z_RPNStructure *zs,
1450 oid_value attributeSet, ODR stream,
1451 int num_bases, char **basenames)
1454 if (zs->which == Z_RPNStructure_complex)
1456 Z_Operator *zop = zs->u.complex->roperator;
1457 rset_bool_parms bool_parms;
1459 bool_parms.rset_l = rpn_search_structure (zh, zs->u.complex->s1,
1460 attributeSet, stream,
1461 num_bases, basenames);
1462 if (bool_parms.rset_l == NULL)
1464 bool_parms.rset_r = rpn_search_structure (zh, zs->u.complex->s2,
1465 attributeSet, stream,
1466 num_bases, basenames);
1467 if (bool_parms.rset_r == NULL)
1469 rset_delete (bool_parms.rset_l);
1472 bool_parms.key_size = sizeof(struct it_key);
1473 bool_parms.cmp = key_compare_it;
1477 case Z_Operator_and:
1478 r = rset_create (rset_kind_and, &bool_parms);
1481 r = rset_create (rset_kind_or, &bool_parms);
1483 case Z_Operator_and_not:
1484 r = rset_create (rset_kind_not, &bool_parms);
1486 case Z_Operator_prox:
1488 if (zop->u.prox->which != Z_ProximityOperator_known)
1494 if (zop->u.prox->which != Z_ProxCode_known)
1502 if (*zop->u.prox->u.known != Z_ProxUnit_word)
1504 char *val = odr_malloc (stream, 16);
1506 zh->errString = val;
1507 sprintf (val, "%d", *zop->u.prox->u.known);
1511 if (*zop->u.prox->proximityUnitCode != Z_ProxUnit_word)
1513 char *val = odr_malloc (stream, 16);
1515 zh->errString = val;
1516 sprintf (val, "%d", *zop->u.prox->proximityUnitCode);
1520 r = rpn_proximity (zh, bool_parms.rset_l, bool_parms.rset_r,
1521 *zop->u.prox->ordered,
1522 (!zop->u.prox->exclusion ? 0 :
1523 *zop->u.prox->exclusion),
1524 *zop->u.prox->relationType,
1525 *zop->u.prox->distance);
1532 else if (zs->which == Z_RPNStructure_simple)
1534 if (zs->u.simple->which == Z_Operand_APT)
1536 logf (LOG_DEBUG, "rpn_search_APT");
1537 r = rpn_search_APT (zh, zs->u.simple->u.attributesPlusTerm,
1538 attributeSet, num_bases, basenames);
1540 else if (zs->u.simple->which == Z_Operand_resultSetId)
1542 logf (LOG_DEBUG, "rpn_search_ref");
1543 r = resultSetRef (zh, zs->u.simple->u.resultSetId);
1545 r = rset_create (rset_kind_null, NULL);
1561 void rpn_search (ZebraHandle zh, ODR stream,
1562 Z_RPNQuery *rpn, int num_bases, char **basenames,
1563 const char *setname)
1567 oid_value attributeSet;
1572 zh->errString = NULL;
1575 attrset = oid_getentbyoid (rpn->attributeSetId);
1576 attributeSet = attrset->value;
1577 rset = rpn_search_structure (zh, rpn->RPNStructure, attributeSet, stream,
1578 num_bases, basenames);
1582 resultSetAdd (zh, setname, 1, rset, &zh->hits);
1584 logf (LOG_DEBUG, "search error: %d", zh->errCode);
1587 struct scan_info_entry {
1593 struct scan_info_entry *list;
1599 static int scan_handle (char *name, const char *info, int pos, void *client)
1601 int len_prefix, idx;
1602 struct scan_info *scan_info = client;
1604 len_prefix = strlen(scan_info->prefix);
1605 if (memcmp (name, scan_info->prefix, len_prefix))
1608 idx = scan_info->after - pos + scan_info->before;
1611 scan_info->list[idx].term = odr_malloc (scan_info->odr,
1612 strlen(name + len_prefix)+1);
1613 strcpy (scan_info->list[idx].term, name + len_prefix);
1614 assert (*info == sizeof(ISAM_P));
1615 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAM_P));
1619 static void scan_term_untrans (ZebraHandle zh, ODR stream, int reg_type,
1620 char **dst, const char *src)
1622 char term_dst[1024];
1624 term_untrans (zh, reg_type, term_dst, src);
1626 *dst = odr_malloc (stream, strlen(term_dst)+1);
1627 strcpy (*dst, term_dst);
1630 static void count_set (RSET r, int *count)
1638 logf (LOG_DEBUG, "count_set");
1641 rfd = rset_open (r, RSETF_READ);
1642 while (rset_read (r, rfd, &key, &term_index))
1644 if (key.sysno != psysno)
1651 rset_close (r, rfd);
1652 logf (LOG_DEBUG, "%d keys, %d records", kno, *count);
1655 void rpn_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
1656 oid_value attributeset,
1657 int num_bases, char **basenames,
1658 int *position, int *num_entries, ZebraScanEntry **list,
1662 int pos = *position;
1663 int num = *num_entries;
1667 char termz[IT_MAX_WORD+20];
1670 struct scan_info *scan_info_array;
1671 ZebraScanEntry *glist;
1672 int ords[32], ord_no = 0;
1676 char *search_type = NULL;
1677 char *rank_type = NULL;
1680 if (attributeset == VAL_NONE)
1681 attributeset = VAL_BIB1;
1683 zlog_scan (zapt, attributeset);
1684 logf (LOG_DEBUG, "position = %d, num = %d", pos, num);
1686 attr_init (&use, zapt, 1);
1687 use_value = attr_find (&use, &attributeset);
1689 if (zebra_maps_attr (zh->zebra_maps, zapt, ®_id, &search_type,
1690 &rank_type, &complete_flag))
1696 if (use_value == -1)
1698 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
1702 data1_local_attribute *local_attr;
1704 if ((r=att_getentbyatt (zh, &attp, attributeset, use_value)))
1706 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
1707 attributeset, use_value);
1713 if (zebraExplain_curDatabase (zh->zei, basenames[base_no]))
1715 zh->errString = basenames[base_no];
1716 zh->errCode = 109; /* Database unavailable */
1719 for (local_attr = attp.local_attributes; local_attr && ord_no < 32;
1720 local_attr = local_attr->next)
1724 ord = zebraExplain_lookupSU (zh->zei, attp.attset_ordinal,
1727 ords[ord_no++] = ord;
1737 scan_info_array = odr_malloc (stream, ord_no * sizeof(*scan_info_array));
1738 for (i = 0; i < ord_no; i++)
1740 int j, prefix_len = 0;
1741 int before_tmp = before, after_tmp = after;
1742 struct scan_info *scan_info = scan_info_array + i;
1743 struct rpn_char_map_info rcmi;
1745 rpn_char_map_prepare (zh, reg_id, &rcmi);
1747 scan_info->before = before;
1748 scan_info->after = after;
1749 scan_info->odr = stream;
1751 scan_info->list = odr_malloc (stream, (before+after)*
1752 sizeof(*scan_info->list));
1753 for (j = 0; j<before+after; j++)
1754 scan_info->list[j].term = NULL;
1755 termz[prefix_len++] = ords[i];
1756 termz[prefix_len++] = reg_id;
1757 termz[prefix_len] = 0;
1758 strcpy (scan_info->prefix, termz);
1760 trans_scan_term (zh, zapt, termz+prefix_len, reg_id);
1762 dict_scan (zh->dict, termz, &before_tmp, &after_tmp, scan_info,
1765 glist = odr_malloc (stream, (before+after)*sizeof(*glist));
1766 for (i = 0; i < ord_no; i++)
1770 for (i = 0; i<after; i++)
1773 const char *mterm = NULL;
1777 for (j = 0; j < ord_no; j++)
1779 if (ptr[j] < before+after &&
1780 (tst=scan_info_array[j].list[ptr[j]].term) &&
1781 (!mterm || strcmp (tst, mterm) < 0))
1789 scan_term_untrans (zh, stream, reg_id,
1790 &glist[i+before].term, mterm);
1791 rset = rset_trunc (zh, &scan_info_array[j0].list[ptr[j0]].isam_p, 1,
1792 glist[i+before].term, strlen(glist[i+before].term),
1796 for (j = j0+1; j<ord_no; j++)
1798 if (ptr[j] < before+after &&
1799 (tst=scan_info_array[j].list[ptr[j]].term) &&
1800 !strcmp (tst, mterm))
1802 rset_bool_parms bool_parms;
1806 rset_trunc (zh, &scan_info_array[j].list[ptr[j]].isam_p, 1,
1807 glist[i+before].term,
1808 strlen(glist[i+before].term), NULL);
1810 bool_parms.key_size = sizeof(struct it_key);
1811 bool_parms.cmp = key_compare_it;
1812 bool_parms.rset_l = rset;
1813 bool_parms.rset_r = rset2;
1815 rset = rset_create (rset_kind_or, &bool_parms);
1820 count_set (rset, &glist[i+before].occurrences);
1825 *num_entries -= (after-i);
1829 for (i = 0; i<ord_no; i++)
1832 for (i = 0; i<before; i++)
1835 const char *mterm = NULL;
1839 for (j = 0; j <ord_no; j++)
1841 if (ptr[j] < before &&
1842 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
1843 (!mterm || strcmp (tst, mterm) > 0))
1852 scan_term_untrans (zh, stream, reg_id,
1853 &glist[before-1-i].term, mterm);
1856 (zh, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1,
1857 glist[before-1-i].term, strlen(glist[before-1-i].term),
1862 for (j = j0+1; j<ord_no; j++)
1864 if (ptr[j] < before &&
1865 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
1866 !strcmp (tst, mterm))
1868 rset_bool_parms bool_parms;
1871 rset2 = rset_trunc (zh,
1872 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1,
1873 glist[before-1-i].term,
1874 strlen(glist[before-1-i].term), NULL);
1876 bool_parms.key_size = sizeof(struct it_key);
1877 bool_parms.cmp = key_compare_it;
1878 bool_parms.rset_l = rset;
1879 bool_parms.rset_r = rset2;
1881 rset = rset_create (rset_kind_or, &bool_parms);
1886 count_set (rset, &glist[before-1-i].occurrences);
1896 *list = glist + i; /* list is set to first 'real' entry */
1898 logf (LOG_DEBUG, "position = %d, num_entries = %d",
1899 *position, *num_entries);
1901 logf (LOG_DEBUG, "scan error: %d", zh->errCode);