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