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