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
21 /* rsbetween is (mostly) used for xml searches. It returns the hits of the
22 * "middle" rset, that are in between the "left" and "right" rsets. For
23 * example "Shakespeare" in between "<author>" and </author>. The thing is
24 * complicated by the inclusion of attributes (from their own rset). If attrs
25 * specified, they must match the "left" rset (start tag). "Hamlet" between
26 * "<title lang = eng>" and "</title>". (This assumes that the attributes are
27 * indexed to the same seqno as the tags).
36 #include <idzebra/util.h>
40 static RSFD r_open(RSET ct, int flag);
41 static void r_close(RSFD rfd);
42 static void r_delete(RSET ct);
43 static int r_forward(RSFD rfd, void *buf,
44 TERMID *term, const void *untilbuf);
45 static int r_read(RSFD rfd, void *buf, TERMID *term );
46 static int r_write(RSFD rfd, const void *buf);
47 static void r_pos(RSFD rfd, double *current, double *total);
48 static void r_get_terms(RSET ct, TERMID *terms, int maxterms, int *curterm);
50 static const struct rset_control control =
68 struct rset_between_info {
69 TERMID startterm; /* pseudo terms for detecting which one we read from */
74 struct rset_between_rfd {
76 void *recbuf; /* a key that tells which record we are in */
77 void *startbuf; /* the start tag */
78 int startbufok; /* we have seen the first start tag */
79 void *attrbuf; /* the attr tag. If these two match, we have attr match */
80 int attrbufok; /* we have seen the first attr tag, can compare */
81 int depth; /* number of start-tags without end-tags */
82 int attrdepth; /* on what depth the attr matched */
86 static int log_level = 0;
87 static int log_level_initialized = 0;
90 /* make sure that the rset has a term attached. If not, create one */
91 /* we need these terms for the tags, to distinguish what we read */
92 static void checkterm(RSET rs, char *tag, NMEM nmem)
96 rs->term = rset_term_create(tag, -1, "", 0, nmem, 0, 0, 0, 0);
102 RSET rset_create_between(NMEM nmem, struct rset_key_control *kcontrol,
104 RSET rset_l, RSET rset_m, RSET rset_r, RSET rset_attr)
106 RSET rnew = rset_create_base(&control, nmem, kcontrol, scope, 0, 0, 0);
107 struct rset_between_info *info=
108 (struct rset_between_info *) nmem_malloc(rnew->nmem,sizeof(*info));
112 if (!log_level_initialized)
114 log_level = yaz_log_module_level("rsbetween");
115 log_level_initialized = 1;
117 rsetarray[STARTTAG] = rset_l;
118 rsetarray[HIT] = rset_m;
119 rsetarray[STOPTAG] = rset_r;
120 rsetarray[ATTRTAG] = rset_attr;
122 /* make sure we have decent terms for all rsets. Create dummies if needed*/
123 checkterm(rsetarray[STARTTAG], "(start)", nmem);
124 checkterm(rsetarray[STOPTAG], "(start)", nmem);
125 info->startterm = rsetarray[STARTTAG]->term;
126 info->stopterm = rsetarray[STOPTAG]->term;
130 checkterm(rsetarray[ATTRTAG], "(start)", nmem);
131 info->attrterm = rsetarray[ATTRTAG]->term;
136 info->attrterm = NULL;
139 rnew->no_children = 1;
140 rnew->children = nmem_malloc(rnew->nmem, sizeof(RSET *));
141 rnew->children[0] = rset_create_and(nmem, kcontrol,
142 scope, n, rsetarray);
144 yaz_log(log_level, "create rset at %p", rnew);
148 static void r_delete(RSET ct)
153 static RSFD r_open(RSET ct, int flag)
156 struct rset_between_rfd *p;
158 if (flag & RSETF_WRITE)
160 yaz_log(YLOG_FATAL, "between set type is read-only");
163 rfd = rfd_create_base(ct);
165 p=(struct rset_between_rfd *)rfd->priv;
167 p = (struct rset_between_rfd *) nmem_malloc(ct->nmem, (sizeof(*p)));
169 p->recbuf = nmem_malloc(ct->nmem, ct->keycontrol->key_size);
170 p->startbuf = nmem_malloc(ct->nmem, ct->keycontrol->key_size);
171 p->attrbuf = nmem_malloc(ct->nmem, ct->keycontrol->key_size);
173 p->andrfd = rset_open(ct->children[0], RSETF_READ);
179 yaz_log(log_level, "open rset=%p rfd=%p", ct, rfd);
183 static void r_close(RSFD rfd)
185 struct rset_between_rfd *p=(struct rset_between_rfd *)rfd->priv;
186 yaz_log(log_level,"close rfd=%p", rfd);
187 rset_close(p->andrfd);
190 static int r_forward(RSFD rfd, void *buf,
191 TERMID *term, const void *untilbuf)
193 struct rset_between_rfd *p=(struct rset_between_rfd *)rfd->priv;
195 yaz_log(log_level, "forwarding ");
196 rc = rset_forward(p->andrfd,buf,term,untilbuf);
200 static void checkattr(RSFD rfd)
202 struct rset_between_info *info =(struct rset_between_info *)
204 struct rset_between_rfd *p = (struct rset_between_rfd *)rfd->priv;
205 const struct rset_key_control *kctrl = rfd->rset->keycontrol;
208 return; /* already found one */
211 p->attrdepth=-1; /* matches always */
214 if ( p->startbufok && p->attrbufok )
215 { /* have buffers to compare */
216 cmp=(kctrl->cmp)(p->startbuf,p->attrbuf);
217 if (0==cmp) /* and the keys match */
219 p->attrdepth = p->depth;
220 yaz_log(log_level, "found attribute match at depth %d",p->attrdepth);
225 static int r_read(RSFD rfd, void *buf, TERMID *term)
227 struct rset_between_info *info =
228 (struct rset_between_info *)rfd->rset->priv;
229 struct rset_between_rfd *p = (struct rset_between_rfd *)rfd->priv;
230 const struct rset_key_control *kctrl = rfd->rset->keycontrol;
232 TERMID dummyterm = 0;
233 yaz_log(log_level, "== read: term=%p",term);
236 while (rset_read(p->andrfd, buf, term))
238 yaz_log(log_level,"read loop term=%p d=%d ad=%d",
239 *term, p->depth, p->attrdepth);
242 memcpy(p->recbuf, buf, kctrl->key_size);
244 cmp = rfd->rset->scope; /* force newrecord */
247 cmp = (kctrl->cmp)(buf, p->recbuf);
248 yaz_log(log_level, "cmp=%d", cmp);
251 if (cmp>=rfd->rset->scope)
253 yaz_log(log_level, "new record");
256 memcpy(p->recbuf, buf, kctrl->key_size);
260 yaz_log(log_level, " term: '%s'", (*term)->name);
261 if (*term==info->startterm)
264 yaz_log(log_level, "read start tag. d=%d", p->depth);
265 memcpy(p->startbuf, buf, kctrl->key_size);
267 checkattr(rfd); /* in case we already saw the attr here */
269 else if (*term==info->stopterm)
271 if (p->depth == p->attrdepth)
272 p->attrdepth = 0; /* ending the tag with attr match */
274 yaz_log(log_level,"read end tag. d=%d ad=%d", p->depth,
277 else if (*term==info->attrterm)
279 yaz_log(log_level,"read attr");
280 memcpy(p->attrbuf, buf, kctrl->key_size);
282 checkattr(rfd); /* in case the start tag came first */
285 { /* mut be a real hit */
286 if (p->depth && p->attrdepth)
289 yaz_log(log_level,"got a hit h="ZINT_FORMAT" d=%d ad=%d",
290 p->hits, p->depth, p->attrdepth);
291 return 1; /* we have everything in place already! */
293 yaz_log(log_level, "Ignoring hit. h="ZINT_FORMAT" d=%d ad=%d",
294 p->hits, p->depth, p->attrdepth);
303 static int r_write(RSFD rfd, const void *buf)
305 yaz_log(YLOG_FATAL, "between set type is read-only");
310 static void r_pos(RSFD rfd, double *current, double *total)
312 struct rset_between_rfd *p=(struct rset_between_rfd *)rfd->priv;
313 rset_pos(p->andrfd, current, total);
314 yaz_log(log_level, "pos: %0.1f/%0.1f ", *current, *total);
317 static void r_get_terms(RSET ct, TERMID *terms, int maxterms, int *curterm)
319 rset_getterms(ct->children[0], terms, maxterms, curterm);
326 * c-file-style: "Stroustrup"
327 * indent-tabs-mode: nil
329 * vim: shiftwidth=4 tabstop=8 expandtab