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