Extended the result set system. Added support for filtering/limits.
[idzebra-moved-to-github.git] / rset / rsbetween.c
1 /* $Id: rsbetween.c,v 1.38 2005-05-03 09:11:35 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, struct rset_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 *)
214         rfd->rset->priv;
215     struct rset_between_rfd *p = (struct rset_between_rfd *)rfd->priv;
216     const struct rset_key_control *kctrl = rfd->rset->keycontrol;
217     int cmp;
218     if (p->attrdepth)
219         return; /* already found one */
220     if (!info->attrterm) 
221     {
222         p->attrdepth=-1; /* matches always */
223         return;
224     }
225     if ( p->startbufok && p->attrbufok )
226     { /* have buffers to compare */
227         cmp=(kctrl->cmp)(p->startbuf,p->attrbuf);
228         if (0==cmp) /* and the keys match */
229         {
230             p->attrdepth = p->depth;
231             yaz_log(log_level, "found attribute match at depth %d",p->attrdepth);
232         }
233     }
234 }
235
236
237 static int r_read(RSFD rfd, void *buf, TERMID *term)
238 {
239     struct rset_between_info *info =
240         (struct rset_between_info *)rfd->rset->priv;
241     struct rset_between_rfd *p = (struct rset_between_rfd *)rfd->priv;
242     const struct rset_key_control *kctrl = rfd->rset->keycontrol;
243     int cmp;
244     TERMID dummyterm = 0;
245     yaz_log(log_level,"== read: term=%p",term);
246     if (!term)
247         term=&dummyterm;
248     while ( rset_read(p->andrfd,buf,term) )
249     {
250         yaz_log(log_level,"read loop term=%p d=%d ad=%d",
251                 *term,p->depth, p->attrdepth);
252         if (p->hits<0) 
253         {/* first time? */
254             memcpy(p->recbuf,buf,kctrl->key_size);
255             p->hits = 0;
256             cmp = rfd->rset->scope; /* force newrecord */
257         }
258         else {
259             cmp=(kctrl->cmp)(buf,p->recbuf);
260             yaz_log(log_level, "cmp=%d",cmp);
261         }
262
263         if (cmp>=rfd->rset->scope)
264         { 
265             yaz_log(log_level,"new record");
266             p->depth = 0;
267             p->attrdepth = 0;
268             memcpy(p->recbuf,buf,kctrl->key_size);
269         }
270
271         if (*term)
272             yaz_log(log_level,"  term: '%s'", (*term)->name);
273         if (*term==info->startterm)
274         {
275             p->depth++;
276             yaz_log(log_level,"read start tag. d=%d",p->depth);
277             memcpy(p->startbuf,buf,kctrl->key_size);
278             p->startbufok = 1;
279             checkattr(rfd); /* in case we already saw the attr here */
280         }
281         else if (*term==info->stopterm)
282         {
283             if (p->depth == p->attrdepth)
284                 p->attrdepth = 0; /* ending the tag with attr match */
285             p->depth--;
286             yaz_log(log_level,"read end tag. d=%d ad=%d",p->depth, p->attrdepth);
287         }
288         else if (*term==info->attrterm)
289         {
290             yaz_log(log_level,"read attr");
291             memcpy(p->attrbuf,buf,kctrl->key_size);
292             p->attrbufok = 1;
293             checkattr(rfd); /* in case the start tag came first */
294         }
295         else 
296         { /* mut be a real hit */
297             if (p->depth && p->attrdepth)
298             {
299                 p->hits++;
300                 yaz_log(log_level,"got a hit h="ZINT_FORMAT" d=%d ad=%d", 
301                         p->hits,p->depth,p->attrdepth);
302                 return 1; /* we have everything in place already! */
303             } else
304                 yaz_log(log_level, "Ignoring hit. h="ZINT_FORMAT" d=%d ad=%d",
305                         p->hits,p->depth,p->attrdepth);
306         }
307     } /* while read */
308
309     return 0;
310
311 }  /* r_read */
312
313
314 static int r_write(RSFD rfd, const void *buf)
315 {
316     yaz_log(YLOG_FATAL, "between set type is read-only");
317     return -1;
318 }
319
320
321 static void r_pos(RSFD rfd, double *current, double *total)
322 {
323     struct rset_between_rfd *p=(struct rset_between_rfd *)rfd->priv;
324     rset_pos(p->andrfd,current, total);
325     yaz_log(log_level,"pos: %0.1f/%0.1f ", *current, *total);
326 }
327
328 static void r_get_terms(RSET ct, TERMID *terms, int maxterms, int *curterm)
329 {
330     struct rset_between_info *info = (struct rset_between_info *) ct->priv;
331     rset_getterms(info->andset, terms, maxterms, curterm);
332 }
333
334