1 /* This file is part of the Zebra server.
2 Copyright (C) 2004-2013 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).
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 void r_pos(RSFD rfd, double *current, double *total);
50 static void r_get_terms(RSET ct, TERMID *terms, int maxterms, int *curterm);
52 static const struct rset_control control =
70 struct rset_between_info {
71 TERMID startterm; /* pseudo terms for detecting which one we read from */
76 struct rset_between_rfd {
78 void *recbuf; /* a key that tells which record we are in */
79 void *startbuf; /* the start tag */
80 int startbufok; /* we have seen the first start tag */
81 void *attrbuf; /* the attr tag. If these two match, we have attr match */
82 int attrbufok; /* we have seen the first attr tag, can compare */
83 int depth; /* number of start-tags without end-tags */
84 int attrdepth; /* on what depth the attr matched */
88 static int log_level = 0;
89 static int log_level_initialized = 0;
92 /* make sure that the rset has a term attached. If not, create one */
93 /* we need these terms for the tags, to distinguish what we read */
94 static void checkterm(RSET rs, char *tag, NMEM nmem)
98 rs->term = rset_term_create(tag, -1, "", 0, nmem, 0, 0, 0, 0);
104 RSET rset_create_between(NMEM nmem, struct rset_key_control *kcontrol,
106 RSET rset_l, RSET rset_m, RSET rset_r, RSET rset_attr)
108 RSET rnew = rset_create_base(&control, nmem, kcontrol, scope, 0, 0, 0);
109 struct rset_between_info *info=
110 (struct rset_between_info *) nmem_malloc(rnew->nmem,sizeof(*info));
114 if (!log_level_initialized)
116 log_level = yaz_log_module_level("rsbetween");
117 log_level_initialized = 1;
119 rsetarray[STARTTAG] = rset_l;
120 rsetarray[HIT] = rset_m;
121 rsetarray[STOPTAG] = rset_r;
122 rsetarray[ATTRTAG] = rset_attr;
124 /* make sure we have decent terms for all rsets. Create dummies if needed*/
125 checkterm(rsetarray[STARTTAG], "(start)", nmem);
126 checkterm(rsetarray[STOPTAG], "(start)", nmem);
127 info->startterm = rsetarray[STARTTAG]->term;
128 info->stopterm = rsetarray[STOPTAG]->term;
132 checkterm(rsetarray[ATTRTAG], "(start)", nmem);
133 info->attrterm = rsetarray[ATTRTAG]->term;
138 info->attrterm = NULL;
141 rnew->no_children = 1;
142 rnew->children = nmem_malloc(rnew->nmem, sizeof(RSET *));
143 rnew->children[0] = rset_create_and(nmem, kcontrol,
144 scope, n, rsetarray);
146 yaz_log(log_level, "create rset at %p", rnew);
150 static void r_delete(RSET ct)
155 static RSFD r_open(RSET ct, int flag)
158 struct rset_between_rfd *p;
160 if (flag & RSETF_WRITE)
162 yaz_log(YLOG_FATAL, "between set type is read-only");
165 rfd = rfd_create_base(ct);
167 p=(struct rset_between_rfd *)rfd->priv;
169 p = (struct rset_between_rfd *) nmem_malloc(ct->nmem, (sizeof(*p)));
171 p->recbuf = nmem_malloc(ct->nmem, ct->keycontrol->key_size);
172 p->startbuf = nmem_malloc(ct->nmem, ct->keycontrol->key_size);
173 p->attrbuf = nmem_malloc(ct->nmem, ct->keycontrol->key_size);
175 p->andrfd = rset_open(ct->children[0], RSETF_READ);
181 yaz_log(log_level, "open rset=%p rfd=%p", ct, rfd);
185 static void r_close(RSFD rfd)
187 struct rset_between_rfd *p=(struct rset_between_rfd *)rfd->priv;
188 yaz_log(log_level,"close rfd=%p", rfd);
189 rset_close(p->andrfd);
192 static int r_forward(RSFD rfd, void *buf,
193 TERMID *term, const void *untilbuf)
195 struct rset_between_rfd *p=(struct rset_between_rfd *)rfd->priv;
197 yaz_log(log_level, "forwarding ");
198 rc = rset_forward(p->andrfd,buf,term,untilbuf);
202 static void checkattr(RSFD rfd)
204 struct rset_between_info *info =(struct rset_between_info *)
206 struct rset_between_rfd *p = (struct rset_between_rfd *)rfd->priv;
207 const struct rset_key_control *kctrl = rfd->rset->keycontrol;
210 return; /* already found one */
213 p->attrdepth=-1; /* matches always */
216 if ( p->startbufok && p->attrbufok )
217 { /* have buffers to compare */
218 cmp=(kctrl->cmp)(p->startbuf,p->attrbuf);
219 if (0==cmp) /* and the keys match */
221 p->attrdepth = p->depth;
222 yaz_log(log_level, "found attribute match at depth %d",p->attrdepth);
227 static int r_read(RSFD rfd, void *buf, TERMID *term)
229 struct rset_between_info *info =
230 (struct rset_between_info *)rfd->rset->priv;
231 struct rset_between_rfd *p = (struct rset_between_rfd *)rfd->priv;
232 const struct rset_key_control *kctrl = rfd->rset->keycontrol;
234 TERMID dummyterm = 0;
235 yaz_log(log_level, "== read: term=%p",term);
238 while (rset_read(p->andrfd, buf, term))
240 yaz_log(log_level,"read loop term=%p d=%d ad=%d",
241 *term, p->depth, p->attrdepth);
244 memcpy(p->recbuf, buf, kctrl->key_size);
246 cmp = rfd->rset->scope; /* force newrecord */
249 cmp = (kctrl->cmp)(buf, p->recbuf);
250 yaz_log(log_level, "cmp=%d", cmp);
253 if (cmp>=rfd->rset->scope)
255 yaz_log(log_level, "new record");
258 memcpy(p->recbuf, buf, kctrl->key_size);
262 yaz_log(log_level, " term: '%s'", (*term)->name);
263 if (*term==info->startterm)
266 yaz_log(log_level, "read start tag. d=%d", p->depth);
267 memcpy(p->startbuf, buf, kctrl->key_size);
269 checkattr(rfd); /* in case we already saw the attr here */
271 else if (*term==info->stopterm)
273 if (p->depth == p->attrdepth)
274 p->attrdepth = 0; /* ending the tag with attr match */
276 yaz_log(log_level,"read end tag. d=%d ad=%d", p->depth,
279 else if (*term==info->attrterm)
281 yaz_log(log_level,"read attr");
282 memcpy(p->attrbuf, buf, kctrl->key_size);
284 checkattr(rfd); /* in case the start tag came first */
287 { /* mut be a real hit */
288 if (p->depth && p->attrdepth)
291 yaz_log(log_level,"got a hit h="ZINT_FORMAT" d=%d ad=%d",
292 p->hits, p->depth, p->attrdepth);
293 return 1; /* we have everything in place already! */
295 yaz_log(log_level, "Ignoring hit. h="ZINT_FORMAT" d=%d ad=%d",
296 p->hits, p->depth, p->attrdepth);
304 static void r_pos(RSFD rfd, double *current, double *total)
306 struct rset_between_rfd *p=(struct rset_between_rfd *)rfd->priv;
307 rset_pos(p->andrfd, current, total);
308 yaz_log(log_level, "pos: %0.1f/%0.1f ", *current, *total);
311 static void r_get_terms(RSET ct, TERMID *terms, int maxterms, int *curterm)
313 rset_getterms(ct->children[0], terms, maxterms, curterm);
320 * c-file-style: "Stroustrup"
321 * indent-tabs-mode: nil
323 * vim: shiftwidth=4 tabstop=8 expandtab