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