1 /* $Id: rsbetween.c,v 1.41 2005-06-07 14:53:39 adam Exp $
2 Copyright (C) 1995-2005
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 "<author>" and </author>. 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 <idzebra/util.h>
43 static RSFD r_open(RSET ct, int flag);
44 static void r_close(RSFD rfd);
45 static void r_delete(RSET ct);
46 static int r_forward(RSFD rfd, void *buf,
47 TERMID *term, const void *untilbuf);
48 static int r_read(RSFD rfd, void *buf, TERMID *term );
49 static int r_write(RSFD rfd, const void *buf);
50 static void r_pos(RSFD rfd, double *current, double *total);
51 static void r_get_terms(RSET ct, TERMID *terms, int maxterms, int *curterm);
53 static const struct rset_control control =
71 struct rset_between_info {
72 TERMID startterm; /* pseudo terms for detecting which one we read from */
77 struct rset_between_rfd {
79 void *recbuf; /* a key that tells which record we are in */
80 void *startbuf; /* the start tag */
81 int startbufok; /* we have seen the first start tag */
82 void *attrbuf; /* the attr tag. If these two match, we have attr match */
83 int attrbufok; /* we have seen the first attr tag, can compare */
84 int depth; /* number of start-tags without end-tags */
85 int attrdepth; /* on what depth the attr matched */
89 static int log_level = 0;
90 static int log_level_initialized = 0;
93 /* make sure that the rset has a term attached. If not, create one */
94 /* we need these terms for the tags, to distinguish what we read */
95 static void checkterm(RSET rs, char *tag, NMEM nmem)
99 rs->term = rset_term_create(tag, strlen(tag), "", 0, nmem, 0, 0);
105 RSET rsbetween_create(NMEM nmem, struct rset_key_control *kcontrol,
107 RSET rset_l, RSET rset_m, RSET rset_r, RSET rset_attr)
109 RSET rnew = rset_create_base(&control, nmem, kcontrol, scope, 0, 0, 0);
110 struct rset_between_info *info=
111 (struct rset_between_info *) nmem_malloc(rnew->nmem,sizeof(*info));
115 if (!log_level_initialized)
117 log_level = yaz_log_module_level("rsbetween");
118 log_level_initialized = 1;
120 rsetarray[STARTTAG] = rset_l;
121 rsetarray[HIT] = rset_m;
122 rsetarray[STOPTAG] = rset_r;
123 rsetarray[ATTRTAG] = rset_attr;
125 /* make sure we have decent terms for all rsets. Create dummies if needed*/
126 checkterm(rsetarray[STARTTAG], "(start)", nmem);
127 checkterm(rsetarray[STOPTAG], "(start)", nmem);
128 info->startterm = rsetarray[STARTTAG]->term;
129 info->stopterm = rsetarray[STOPTAG]->term;
133 checkterm(rsetarray[ATTRTAG], "(start)", nmem);
134 info->attrterm = rsetarray[ATTRTAG]->term;
139 info->attrterm = NULL;
142 rnew->no_children = 1;
143 rnew->children = nmem_malloc(rnew->nmem, sizeof(RSET *));
144 rnew->children[0] = rsmulti_and_create(nmem, kcontrol,
145 scope, n, rsetarray);
147 yaz_log(log_level, "create rset at %p", rnew);
151 static void r_delete(RSET ct)
156 static RSFD r_open(RSET ct, int flag)
159 struct rset_between_rfd *p;
161 if (flag & RSETF_WRITE)
163 yaz_log(YLOG_FATAL, "between set type is read-only");
166 rfd = rfd_create_base(ct);
168 p=(struct rset_between_rfd *)rfd->priv;
170 p = (struct rset_between_rfd *) nmem_malloc(ct->nmem, (sizeof(*p)));
172 p->recbuf = nmem_malloc(ct->nmem, ct->keycontrol->key_size);
173 p->startbuf = nmem_malloc(ct->nmem, ct->keycontrol->key_size);
174 p->attrbuf = nmem_malloc(ct->nmem, ct->keycontrol->key_size);
176 p->andrfd = rset_open(ct->children[0], RSETF_READ);
182 yaz_log(log_level, "open rset=%p rfd=%p", ct, rfd);
186 static void r_close(RSFD rfd)
188 struct rset_between_rfd *p=(struct rset_between_rfd *)rfd->priv;
189 yaz_log(log_level,"close rfd=%p", rfd);
190 rset_close(p->andrfd);
193 static int r_forward(RSFD rfd, void *buf,
194 TERMID *term, const void *untilbuf)
196 struct rset_between_rfd *p=(struct rset_between_rfd *)rfd->priv;
198 yaz_log(log_level, "forwarding ");
199 rc = rset_forward(p->andrfd,buf,term,untilbuf);
203 static void checkattr(RSFD rfd)
205 struct rset_between_info *info =(struct rset_between_info *)
207 struct rset_between_rfd *p = (struct rset_between_rfd *)rfd->priv;
208 const struct rset_key_control *kctrl = rfd->rset->keycontrol;
211 return; /* already found one */
214 p->attrdepth=-1; /* matches always */
217 if ( p->startbufok && p->attrbufok )
218 { /* have buffers to compare */
219 cmp=(kctrl->cmp)(p->startbuf,p->attrbuf);
220 if (0==cmp) /* and the keys match */
222 p->attrdepth = p->depth;
223 yaz_log(log_level, "found attribute match at depth %d",p->attrdepth);
228 static int r_read(RSFD rfd, void *buf, TERMID *term)
230 struct rset_between_info *info =
231 (struct rset_between_info *)rfd->rset->priv;
232 struct rset_between_rfd *p = (struct rset_between_rfd *)rfd->priv;
233 const struct rset_key_control *kctrl = rfd->rset->keycontrol;
235 TERMID dummyterm = 0;
236 yaz_log(log_level, "== read: term=%p",term);
239 while (rset_read(p->andrfd, buf, term))
241 yaz_log(log_level,"read loop term=%p d=%d ad=%d",
242 *term, p->depth, p->attrdepth);
245 memcpy(p->recbuf, buf, kctrl->key_size);
247 cmp = rfd->rset->scope; /* force newrecord */
250 cmp = (kctrl->cmp)(buf, p->recbuf);
251 yaz_log(log_level, "cmp=%d", cmp);
254 if (cmp>=rfd->rset->scope)
256 yaz_log(log_level, "new record");
259 memcpy(p->recbuf, buf, kctrl->key_size);
263 yaz_log(log_level, " term: '%s'", (*term)->name);
264 if (*term==info->startterm)
267 yaz_log(log_level, "read start tag. d=%d", p->depth);
268 memcpy(p->startbuf, buf, kctrl->key_size);
270 checkattr(rfd); /* in case we already saw the attr here */
272 else if (*term==info->stopterm)
274 if (p->depth == p->attrdepth)
275 p->attrdepth = 0; /* ending the tag with attr match */
277 yaz_log(log_level,"read end tag. d=%d ad=%d", p->depth,
280 else if (*term==info->attrterm)
282 yaz_log(log_level,"read attr");
283 memcpy(p->attrbuf, buf, kctrl->key_size);
285 checkattr(rfd); /* in case the start tag came first */
288 { /* mut be a real hit */
289 if (p->depth && p->attrdepth)
292 yaz_log(log_level,"got a hit h="ZINT_FORMAT" d=%d ad=%d",
293 p->hits, p->depth, p->attrdepth);
294 return 1; /* we have everything in place already! */
296 yaz_log(log_level, "Ignoring hit. h="ZINT_FORMAT" d=%d ad=%d",
297 p->hits, p->depth, p->attrdepth);
306 static int r_write(RSFD rfd, const void *buf)
308 yaz_log(YLOG_FATAL, "between set type is read-only");
313 static void r_pos(RSFD rfd, double *current, double *total)
315 struct rset_between_rfd *p=(struct rset_between_rfd *)rfd->priv;
316 rset_pos(p->andrfd, current, total);
317 yaz_log(log_level, "pos: %0.1f/%0.1f ", *current, *total);
320 static void r_get_terms(RSET ct, TERMID *terms, int maxterms, int *curterm)
322 rset_getterms(ct->children[0], terms, maxterms, curterm);