Do not build for Ubuntu raring, quantal (obsolete)
[idzebra-moved-to-github.git] / rset / rsbetween.c
1 /* This file is part of the Zebra server.
2    Copyright (C) Index Data
3
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
7 version.
8
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
12 for more details.
13
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
17
18 */
19
20
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).
28  *
29 */
30
31 #if HAVE_CONFIG_H
32 #include <config.h>
33 #endif
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <assert.h>
38
39 #include <idzebra/util.h>
40 #include <rset.h>
41
42
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);
51
52 static const struct rset_control control =
53 {
54     "between",
55     r_delete,
56     r_get_terms,
57     r_open,
58     r_close,
59     r_forward,
60     r_pos,
61     r_read,
62     rset_no_write,
63 };
64
65 #define STARTTAG 0
66 #define HIT 1
67 #define STOPTAG 2
68 #define ATTRTAG 3
69
70 struct rset_between_info {
71     TERMID startterm; /* pseudo terms for detecting which one we read from */
72     TERMID stopterm;
73     TERMID attrterm;
74 };
75
76 struct rset_between_rfd {
77     RSFD andrfd;
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 */
85     zint hits;
86 };
87
88 static int log_level = 0;
89 static int log_level_initialized = 0;
90
91
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)
95 {
96     if (!rs->term)
97     {
98         rs->term = rset_term_create(tag, -1, "", 0, nmem, 0, 0, 0, 0);
99         rs->term->rset = rs;
100     }
101 }
102
103
104 RSET rset_create_between(NMEM nmem, struct rset_key_control *kcontrol,
105                          int scope,
106                          RSET rset_l, RSET rset_m, RSET rset_r, RSET rset_attr)
107 {
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));
111     RSET rsetarray[4];
112     int n = 4;
113
114     if (!log_level_initialized)
115     {
116         log_level = yaz_log_module_level("rsbetween");
117         log_level_initialized = 1;
118     }
119     rsetarray[STARTTAG] = rset_l;
120     rsetarray[HIT] = rset_m;
121     rsetarray[STOPTAG] = rset_r;
122     rsetarray[ATTRTAG] = rset_attr;
123
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;
129
130     if (rset_attr)
131     {
132         checkterm(rsetarray[ATTRTAG], "(start)", nmem);
133         info->attrterm = rsetarray[ATTRTAG]->term;
134         n = 4;
135     }
136     else
137     {
138         info->attrterm = NULL;
139         n = 3;
140     }
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);
145     rnew->priv = info;
146     yaz_log(log_level, "create rset at %p", rnew);
147     return rnew;
148 }
149
150 static void r_delete(RSET ct)
151 {
152 }
153
154
155 static RSFD r_open(RSET ct, int flag)
156 {
157     RSFD rfd;
158     struct rset_between_rfd *p;
159
160     if (flag & RSETF_WRITE)
161     {
162         yaz_log(YLOG_FATAL, "between set type is read-only");
163         return NULL;
164     }
165     rfd = rfd_create_base(ct);
166     if (rfd->priv)
167         p=(struct rset_between_rfd *)rfd->priv;
168     else {
169         p = (struct rset_between_rfd *) nmem_malloc(ct->nmem, (sizeof(*p)));
170         rfd->priv = 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);
174     }
175     p->andrfd = rset_open(ct->children[0], RSETF_READ);
176     p->hits = -1;
177     p->depth = 0;
178     p->attrdepth = 0;
179     p->attrbufok = 0;
180     p->startbufok = 0;
181     yaz_log(log_level, "open rset=%p rfd=%p", ct, rfd);
182     return rfd;
183 }
184
185 static void r_close(RSFD rfd)
186 {
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);
190 }
191
192 static int r_forward(RSFD rfd, void *buf,
193                      TERMID *term, const void *untilbuf)
194 {
195     struct rset_between_rfd *p=(struct rset_between_rfd *)rfd->priv;
196     int rc;
197     yaz_log(log_level, "forwarding ");
198     rc = rset_forward(p->andrfd,buf,term,untilbuf);
199     return rc;
200 }
201
202 static void checkattr(RSFD rfd)
203 {
204     struct rset_between_info *info =(struct rset_between_info *)
205         rfd->rset->priv;
206     struct rset_between_rfd *p = (struct rset_between_rfd *)rfd->priv;
207     const struct rset_key_control *kctrl = rfd->rset->keycontrol;
208     int cmp;
209     if (p->attrdepth)
210         return; /* already found one */
211     if (!info->attrterm)
212     {
213         p->attrdepth=-1; /* matches always */
214         return;
215     }
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 */
220         {
221             p->attrdepth = p->depth;
222             yaz_log(log_level, "found attribute match at depth %d",p->attrdepth);
223         }
224     }
225 }
226
227 static int r_read(RSFD rfd, void *buf, TERMID *term)
228 {
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;
233     int cmp;
234     TERMID dummyterm = 0;
235     yaz_log(log_level, "== read: term=%p",term);
236     if (!term)
237         term = &dummyterm;
238     while (rset_read(p->andrfd, buf, term))
239     {
240         yaz_log(log_level,"read loop term=%p d=%d ad=%d",
241                 *term, p->depth, p->attrdepth);
242         if (p->hits<0)
243         {/* first time? */
244             memcpy(p->recbuf, buf, kctrl->key_size);
245             p->hits = 0;
246             cmp = rfd->rset->scope; /* force newrecord */
247         }
248         else {
249             cmp = (kctrl->cmp)(buf, p->recbuf);
250             yaz_log(log_level, "cmp=%d", cmp);
251         }
252
253         if (cmp>=rfd->rset->scope)
254         {
255             yaz_log(log_level, "new record");
256             p->depth = 0;
257             p->attrdepth = 0;
258             memcpy(p->recbuf, buf, kctrl->key_size);
259         }
260
261         if (*term)
262             yaz_log(log_level, "  term: '%s'", (*term)->name);
263         if (*term==info->startterm)
264         {
265             p->depth++;
266             yaz_log(log_level, "read start tag. d=%d", p->depth);
267             memcpy(p->startbuf, buf, kctrl->key_size);
268             p->startbufok = 1;
269             checkattr(rfd); /* in case we already saw the attr here */
270         }
271         else if (*term==info->stopterm)
272         {
273             if (p->depth == p->attrdepth)
274                 p->attrdepth = 0; /* ending the tag with attr match */
275             p->depth--;
276             yaz_log(log_level,"read end tag. d=%d ad=%d", p->depth,
277                     p->attrdepth);
278         }
279         else if (*term==info->attrterm)
280         {
281             yaz_log(log_level,"read attr");
282             memcpy(p->attrbuf, buf, kctrl->key_size);
283             p->attrbufok = 1;
284             checkattr(rfd); /* in case the start tag came first */
285         }
286         else
287         { /* mut be a real hit */
288             if (p->depth && p->attrdepth)
289             {
290                 p->hits++;
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! */
294             } else
295                 yaz_log(log_level, "Ignoring hit. h="ZINT_FORMAT" d=%d ad=%d",
296                         p->hits, p->depth, p->attrdepth);
297         }
298     } /* while read */
299
300     return 0;
301
302 }  /* r_read */
303
304 static void r_pos(RSFD rfd, double *current, double *total)
305 {
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);
309 }
310
311 static void r_get_terms(RSET ct, TERMID *terms, int maxterms, int *curterm)
312 {
313     rset_getterms(ct->children[0], terms, maxterms, curterm);
314 }
315
316
317 /*
318  * Local variables:
319  * c-basic-offset: 4
320  * c-file-style: "Stroustrup"
321  * indent-tabs-mode: nil
322  * End:
323  * vim: shiftwidth=4 tabstop=8 expandtab
324  */
325