1 /* This file is part of the Zebra server.
2 Copyright (C) 1994-2011 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 int r_write(RSFD rfd, const void *buf);
41 static void r_pos(RSFD rfd, double *current, double *total);
42 static void r_get_terms(RSET ct, TERMID *terms, int maxterms, int *curterm);
44 static const struct rset_control control =
57 struct rset_prox_info {
64 struct rset_prox_rfd {
66 char **buf; /* lookahead key buffers */
67 char *more; /* more in each lookahead? */
68 TERMID *terms; /* lookahead terms */
73 RSET rset_create_prox(NMEM nmem, struct rset_key_control *kcontrol,
75 int rset_no, RSET *rset,
76 int ordered, int exclusion,
77 int relation, int distance)
79 RSET rnew = rset_create_base(&control, nmem, kcontrol, scope, 0,
81 struct rset_prox_info *info;
82 info = (struct rset_prox_info *) nmem_malloc(rnew->nmem,sizeof(*info));
83 info->ordered = ordered;
84 info->exclusion = exclusion;
85 info->relation = relation;
86 info->distance = distance;
91 static void r_delete(RSET ct)
95 static RSFD r_open(RSET ct, int flag)
98 struct rset_prox_rfd *p;
101 if (flag & RSETF_WRITE)
103 yaz_log(YLOG_FATAL, "prox set type is read-only");
106 rfd = rfd_create_base(ct);
108 p = (struct rset_prox_rfd *)(rfd->priv);
111 p = (struct rset_prox_rfd *) nmem_malloc(ct->nmem,sizeof(*p));
113 p->more = nmem_malloc(ct->nmem,sizeof(*p->more) * ct->no_children);
114 p->buf = nmem_malloc(ct->nmem,sizeof(*p->buf) * ct->no_children);
115 p->terms = nmem_malloc(ct->nmem,sizeof(*p->terms) * ct->no_children);
116 for (i = 0; i < ct->no_children; i++)
118 p->buf[i] = nmem_malloc(ct->nmem,ct->keycontrol->key_size);
121 p->rfd = nmem_malloc(ct->nmem,sizeof(*p->rfd) * ct->no_children);
123 yaz_log(YLOG_DEBUG,"rsprox (%s) open [%p] n=%d",
124 ct->control->desc, rfd, ct->no_children);
126 for (i = 0; i < ct->no_children; i++)
128 p->rfd[i] = rset_open(ct->children[i], RSETF_READ);
129 p->more[i] = rset_read(p->rfd[i], p->buf[i], &p->terms[i]);
135 static void r_close(RSFD rfd)
138 struct rset_prox_rfd *p = (struct rset_prox_rfd *)(rfd->priv);
141 for (i = 0; i < ct->no_children; i++)
142 rset_close(p->rfd[i]);
145 static int r_forward(RSFD rfd, void *buf, TERMID *term, const void *untilbuf)
148 struct rset_prox_info *info = (struct rset_prox_info *)(ct->priv);
149 struct rset_prox_rfd *p = (struct rset_prox_rfd *)(rfd->priv);
150 const struct rset_key_control *kctrl = ct->keycontrol;
156 /* it is enough to forward first one. Other will follow. */
157 if (p->more[0] && /* was: cmp >=2 */
158 ((kctrl->cmp)(untilbuf, p->buf[0]) >= rfd->rset->scope) )
159 p->more[0] = rset_forward(p->rfd[0], p->buf[0],
160 &p->terms[0], untilbuf);
162 if (info->ordered && info->relation <= 3 && info->exclusion == 0)
166 for (i = 1; i < ct->no_children; i++)
170 p->more[0] = 0; /* saves us a goto out of while loop. */
173 cmp = (*kctrl->cmp)(p->buf[i], p->buf[i-1]);
174 if (cmp >= rfd->rset->scope) /* not same record */
176 p->more[i-1] = rset_forward(p->rfd[i-1],
182 else if (cmp > 0) /* within record and ordered */
184 zint diff = (*kctrl->getseq)(p->buf[i]) -
185 (*kctrl->getseq)(p->buf[i-1]);
186 if (info->relation == 3 && diff == info->distance)
188 else if (info->relation == 2 && diff <= info->distance)
190 else if (info->relation == 1 && diff < info->distance)
193 p->more[i-1] = rset_read(p->rfd[i-1], p->buf[i-1],
197 else /* within record - wrong order */
199 p->more[i] = rset_forward(p->rfd[i], p->buf[i],
200 &p->terms[i], p->buf[i-1]);
204 if (i == ct->no_children)
206 i = ct->no_children-1;
207 memcpy(buf, p->buf[i], kctrl->key_size);
210 p->more[i] = rset_read(p->rfd[i], p->buf[i], &p->terms[i]);
216 else if (ct->no_children == 2)
218 while (p->more[0] && p->more[1])
220 int cmp = (*kctrl->cmp)(p->buf[0], p->buf[1]);
221 if ( cmp <= - rfd->rset->scope) /* cmp<-1*/
222 p->more[0] = rset_forward(p->rfd[0], p->buf[0],
223 &p->terms[0],p->buf[1]);
224 else if ( cmp >= rfd->rset->scope ) /* cmp>1 */
225 p->more[1] = rset_forward(p->rfd[1], p->buf[1],
226 &p->terms[1],p->buf[0]);
229 zint seqno[500]; /* FIXME - why 500 ?? */
232 seqno[n++] = (*kctrl->getseq)(p->buf[0]);
233 while ((p->more[0] = rset_read(p->rfd[0],
234 p->buf[0], &p->terms[0])))
236 cmp = (*kctrl->cmp)(p->buf[0], p->buf[1]);
237 if (cmp <= - rfd->rset->scope || cmp >= rfd->rset->scope)
240 seqno[n++] = (*kctrl->getseq)(p->buf[0]);
242 /* set up return buffer.. (save buf[1]) */
243 memcpy(buf, p->buf[1], kctrl->key_size);
248 for (i = 0; i < n; i++)
250 zint diff = (*kctrl->getseq)(p->buf[1]) - seqno[i];
251 int excl = info->exclusion;
252 if (!info->ordered && diff < 0)
254 switch (info->relation)
257 if (diff < info->distance && diff >= 0)
261 if (diff <= info->distance && diff >= 0)
265 if (diff == info->distance && diff >= 0)
269 if (diff >= info->distance && diff >= 0)
273 if (diff > info->distance && diff >= 0)
277 if (diff != info->distance && diff >= 0)
283 p->more[1] = rset_read( p->rfd[1], p->buf[1],
289 p->more[1] = rset_read(p->rfd[1], p->buf[1], &p->terms[1]);
292 cmp = (*kctrl->cmp)(buf, p->buf[1]);
293 if (cmp <= - rfd->rset->scope || cmp >= rfd->rset->scope)
303 static int r_read(RSFD rfd, void *buf, TERMID *term)
305 return r_forward(rfd, buf, term, 0);
308 static int r_write(RSFD rfd, const void *buf)
310 yaz_log(YLOG_FATAL, "prox set type is read-only");
314 static void r_pos(RSFD rfd, double *current, double *total)
317 struct rset_prox_rfd *p = (struct rset_prox_rfd *)(rfd->priv);
321 for (i = 0; i < ct->no_children; i++)
324 rset_pos(p->rfd[i], &cur, &tot);
327 double nratio = cur / tot;
332 *current = (double) p->hits;
334 *total = *current/ratio;
338 yaz_log(YLOG_DEBUG, "prox_pos: [%d] %0.1f/%0.1f= %0.4f ",
339 i, *current, *total, ratio);
342 static void r_get_terms(RSET ct, TERMID *terms, int maxterms, int *curterm)
345 for (i = 0; i < ct->no_children; i++)
346 rset_getterms(ct->children[i], terms, maxterms, curterm);
352 * c-file-style: "Stroustrup"
353 * indent-tabs-mode: nil
355 * vim: shiftwidth=4 tabstop=8 expandtab