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