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