1 /* This file is part of the Zebra server.
2 Copyright (C) 1994-2009 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
25 #include <idzebra/util.h>
32 static RSFD r_open (RSET ct, int flag);
33 static void r_close (RSFD rfd);
34 static void r_delete (RSET ct);
35 static int r_forward(RSFD rfd, void *buf, TERMID *term, const void *untilbuf);
36 static int r_read (RSFD rfd, void *buf, TERMID *term);
37 static int r_write (RSFD rfd, const void *buf);
38 static void r_pos (RSFD rfd, double *current, double *total);
39 static void r_get_terms(RSET ct, TERMID *terms, int maxterms, int *curterm);
41 static const struct rset_control control =
54 struct rset_prox_info {
61 struct rset_prox_rfd {
63 char **buf; /* lookahead key buffers */
64 char *more; /* more in each lookahead? */
65 TERMID *terms; /* lookahead terms */
70 RSET rset_create_prox(NMEM nmem, struct rset_key_control *kcontrol,
72 int rset_no, RSET *rset,
73 int ordered, int exclusion,
74 int relation, int distance)
76 RSET rnew = rset_create_base(&control, nmem, kcontrol, scope, 0,
78 struct rset_prox_info *info;
79 info = (struct rset_prox_info *) nmem_malloc(rnew->nmem,sizeof(*info));
80 info->ordered = ordered;
81 info->exclusion = exclusion;
82 info->relation = relation;
83 info->distance = distance;
88 static void r_delete (RSET ct)
92 static RSFD r_open (RSET ct, int flag)
95 struct rset_prox_rfd *p;
98 if (flag & RSETF_WRITE)
100 yaz_log(YLOG_FATAL, "prox set type is read-only");
103 rfd = rfd_create_base(ct);
105 p = (struct rset_prox_rfd *)(rfd->priv);
107 p = (struct rset_prox_rfd *) nmem_malloc(ct->nmem,sizeof(*p));
109 p->more = nmem_malloc (ct->nmem,sizeof(*p->more) * ct->no_children);
110 p->buf = nmem_malloc(ct->nmem,sizeof(*p->buf) * ct->no_children);
111 p->terms = nmem_malloc(ct->nmem,sizeof(*p->terms) * ct->no_children);
112 for (i = 0; i < ct->no_children; i++)
114 p->buf[i] = nmem_malloc(ct->nmem,ct->keycontrol->key_size);
117 p->rfd = nmem_malloc(ct->nmem,sizeof(*p->rfd) * ct->no_children);
119 yaz_log(YLOG_DEBUG,"rsprox (%s) open [%p] n=%d",
120 ct->control->desc, rfd, ct->no_children);
122 for (i = 0; i < ct->no_children; i++) {
123 p->rfd[i] = rset_open (ct->children[i], RSETF_READ);
124 p->more[i] = rset_read (p->rfd[i], p->buf[i], &p->terms[i]);
130 static void r_close (RSFD rfd)
133 struct rset_prox_rfd *p = (struct rset_prox_rfd *)(rfd->priv);
136 for (i = 0; i<ct->no_children; i++)
137 rset_close(p->rfd[i]);
140 static int r_forward(RSFD rfd, void *buf, TERMID *term, const void *untilbuf)
143 struct rset_prox_info *info = (struct rset_prox_info *)(ct->priv);
144 struct rset_prox_rfd *p = (struct rset_prox_rfd *)(rfd->priv);
145 const struct rset_key_control *kctrl = ct->keycontrol;
151 /* it is enough to forward first one. Other will follow. */
152 if ( p->more[0] && /* was: cmp >=2 */
153 ((kctrl->cmp)(untilbuf, p->buf[0]) >= rfd->rset->scope) )
154 p->more[0] = rset_forward(p->rfd[0], p->buf[0],
155 &p->terms[0], untilbuf);
157 if (info->ordered && info->relation <= 3 && info->exclusion == 0)
161 for (i = 1; i < ct->no_children; i++)
165 p->more[0] = 0; /* saves us a goto out of while loop. */
168 cmp = (*kctrl->cmp)(p->buf[i], p->buf[i-1]);
169 if (cmp >= rfd->rset->scope) /* not same record */
171 p->more[i-1] = rset_forward (p->rfd[i-1],
177 else if (cmp > 0) /* within record and ordered */
179 zint diff = (*kctrl->getseq)(p->buf[i]) -
180 (*kctrl->getseq)(p->buf[i-1]);
181 if (info->relation == 3 && diff == info->distance)
183 else if (info->relation == 2 && diff <= info->distance)
185 else if (info->relation == 1 && diff < info->distance)
188 p->more[i-1] = rset_read(p->rfd[i-1], p->buf[i-1],
192 else /* within record - wrong order */
194 p->more[i] = rset_forward(p->rfd[i], p->buf[i],
195 &p->terms[i], p->buf[i-1]);
199 if (i == ct->no_children)
201 i = ct->no_children-1;
202 memcpy(buf, p->buf[i], kctrl->key_size);
205 p->more[i] = rset_read(p->rfd[i], p->buf[i], &p->terms[i]);
211 else if (ct->no_children == 2)
213 while (p->more[0] && p->more[1])
215 int cmp = (*kctrl->cmp)(p->buf[0], p->buf[1]);
216 if ( cmp <= - rfd->rset->scope) /* cmp<-1*/
217 p->more[0] = rset_forward (p->rfd[0], p->buf[0],
218 &p->terms[0],p->buf[1]);
219 else if ( cmp >= rfd->rset->scope ) /* cmp>1 */
220 p->more[1] = rset_forward (p->rfd[1], p->buf[1],
221 &p->terms[1],p->buf[0]);
224 zint seqno[500]; /* FIXME - why 500 ?? */
227 seqno[n++] = (*kctrl->getseq)(p->buf[0]);
228 while ((p->more[0] = rset_read (p->rfd[0],
229 p->buf[0], &p->terms[0])))
231 cmp = (*kctrl->cmp)(p->buf[0], p->buf[1]);
232 if (cmp <= - rfd->rset->scope || cmp >= rfd->rset->scope)
235 seqno[n++] = (*kctrl->getseq)(p->buf[0]);
237 /* set up return buffer.. (save buf[1]) */
238 memcpy(buf, p->buf[1], kctrl->key_size);
243 for (i = 0; i < n; i++)
245 zint diff = (*kctrl->getseq)(p->buf[1]) - seqno[i];
246 int excl = info->exclusion;
247 if (!info->ordered && diff < 0)
249 switch (info->relation)
252 if (diff < info->distance && diff >= 0)
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)
278 p->more[1] = rset_read ( p->rfd[1], p->buf[1],
284 p->more[1] = rset_read(p->rfd[1], p->buf[1], &p->terms[1]);
287 cmp = (*kctrl->cmp)(buf, p->buf[1]);
288 if (cmp <= - rfd->rset->scope || cmp >= rfd->rset->scope)
298 static int r_read (RSFD rfd, void *buf, TERMID *term)
300 return r_forward(rfd, buf, term, 0);
303 static int r_write (RSFD rfd, const void *buf)
305 yaz_log(YLOG_FATAL, "prox set type is read-only");
309 static void r_pos (RSFD rfd, double *current, double *total)
312 struct rset_prox_rfd *p = (struct rset_prox_rfd *)(rfd->priv);
315 double cur, tot = -1.0;
316 double scur = 0.0, stot = 0.0;
318 yaz_log(YLOG_DEBUG, "rsprox_pos");
320 for (i = 0; i < ct->no_children; i++)
322 rset_pos(p->rfd[i], &cur, &tot);
328 if (tot <0) { /* nothing found */
331 } else if (tot < 1) { /* most likely tot==0 */
336 *current = (double) p->hits;
339 yaz_log(YLOG_DEBUG,"prox_pos: [%d] %0.1f/%0.1f= %0.4f ",
340 i,*current, *total, r);
343 static void r_get_terms(RSET ct, TERMID *terms, int maxterms, int *curterm)
346 for (i = 0; i<ct->no_children; i++)
347 rset_getterms(ct->children[i], terms, maxterms, curterm);
353 * c-file-style: "Stroustrup"
354 * indent-tabs-mode: nil
356 * vim: shiftwidth=4 tabstop=8 expandtab