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