57d260dba10a12672631ac1943f0964a96432fc4
[idzebra-moved-to-github.git] / data1 / d1_doespec.c
1 /* $Id: d1_doespec.c,v 1.7 2005-01-16 23:14:56 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 #include <assert.h>
24 #include <stdlib.h>
25
26 #include <yaz/log.h>
27 #include <yaz/oid.h>
28 #include <yaz/proto.h>
29 #include <idzebra/data1.h>
30
31 static int match_children(data1_handle dh, data1_node *n,
32                           Z_Espec1 *e, int i, Z_ETagUnit **t,
33     int num);
34
35 static int match_children_wildpath(data1_handle dh, data1_node *n,
36                                    Z_Espec1 *e, int i,
37                                    Z_ETagUnit **t, int num)
38 {
39     return 0;
40 }
41
42 /*
43  * Locate a specific triple within a variant.
44  * set is the set to look for, universal set is the set that applies to a
45  * triple with an unknown set.
46  */
47 static Z_Triple *find_triple(Z_Variant *var, oid_value universalset,
48     oid_value set, int zclass, int type)
49 {
50     int i;
51     oident *defaultsetent = oid_getentbyoid(var->globalVariantSetId);
52     oid_value defaultset = defaultsetent ? defaultsetent->value :
53         universalset;
54
55     for (i = 0; i < var->num_triples; i++)
56     {
57         oident *cursetent =
58             oid_getentbyoid(var->triples[i]->variantSetId);
59         oid_value curset = cursetent ? cursetent->value : defaultset;
60
61         if (set == curset &&
62             *var->triples[i]->zclass == zclass &&
63             *var->triples[i]->type == type)
64             return var->triples[i];
65     }
66     return 0;
67 }
68
69 static void mark_subtree(data1_node *n, int make_variantlist, int no_data,
70     int get_bytes, Z_Variant *vreq)
71 {
72     data1_node *c;
73
74 #if 1
75     if (n->which == DATA1N_tag)
76 #else
77     if (n->which == DATA1N_tag && (!n->child || n->child->which != DATA1N_tag))
78     /*
79      * This seems to cause multi-level elements to fall out when only a
80      * top-level elementRequest has been given... Problem is, I can't figure
81      * out what it was supposed to ACHIEVE.... delete when code has been
82      * verified.
83      */
84 #endif
85     {
86         n->u.tag.node_selected = 1;
87         n->u.tag.make_variantlist = make_variantlist;
88         n->u.tag.no_data_requested = no_data;
89         n->u.tag.get_bytes = get_bytes;
90     }
91
92     for (c = n->child; c; c = c->next)
93     {
94         if (c->which == DATA1N_tag && (!n->child ||
95             n->child->which != DATA1N_tag))
96         {
97             c->u.tag.node_selected = 1;
98             c->u.tag.make_variantlist = make_variantlist;
99             c->u.tag.no_data_requested = no_data;
100             c->u.tag.get_bytes = get_bytes;
101         }
102         mark_subtree(c, make_variantlist, no_data, get_bytes, vreq);
103     }
104 }
105
106
107 static void match_triple (data1_handle dh, Z_Variant *vreq,
108                           oid_value defsetval,
109                           oid_value var1, data1_node *n)
110 {
111     data1_node **c;
112
113     if (!(n = n->child))
114         return;
115     if (n->which != DATA1N_variant)
116         return;
117     c = &n->child;
118     while (*c)
119     {
120         int remove_flag = 0;
121         Z_Triple *r;
122         
123         assert ((*c)->which == DATA1N_variant);
124         
125         if ((*c)->u.variant.type->zclass->zclass == 4 &&
126             (*c)->u.variant.type->type == 1)
127         {
128             if ((r = find_triple(vreq, defsetval, var1, 4, 1)) &&
129                 (r->which == Z_Triple_internationalString))
130             {
131                 const char *string_value =
132                     r->value.internationalString;
133                 if (strcmp ((*c)->u.variant.value, string_value))
134                     remove_flag = 1;
135             }
136         }
137         if (remove_flag)
138         {
139             data1_free_tree (dh, *c);
140             *c = (*c)->next;
141         }
142         else
143         {
144             match_triple (dh, vreq, defsetval, var1, *c);
145             c = &(*c)->next;
146         }
147     }
148 }
149                                 
150 static int match_children_here (data1_handle dh, data1_node *n,
151                                 Z_Espec1 *e, int i,
152                                 Z_ETagUnit **t, int num)
153 {
154     int counter = 0, hits = 0;
155     data1_node *c;
156     Z_ETagUnit *tp = *t;
157     Z_Occurrences *occur;
158
159     for (c = n->child; c ; c = c->next)
160     {
161         data1_tag *tag = 0;
162
163         if (c->which != DATA1N_tag)
164             continue;
165
166         if (tp->which == Z_ETagUnit_specificTag)
167         {
168             Z_SpecificTag *want = tp->u.specificTag;
169             occur = want->occurrences;
170             if (c->u.tag.element)
171                 tag = c->u.tag.element->tag;
172             if (*want->tagType != ((tag && tag->tagset) ? tag->tagset->type :
173                 3))
174                 continue;
175             if (want->tagValue->which == Z_StringOrNumeric_numeric)
176             {
177                 if (!tag || tag->which != DATA1T_numeric)
178                     continue;
179                 if (*want->tagValue->u.numeric != tag->value.numeric)
180                     continue;
181             }
182             else
183             {
184                 assert(want->tagValue->which == Z_StringOrNumeric_string);
185                 if (tag && tag->which != DATA1T_string)
186                     continue;
187                 if (data1_matchstr(want->tagValue->u.string,
188                     tag ? tag->value.string : c->u.tag.tag))
189                     continue;
190             }
191         }
192         else
193             occur = tp->u.wildThing;
194
195         /*
196          * Ok, so we have a matching tag. Are we within occurrences-range?
197          */
198         counter++;
199         if (occur && occur->which == Z_Occurrences_last)
200         {
201             yaz_log(YLOG_WARN, "Can't do occurrences=last (yet)");
202             return 0;
203         }
204         if (!occur || occur->which == Z_Occurrences_all ||
205             (occur->which == Z_Occurrences_values && counter >=
206             *occur->u.values->start))
207         {
208             if (match_children(dh, c, e, i, t + 1, num - 1))
209             {
210                 c->u.tag.node_selected = 1;
211                 /*
212                  * Consider the variant specification if this is a complete
213                  * match.
214                  */
215                 if (num == 1)
216                 {
217                     int show_variantlist = 0;
218                     int no_data = 0;
219                     int get_bytes = -1;
220
221                     Z_Variant *vreq =
222                         e->elements[i]->u.simpleElement->variantRequest;
223                     oident *defset = oid_getentbyoid(e->defaultVariantSetId);
224                     oid_value defsetval = defset ? defset->value : VAL_NONE;
225                     oid_value var1 = oid_getvalbyname("Variant-1");
226
227                     if (!vreq)
228                         vreq = e->defaultVariantRequest;
229
230                     if (vreq)
231                     {
232                         Z_Triple *r;
233
234                         /*
235                          * 6,5: meta-data requested, variant list.
236                          */
237                         if (find_triple(vreq, defsetval, var1, 6, 5))
238                             show_variantlist = 1;
239                         /*
240                          * 9,1: Miscellaneous, no data requested.
241                          */
242                         if (find_triple(vreq, defsetval, var1, 9, 1))
243                             no_data = 1;
244
245                         /* howmuch */
246                         if ((r = find_triple(vreq, defsetval, var1, 5, 5)))
247                             if (r->which == Z_Triple_integer)
248                                 get_bytes = *r->value.integer;
249
250                         if (!show_variantlist)
251                             match_triple (dh, vreq, defsetval, var1, c);
252                     }
253                     mark_subtree(c, show_variantlist, no_data, get_bytes, vreq);
254                 }
255                 hits++;
256                 /*
257                  * have we looked at enough children?
258                  */
259                 if (!occur || (occur->which == Z_Occurrences_values &&
260                     (!occur->u.values->howMany ||
261                     counter - *occur->u.values->start >=
262                     *occur->u.values->howMany - 1)))
263                     return hits;
264             }
265         }
266     }
267     return hits;
268 }
269
270 static int match_children(data1_handle dh, data1_node *n, Z_Espec1 *e,
271                           int i, Z_ETagUnit **t, int num)
272 {
273     int res;
274
275     if (!num)
276         return 1;
277     switch (t[0]->which)
278     {
279     case Z_ETagUnit_wildThing:
280     case Z_ETagUnit_specificTag:
281         res = match_children_here(dh, n, e, i, t, num); break;
282     case Z_ETagUnit_wildPath:
283         res = match_children_wildpath(dh, n, e, i, t, num); break;
284     default:
285         abort();
286     }
287     return res;
288 }
289
290 int data1_doespec1 (data1_handle dh, data1_node *n, Z_Espec1 *e)
291 {
292     int i;
293
294     n = data1_get_root_tag (dh, n);
295     if (n && n->which == DATA1N_tag)
296         n->u.tag.node_selected = 1;
297     
298     for (i = 0; i < e->num_elements; i++)
299     {
300         if (e->elements[i]->which != Z_ERequest_simpleElement)
301             return 100;
302         match_children(dh, n, e, i,
303                        e->elements[i]->u.simpleElement->path->tags,
304                        e->elements[i]->u.simpleElement->path->num_tags);
305     }
306     return 0;
307 }