Using the new ylog.h everywhere, and fixing what that breaks!
[idzebra-moved-to-github.git] / rset / rsbetween.c
1 /* $Id: rsbetween.c,v 1.32 2004-11-19 10:27:13 heikki Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
3    Index Data Aps
4
5 This file is part of the Zebra server.
6
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
10 version.
11
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
15 for more details.
16
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
20 02111-1307, USA.
21 */
22
23
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).
31  *
32 */ 
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <assert.h>
38
39 #include <zebrautl.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
67 const struct rset_control *rset_kind_between = &control;
68
69 #define STARTTAG 0
70 #define HIT 1
71 #define STOPTAG 2
72 #define ATTRTAG 3
73
74 struct rset_between_info {
75     RSET andset; /* the multi-and of the above */
76     TERMID startterm; /* pseudo terms for detecting which one we read from */
77     TERMID stopterm;
78     TERMID attrterm;
79 };
80
81 struct rset_between_rfd {
82     RSFD andrfd;
83     void *recbuf; /* a key that tells which record we are in */
84     void *startbuf; /* the start tag */
85     int startbufok; /* we have seen the first start tag */
86     void *attrbuf;  /* the attr tag. If these two match, we have attr match */
87     int attrbufok; /* we have seen the first attr tag, can compare */
88     int depth; /* number of start-tags without end-tags */
89     int attrdepth; /* on what depth the attr matched */
90     zint hits;
91 };    
92
93 static int log_level=0;
94 static int log_level_initialized=0;
95
96
97 /* make sure that the rset has a term attached. If not, create one */
98 /* we need these terms for the tags, to distinguish what we read */
99 static void checkterm( RSET rs, char *tag, NMEM nmem)
100 {
101     if (!rs->term)
102     {
103         rs->term=
104              rset_term_create(tag,strlen(tag),"",0,nmem);
105         rs->term->rset=rs;
106     }
107 }
108
109
110 RSET rsbetween_create( NMEM nmem, const struct key_control *kcontrol,
111             int scope,
112             RSET rset_l, RSET rset_m, RSET rset_r, RSET rset_attr)
113 {
114     RSET rnew=rset_create_base(&control, nmem, kcontrol, scope,0);
115     struct rset_between_info *info=
116         (struct rset_between_info *) nmem_malloc(rnew->nmem,sizeof(*info));
117     RSET rsetarray[4];
118     int n=4;
119     
120     if (!log_level_initialized)
121     {
122         log_level=yaz_log_module_level("rsbetween");
123         log_level_initialized=1;
124     }
125     rsetarray[STARTTAG] = rset_l;
126     rsetarray[HIT] = rset_m;
127     rsetarray[STOPTAG] = rset_r;
128     rsetarray[ATTRTAG] = rset_attr;
129
130     /* make sure we have decent terms for all rsets. Create dummies if needed*/
131     checkterm( rsetarray[STARTTAG], "(start)",nmem);
132     checkterm( rsetarray[STOPTAG], "(start)",nmem);
133     info->startterm=rsetarray[STARTTAG]->term;
134     info->stopterm=rsetarray[STOPTAG]->term;
135
136     if (rset_attr)
137     {
138         checkterm( rsetarray[ATTRTAG], "(start)",nmem);
139         info->attrterm=rsetarray[ATTRTAG]->term;
140         n=4;
141     }
142     else
143     {
144         info->attrterm=NULL;
145         n=3; 
146     }
147     info->andset=rsmultiand_create( nmem, kcontrol, scope, n, rsetarray);
148     rnew->priv=info;
149     yaz_log(log_level,"create rset at %p",rnew);
150     return rnew;
151 }
152
153
154 static void r_delete (RSET ct)
155 {
156     struct rset_between_info *info = (struct rset_between_info *) ct->priv;
157     yaz_log(log_level,"delete rset at %p",ct);
158     rset_delete(info->andset);
159 }
160
161
162 static RSFD r_open (RSET ct, int flag)
163 {
164     struct rset_between_info *info = (struct rset_between_info *) ct->priv;
165     RSFD rfd;
166     struct rset_between_rfd *p;
167
168     if (flag & RSETF_WRITE)
169     {
170         yaz_log (YLOG_FATAL, "between set type is read-only");
171         return NULL;
172     }
173     rfd=rfd_create_base(ct);
174     if (rfd->priv)  
175         p=(struct rset_between_rfd *)rfd->priv;
176     else {
177         p = (struct rset_between_rfd *) nmem_malloc(ct->nmem, (sizeof(*p)));
178         rfd->priv=p;
179         p->recbuf = nmem_malloc(ct->nmem, (ct->keycontrol->key_size)); 
180         p->startbuf = nmem_malloc(ct->nmem, (ct->keycontrol->key_size)); 
181         p->attrbuf = nmem_malloc(ct->nmem, (ct->keycontrol->key_size)); 
182     }
183     p->andrfd = rset_open (info->andset, RSETF_READ);
184     p->hits=-1;
185     p->depth=0;
186     p->attrdepth=0;
187     p->attrbufok=0;
188     p->startbufok=0;
189     yaz_log(log_level,"open rset=%p rfd=%p", ct, rfd);
190     return rfd;
191 }
192
193 static void r_close (RSFD rfd)
194 {
195     struct rset_between_rfd *p=(struct rset_between_rfd *)rfd->priv;
196     yaz_log(log_level,"close rfd=%p", rfd);
197     rset_close (p->andrfd);
198     rfd_delete_base(rfd);
199 }
200
201
202
203 static int r_forward(RSFD rfd, void *buf, 
204                      TERMID *term, const void *untilbuf)
205 {
206     struct rset_between_rfd *p=(struct rset_between_rfd *)rfd->priv;
207     int rc;
208     yaz_log(log_level, "forwarding ");
209     rc=rset_forward(p->andrfd,buf,term,untilbuf);
210     return rc;
211 }
212
213
214
215 static void checkattr(RSFD rfd)
216 {
217     struct rset_between_info *info =(struct rset_between_info *)rfd->rset->priv;
218     struct rset_between_rfd *p=(struct rset_between_rfd *)rfd->priv;
219     const struct key_control *kctrl=rfd->rset->keycontrol;
220     int cmp;
221     if (p->attrdepth)
222         return; /* already found one */
223     if (!info->attrterm) 
224     {
225         p->attrdepth=-1; /* matches always */
226         return;
227     }
228     if ( p->startbufok && p->attrbufok )
229     { /* have buffers to compare */
230         cmp=(kctrl->cmp)(p->startbuf,p->attrbuf);
231         if (0==cmp) /* and the keys match */
232         {
233             p->attrdepth=p->depth;
234             yaz_log(log_level, "found attribute match at depth %d",p->attrdepth);
235         }
236     }
237 }
238
239
240 static int r_read (RSFD rfd, void *buf, TERMID *term)
241 {
242     struct rset_between_info *info =(struct rset_between_info *)rfd->rset->priv;
243     struct rset_between_rfd *p=(struct rset_between_rfd *)rfd->priv;
244     const struct key_control *kctrl=rfd->rset->keycontrol;
245     int cmp;
246     yaz_log(log_level,"== read: term=%p",term);
247     TERMID dummyterm=0;
248     if (!term)
249         term=&dummyterm;
250     while ( rset_read(p->andrfd,buf,term) )
251     {
252         yaz_log(log_level,"read loop term=%p d=%d ad=%d",
253                 *term,p->depth, p->attrdepth);
254         if (p->hits<0) 
255         {/* first time? */
256             memcpy(p->recbuf,buf,kctrl->key_size);
257             p->hits=0;
258             cmp=rfd->rset->scope; /* force newrecord */
259         }
260         else {
261             cmp=(kctrl->cmp)(buf,p->recbuf);
262             yaz_log(log_level, "cmp=%d",cmp);
263         }
264
265         if (cmp>=rfd->rset->scope)
266         { 
267             yaz_log(log_level,"new record");
268             p->depth=0;
269             p->attrdepth=0;
270             memcpy(p->recbuf,buf,kctrl->key_size);
271         }
272
273         if (*term)
274             yaz_log(log_level,"  term: '%s'", (*term)->name);
275         if (*term==info->startterm)
276         {
277             p->depth++;
278             yaz_log(log_level,"read start tag. d=%d",p->depth);
279             memcpy(p->startbuf,buf,kctrl->key_size);
280             p->startbufok=1;
281             checkattr(rfd); /* in case we already saw the attr here */
282         }
283         else if (*term==info->stopterm)
284         {
285             if (p->depth == p->attrdepth)
286                 p->attrdepth=0; /* ending the tag with attr match */
287             p->depth--;
288             yaz_log(log_level,"read end tag. d=%d ad=%d",p->depth, p->attrdepth);
289         }
290         else if (*term==info->attrterm)
291         {
292             yaz_log(log_level,"read attr");
293             memcpy(p->attrbuf,buf,kctrl->key_size);
294             p->attrbufok=1;
295             checkattr(rfd); /* in case the start tag came first */
296         }
297         else 
298         { /* mut be a real hit */
299             if (p->depth && p->attrdepth)
300             {
301                 p->hits++;
302                 yaz_log(log_level,"got a hit h="ZINT_FORMAT" d=%d ad=%d", 
303                         p->hits,p->depth,p->attrdepth);
304                 return 1; /* we have everything in place already! */
305             } else
306                 yaz_log(log_level, "Ignoring hit. h="ZINT_FORMAT" d=%d ad=%d",
307                         p->hits,p->depth,p->attrdepth);
308         }
309     } /* while read */
310
311     return 0;
312
313 }  /* r_read */
314
315
316 static int r_write (RSFD rfd, const void *buf)
317 {
318     yaz_log (YLOG_FATAL, "between set type is read-only");
319     return -1;
320 }
321
322
323 static void r_pos (RSFD rfd, double *current, double *total)
324 {
325     struct rset_between_rfd *p=(struct rset_between_rfd *)rfd->priv;
326     rset_pos(p->andrfd,current, total);
327     yaz_log(log_level,"pos: %0.1f/%0.1f ", *current, *total);
328 }
329
330 static void r_get_terms(RSET ct, TERMID *terms, int maxterms, int *curterm)
331 {
332     struct rset_between_info *info = (struct rset_between_info *) ct->priv;
333     rset_getterms(info->andset, terms, maxterms, curterm);
334 }
335
336