2 * Copyright (C) 1995-1998, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.76 1998-04-02 14:35:29 adam
8 * First version of Zebra that works with compiled ASN.1.
10 * Revision 1.75 1998/03/05 08:45:13 adam
11 * New result set model and modular ranking system. Moved towards
12 * descent server API. System information stored as "SGML" records.
14 * Revision 1.74 1998/02/10 12:03:06 adam
17 * Revision 1.73 1998/01/29 13:40:11 adam
18 * Better logging for scan service.
20 * Revision 1.72 1998/01/07 13:53:41 adam
21 * Queries using simple ranked operands returns right number of hits.
23 * Revision 1.71 1997/12/18 10:54:24 adam
24 * New method result set method rs_hits that returns the number of
25 * hits in result-set (if known). The ranked result set returns real
26 * number of hits but only when not combined with other operands.
28 * Revision 1.70 1997/10/31 12:34:43 adam
29 * Changed a few log statements.
31 * Revision 1.69 1997/10/29 12:05:02 adam
32 * Server produces diagnostic "Unsupported Attribute Set" when appropriate.
34 * Revision 1.68 1997/10/27 14:33:06 adam
35 * Moved towards generic character mapping depending on "structure"
36 * field in abstract syntax file. Fixed a few memory leaks. Fixed
37 * bug with negative integers when doing searches with relational
40 * Revision 1.67 1997/09/29 09:06:10 adam
41 * Removed one static var in order to make this module thread safe.
43 * Revision 1.66 1997/09/25 14:58:03 adam
46 * Revision 1.65 1997/09/22 12:39:06 adam
47 * Added get_pos method for the ranked result sets.
49 * Revision 1.64 1997/09/18 08:59:20 adam
50 * Extra generic handle for the character mapping routines.
52 * Revision 1.63 1997/09/17 12:19:18 adam
53 * Zebra version corresponds to YAZ version 1.4.
54 * Changed Zebra server so that it doesn't depend on global common_resource.
56 * Revision 1.62 1997/09/05 15:30:09 adam
57 * Changed prototype for chr_map_input - added const.
58 * Added support for C++, headers uses extern "C" for public definitions.
60 * Revision 1.61 1997/02/10 10:21:14 adam
61 * Bug fix: in search terms character (^) wasn't observed.
63 * Revision 1.60 1997/01/31 11:10:34 adam
64 * Bug fix: Leading and trailing white space weren't removed in scan tokens.
66 * Revision 1.59 1997/01/17 11:31:46 adam
67 * Bug fix: complete phrase search didn't work.
69 * Revision 1.58 1996/12/23 15:30:45 adam
71 * Bug fix: result sets weren't deleted after server shut down.
73 * Revision 1.57 1996/11/11 13:38:02 adam
74 * Added proximity support in search.
76 * Revision 1.56 1996/11/08 11:10:32 adam
77 * Buffers used during file match got bigger.
78 * Compressed ISAM support everywhere.
79 * Bug fixes regarding masking characters in queries.
80 * Redesigned Regexp-2 queries.
82 * Revision 1.55 1996/11/04 14:07:44 adam
83 * Moved truncation code to trunc.c.
85 * Revision 1.54 1996/10/29 14:09:52 adam
86 * Use of cisam system - enabled if setting isamc is 1.
88 * Revision 1.53 1996/06/26 09:21:43 adam
89 * Bug fix: local attribute set wasn't obeyed in scan.
91 * Revision 1.52 1996/06/17 14:26:20 adam
92 * Function gen_regular_rel changed to handle negative numbers.
94 * Revision 1.51 1996/06/11 10:54:15 quinn
97 * Revision 1.50 1996/06/07 08:51:53 adam
98 * Bug fix: Character mapping was broken (introducued by last revision).
100 * Revision 1.49 1996/06/04 10:18:11 adam
101 * Search/scan uses character mapping module.
103 * Revision 1.48 1996/05/28 15:15:01 adam
104 * Bug fix: Didn't handle unknown database correctly.
106 * Revision 1.47 1996/05/15 18:36:28 adam
107 * Function trans_term transforms unsearchable characters to blanks.
109 * Revision 1.46 1996/05/15 11:57:56 adam
110 * Fixed bug introduced by set/field mapping in search operations.
112 * Revision 1.45 1996/05/14 11:34:00 adam
113 * Scan support in multiple registers/databases.
115 * Revision 1.44 1996/05/14 06:16:44 adam
116 * Compact use/set bytes used in search service.
118 * Revision 1.43 1996/05/09 09:54:43 adam
119 * Server supports maps from one logical attributes to a list of physical
121 * The extraction process doesn't make space consuming 'any' keys.
123 * Revision 1.42 1996/05/09 07:28:56 quinn
124 * Work towards phrases and multiple registers
126 * Revision 1.41 1996/03/20 09:36:43 adam
127 * Function dict_lookup_grep got extra parameter, init_pos, which marks
128 * from which position in pattern approximate pattern matching should occur.
129 * Approximate pattern matching is used in relevance=re-2.
131 * Revision 1.40 1996/02/02 13:44:44 adam
132 * The public dictionary functions simply use char instead of Dict_char
133 * to represent search strings. Dict_char is used internally only.
135 * Revision 1.39 1996/01/03 16:22:13 quinn
136 * operator->roperator
138 * Revision 1.38 1995/12/11 09:12:55 adam
139 * The rec_get function returns NULL if record doesn't exist - will
140 * happen in the server if the result set records have been deleted since
141 * the creation of the set (i.e. the search).
142 * The server saves a result temporarily if it is 'volatile', i.e. the
143 * set is register dependent.
145 * Revision 1.37 1995/12/06 15:05:28 adam
146 * More verbose in count_set.
148 * Revision 1.36 1995/12/06 12:41:27 adam
149 * New command 'stat' for the index program.
150 * Filenames can be read from stdin by specifying '-'.
151 * Bug fix/enhancement of the transformation from terms to regular
152 * expressons in the search engine.
154 * Revision 1.35 1995/11/27 09:29:00 adam
155 * Bug fixes regarding conversion to regular expressions.
157 * Revision 1.34 1995/11/16 17:00:56 adam
158 * Better logging of rpn query.
160 * Revision 1.33 1995/11/01 13:58:28 quinn
161 * Moving data1 to yaz/retrieval
163 * Revision 1.32 1995/10/27 14:00:11 adam
164 * Implemented detection of database availability.
166 * Revision 1.31 1995/10/17 18:02:10 adam
167 * New feature: databases. Implemented as prefix to words in dictionary.
169 * Revision 1.30 1995/10/16 09:32:38 adam
170 * More work on relational op.
172 * Revision 1.29 1995/10/13 16:01:49 adam
175 * Revision 1.28 1995/10/13 12:26:43 adam
176 * Optimization of truncation.
178 * Revision 1.27 1995/10/12 17:07:22 adam
181 * Revision 1.26 1995/10/12 12:40:54 adam
182 * Bug fixes in rpn_prox.
184 * Revision 1.25 1995/10/10 13:59:24 adam
185 * Function rset_open changed its wflag parameter to general flags.
187 * Revision 1.24 1995/10/09 16:18:37 adam
188 * Function dict_lookup_grep got extra client data parameter.
190 * Revision 1.23 1995/10/06 16:33:37 adam
191 * Use attribute mappings.
193 * Revision 1.22 1995/10/06 15:07:39 adam
194 * Structure 'local-number' handled.
196 * Revision 1.21 1995/10/06 13:52:06 adam
197 * Bug fixes. Handler may abort further scanning.
199 * Revision 1.20 1995/10/06 11:06:33 adam
200 * Scan entries include 'occurrences' now.
202 * Revision 1.19 1995/10/06 10:43:56 adam
203 * Scan added. 'occurrences' in scan entries not set yet.
205 * Revision 1.18 1995/10/04 16:57:20 adam
206 * Key input and merge sort in one pass.
208 * Revision 1.17 1995/10/04 12:55:17 adam
209 * Bug fix in ranked search. Use=Any keys inserted.
211 * Revision 1.16 1995/10/02 16:24:40 adam
212 * Use attribute actually used in search requests.
214 * Revision 1.15 1995/10/02 15:18:52 adam
215 * New member in recRetrieveCtrl: diagnostic.
217 * Revision 1.14 1995/09/28 12:10:32 adam
218 * Bug fixes. Field prefix used in queries.
220 * Revision 1.13 1995/09/18 14:17:50 adam
223 * Revision 1.12 1995/09/15 14:45:21 adam
225 * Work on truncation.
227 * Revision 1.11 1995/09/14 11:53:27 adam
228 * First work on regular expressions/truncations.
230 * Revision 1.10 1995/09/11 15:23:26 adam
231 * More work on relevance search.
233 * Revision 1.9 1995/09/11 13:09:35 adam
234 * More work on relevance feedback.
236 * Revision 1.8 1995/09/08 14:52:27 adam
237 * Minor changes. Dictionary is lower case now.
239 * Revision 1.7 1995/09/07 13:58:36 adam
240 * New parameter: result-set file descriptor (RSFD) to support multiple
241 * positions within the same result-set.
242 * Boolean operators: and, or, not implemented.
243 * Result-set references.
245 * Revision 1.6 1995/09/06 16:11:18 adam
246 * Option: only one word key per file.
248 * Revision 1.5 1995/09/06 10:33:04 adam
249 * More work on present. Some log messages removed.
251 * Revision 1.4 1995/09/05 15:28:40 adam
252 * More work on search engine.
254 * Revision 1.3 1995/09/04 15:20:22 adam
257 * Revision 1.2 1995/09/04 12:33:43 adam
258 * Various cleanup. YAZ util used instead.
260 * Revision 1.1 1995/09/04 09:10:40 adam
261 * More work on index add/del/update.
262 * Merge sort implemented.
263 * Initial work on z39 server.
282 struct rpn_char_map_info {
287 static const char **rpn_char_map_handler (void *vp, const char **from, int len)
289 struct rpn_char_map_info *p = vp;
290 return zebra_maps_input (p->zm, p->reg_type, from, len);
293 static void rpn_char_map_prepare (ZebraHandle zh, int reg_type,
294 struct rpn_char_map_info *map_info)
296 map_info->zm = zh->zebra_maps;
297 map_info->reg_type = reg_type;
298 dict_grep_cmap (zh->dict, map_info, rpn_char_map_handler);
305 Z_AttributesPlusTerm *zapt;
308 static int attr_find (AttrType *src, oid_value *attributeSetP)
313 num_attributes = src->zapt->attributes->num_attributes;
315 num_attributes = src->zapt->num_attributes;
317 while (src->major < num_attributes)
319 Z_AttributeElement *element;
322 element = src->zapt->attributes->attributes[src->major];
324 element = src->zapt->attributeList[src->major];
326 if (src->type == *element->attributeType)
328 switch (element->which)
330 case Z_AttributeValue_numeric:
332 if (element->attributeSet && attributeSetP)
336 attrset = oid_getentbyoid (element->attributeSet);
337 *attributeSetP = attrset->value;
339 return *element->value.numeric;
341 case Z_AttributeValue_complex:
342 if (src->minor >= element->value.complex->num_list ||
343 element->value.complex->list[src->minor]->which !=
344 Z_StringOrNumeric_numeric)
347 if (element->attributeSet && attributeSetP)
351 attrset = oid_getentbyoid (element->attributeSet);
352 *attributeSetP = attrset->value;
354 return *element->value.complex->list[src->minor-1]->u.numeric;
364 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
386 static void term_untrans (ZebraHandle zh, int reg_type,
387 char *dst, const char *src)
391 const char *cp = zebra_maps_output (zh->zebra_maps, reg_type, &src);
398 static void add_isam_p (const char *name, const char *info,
402 if (p->isam_p_indx == p->isam_p_size)
404 ISAM_P *new_isam_p_buf;
408 p->isam_p_size = 2*p->isam_p_size + 100;
409 new_isam_p_buf = xmalloc (sizeof(*new_isam_p_buf) *
413 memcpy (new_isam_p_buf, p->isam_p_buf,
414 p->isam_p_indx * sizeof(*p->isam_p_buf));
415 xfree (p->isam_p_buf);
417 p->isam_p_buf = new_isam_p_buf;
420 new_term_no = xmalloc (sizeof(*new_term_no) *
424 memcpy (new_term_no, p->isam_p_buf,
425 p->isam_p_indx * sizeof(*p->term_no));
428 p->term_no = new_term_no;
431 assert (*info == sizeof(*p->isam_p_buf));
432 memcpy (p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
434 term_untrans (p->zh, p->reg_type, term_tmp, name+2);
435 logf (LOG_DEBUG, "grep: %s", term_tmp);
440 static int grep_handle (char *name, const char *info, void *p)
442 add_isam_p (name, info, p);
446 static int term_pre (ZebraMaps zebra_maps, int reg_type, const char **src,
447 const char *ct1, const char *ct2)
449 const char *s1, *s0 = *src;
452 /* skip white space */
455 if (ct1 && strchr (ct1, *s0))
457 if (ct2 && strchr (ct2, *s0))
460 map = zebra_maps_input (zebra_maps, reg_type, &s1, strlen(s1));
461 if (**map != *CHR_SPACE)
469 static int term_100 (ZebraMaps zebra_maps, int reg_type,
470 const char **src, char *dst, int space_split,
478 if (!term_pre (zebra_maps, reg_type, src, NULL, NULL))
484 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
485 if (space_split && **map == *CHR_SPACE)
489 if (!isalnum (*s1) && *s1 != '-')
501 static int term_101 (ZebraMaps zebra_maps, int reg_type,
502 const char **src, char *dst, int space_split,
510 if (!term_pre (zebra_maps, reg_type, src, "#", "#"))
519 dst_term[j++] = *s0++;
524 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
525 if (space_split && **map == *CHR_SPACE)
537 dst_term[j++] = '\0';
543 static int term_103 (ZebraMaps zebra_maps, int reg_type, const char **src,
544 char *dst, int *errors, int space_split,
552 if (!term_pre (zebra_maps, reg_type, src, "^\\()[].*+?|", "("))
555 if (errors && *s0 == '+' && s0[1] && s0[2] == '+' && s0[3] &&
558 *errors = s0[1] - '0';
565 if (strchr ("^\\()[].*+?|-", *s0))
573 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
574 if (**map == *CHR_SPACE)
591 static int term_102 (ZebraMaps zebra_maps, int reg_type, const char **src,
592 char *dst, int space_split, char *dst_term)
594 return term_103 (zebra_maps, reg_type, src, dst, NULL, space_split,
598 /* gen_regular_rel - generate regular expression from relation
599 * val: border value (inclusive)
600 * islt: 1 if <=; 0 if >=.
602 static void gen_regular_rel (char *dst, int val, int islt)
609 logf (LOG_DEBUG, "gen_regular_rel. val=%d, islt=%d", val, islt);
613 strcpy (dst, "(-[0-9]+|(");
621 strcpy (dst, "([0-9]+|-(");
622 dst_p = strlen (dst);
632 dst_p = strlen (dst);
633 sprintf (numstr, "%d", val);
634 for (w = strlen(numstr); --w >= 0; pos++)
653 strcpy (dst + dst_p, numstr);
654 dst_p = strlen(dst) - pos - 1;
682 for (i = 0; i<pos; i++)
695 for (i=1; i<pos; i++)
696 strcat (dst, "[0-9]?");
700 for (i = 0; i <= pos; i++)
701 strcat (dst, "[0-9]");
702 strcat (dst, "[0-9]*");
707 static int relational_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
708 const char **term_sub,
710 oid_value attributeSet,
711 struct grep_info *grep_info,
720 char *term_tmp = term_dict + strlen(term_dict);
722 attr_init (&relation, zapt, 2);
723 relation_value = attr_find (&relation, NULL);
725 logf (LOG_DEBUG, "relation value=%d", relation_value);
726 switch (relation_value)
729 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_tmp, 1,
732 term_value = atoi (term_tmp);
733 logf (LOG_DEBUG, "Relation <");
734 gen_regular_rel (term_tmp, term_value-1, 1);
737 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_tmp, 1,
740 term_value = atoi (term_tmp);
741 logf (LOG_DEBUG, "Relation <=");
742 gen_regular_rel (term_tmp, term_value, 1);
745 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_tmp, 1,
748 term_value = atoi (term_tmp);
749 logf (LOG_DEBUG, "Relation >=");
750 gen_regular_rel (term_tmp, term_value, 0);
753 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_tmp, 1,
756 term_value = atoi (term_tmp);
757 logf (LOG_DEBUG, "Relation >");
758 gen_regular_rel (term_tmp, term_value+1, 0);
763 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_tmp);
764 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info, max_pos,
767 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
768 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
772 static int field_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
773 const char **term_sub,
774 oid_value attributeSet, struct grep_info *grep_info,
775 int reg_type, int complete_flag,
776 int num_bases, char **basenames,
779 char term_dict[2*IT_MAX_WORD+2];
782 int truncation_value;
785 oid_value curAttributeSet = attributeSet;
787 struct rpn_char_map_info rcmi;
788 int space_split = complete_flag ? 0 : 1;
790 rpn_char_map_prepare (zh, reg_type, &rcmi);
791 attr_init (&use, zapt, 1);
792 use_value = attr_find (&use, &curAttributeSet);
793 logf (LOG_DEBUG, "field_term, use value %d", use_value);
794 attr_init (&truncation, zapt, 5);
795 truncation_value = attr_find (&truncation, NULL);
796 logf (LOG_DEBUG, "truncation value %d", truncation_value);
801 for (base_no = 0; base_no < num_bases; base_no++)
804 data1_local_attribute *local_attr;
805 int max_pos, prefix_len = 0;
808 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
810 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
811 curAttributeSet, use_value, r);
818 if (zebraExplain_curDatabase (zh->zei, basenames[base_no]))
820 zh->errCode = 109; /* Database unavailable */
821 zh->errString = basenames[base_no];
824 for (local_attr = attp.local_attributes; local_attr;
825 local_attr = local_attr->next)
829 ord = zebraExplain_lookupSU (zh->zei, attp.attset_ordinal,
834 term_dict[prefix_len++] = '|';
836 term_dict[prefix_len++] = '(';
837 term_dict[prefix_len++] = 1;
838 term_dict[prefix_len++] = ord;
845 term_dict[prefix_len++] = ')';
846 term_dict[prefix_len++] = 1;
847 term_dict[prefix_len++] = reg_type;
848 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
849 term_dict[prefix_len] = '\0';
850 if (!relational_term (zh, zapt, &termp, term_dict,
851 attributeSet, grep_info, &max_pos, reg_type,
855 switch (truncation_value)
857 case -1: /* not specified */
858 case 100: /* do not truncate */
859 term_dict[j++] = '(';
860 if (!term_100 (zh->zebra_maps, reg_type,
861 &termp, term_dict + j, space_split, term_dst))
863 strcat (term_dict, ")");
864 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
865 &max_pos, 0, grep_handle);
867 logf (LOG_WARN, "dict_lookup_grep err, trunc=none:%d", r);
869 case 1: /* right truncation */
870 term_dict[j++] = '(';
871 if (!term_100 (zh->zebra_maps, reg_type,
872 &termp, term_dict + j, space_split, term_dst))
874 strcat (term_dict, ".*)");
875 dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
876 &max_pos, 0, grep_handle);
878 case 2: /* left truncation */
879 case 3: /* left&right truncation */
882 case 101: /* process # in term */
883 term_dict[j++] = '(';
884 if (!term_101 (zh->zebra_maps, reg_type,
885 &termp, term_dict + j, space_split, term_dst))
887 strcat (term_dict, ")");
888 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
889 &max_pos, 0, grep_handle);
891 logf (LOG_WARN, "dict_lookup_grep err, trunc=#: %d", r);
893 case 102: /* Regexp-1 */
894 term_dict[j++] = '(';
895 if (!term_102 (zh->zebra_maps, reg_type,
896 &termp, term_dict + j, space_split, term_dst))
898 strcat (term_dict, ")");
899 logf (LOG_DEBUG, "Regexp-1 tolerance=%d", r);
900 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
901 &max_pos, 0, grep_handle);
903 logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
906 case 103: /* Regexp-2 */
908 term_dict[j++] = '(';
909 if (!term_103 (zh->zebra_maps, reg_type,
910 &termp, term_dict + j, &r, space_split, term_dst))
912 strcat (term_dict, ")");
913 logf (LOG_DEBUG, "Regexp-2 tolerance=%d", r);
914 r = dict_lookup_grep (zh->dict, term_dict, r, grep_info,
915 &max_pos, 2, grep_handle);
917 logf (LOG_WARN, "dict_lookup_grep err, trunc=eregular: %d",
924 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
928 static void trans_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
932 Z_Term *term = zapt->term;
934 sizez = term->u.general->len;
935 if (sizez > IT_MAX_WORD-1)
936 sizez = IT_MAX_WORD-1;
937 memcpy (termz, term->u.general->buf, sizez);
941 static void trans_scan_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
942 char *termz, int reg_type)
944 Z_Term *term = zapt->term;
946 const char *cp = (const char *) term->u.general->buf;
947 const char *cp_end = cp + term->u.general->len;
950 const char *space_map = NULL;
953 while ((len = (cp_end - cp)) > 0)
955 map = zebra_maps_input (zh->zebra_maps, reg_type, &cp, len);
956 if (**map == *CHR_SPACE)
961 for (src = space_map; *src; src++)
964 for (src = *map; *src; src++)
971 static RSET rpn_proximity (ZebraHandle zh, RSET rset1, RSET rset2,
973 int exclusion, int relation, int distance)
978 struct it_key buf1, buf2;
981 rset_temp_parms parms;
984 rsfd1 = rset_open (rset1, RSETF_READ);
985 more1 = rset_read (rset1, rsfd1, &buf1, &term_index);
987 rsfd2 = rset_open (rset2, RSETF_READ);
988 more2 = rset_read (rset2, rsfd2, &buf2, &term_index);
990 parms.key_size = sizeof (struct it_key);
991 parms.temp_path = res_get (zh->res, "setTmpDir");
992 result = rset_create (rset_kind_temp, &parms);
993 rsfd_result = rset_open (result, RSETF_WRITE);
995 logf (LOG_DEBUG, "rpn_proximity excl=%d ord=%d rel=%d dis=%d",
996 exclusion, ordered, relation, distance);
997 while (more1 && more2)
999 int cmp = key_compare_it (&buf1, &buf2);
1001 more1 = rset_read (rset1, rsfd1, &buf1, &term_index);
1003 more2 = rset_read (rset2, rsfd2, &buf2, &term_index);
1006 int sysno = buf1.sysno;
1010 seqno[n++] = buf1.seqno;
1011 while ((more1 = rset_read (rset1, rsfd1, &buf1, &term_index)) &&
1012 sysno == buf1.sysno)
1014 seqno[n++] = buf1.seqno;
1017 for (i = 0; i<n; i++)
1019 int diff = buf2.seqno - seqno[i];
1020 int excl = exclusion;
1021 if (!ordered && diff < 0)
1026 if (diff < distance)
1030 if (diff <= distance)
1034 if (diff == distance)
1038 if (diff >= distance)
1042 if (diff > distance)
1046 if (diff != distance)
1051 rset_write (result, rsfd_result, &buf2);
1053 } while ((more2 = rset_read (rset2, rsfd2, &buf2, &term_index)) &&
1054 sysno == buf2.sysno);
1057 rset_close (result, rsfd_result);
1058 rset_close (rset1, rsfd1);
1059 rset_close (rset2, rsfd2);
1063 static RSET rpn_prox (ZebraHandle zh, RSET *rset, int rset_no)
1068 struct it_key **buf;
1070 char prox_term[1024];
1071 int length_prox_term = 0;
1072 int min_nn = 10000000;
1074 const char *flags = NULL;
1076 rsfd = xmalloc (sizeof(*rsfd)*rset_no);
1077 more = xmalloc (sizeof(*more)*rset_no);
1078 buf = xmalloc (sizeof(*buf)*rset_no);
1080 for (i = 0; i<rset_no; i++)
1083 buf[i] = xmalloc (sizeof(**buf));
1084 rsfd[i] = rset_open (rset[i], RSETF_READ);
1085 if (!(more[i] = rset_read (rset[i], rsfd[i], buf[i], &term_index)))
1087 for (j = 0; j<rset[i]->no_rset_terms; j++)
1089 const char *nflags = rset[i]->rset_terms[j]->flags;
1090 char *term = rset[i]->rset_terms[j]->name;
1091 int lterm = strlen(term);
1092 if (length_prox_term)
1093 prox_term[length_prox_term++] = ' ';
1094 strcpy (prox_term + length_prox_term, term);
1095 length_prox_term += lterm;
1096 if (min_nn > rset[i]->rset_terms[j]->nn)
1097 min_nn = rset[i]->rset_terms[j]->nn;
1103 rset_null_parms parms;
1107 rset_close (rset[i], rsfd[i]);
1111 parms.rset_term = rset_term_create (prox_term, -1, flags);
1112 parms.rset_term->nn = 0;
1113 result = rset_create (rset_kind_null, &parms);
1117 rset_temp_parms parms;
1120 parms.rset_term = rset_term_create (prox_term, -1, flags);
1121 parms.rset_term->nn = min_nn;
1122 parms.key_size = sizeof (struct it_key);
1123 parms.temp_path = res_get (zh->res, "setTmpDir");
1124 result = rset_create (rset_kind_temp, &parms);
1125 rsfd_result = rset_open (result, RSETF_WRITE);
1129 for (i = 1; i<rset_no; i++)
1138 cmp = key_compare_it (buf[i], buf[i-1]);
1141 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1142 buf[i-1], &term_index);
1147 if (buf[i-1]->seqno+1 != buf[i]->seqno)
1149 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1150 buf[i-1], &term_index);
1156 more[i] = rset_read (rset[i], rsfd[i], buf[i],
1163 rset_write (result, rsfd_result, buf[0]);
1164 more[0] = rset_read (*rset, *rsfd, *buf, &term_index);
1168 for (i = 0; i<rset_no; i++)
1170 rset_close (rset[i], rsfd[i]);
1173 rset_close (result, rsfd_result);
1181 static RSET rpn_search_APT_phrase (ZebraHandle zh,
1182 Z_AttributesPlusTerm *zapt,
1184 oid_value attributeSet,
1185 int reg_type, int complete_flag,
1186 const char *rank_type,
1187 int num_bases, char **basenames)
1189 char term_dst[IT_MAX_WORD+1];
1190 const char *termp = termz;
1191 RSET rset[60], result;
1192 int i, r, rset_no = 0;
1193 struct grep_info grep_info;
1196 grep_info.term_no = 0;
1198 grep_info.isam_p_size = 0;
1199 grep_info.isam_p_buf = NULL;
1201 grep_info.reg_type = reg_type;
1205 logf (LOG_DEBUG, "APT_phrase termp=%s", termp);
1206 grep_info.isam_p_indx = 0;
1207 r = field_term (zh, zapt, &termp, attributeSet, &grep_info,
1208 reg_type, complete_flag, num_bases, basenames,
1212 logf (LOG_DEBUG, "term: %s", term_dst);
1213 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1214 grep_info.isam_p_indx, term_dst,
1215 strlen(term_dst), rank_type);
1216 assert (rset[rset_no]);
1217 if (++rset_no >= sizeof(rset)/sizeof(*rset))
1221 xfree(grep_info.term_no);
1223 xfree (grep_info.isam_p_buf);
1226 rset_null_parms parms;
1228 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1229 return rset_create (rset_kind_null, &parms);
1231 else if (rset_no == 1)
1233 result = rpn_prox (zh, rset, rset_no);
1234 for (i = 0; i<rset_no; i++)
1235 rset_delete (rset[i]);
1239 static RSET rpn_search_APT_or_list (ZebraHandle zh,
1240 Z_AttributesPlusTerm *zapt,
1242 oid_value attributeSet,
1243 int reg_type, int complete_flag,
1244 const char *rank_type,
1245 int num_bases, char **basenames)
1247 char term_dst[IT_MAX_WORD+1];
1248 const char *termp = termz;
1249 RSET rset[60], result;
1250 int i, r, rset_no = 0;
1251 struct grep_info grep_info;
1254 grep_info.term_no = 0;
1256 grep_info.isam_p_size = 0;
1257 grep_info.isam_p_buf = NULL;
1259 grep_info.reg_type = reg_type;
1263 logf (LOG_DEBUG, "APT_or_list termp=%s", termp);
1264 grep_info.isam_p_indx = 0;
1265 r = field_term (zh, zapt, &termp, attributeSet, &grep_info,
1266 reg_type, complete_flag, num_bases, basenames,
1270 logf (LOG_DEBUG, "term: %s", term_dst);
1271 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1272 grep_info.isam_p_indx, term_dst,
1273 strlen(term_dst), rank_type);
1274 assert (rset[rset_no]);
1275 if (++rset_no >= sizeof(rset)/sizeof(*rset))
1279 xfree(grep_info.term_no);
1281 xfree (grep_info.isam_p_buf);
1284 rset_null_parms parms;
1286 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1287 return rset_create (rset_kind_null, &parms);
1290 for (i = 1; i<rset_no; i++)
1292 rset_bool_parms bool_parms;
1294 bool_parms.rset_l = result;
1295 bool_parms.rset_r = rset[i];
1296 bool_parms.key_size = sizeof(struct it_key);
1297 bool_parms.cmp = key_compare_it;
1298 result = rset_create (rset_kind_or, &bool_parms);
1303 static RSET rpn_search_APT_and_list (ZebraHandle zh,
1304 Z_AttributesPlusTerm *zapt,
1306 oid_value attributeSet,
1307 int reg_type, int complete_flag,
1308 const char *rank_type,
1309 int num_bases, char **basenames)
1311 char term_dst[IT_MAX_WORD+1];
1312 const char *termp = termz;
1313 RSET rset[60], result;
1314 int i, r, rset_no = 0;
1315 struct grep_info grep_info;
1318 grep_info.term_no = 0;
1320 grep_info.isam_p_size = 0;
1321 grep_info.isam_p_buf = NULL;
1323 grep_info.reg_type = reg_type;
1327 logf (LOG_DEBUG, "APT_and_list termp=%s", termp);
1328 grep_info.isam_p_indx = 0;
1329 r = field_term (zh, zapt, &termp, attributeSet, &grep_info,
1330 reg_type, complete_flag, num_bases, basenames,
1334 logf (LOG_DEBUG, "term: %s", term_dst);
1335 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1336 grep_info.isam_p_indx, term_dst,
1337 strlen(term_dst), rank_type);
1338 assert (rset[rset_no]);
1339 if (++rset_no >= sizeof(rset)/sizeof(*rset))
1343 xfree(grep_info.term_no);
1345 xfree (grep_info.isam_p_buf);
1348 rset_null_parms parms;
1350 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1351 return rset_create (rset_kind_null, &parms);
1354 for (i = 1; i<rset_no; i++)
1356 rset_bool_parms bool_parms;
1358 bool_parms.rset_l = result;
1359 bool_parms.rset_r = rset[i];
1360 bool_parms.key_size = sizeof(struct it_key);
1361 bool_parms.cmp = key_compare_it;
1362 result = rset_create (rset_kind_and, &bool_parms);
1368 static RSET rpn_search_APT_local (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1370 oid_value attributeSet,
1371 const char *rank_type)
1376 rset_temp_parms parms;
1378 parms.rset_term = rset_term_create (termz, -1, rank_type);
1379 parms.key_size = sizeof (struct it_key);
1380 parms.temp_path = res_get (zh->res, "setTmpDir");
1381 result = rset_create (rset_kind_temp, &parms);
1382 rsfd = rset_open (result, RSETF_WRITE);
1384 key.sysno = atoi (termz);
1388 rset_write (result, rsfd, &key);
1389 rset_close (result, rsfd);
1393 static RSET rpn_search_APT (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1394 oid_value attributeSet,
1395 int num_bases, char **basenames)
1398 char *search_type = NULL;
1399 char *rank_type = NULL;
1401 char termz[IT_MAX_WORD+1];
1403 zebra_maps_attr (zh->zebra_maps, zapt, ®_id, &search_type,
1404 &rank_type, &complete_flag);
1406 logf (LOG_DEBUG, "reg_id=%c", reg_id);
1407 logf (LOG_DEBUG, "complete_flag=%d", complete_flag);
1408 logf (LOG_DEBUG, "search_type=%s", search_type);
1409 logf (LOG_DEBUG, "rank_type=%s", rank_type);
1411 if (zapt->term->which != Z_Term_general)
1416 trans_term (zh, zapt, termz);
1418 if (!strcmp (search_type, "phrase"))
1420 return rpn_search_APT_phrase (zh, zapt, termz, attributeSet,
1421 reg_id, complete_flag, rank_type,
1422 num_bases, basenames);
1424 else if (!strcmp (search_type, "and-list"))
1426 return rpn_search_APT_and_list (zh, zapt, termz, attributeSet,
1427 reg_id, complete_flag, rank_type,
1428 num_bases, basenames);
1430 else if (!strcmp (search_type, "or-list"))
1432 return rpn_search_APT_or_list (zh, zapt, termz, attributeSet,
1433 reg_id, complete_flag, rank_type,
1434 num_bases, basenames);
1436 else if (!strcmp (search_type, "local"))
1438 return rpn_search_APT_local (zh, zapt, termz, attributeSet,
1445 static RSET rpn_search_structure (ZebraHandle zh, Z_RPNStructure *zs,
1446 oid_value attributeSet, ODR stream,
1447 int num_bases, char **basenames)
1450 if (zs->which == Z_RPNStructure_complex)
1452 Z_Operator *zop = zs->u.complex->roperator;
1453 rset_bool_parms bool_parms;
1455 bool_parms.rset_l = rpn_search_structure (zh, zs->u.complex->s1,
1456 attributeSet, stream,
1457 num_bases, basenames);
1458 if (bool_parms.rset_l == NULL)
1460 bool_parms.rset_r = rpn_search_structure (zh, zs->u.complex->s2,
1461 attributeSet, stream,
1462 num_bases, basenames);
1463 if (bool_parms.rset_r == NULL)
1465 rset_delete (bool_parms.rset_l);
1468 bool_parms.key_size = sizeof(struct it_key);
1469 bool_parms.cmp = key_compare_it;
1473 case Z_Operator_and:
1474 r = rset_create (rset_kind_and, &bool_parms);
1477 r = rset_create (rset_kind_or, &bool_parms);
1479 case Z_Operator_and_not:
1480 r = rset_create (rset_kind_not, &bool_parms);
1482 case Z_Operator_prox:
1484 if (zop->u.prox->which != Z_ProximityOperator_known)
1490 if (zop->u.prox->which != Z_ProxCode_known)
1498 if (*zop->u.prox->u.known != Z_ProxUnit_word)
1500 char *val = odr_malloc (stream, 16);
1502 zh->errString = val;
1503 sprintf (val, "%d", *zop->u.prox->u.known);
1507 if (*zop->u.prox->proximityUnitCode != Z_ProxUnit_word)
1509 char *val = odr_malloc (stream, 16);
1511 zh->errString = val;
1512 sprintf (val, "%d", *zop->u.prox->proximityUnitCode);
1516 r = rpn_proximity (zh, bool_parms.rset_l, bool_parms.rset_r,
1517 *zop->u.prox->ordered,
1518 (!zop->u.prox->exclusion ? 0 :
1519 *zop->u.prox->exclusion),
1520 *zop->u.prox->relationType,
1521 *zop->u.prox->distance);
1528 else if (zs->which == Z_RPNStructure_simple)
1530 if (zs->u.simple->which == Z_Operand_APT)
1532 logf (LOG_DEBUG, "rpn_search_APT");
1533 r = rpn_search_APT (zh, zs->u.simple->u.attributesPlusTerm,
1534 attributeSet, num_bases, basenames);
1536 else if (zs->u.simple->which == Z_Operand_resultSetId)
1538 logf (LOG_DEBUG, "rpn_search_ref");
1539 r = resultSetRef (zh, zs->u.simple->u.resultSetId);
1541 r = rset_create (rset_kind_null, NULL);
1557 void rpn_search (ZebraHandle zh, ODR stream,
1558 Z_RPNQuery *rpn, int num_bases, char **basenames,
1559 const char *setname)
1563 oid_value attributeSet;
1568 zh->errString = NULL;
1571 attrset = oid_getentbyoid (rpn->attributeSetId);
1572 attributeSet = attrset->value;
1573 rset = rpn_search_structure (zh, rpn->RPNStructure, attributeSet, stream,
1574 num_bases, basenames);
1578 resultSetAdd (zh, setname, 1, rset, &zh->hits);
1580 logf (LOG_DEBUG, "search error: %d", zh->errCode);
1583 struct scan_info_entry {
1589 struct scan_info_entry *list;
1595 static int scan_handle (char *name, const char *info, int pos, void *client)
1597 int len_prefix, idx;
1598 struct scan_info *scan_info = client;
1600 len_prefix = strlen(scan_info->prefix);
1601 if (memcmp (name, scan_info->prefix, len_prefix))
1604 idx = scan_info->after - pos + scan_info->before;
1607 scan_info->list[idx].term = odr_malloc (scan_info->odr,
1608 strlen(name + len_prefix)+1);
1609 strcpy (scan_info->list[idx].term, name + len_prefix);
1610 assert (*info == sizeof(ISAM_P));
1611 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAM_P));
1615 static void scan_term_untrans (ZebraHandle zh, ODR stream, int reg_type,
1616 char **dstp, const char *src)
1618 char term_dst[1024], **dst;
1620 term_untrans (zh, reg_type, term_dst, src);
1622 *dst = odr_malloc (stream, strlen(term_dst)+1);
1623 strcpy (*dst, term_dst);
1626 static void count_set (RSET r, int *count)
1634 logf (LOG_DEBUG, "count_set");
1637 rfd = rset_open (r, RSETF_READ);
1638 while (rset_read (r, rfd, &key, &term_index))
1640 if (key.sysno != psysno)
1647 rset_close (r, rfd);
1648 logf (LOG_DEBUG, "%d keys, %d records", kno, *count);
1651 void rpn_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
1652 oid_value attributeset,
1653 int num_bases, char **basenames,
1654 int *position, int *num_entries, ZebraScanEntry **list,
1658 int pos = *position;
1659 int num = *num_entries;
1663 char termz[IT_MAX_WORD+20];
1666 struct scan_info *scan_info_array;
1667 ZebraScanEntry *glist;
1668 int ords[32], ord_no = 0;
1672 char *search_type = NULL;
1673 char *rank_type = NULL;
1676 if (attributeset == VAL_NONE)
1677 attributeset = VAL_BIB1;
1679 zlog_scan (zapt, attributeset);
1680 logf (LOG_DEBUG, "position = %d, num = %d", pos, num);
1682 attr_init (&use, zapt, 1);
1683 use_value = attr_find (&use, &attributeset);
1685 if (zebra_maps_attr (zh->zebra_maps, zapt, ®_id, &search_type,
1686 &rank_type, &complete_flag))
1692 if (use_value == -1)
1694 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
1698 data1_local_attribute *local_attr;
1700 if ((r=att_getentbyatt (zh, &attp, attributeset, use_value)))
1702 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
1703 attributeset, use_value);
1709 if (zebraExplain_curDatabase (zh->zei, basenames[base_no]))
1711 zh->errString = basenames[base_no];
1712 zh->errCode = 109; /* Database unavailable */
1715 for (local_attr = attp.local_attributes; local_attr && ord_no < 32;
1716 local_attr = local_attr->next)
1720 ord = zebraExplain_lookupSU (zh->zei, attp.attset_ordinal,
1723 ords[ord_no++] = ord;
1733 scan_info_array = odr_malloc (stream, ord_no * sizeof(*scan_info_array));
1734 for (i = 0; i < ord_no; i++)
1736 int j, prefix_len = 0;
1737 int before_tmp = before, after_tmp = after;
1738 struct scan_info *scan_info = scan_info_array + i;
1739 struct rpn_char_map_info rcmi;
1741 rpn_char_map_prepare (zh, reg_id, &rcmi);
1743 scan_info->before = before;
1744 scan_info->after = after;
1745 scan_info->odr = stream;
1747 scan_info->list = odr_malloc (stream, (before+after)*
1748 sizeof(*scan_info->list));
1749 for (j = 0; j<before+after; j++)
1750 scan_info->list[j].term = NULL;
1751 termz[prefix_len++] = ords[i];
1752 termz[prefix_len++] = reg_id;
1753 termz[prefix_len] = 0;
1754 strcpy (scan_info->prefix, termz);
1756 trans_scan_term (zh, zapt, termz+prefix_len, reg_id);
1758 dict_scan (zh->dict, termz, &before_tmp, &after_tmp, scan_info,
1761 glist = odr_malloc (stream, (before+after)*sizeof(*glist));
1762 for (i = 0; i < ord_no; i++)
1766 for (i = 0; i<after; i++)
1769 const char *mterm = NULL;
1773 for (j = 0; j < ord_no; j++)
1775 if (ptr[j] < before+after &&
1776 (tst=scan_info_array[j].list[ptr[j]].term) &&
1777 (!mterm || strcmp (tst, mterm) < 0))
1785 scan_term_untrans (zh, stream, reg_id,
1786 &glist[i+before].term, mterm);
1787 rset = rset_trunc (zh, &scan_info_array[j0].list[ptr[j0]].isam_p, 1,
1788 glist[i+before].term, strlen(glist[i+before].term),
1792 for (j = j0+1; j<ord_no; j++)
1794 if (ptr[j] < before+after &&
1795 (tst=scan_info_array[j].list[ptr[j]].term) &&
1796 !strcmp (tst, mterm))
1798 rset_bool_parms bool_parms;
1802 rset_trunc (zh, &scan_info_array[j].list[ptr[j]].isam_p, 1,
1803 glist[i+before].term,
1804 strlen(glist[i+before].term), NULL);
1806 bool_parms.key_size = sizeof(struct it_key);
1807 bool_parms.cmp = key_compare_it;
1808 bool_parms.rset_l = rset;
1809 bool_parms.rset_r = rset2;
1811 rset = rset_create (rset_kind_or, &bool_parms);
1816 count_set (rset, &glist[i+before].occurrences);
1821 *num_entries -= (after-i);
1825 for (i = 0; i<ord_no; i++)
1828 for (i = 0; i<before; i++)
1831 const char *mterm = NULL;
1835 for (j = 0; j <ord_no; j++)
1837 if (ptr[j] < before &&
1838 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
1839 (!mterm || strcmp (tst, mterm) > 0))
1848 scan_term_untrans (zh, stream, reg_id,
1849 &glist[before-1-i].term, mterm);
1852 (zh, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1,
1853 glist[before-1-i].term, strlen(glist[before-1-i].term),
1858 for (j = j0+1; j<ord_no; j++)
1860 if (ptr[j] < before &&
1861 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
1862 !strcmp (tst, mterm))
1864 rset_bool_parms bool_parms;
1867 rset2 = rset_trunc (zh,
1868 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1,
1869 glist[before-1-i].term,
1870 strlen(glist[before-1-i].term), NULL);
1872 bool_parms.key_size = sizeof(struct it_key);
1873 bool_parms.cmp = key_compare_it;
1874 bool_parms.rset_l = rset;
1875 bool_parms.rset_r = rset2;
1877 rset = rset_create (rset_kind_or, &bool_parms);
1882 count_set (rset, &glist[before-1-i].occurrences);
1892 *list = glist + i; /* list is set to first 'real' entry */
1894 logf (LOG_DEBUG, "position = %d, num_entries = %d",
1895 *position, *num_entries);
1897 logf (LOG_DEBUG, "scan error: %d", zh->errCode);