1 /* This file is part of the Zebra server.
2 Copyright (C) Index Data
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 #include <idzebra/util.h>
35 static RSFD r_open(RSET ct, int flag);
36 static void r_close(RSFD rfd);
37 static void r_delete(RSET ct);
38 static int r_forward(RSFD rfd, void *buf, TERMID *term, const void *untilbuf);
39 static int r_read(RSFD rfd, void *buf, TERMID *term);
40 static void r_pos(RSFD rfd, double *current, double *total);
41 static void r_get_terms(RSET ct, TERMID *terms, int maxterms, int *curterm);
43 static const struct rset_control control =
56 struct rset_prox_info {
63 struct rset_prox_rfd {
65 char **buf; /* lookahead key buffers */
66 char *more; /* more in each lookahead? */
67 TERMID *terms; /* lookahead terms */
72 RSET rset_create_prox(NMEM nmem, struct rset_key_control *kcontrol,
74 int rset_no, RSET *rset,
75 int ordered, int exclusion,
76 int relation, int distance)
78 RSET rnew = rset_create_base(&control, nmem, kcontrol, scope, 0,
80 struct rset_prox_info *info;
81 info = (struct rset_prox_info *) nmem_malloc(rnew->nmem,sizeof(*info));
82 info->ordered = ordered;
83 info->exclusion = exclusion;
84 info->relation = relation;
85 info->distance = distance;
90 static void r_delete(RSET ct)
94 static RSFD r_open(RSET ct, int flag)
97 struct rset_prox_rfd *p;
100 if (flag & RSETF_WRITE)
102 yaz_log(YLOG_FATAL, "prox set type is read-only");
105 rfd = rfd_create_base(ct);
107 p = (struct rset_prox_rfd *)(rfd->priv);
110 p = (struct rset_prox_rfd *) nmem_malloc(ct->nmem,sizeof(*p));
112 p->more = nmem_malloc(ct->nmem,sizeof(*p->more) * ct->no_children);
113 p->buf = nmem_malloc(ct->nmem,sizeof(*p->buf) * ct->no_children);
114 p->terms = nmem_malloc(ct->nmem,sizeof(*p->terms) * ct->no_children);
115 for (i = 0; i < ct->no_children; i++)
117 p->buf[i] = nmem_malloc(ct->nmem,ct->keycontrol->key_size);
120 p->rfd = nmem_malloc(ct->nmem,sizeof(*p->rfd) * ct->no_children);
122 yaz_log(YLOG_DEBUG,"rsprox (%s) open [%p] n=%d",
123 ct->control->desc, rfd, ct->no_children);
125 for (i = 0; i < ct->no_children; i++)
127 p->rfd[i] = rset_open(ct->children[i], RSETF_READ);
128 p->more[i] = rset_read(p->rfd[i], p->buf[i], &p->terms[i]);
134 static void r_close(RSFD rfd)
137 struct rset_prox_rfd *p = (struct rset_prox_rfd *)(rfd->priv);
140 for (i = 0; i < ct->no_children; i++)
141 rset_close(p->rfd[i]);
144 static int r_forward(RSFD rfd, void *buf, TERMID *term, const void *untilbuf)
147 struct rset_prox_info *info = (struct rset_prox_info *)(ct->priv);
148 struct rset_prox_rfd *p = (struct rset_prox_rfd *)(rfd->priv);
149 const struct rset_key_control *kctrl = ct->keycontrol;
155 /* it is enough to forward first one. Other will follow. */
156 if (p->more[0] && /* was: cmp >=2 */
157 ((kctrl->cmp)(untilbuf, p->buf[0]) >= rfd->rset->scope) )
158 p->more[0] = rset_forward(p->rfd[0], p->buf[0],
159 &p->terms[0], untilbuf);
161 if (info->ordered && info->relation <= 3 && info->exclusion == 0)
165 for (i = 1; i < ct->no_children; i++)
169 p->more[0] = 0; /* saves us a goto out of while loop. */
172 cmp = (*kctrl->cmp)(p->buf[i], p->buf[i-1]);
173 if (cmp >= rfd->rset->scope) /* not same record */
175 p->more[i-1] = rset_forward(p->rfd[i-1],
181 else if (cmp > 0) /* within record and ordered */
183 zint diff = (*kctrl->getseq)(p->buf[i]) -
184 (*kctrl->getseq)(p->buf[i-1]);
185 if (info->relation == 3 && diff == info->distance)
187 else if (info->relation == 2 && diff <= info->distance)
189 else if (info->relation == 1 && diff < info->distance)
192 p->more[i-1] = rset_read(p->rfd[i-1], p->buf[i-1],
196 else /* within record - wrong order */
198 p->more[i] = rset_forward(p->rfd[i], p->buf[i],
199 &p->terms[i], p->buf[i-1]);
203 if (i == ct->no_children)
205 i = ct->no_children-1;
206 memcpy(buf, p->buf[i], kctrl->key_size);
209 p->more[i] = rset_read(p->rfd[i], p->buf[i], &p->terms[i]);
215 else if (ct->no_children == 2)
217 while (p->more[0] && p->more[1])
219 int cmp = (*kctrl->cmp)(p->buf[0], p->buf[1]);
220 if ( cmp <= - rfd->rset->scope) /* cmp<-1*/
221 p->more[0] = rset_forward(p->rfd[0], p->buf[0],
222 &p->terms[0],p->buf[1]);
223 else if ( cmp >= rfd->rset->scope ) /* cmp>1 */
224 p->more[1] = rset_forward(p->rfd[1], p->buf[1],
225 &p->terms[1],p->buf[0]);
228 zint seqno[500]; /* FIXME - why 500 ?? */
231 seqno[n++] = (*kctrl->getseq)(p->buf[0]);
232 while ((p->more[0] = rset_read(p->rfd[0],
233 p->buf[0], &p->terms[0])))
235 cmp = (*kctrl->cmp)(p->buf[0], p->buf[1]);
236 if (cmp <= - rfd->rset->scope || cmp >= rfd->rset->scope)
239 seqno[n++] = (*kctrl->getseq)(p->buf[0]);
241 /* set up return buffer.. (save buf[1]) */
242 memcpy(buf, p->buf[1], kctrl->key_size);
247 for (i = 0; i < n; i++)
249 zint diff = (*kctrl->getseq)(p->buf[1]) - seqno[i];
250 int excl = info->exclusion;
251 if (!info->ordered && diff < 0)
253 switch (info->relation)
256 if (diff < info->distance && diff >= 0)
260 if (diff <= info->distance && diff >= 0)
264 if (diff == info->distance && diff >= 0)
268 if (diff >= info->distance && diff >= 0)
272 if (diff > info->distance && diff >= 0)
276 if (diff != info->distance && diff >= 0)
282 p->more[1] = rset_read( p->rfd[1], p->buf[1],
288 p->more[1] = rset_read(p->rfd[1], p->buf[1], &p->terms[1]);
291 cmp = (*kctrl->cmp)(buf, p->buf[1]);
292 if (cmp <= - rfd->rset->scope || cmp >= rfd->rset->scope)
302 static int r_read(RSFD rfd, void *buf, TERMID *term)
304 return r_forward(rfd, buf, term, 0);
307 static void r_pos(RSFD rfd, double *current, double *total)
310 struct rset_prox_rfd *p = (struct rset_prox_rfd *)(rfd->priv);
314 for (i = 0; i < ct->no_children; i++)
317 rset_pos(p->rfd[i], &cur, &tot);
320 double nratio = cur / tot;
325 *current = (double) p->hits;
327 *total = *current/ratio;
331 yaz_log(YLOG_DEBUG, "prox_pos: [%d] %0.1f/%0.1f= %0.4f ",
332 i, *current, *total, ratio);
335 static void r_get_terms(RSET ct, TERMID *terms, int maxterms, int *curterm)
338 for (i = 0; i < ct->no_children; i++)
339 rset_getterms(ct->children[i], terms, maxterms, curterm);
345 * c-file-style: "Stroustrup"
346 * indent-tabs-mode: nil
348 * vim: shiftwidth=4 tabstop=8 expandtab