1 /* This file is part of the yazpp toolkit.
2 * Copyright (C) 1998-2009 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,
15 Odr_int *next, Odr_int *pres,
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_PresentStatus_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_PresentStatus_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_PresentStatus_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 Odr_int *nulint = (Odr_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 = (Odr_int *)
140 odr_malloc (odr_encode(), sizeof(Odr_int));
141 *res->presentStatus = Z_PresentStatus_success;
143 pack_records(s, req->resultSetName, 1, toget, compp,
144 res->nextResultSetPosition,
146 req->preferredRecordSyntax);
149 if (res->records->which == Z_Records_DBOSD)
150 *res->numberOfRecordsReturned =
151 res->records->u.databaseOrSurDiagnostics->num_records;
152 res->searchStatus = sr;
153 res->resultSetStatus = 0;
158 *res->nextResultSetPosition = 1;
159 res->numberOfRecordsReturned = nulint;
160 res->searchStatus = sr;
161 res->resultSetStatus = 0;
162 res->presentStatus = 0;
166 void Yaz_Facility_Retrieval::fetch_via_present (Z_Server *s,
167 Z_PresentRequest *req,
168 Z_PresentResponse *res)
171 pack_records (s, req->resultSetId,*req->resultSetStartPoint,
172 *req->numberOfRecordsRequested,
173 req->recordComposition,
174 res->nextResultSetPosition,
176 req->preferredRecordSyntax);
177 if (res->records->which == Z_Records_DBOSD)
178 *res->numberOfRecordsReturned =
179 res->records->u.databaseOrSurDiagnostics->num_records;
182 int Yaz_Facility_Retrieval::init(Z_Server *s, Z_InitRequest *initRequest,
183 Z_InitResponse *initResponse)
185 Z_Options *req = initRequest->options;
186 Z_Options *res = initResponse->options;
188 if (ODR_MASK_GET(req, Z_Options_search))
189 ODR_MASK_SET(res, Z_Options_search);
190 if (ODR_MASK_GET(req, Z_Options_present))
191 ODR_MASK_SET(res, Z_Options_present);
192 m_preferredMessageSize = *initRequest->preferredMessageSize;
193 m_maximumRecordSize = *initRequest->maximumRecordSize;
194 return sr_init (initRequest, initResponse);
197 ODR Yaz_Facility_Retrieval::odr_encode()
202 ODR Yaz_Facility_Retrieval::odr_decode()
207 int Yaz_Facility_Retrieval::recv(Z_Server *s, Z_APDU *apdu_request)
209 Z_APDU *apdu_response;
210 m_odr_encode = s->odr_encode();
211 m_odr_decode = s->odr_decode();
212 switch (apdu_request->which)
214 case Z_APDU_searchRequest:
215 apdu_response = s->create_Z_PDU(Z_APDU_searchResponse);
216 s->transfer_referenceId(apdu_request, apdu_response);
217 sr_search (apdu_request->u.searchRequest,
218 apdu_response->u.searchResponse);
219 if (!apdu_response->u.searchResponse->records)
221 fetch_via_piggyback(s, apdu_request->u.searchRequest,
222 apdu_response->u.searchResponse);
224 s->send_Z_PDU(apdu_response, 0);
226 case Z_APDU_presentRequest:
227 apdu_response = s->create_Z_PDU(Z_APDU_presentResponse);
228 s->transfer_referenceId(apdu_request, apdu_response);
229 sr_present (apdu_request->u.presentRequest,
230 apdu_response->u.presentResponse);
231 if (!apdu_response->u.presentResponse->records)
232 fetch_via_present(s, apdu_request->u.presentRequest,
233 apdu_response->u.presentResponse);
234 s->send_Z_PDU(apdu_response, 0);
242 * c-file-style: "Stroustrup"
243 * indent-tabs-mode: nil
245 * vim: shiftwidth=4 tabstop=8 expandtab