36270720961cc8fdfce718e0865eeca3e796c182
[yaz-moved-to-github.git] / retrieval / d1_doespec.c
1 /*
2  * Copyright (c) 1995, Index Data.
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: d1_doespec.c,v $
7  * Revision 1.9  1997-09-17 12:10:35  adam
8  * YAZ version 1.4.
9  *
10  * Revision 1.8  1997/05/14 06:54:02  adam
11  * C++ support.
12  *
13  * Revision 1.7  1997/04/30 08:52:11  quinn
14  * Null
15  *
16  * Revision 1.6  1996/10/11  11:57:22  quinn
17  * Smallish
18  *
19  * Revision 1.5  1996/07/06  19:58:34  quinn
20  * System headerfiles gathered in yconfig
21  *
22  * Revision 1.4  1996/06/07  11:04:32  quinn
23  * Fixed tag->tagset dependency
24  *
25  * Revision 1.3  1995/11/13  09:27:33  quinn
26  * Fiddling with the variant stuff.
27  *
28  * Revision 1.2  1995/11/01  13:54:45  quinn
29  * Minor adjustments
30  *
31  * Revision 1.1  1995/11/01  11:56:07  quinn
32  * Added Retrieval (data management) functions en masse.
33  *
34  *
35  */
36
37
38 #include <assert.h>
39 #include <oid.h>
40 #include <log.h>
41 #include <proto.h>
42 #include <data1.h>
43
44 static int match_children(data1_node *n, Z_Espec1 *e, int i, Z_ETagUnit **t,
45     int num);
46
47 static int match_children_wildpath(data1_node *n, Z_Espec1 *e, int i,
48     Z_ETagUnit **t, int num)
49 {return 0;}
50
51 /*
52  * Locate a specific triple within a variant.
53  * set is the set to look for, universal set is the set that applies to a
54  * triple with an unknown set.
55  */
56 static Z_Triple *find_triple(Z_Variant *var, oid_value universalset,
57     oid_value set, int zclass, int type)
58 {
59     int i;
60     oident *defaultsetent = oid_getentbyoid(var->globalVariantSetId);
61     oid_value defaultset = defaultsetent ? defaultsetent->value :
62         universalset;
63
64     for (i = 0; i < var->num_triples; i++)
65     {
66         oident *cursetent =
67             oid_getentbyoid(var->triples[i]->variantSetId);
68         oid_value curset = cursetent ? cursetent->value : defaultset;
69
70         if (set == curset &&
71             *var->triples[i]->zclass == zclass &&
72             *var->triples[i]->type == type)
73             return var->triples[i];
74     }
75     return 0;
76 }
77
78 static void mark_subtree(data1_node *n, int make_variantlist, int no_data,
79     int get_bytes, Z_Variant *vreq)
80 {
81     data1_node *c;
82
83     if (n->which == DATA1N_tag && (!n->child || n->child->which != DATA1N_tag))
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 static int match_children_here(data1_node *n, Z_Espec1 *e, int i,
106     Z_ETagUnit **t, int num)
107 {
108     int counter = 0, hits = 0;
109     data1_node *c;
110     Z_ETagUnit *tp = *t;
111     Z_Occurrences *occur;
112
113     for (c = n->child; c ; c = c->next)
114     {
115         data1_tag *tag = 0;
116
117         if (c->which != DATA1N_tag)
118             return 0;
119
120         if (tp->which == Z_ETagUnit_specificTag)
121         {
122             Z_SpecificTag *want = tp->u.specificTag;
123             occur = want->occurrences;
124             if (c->u.tag.element)
125                 tag = c->u.tag.element->tag;
126             if (*want->tagType != ((tag && tag->tagset) ? tag->tagset->type :
127                 3))
128                 continue;
129             if (want->tagValue->which == Z_StringOrNumeric_numeric)
130             {
131                 if (!tag || tag->which != DATA1T_numeric)
132                     continue;
133                 if (*want->tagValue->u.numeric != tag->value.numeric)
134                     continue;
135             }
136             else
137             {
138                 assert(want->tagValue->which == Z_StringOrNumeric_string);
139                 if (tag && tag->which != DATA1T_string)
140                     continue;
141                 if (data1_matchstr(want->tagValue->u.string,
142                     tag ? tag->value.string : c->u.tag.tag))
143                     continue;
144             }
145         }
146         else
147             occur = tp->u.wildThing;
148
149         /*
150          * Ok, so we have a matching tag. Are we within occurrences-range?
151          */
152         counter++;
153         if (occur && occur->which == Z_Occurrences_last)
154         {
155             logf(LOG_WARN, "Can't do occurrences=last (yet)");
156             return 0;
157         }
158         if (!occur || occur->which == Z_Occurrences_all ||
159             (occur->which == Z_Occurrences_values && counter >=
160             *occur->u.values->start))
161         {
162             if (match_children(c, e, i, t + 1, num - 1))
163             {
164                 c->u.tag.node_selected = 1;
165                 /*
166                  * Consider the variant specification if this is a complete
167                  * match.
168                  */
169                 if (num == 1)
170                 {
171                     int show_variantlist = 0;
172                     int no_data = 0;
173                     int get_bytes = -1;
174
175                     Z_Variant *vreq =
176                         e->elements[i]->u.simpleElement->variantRequest;
177                     oident *defset = oid_getentbyoid(e->defaultVariantSetId);
178                     oid_value defsetval = defset ? defset->value : VAL_NONE;
179                     oid_value var1 = oid_getvalbyname("Variant-1");
180
181                     if (!vreq)
182                         vreq = e->defaultVariantRequest;
183
184                     if (vreq)
185                     {
186                         Z_Triple *r;
187
188                         /*
189                          * 6,5: meta-data requested, variant list.
190                          */
191                         if (find_triple(vreq, defsetval, var1, 6, 5))
192                             show_variantlist = 1;
193                         /*
194                          * 9,1: Miscellaneous, no data requested.
195                          */
196                         if (find_triple(vreq, defsetval, var1, 9, 1))
197                             no_data = 1;
198
199                         /* howmuch */
200                         if ((r = find_triple(vreq, defsetval, var1, 5, 5)))
201                             if (r->which == Z_Triple_integer)
202                                 get_bytes = *r->value.integer;
203                     }
204                     mark_subtree(c, show_variantlist, no_data, get_bytes, vreq);
205                 }
206                 hits++;
207                 /*
208                  * have we looked at enough children?
209                  */
210                 if (!occur || (occur->which == Z_Occurrences_values &&
211                     (!occur->u.values->howMany ||
212                     counter - *occur->u.values->start >=
213                     *occur->u.values->howMany - 1)))
214                     return hits;
215             }
216         }
217     }
218     return hits;
219 }
220
221 static int match_children(data1_node *n, Z_Espec1 *e, int i, Z_ETagUnit **t,
222     int num)
223 {
224     int res;
225
226     if (!num)
227         return 1;
228     switch (t[0]->which)
229     {
230         case Z_ETagUnit_wildThing:
231         case Z_ETagUnit_specificTag: res = match_children_here(n, e, i,
232             t, num); break;
233         case Z_ETagUnit_wildPath: res = match_children_wildpath(n, e, i,
234             t, num); break;
235         default:
236             abort();
237     }
238     return res;
239 }
240
241 int data1_doespec1 (data1_handle dh, data1_node *n, Z_Espec1 *e)
242 {
243     int i;
244
245     for (i = 0; i < e->num_elements; i++)
246     {
247         if (e->elements[i]->which != Z_ERequest_simpleElement)
248             return 100;
249         match_children(n, e, i, e->elements[i]->u.simpleElement->path->tags,
250             e->elements[i]->u.simpleElement->path->num_tags);
251     }
252     return 0;
253 }