Added piggyback support for filters backend_test + multi.
[metaproxy-moved-to-github.git] / src / util.cpp
1 /* $Id: util.cpp,v 1.9 2006-01-17 17:55:40 adam Exp $
2    Copyright (c) 2005, Index Data.
3
4 %LICENSE%
5  */
6
7 #include "config.hpp"
8
9 #include <yaz/odr.h>
10 #include <yaz/pquery.h>
11 #include <yaz/otherinfo.h>
12 #include "util.hpp"
13
14 void yp2::util::piggyback(int smallSetUpperBound,
15                           int largeSetLowerBound,
16                           int mediumSetPresentNumber,
17                           int result_set_size,
18                           int &number_to_present)
19 {
20     // deal with piggyback
21
22     if (result_set_size < smallSetUpperBound)
23     {
24         // small set . Return all records in set
25         number_to_present = result_set_size;
26     }
27     else if (result_set_size > largeSetLowerBound)
28     {
29         // large set . Return no records
30         number_to_present = 0;
31     }
32     else
33     {
34         // medium set . Return mediumSetPresentNumber records
35         number_to_present = mediumSetPresentNumber;
36         if (number_to_present > result_set_size)
37             number_to_present = result_set_size;
38     }
39 }
40
41 bool yp2::util::pqf(ODR odr, Z_APDU *apdu, const std::string &q) {
42     YAZ_PQF_Parser pqf_parser = yaz_pqf_create();
43     
44     Z_RPNQuery *rpn = yaz_pqf_parse(pqf_parser, odr, q.c_str());
45     if (!rpn)
46     {
47         yaz_pqf_destroy(pqf_parser);
48         return false;
49     }
50     yaz_pqf_destroy(pqf_parser);
51     Z_Query *query = (Z_Query *) odr_malloc(odr, sizeof(Z_Query));
52     query->which = Z_Query_type_1;
53     query->u.type_1 = rpn;
54     
55     apdu->u.searchRequest->query = query;
56     return true;
57 }
58
59 void yp2::util::get_default_diag(Z_DefaultDiagFormat *r,
60                                  int &error_code, std::string &addinfo)
61 {
62     error_code = *r->condition;
63     switch (r->which)
64     {
65     case Z_DefaultDiagFormat_v2Addinfo:
66         addinfo = std::string(r->u.v2Addinfo);
67         break;
68     case Z_DefaultDiagFormat_v3Addinfo:
69         addinfo = r->u.v3Addinfo;
70         break;
71     }
72 }
73
74 void yp2::util::get_init_diagnostics(Z_InitResponse *initrs,
75                                      int &error_code, std::string &addinfo)
76 {
77     Z_External *uif = initrs->userInformationField;
78     
79     if (uif && uif->which == Z_External_userInfo1)
80     {
81         Z_OtherInformation *ui = uif->u.userInfo1;
82         int i;
83         for (i = 0; i < ui->num_elements; i++)
84         {
85             Z_OtherInformationUnit *unit = ui->list[i];
86             if (unit->which == Z_OtherInfo_externallyDefinedInfo &&
87                 unit->information.externallyDefinedInfo &&
88                 unit->information.externallyDefinedInfo->which ==
89                 Z_External_diag1) 
90             {
91                 Z_DiagnosticFormat *diag = 
92                     unit->information.externallyDefinedInfo->u.diag1;
93
94                 if (diag->num > 0)
95                 {
96                     Z_DiagnosticFormat_s *ds = diag->elements[0];
97                     if (ds->which == Z_DiagnosticFormat_s_defaultDiagRec)
98                         yp2::util::get_default_diag(ds->u.defaultDiagRec,
99                                                     error_code, addinfo);
100                 }
101             } 
102         }
103     }
104 }
105
106 int yp2::util::get_vhost_otherinfo(Z_OtherInformation **otherInformation,
107                                    bool remove_flag,
108                                    std::list<std::string> &vhosts)
109 {
110     int cat;
111     for (cat = 1; ; cat++)
112     {
113         // check virtual host
114         const char *vhost =
115             yaz_oi_get_string_oidval(otherInformation,
116                                      VAL_PROXY, 
117                                      cat /* categoryValue */,
118                                      remove_flag /* delete flag */);
119         if (!vhost)
120             break;
121         vhosts.push_back(std::string(vhost));
122     }
123     --cat;
124     return cat;
125 }
126
127 void yp2::util::split_zurl(std::string zurl, std::string &host,
128                            std::list<std::string> &db)
129 {
130     const char *zurl_cstr = zurl.c_str();
131     const char *sep = strchr(zurl_cstr, '/');
132     
133     if (sep)
134     {
135         host = std::string(zurl_cstr, sep - zurl_cstr);
136
137         const char *cp1 = sep+1;
138         while(1)
139         {
140             const char *cp2 = strchr(cp1, '+');
141             if (cp2)
142                 db.push_back(std::string(cp1, cp2 - cp1));
143             else
144             {
145                 db.push_back(std::string(cp1));
146                 break;
147             }
148             cp1 = cp2+1;
149         }
150     }
151     else
152     {
153         host = zurl;
154     }
155 }
156
157 bool yp2::util::set_databases_from_zurl(ODR odr, std::string zurl,
158                                         int *db_num, char ***db_strings)
159 {
160     std::string host;
161     std::list<std::string> dblist;
162
163     split_zurl(zurl, host, dblist);
164    
165     if (dblist.size() == 0)
166         return false;
167     *db_num = dblist.size();
168     *db_strings = (char **) odr_malloc(odr, sizeof(char*) * (*db_num));
169
170     std::list<std::string>::const_iterator it = dblist.begin();
171     for (int i = 0; it != dblist.end(); it++, i++)
172         (*db_strings)[i] = odr_strdup(odr, it->c_str());
173     return true;
174 }
175
176 yp2::odr::odr(int type)
177 {
178     m_odr = odr_createmem(type);
179 }
180
181 yp2::odr::odr()
182 {
183     m_odr = odr_createmem(ODR_ENCODE);
184 }
185
186 yp2::odr::~odr()
187 {
188     odr_destroy(m_odr);
189 }
190
191 yp2::odr::operator ODR() const
192 {
193     return m_odr;
194 }
195
196 Z_APDU *yp2::odr::create_close(Z_APDU *in_apdu,
197                                int reason, const char *addinfo)
198 {
199     Z_APDU *apdu = create_APDU(Z_APDU_close, in_apdu);
200     
201     *apdu->u.close->closeReason = reason;
202     if (addinfo)
203         apdu->u.close->diagnosticInformation = odr_strdup(m_odr, addinfo);
204     return apdu;
205 }
206
207 Z_APDU *yp2::odr::create_APDU(int type, Z_APDU *in_apdu)
208 {
209     return yp2::util::create_APDU(m_odr, type, in_apdu);
210 }
211
212 Z_APDU *yp2::util::create_APDU(ODR odr, int type, Z_APDU *in_apdu)
213 {
214     Z_APDU *out_apdu = zget_APDU(odr, type);
215
216     Z_ReferenceId **id_to = yp2::util::get_referenceId(out_apdu);
217     *id_to = 0;
218     if (in_apdu)
219     {
220         Z_ReferenceId **id_from = yp2::util::get_referenceId(in_apdu);
221         if (id_from && *id_from && id_to)
222         {
223             *id_to = (Z_ReferenceId*) odr_malloc (odr, sizeof(**id_to));
224             (*id_to)->size = (*id_to)->len = (*id_from)->len;
225             (*id_to)->buf = (unsigned char*) odr_malloc(odr, (*id_to)->len);
226             memcpy((*id_to)->buf, (*id_from)->buf, (*id_to)->len);
227         }
228         else if (id_to)
229             *id_to = 0;
230     }
231     return out_apdu;
232 }
233
234 Z_APDU *yp2::odr::create_initResponse(Z_APDU *in_apdu,
235                                       int error, const char *addinfo)
236 {
237     Z_APDU *apdu = create_APDU(Z_APDU_initResponse, in_apdu);
238     if (error)
239     {
240         apdu->u.initResponse->userInformationField =
241             zget_init_diagnostics(m_odr, error, addinfo);
242         *apdu->u.initResponse->result = 0;
243     }
244     return apdu;
245 }
246
247 Z_APDU *yp2::odr::create_searchResponse(Z_APDU *in_apdu,
248                                         int error, const char *addinfo)
249 {
250     Z_APDU *apdu = create_APDU(Z_APDU_searchResponse, in_apdu);
251     if (error)
252     {
253         Z_Records *rec = (Z_Records *) odr_malloc(m_odr, sizeof(Z_Records));
254         *apdu->u.searchResponse->searchStatus = 0;
255         apdu->u.searchResponse->records = rec;
256         rec->which = Z_Records_NSD;
257         rec->u.nonSurrogateDiagnostic =
258             zget_DefaultDiagFormat(m_odr, error, addinfo);
259         
260     }
261     return apdu;
262 }
263
264 Z_APDU *yp2::odr::create_presentResponse(Z_APDU *in_apdu,
265                                          int error, const char *addinfo)
266 {
267     Z_APDU *apdu = create_APDU(Z_APDU_presentResponse, in_apdu);
268     if (error)
269     {
270         Z_Records *rec = (Z_Records *) odr_malloc(m_odr, sizeof(Z_Records));
271         apdu->u.presentResponse->records = rec;
272         rec->which = Z_Records_NSD;
273         rec->u.nonSurrogateDiagnostic =
274             zget_DefaultDiagFormat(m_odr, error, addinfo);
275         *apdu->u.presentResponse->presentStatus = Z_PresentStatus_failure;
276     }
277     return apdu;
278 }
279
280 Z_APDU *yp2::odr::create_scanResponse(Z_APDU *in_apdu,
281                                       int error, const char *addinfo)
282 {
283     Z_APDU *apdu = create_APDU(Z_APDU_scanResponse, in_apdu);
284     if (error)
285     {
286         Z_ScanResponse *res = apdu->u.scanResponse;
287         res->entries = (Z_ListEntries *) odr_malloc(m_odr, sizeof(*res->entries));
288         *res->scanStatus = Z_Scan_failure;
289
290         res->entries->num_entries = 0;
291         res->entries->entries = 0;
292         res->entries->num_nonsurrogateDiagnostics = 1;
293         res->entries->nonsurrogateDiagnostics = (Z_DiagRec **)
294             odr_malloc(m_odr, sizeof(Z_DiagRec *));
295         res->entries->nonsurrogateDiagnostics[0] = 
296             zget_DiagRec(m_odr, error, addinfo);
297     }
298     return apdu;
299 }
300
301 Z_ReferenceId **yp2::util::get_referenceId(Z_APDU *apdu)
302 {
303     switch (apdu->which)
304     {
305     case  Z_APDU_initRequest:
306         return &apdu->u.initRequest->referenceId; 
307     case  Z_APDU_initResponse:
308         return &apdu->u.initResponse->referenceId;
309     case  Z_APDU_searchRequest:
310         return &apdu->u.searchRequest->referenceId;
311     case  Z_APDU_searchResponse:
312         return &apdu->u.searchResponse->referenceId;
313     case  Z_APDU_presentRequest:
314         return &apdu->u.presentRequest->referenceId;
315     case  Z_APDU_presentResponse:
316         return &apdu->u.presentResponse->referenceId;
317     case  Z_APDU_deleteResultSetRequest:
318         return &apdu->u.deleteResultSetRequest->referenceId;
319     case  Z_APDU_deleteResultSetResponse:
320         return &apdu->u.deleteResultSetResponse->referenceId;
321     case  Z_APDU_accessControlRequest:
322         return &apdu->u.accessControlRequest->referenceId;
323     case  Z_APDU_accessControlResponse:
324         return &apdu->u.accessControlResponse->referenceId;
325     case  Z_APDU_resourceControlRequest:
326         return &apdu->u.resourceControlRequest->referenceId;
327     case  Z_APDU_resourceControlResponse:
328         return &apdu->u.resourceControlResponse->referenceId;
329     case  Z_APDU_triggerResourceControlRequest:
330         return &apdu->u.triggerResourceControlRequest->referenceId;
331     case  Z_APDU_resourceReportRequest:
332         return &apdu->u.resourceReportRequest->referenceId;
333     case  Z_APDU_resourceReportResponse:
334         return &apdu->u.resourceReportResponse->referenceId;
335     case  Z_APDU_scanRequest:
336         return &apdu->u.scanRequest->referenceId;
337     case  Z_APDU_scanResponse:
338         return &apdu->u.scanResponse->referenceId;
339     case  Z_APDU_sortRequest:
340         return &apdu->u.sortRequest->referenceId;
341     case  Z_APDU_sortResponse:
342         return &apdu->u.sortResponse->referenceId;
343     case  Z_APDU_segmentRequest:
344         return &apdu->u.segmentRequest->referenceId;
345     case  Z_APDU_extendedServicesRequest:
346         return &apdu->u.extendedServicesRequest->referenceId;
347     case  Z_APDU_extendedServicesResponse:
348         return &apdu->u.extendedServicesResponse->referenceId;
349     case  Z_APDU_close:
350         return &apdu->u.close->referenceId;
351     }
352     return 0;
353 }
354
355
356 /*
357  * Local variables:
358  * c-basic-offset: 4
359  * indent-tabs-mode: nil
360  * c-file-style: "stroustrup"
361  * End:
362  * vim: shiftwidth=4 tabstop=8 expandtab
363  */