1 /* This file is part of the yazpp toolkit.
2 * Copyright (C) 1998-2008 Index Data and Mike Taylor
3 * See the file LICENSE for details.
7 #include <yazpp/z-server.h>
9 using namespace yazpp_1;
11 Z_Records *Yaz_Facility_Retrieval::pack_records (Z_Server *s,
12 const char *resultSetName,
14 Z_RecordComposition *comp,
18 int recno, total_length = 0, toget = xnum, dumped_records = 0;
20 (Z_Records *) odr_malloc (odr_encode(), sizeof(*records));
21 Z_NamePlusRecordList *reclist =
22 (Z_NamePlusRecordList *) odr_malloc (odr_encode(), sizeof(*reclist));
23 Z_NamePlusRecord **list =
24 (Z_NamePlusRecord **) odr_malloc (odr_encode(), sizeof(*list) * toget);
26 records->which = Z_Records_DBOSD;
27 records->u.databaseOrSurDiagnostics = reclist;
28 reclist->num_records = 0;
29 reclist->records = list;
30 *pres = Z_PRES_SUCCESS;
33 for (recno = start; reclist->num_records < toget; recno++)
35 Z_NamePlusRecord *this_rec =
36 (Z_NamePlusRecord *) odr_malloc (odr_encode(), sizeof(*this_rec));
37 this_rec->databaseName = 0;
38 this_rec->which = Z_NamePlusRecord_databaseRecord;
39 this_rec->u.databaseRecord = 0;
43 sr_record (resultSetName, recno, format, comp, this_rec, records);
45 if (records->which != Z_Records_DBOSD)
47 *pres = Z_PRES_FAILURE;
51 if (this_rec->which == Z_NamePlusRecord_databaseRecord &&
52 this_rec->u.databaseRecord == 0)
53 { // handler did not return a record..
54 create_surrogateDiagnostics(odr_encode(), this_rec, 0, 14, 0);
57 * we get the number of bytes allocated on the stream before any
58 * allocation done by the backend - this should give us a reasonable
59 * idea of the total size of the data so far.
61 total_length = odr_total(odr_encode()) - dumped_records;
62 this_length = odr_total(odr_encode()) - total_length;
63 if (this_length + total_length > m_preferredMessageSize)
65 /* record is small enough, really */
66 if (this_length <= m_preferredMessageSize)
68 *pres = Z_PRES_PARTIAL_2;
71 if (this_length >= m_maximumRecordSize)
72 { /* too big entirely */
73 reclist->records[reclist->num_records] = this_rec;
74 create_surrogateDiagnostics(odr_encode(), this_rec,
75 this_rec->databaseName, 17, 0);
76 reclist->num_records++;
78 dumped_records += this_length;
81 else /* record can only be fetched by itself */
85 yaz_log(YLOG_DEBUG, " Dropped it");
86 reclist->records[reclist->num_records] = this_rec;
87 create_surrogateDiagnostics(odr_encode(), this_rec,
88 this_rec->databaseName,
90 reclist->num_records++;
91 // *next = freq.last_in_set ? 0 : recno + 1;
93 dumped_records += this_length;
98 reclist->records[reclist->num_records] = this_rec;
99 reclist->num_records++;
105 void Yaz_Facility_Retrieval::fetch_via_piggyback (Z_Server *s,
106 Z_SearchRequest *req,
107 Z_SearchResponse *res)
109 bool_t *sr = (bool_t *)odr_malloc (odr_encode(), sizeof(*sr));
114 Z_RecordComposition comp, *compp = 0;
115 int hits = *res->resultCount;
117 int *nulint = (int *)odr_malloc (odr_encode(), sizeof(*nulint));
120 comp.which = Z_RecordComp_simple;
121 /* how many records does the user agent want, then? */
122 if (hits <= *req->smallSetUpperBound)
125 if ((comp.u.simple = req->smallSetElementSetNames))
128 else if (hits < *req->largeSetLowerBound)
130 toget = *req->mediumSetPresentNumber;
133 if ((comp.u.simple = req->mediumSetElementSetNames))
137 if (toget && !res->records)
139 res->presentStatus = (int *) odr_malloc (odr_encode(), sizeof(int));
140 *res->presentStatus = Z_PRES_SUCCESS;
142 pack_records(s, req->resultSetName, 1, toget, compp,
143 res->nextResultSetPosition,
145 req->preferredRecordSyntax);
148 if (res->records->which == Z_Records_DBOSD)
149 *res->numberOfRecordsReturned =
150 res->records->u.databaseOrSurDiagnostics->num_records;
151 res->searchStatus = sr;
152 res->resultSetStatus = 0;
157 *res->nextResultSetPosition = 1;
158 res->numberOfRecordsReturned = nulint;
159 res->searchStatus = sr;
160 res->resultSetStatus = 0;
161 res->presentStatus = 0;
165 void Yaz_Facility_Retrieval::fetch_via_present (Z_Server *s,
166 Z_PresentRequest *req,
167 Z_PresentResponse *res)
170 pack_records (s, req->resultSetId,*req->resultSetStartPoint,
171 *req->numberOfRecordsRequested,
172 req->recordComposition,
173 res->nextResultSetPosition,
175 req->preferredRecordSyntax);
176 if (res->records->which == Z_Records_DBOSD)
177 *res->numberOfRecordsReturned =
178 res->records->u.databaseOrSurDiagnostics->num_records;
181 int Yaz_Facility_Retrieval::init(Z_Server *s, Z_InitRequest *initRequest,
182 Z_InitResponse *initResponse)
184 Z_Options *req = initRequest->options;
185 Z_Options *res = initResponse->options;
187 if (ODR_MASK_GET(req, Z_Options_search))
188 ODR_MASK_SET(res, Z_Options_search);
189 if (ODR_MASK_GET(req, Z_Options_present))
190 ODR_MASK_SET(res, Z_Options_present);
191 m_preferredMessageSize = *initRequest->preferredMessageSize;
192 m_maximumRecordSize = *initRequest->maximumRecordSize;
193 return sr_init (initRequest, initResponse);
196 ODR Yaz_Facility_Retrieval::odr_encode()
201 ODR Yaz_Facility_Retrieval::odr_decode()
206 int Yaz_Facility_Retrieval::recv(Z_Server *s, Z_APDU *apdu_request)
208 Z_APDU *apdu_response;
209 m_odr_encode = s->odr_encode();
210 m_odr_decode = s->odr_decode();
211 switch (apdu_request->which)
213 case Z_APDU_searchRequest:
214 apdu_response = s->create_Z_PDU(Z_APDU_searchResponse);
215 s->transfer_referenceId(apdu_request, apdu_response);
216 sr_search (apdu_request->u.searchRequest,
217 apdu_response->u.searchResponse);
218 if (!apdu_response->u.searchResponse->records)
220 fetch_via_piggyback(s, apdu_request->u.searchRequest,
221 apdu_response->u.searchResponse);
223 s->send_Z_PDU(apdu_response, 0);
225 case Z_APDU_presentRequest:
226 apdu_response = s->create_Z_PDU(Z_APDU_presentResponse);
227 s->transfer_referenceId(apdu_request, apdu_response);
228 sr_present (apdu_request->u.presentRequest,
229 apdu_response->u.presentResponse);
230 if (!apdu_response->u.presentResponse->records)
231 fetch_via_present(s, apdu_request->u.presentRequest,
232 apdu_response->u.presentResponse);
233 s->send_Z_PDU(apdu_response, 0);
241 * indent-tabs-mode: nil
243 * vim: shiftwidth=4 tabstop=8 expandtab