New rsbeween implementation. Much shorter and easier, and works well
[idzebra-moved-to-github.git] / rset / rsbetween.c
1 /* $Id: rsbetween.c,v 1.30 2004-11-05 17:44:32 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 RSET rsbetween_create( NMEM nmem, const struct key_control *kcontrol,
98             int scope,
99             RSET rset_l, RSET rset_m, RSET rset_r, RSET rset_attr)
100 {
101     RSET rnew=rset_create_base(&control, nmem, kcontrol, scope,0);
102     struct rset_between_info *info=
103         (struct rset_between_info *) nmem_malloc(rnew->nmem,sizeof(*info));
104     RSET rsetarray[4];
105     int n=4;
106     
107     if (!log_level_initialized)
108     {
109         log_level=yaz_log_module_level("rsbetween");
110         log_level_initialized=1;
111     }
112     rsetarray[STARTTAG] = rset_l;
113     rsetarray[HIT] = rset_m;
114     rsetarray[STOPTAG] = rset_r;
115     rsetarray[ATTRTAG] = rset_attr;
116
117     /* make sure we have decent terms for all rsets. Create dummies if needed*/
118     if (!rsetarray[STARTTAG]->term)
119     {
120         rsetarray[STARTTAG]->term=
121              rset_term_create("<starttag>",strlen("<starttag>"),"",0,nmem);
122         rsetarray[STARTTAG]->term->rset=rsetarray[STARTTAG];
123     }
124     info->startterm=rsetarray[STARTTAG]->term;
125
126     if (!rsetarray[STOPTAG]->term)
127     {
128         rsetarray[STOPTAG]->term=
129              rset_term_create("<stoptag>",strlen("<stoptag>"),"",0,nmem);
130         rsetarray[STOPTAG]->term->rset=rsetarray[STOPTAG];
131     }
132     info->stopterm=rsetarray[STOPTAG]->term;
133
134     if (rset_attr)
135     {
136         if (!rsetarray[ATTRTAG]->term)
137         {
138             rsetarray[ATTRTAG]->term=
139                  rset_term_create("<attrtag>",strlen("<attrtag>"),"",0,nmem);
140             rsetarray[ATTRTAG]->term->rset=rsetarray[ATTRTAG];
141         }
142         info->attrterm=rsetarray[ATTRTAG]->term;
143     }
144     else
145     {
146         info->attrterm=NULL;
147         n--; /* smaller and */
148     }
149     info->andset=rsmultiand_create( nmem, kcontrol, scope, n, rsetarray);
150     rnew->priv=info;
151     logf(log_level,"create rset at %p",rnew);
152     return rnew;
153 }
154
155
156 static void r_delete (RSET ct)
157 {
158     struct rset_between_info *info = (struct rset_between_info *) ct->priv;
159     logf(log_level,"delete rset at %p",ct);
160     rset_delete(info->andset);
161 }
162
163
164 static RSFD r_open (RSET ct, int flag)
165 {
166     struct rset_between_info *info = (struct rset_between_info *) ct->priv;
167     RSFD rfd;
168     struct rset_between_rfd *p;
169
170     if (flag & RSETF_WRITE)
171     {
172         logf (LOG_FATAL, "between set type is read-only");
173         return NULL;
174     }
175     rfd=rfd_create_base(ct);
176     if (rfd->priv)  
177         p=(struct rset_between_rfd *)rfd->priv;
178     else {
179         p = (struct rset_between_rfd *) nmem_malloc(ct->nmem, (sizeof(*p)));
180         rfd->priv=p;
181         p->recbuf = nmem_malloc(ct->nmem, (ct->keycontrol->key_size)); 
182         p->startbuf = nmem_malloc(ct->nmem, (ct->keycontrol->key_size)); 
183         p->attrbuf = nmem_malloc(ct->nmem, (ct->keycontrol->key_size)); 
184     }
185     p->andrfd = rset_open (info->andset, RSETF_READ);
186     p->hits=-1;
187     p->depth=0;
188     p->attrdepth=0;
189     p->attrbufok=0;
190     p->startbufok=0;
191     logf(log_level,"open rset=%p rfd=%p", ct, rfd);
192     return rfd;
193 }
194
195 static void r_close (RSFD rfd)
196 {
197     struct rset_between_rfd *p=(struct rset_between_rfd *)rfd->priv;
198     logf(log_level,"close rfd=%p", rfd);
199     rset_close (p->andrfd);
200     rfd_delete_base(rfd);
201 }
202
203
204
205 static int r_forward(RSFD rfd, void *buf, 
206                      TERMID *term, const void *untilbuf)
207 {
208     struct rset_between_rfd *p=(struct rset_between_rfd *)rfd->priv;
209     int rc;
210     logf(log_level, "forwarding ");
211     rc=rset_forward(p->andrfd,buf,term,untilbuf);
212     return rc;
213 }
214
215
216
217 static void checkattr(RSFD rfd)
218 {
219     struct rset_between_info *info =(struct rset_between_info *)rfd->rset->priv;
220     struct rset_between_rfd *p=(struct rset_between_rfd *)rfd->priv;
221     const struct key_control *kctrl=rfd->rset->keycontrol;
222     int cmp;
223     if (p->attrdepth)
224         return; /* already found one */
225     if (!info->attrterm) 
226     {
227         p->attrdepth=-1; /* matches always */
228         return;
229     }
230     if ( p->startbufok && p->attrbufok )
231     { /* have buffers to compare */
232         cmp=(kctrl->cmp)(p->startbuf,p->attrbuf);
233         if (0==cmp) /* and the keys match */
234         {
235             p->attrdepth=p->depth;
236             logf(log_level, "found attribute match at depth %d",p->attrdepth);
237         }
238     }
239 }
240
241
242 static int r_read (RSFD rfd, void *buf, TERMID *term)
243 {
244     struct rset_between_info *info =(struct rset_between_info *)rfd->rset->priv;
245     struct rset_between_rfd *p=(struct rset_between_rfd *)rfd->priv;
246     const struct key_control *kctrl=rfd->rset->keycontrol;
247     int cmp;
248     logf(log_level,"== read: term=%p",term);
249     TERMID dummyterm=0;
250     if (!term)
251         term=&dummyterm;
252     while ( rset_read(p->andrfd,buf,term) )
253     {
254         logf(log_level,"read loop term=%p d=%d ad=%d",
255                 *term,p->depth, p->attrdepth);
256         if (p->hits<0) 
257         {/* first time? */
258             memcpy(p->recbuf,buf,kctrl->key_size);
259             p->hits=0;
260             cmp=rfd->rset->scope; /* force newrecord */
261         }
262         else {
263             cmp=(kctrl->cmp)(buf,p->recbuf);
264             logf(log_level, "cmp=%d",cmp);
265         }
266
267         if (cmp>=rfd->rset->scope)
268         { 
269             logf(log_level,"new record");
270             p->depth=0;
271             p->attrdepth=0;
272             memcpy(p->recbuf,buf,kctrl->key_size);
273         }
274
275         if (*term)
276             logf(log_level,"  term: '%s'", (*term)->name);
277         if (*term==info->startterm)
278         {
279             p->depth++;
280             logf(log_level,"read start tag. d=%d",p->depth);
281             memcpy(p->startbuf,buf,kctrl->key_size);
282             p->startbufok=1;
283             checkattr(rfd); /* in case we already saw the attr here */
284         }
285         else if (*term==info->stopterm)
286         {
287             if (p->depth == p->attrdepth)
288                 p->attrdepth=0; /* ending the tag with attr match */
289             p->depth--;
290             logf(log_level,"read end tag. d=%d ad=%d",p->depth, p->attrdepth);
291         }
292         else if (*term==info->attrterm)
293         {
294             logf(log_level,"read attr");
295             memcpy(p->attrbuf,buf,kctrl->key_size);
296             p->attrbufok=1;
297             checkattr(rfd); /* in case the start tag came first */
298         }
299         else 
300         { /* mut be a real hit */
301             if (p->depth && p->attrdepth)
302             {
303                 p->hits++;
304                 logf(log_level,"got a hit h="ZINT_FORMAT" d=%d ad=%d", 
305                         p->hits,p->depth,p->attrdepth);
306                 return 1; /* we have everything in place already! */
307             } else
308                 logf(log_level, "Ignoring hit. h="ZINT_FORMAT" d=%d ad=%d",
309                         p->hits,p->depth,p->attrdepth);
310         }
311     } /* while read */
312
313     return 0;
314
315 }  /* r_read */
316
317
318 static int r_write (RSFD rfd, const void *buf)
319 {
320     logf (LOG_FATAL, "between set type is read-only");
321     return -1;
322 }
323
324
325 static void r_pos (RSFD rfd, double *current, double *total)
326 {
327     struct rset_between_rfd *p=(struct rset_between_rfd *)rfd->priv;
328     rset_pos(p->andrfd,current, total);
329     logf(log_level,"pos: %0.1f/%0.1f ", *current, *total);
330 }
331
332 static void r_get_terms(RSET ct, TERMID *terms, int maxterms, int *curterm)
333 {
334     struct rset_between_info *info = (struct rset_between_info *) ct->priv;
335     rset_getterms(info->andset, terms, maxterms, curterm);
336 }
337
338