1 /* $Id: rsbetween.c,v 1.20 2004-08-24 14:25:16 heikki Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 /* rsbetween is (mostly) used for xml searches. It returns the hits of the
25 * "middle" rset, that are in between the "left" and "right" rsets. For
26 * example "Shakespeare" in between "<title>" and </title>. The thing is
27 * complicated by the inclusion of attributes (from their own rset). If attrs
28 * specified, they must match the "left" rset (start tag). "Hamlet" between
29 * "<title lang=eng>" and "</title>". (This assumes that the attributes are
30 * indexed to the same seqno as the tags).
39 #include <rsbetween.h>
41 #define RSBETWEEN_DEBUG 0
43 static RSFD r_open_between (RSET ct, int flag);
44 static void r_close_between (RSFD rfd);
45 static void r_delete_between (RSET ct);
46 static void r_rewind_between (RSFD rfd);
47 static int r_forward_between(RSET ct, RSFD rfd, void *buf,
48 int (*cmpfunc)(const void *p1, const void *p2),
49 const void *untilbuf);
50 static int r_read_between (RSFD rfd, void *buf);
51 static int r_write_between (RSFD rfd, const void *buf);
52 static void r_pos_between (RSFD rfd, double *current, double *total);
54 static const struct rset_control control =
61 r_forward_between, /* rset_default_forward, */
68 const struct rset_control *rset_kind_between = &control;
70 struct rset_between_info {
76 int (*cmp)(const void *p1, const void *p2);
77 char *(*printer)(const void *p1, char *buf);
78 struct rset_between_rfd *rfd_list;
81 struct rset_between_rfd {
95 struct rset_between_rfd *next;
96 struct rset_between_info *info;
101 static void log2 (struct rset_between_rfd *p, char *msg, int cmp_l, int cmp_r)
106 logf(LOG_DEBUG,"btw: %s l=%s(%d/%d) m=%s(%d) r=%s(%d/%d), lev=%d",
108 (*p->info->printer)(p->buf_l, buf_l), p->more_l, cmp_l,
109 (*p->info->printer)(p->buf_m, buf_m), p->more_m,
110 (*p->info->printer)(p->buf_r, buf_r), p->more_r, cmp_r,
115 RSET rsbetween_create( NMEM nmem, int key_size,
116 int (*cmp)(const void *p1, const void *p2),
117 RSET rset_l, RSET rset_m, RSET rset_r, RSET rset_attr,
118 char *(*printer)(const void *p1, char *buf) )
120 RSET rnew=rset_create_base(&control, nmem);
121 struct rset_between_info *info;
122 info = (struct rset_between_info *) nmem_malloc(rnew->nmem,sizeof(*info));
123 info->key_size = key_size;
124 info->rset_l = rset_l;
125 info->rset_m = rset_m;
126 info->rset_r = rset_r;
127 info->rset_attr = rset_attr;
129 info->printer = printer;
130 info->rfd_list = NULL;
137 static void *r_create_between (RSET ct, const struct rset_control *sel,
140 rset_between_parms *between_parms = (rset_between_parms *) parms;
141 struct rset_between_info *info;
143 info = (struct rset_between_info *) xmalloc (sizeof(*info));
144 info->key_size = between_parms->key_size;
145 info->rset_l = between_parms->rset_l;
146 info->rset_m = between_parms->rset_m;
147 info->rset_r = between_parms->rset_r;
148 info->rset_attr = between_parms->rset_attr;
149 info->cmp = between_parms->cmp;
150 info->printer = between_parms->printer;
151 info->rfd_list = NULL;
157 static void r_delete_between (RSET ct)
159 struct rset_between_info *info = (struct rset_between_info *) ct->priv;
161 assert (info->rfd_list == NULL);
162 rset_delete (info->rset_l);
163 rset_delete (info->rset_m);
164 rset_delete (info->rset_r);
166 rset_delete (info->rset_attr);
171 static RSFD r_open_between (RSET ct, int flag)
173 struct rset_between_info *info = (struct rset_between_info *) ct->priv;
174 struct rset_between_rfd *rfd;
176 if (flag & RSETF_WRITE)
178 logf (LOG_FATAL, "between set type is read-only");
181 rfd = (struct rset_between_rfd *) xmalloc (sizeof(*rfd));
182 rfd->next = info->rfd_list;
183 info->rfd_list = rfd;
186 rfd->buf_l = xmalloc (info->key_size);
187 rfd->buf_m = xmalloc (info->key_size);
188 rfd->buf_r = xmalloc (info->key_size);
189 rfd->buf_attr = xmalloc (info->key_size);
191 rfd->rfd_l = rset_open (info->rset_l, RSETF_READ);
192 rfd->rfd_m = rset_open (info->rset_m, RSETF_READ);
193 rfd->rfd_r = rset_open (info->rset_r, RSETF_READ);
195 rfd->more_l = rset_read (info->rset_l, rfd->rfd_l, rfd->buf_l);
196 rfd->more_m = rset_read (info->rset_m, rfd->rfd_m, rfd->buf_m);
197 rfd->more_r = rset_read (info->rset_r, rfd->rfd_r, rfd->buf_r);
200 rfd->rfd_attr = rset_open (info->rset_attr, RSETF_READ);
201 rfd->more_attr = rset_read (info->rset_attr, rfd->rfd_attr,
209 static void r_close_between (RSFD rfd)
211 struct rset_between_info *info = ((struct rset_between_rfd*)rfd)->info;
212 struct rset_between_rfd **rfdp;
214 for (rfdp = &info->rfd_list; *rfdp; rfdp = &(*rfdp)->next)
217 xfree ((*rfdp)->buf_l);
218 xfree ((*rfdp)->buf_m);
219 xfree ((*rfdp)->buf_r);
220 xfree ((*rfdp)->buf_attr);
221 rset_close (info->rset_l, (*rfdp)->rfd_l);
222 rset_close (info->rset_m, (*rfdp)->rfd_m);
223 rset_close (info->rset_r, (*rfdp)->rfd_r);
225 rset_close (info->rset_attr, (*rfdp)->rfd_attr);
227 *rfdp = (*rfdp)->next;
231 logf (LOG_FATAL, "r_close_between but no rfd match!");
235 static void r_rewind_between (RSFD rfd)
237 struct rset_between_info *info = ((struct rset_between_rfd*)rfd)->info;
238 struct rset_between_rfd *p = (struct rset_between_rfd *) rfd;
241 logf (LOG_DEBUG, "rsbetween_rewind");
243 rset_rewind (info->rset_l, p->rfd_l);
244 rset_rewind (info->rset_m, p->rfd_m);
245 rset_rewind (info->rset_r, p->rfd_r);
246 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l);
247 p->more_m = rset_read (info->rset_m, p->rfd_m, p->buf_m);
248 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r);
251 rset_rewind (info->rset_attr, p->rfd_attr);
252 p->more_attr = rset_read (info->rset_attr, p->rfd_attr, p->buf_attr);
260 static int r_forward_between(RSET ct, RSFD rfd, void *buf,
261 int (*cmpfunc)(const void *p1, const void *p2),
262 const void *untilbuf)
264 struct rset_between_info *info = ((struct rset_between_rfd*)rfd)->info;
265 struct rset_between_rfd *p = (struct rset_between_rfd *) rfd;
268 log2( p, "fwd: before forward", 0,0);
270 /* It is enough to forward the m pointer here, the read will */
271 /* naturally forward the l, m, and attr pointers */
273 p->more_m=rset_forward(info->rset_m,p->rfd_m, p->buf_m,
276 log2( p, "fwd: after forward M", 0,0);
278 rc = r_read_between(rfd, buf);
280 log2( p, "fwd: after forward", 0,0);
288 static int r_read_between (RSFD rfd, void *buf)
290 struct rset_between_rfd *p = (struct rset_between_rfd *) rfd;
291 struct rset_between_info *info = p->info;
299 log2( p, "start of loop", cmp_l, cmp_r);
302 /* forward L until past m, count levels, note rec boundaries */
304 cmp_l= (*info->cmp)(p->buf_l, p->buf_m);
308 cmp_l=2; /* past this record */
311 log2( p, "after first L", cmp_l, cmp_r);
314 while (cmp_l < 0) /* l before m */
317 p->level=0; /* earlier record */
320 p->level++; /* relevant start tag */
322 if (!info->rset_attr)
330 cmp_attr = (*info->cmp)(p->buf_attr, p->buf_l);
336 else if (cmp_attr > 0)
338 else if (cmp_attr==-1)
339 p->more_attr = rset_read (info->rset_attr,
340 p->rfd_attr, p->buf_attr);
341 /* if we had a forward that went all the way to
342 * the seqno, we could use that. But fwd only goes
344 else if (cmp_attr==-2)
346 p->more_attr = rset_forward(
347 info->rset_attr, p->rfd_attr,
348 p->buf_attr, info->cmp, p->buf_l);
350 logf(LOG_DEBUG, "btw: after frowarding attr m=%d",p->more_attr);
353 } /* while more_attr */
362 p->more_l=rset_forward( info->rset_l, p->rfd_l,
363 p->buf_l, info->cmp, p->buf_m);
365 cmp_l= (*info->cmp)(p->buf_l, p->buf_m);
369 log2( p, "after forwarding L", cmp_l, cmp_r);
374 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l);
377 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l);
381 cmp_l= (*info->cmp)(p->buf_l, p->buf_m);
386 log2( p, "end of L loop", cmp_l, cmp_r);
391 /* forward R until past m, count levels */
393 log2( p, "Before moving R", cmp_l, cmp_r);
396 cmp_r= (*info->cmp)(p->buf_r, p->buf_m);
400 log2( p, "after first R", cmp_l, cmp_r);
402 while (cmp_r < 0) /* r before m */
404 /* -2, earlier record, don't count level */
406 p->level--; /* relevant end tag */
412 p->more_r=rset_forward( info->rset_r, p->rfd_r,
413 p->buf_r, info->cmp, p->buf_m);
416 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r);
419 cmp_r= (*info->cmp)(p->buf_r, p->buf_m);
422 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r);
423 cmp_r= (*info->cmp)(p->buf_r, p->buf_m);
429 log2( p, "End of R loop", cmp_l, cmp_r);
433 if ( ( p->level <= 0 ) && ! p->more_l)
434 return 0; /* no more start tags, nothing more to find */
436 if ( attr_match && p->level > 0) /* within a tag pair (or deeper) */
438 memcpy (buf, p->buf_m, info->key_size);
440 log2( p, "Returning a hit (and forwarding m)", cmp_l, cmp_r);
442 p->more_m = rset_read (info->rset_m, p->rfd_m, p->buf_m);
448 else if ( ! p->more_l ) /* not in data, no more starts */
451 log2( p, "no more starts, exiting without a hit", cmp_l, cmp_r);
453 return 0; /* ergo, nothing can be found. stop scanning */
459 p->more_m=rset_forward( info->rset_m, p->rfd_m,
460 p->buf_m, info->cmp, p->buf_l);
463 p->more_m = rset_read (info->rset_m, p->rfd_m, p->buf_m);
468 p->more_m = rset_read (info->rset_m, p->rfd_m, p->buf_m);
471 log2( p, "End of M loop", cmp_l, cmp_r);
476 log2( p, "Exiting, nothing more in m", cmp_l, cmp_r);
478 return 0; /* no more data possible */
484 static int r_write_between (RSFD rfd, const void *buf)
486 logf (LOG_FATAL, "between set type is read-only");
491 static void r_pos_between (RSFD rfd, double *current, double *total)
493 struct rset_between_rfd *p = (struct rset_between_rfd *) rfd;
494 struct rset_between_info *info = p->info;
500 rset_pos(info->rset_l, p->rfd_l, &lcur, <ot);
501 rset_pos(info->rset_m, p->rfd_m, &mcur, &mtot);
502 rset_pos(info->rset_r, p->rfd_r, &rcur, &rtot);
503 if ( (ltot<0) && (mtot<0) && (rtot<0) ) { /*no position */
504 *current=mcur; /* return same as you got */
505 *total=mtot; /* probably -1 for not available */
507 if ( ltot<0) { ltot=0; lcur=0;} /* if only one useful, use it */
508 if ( mtot<0) { mtot=0; mcur=0;}
509 if ( rtot<0) { rtot=0; rcur=0;}
510 if ( ltot+mtot+rtot < 1 ) { /* empty rset */
515 r=1.0*(lcur+mcur+rcur)/(ltot+mtot+rtot); /* weighed average of l and r */
519 yaz_log(LOG_DEBUG,"betw_pos: (%s/%s) %0.1f/%0.1f= %0.4f ",
520 info->rset_l->control->desc, info->rset_r->control->desc,
521 *current, *total, r);