1 /* This file is part of the yazpp toolkit.
2 * Copyright (C) Index Data
3 * See the file LICENSE for details.
10 #include <yazpp/z-server.h>
12 using namespace yazpp_1;
14 Z_Records *Yaz_Facility_Retrieval::pack_records (Z_Server *s,
15 const char *resultSetName,
17 Z_RecordComposition *comp,
18 Odr_int *next, Odr_int *pres,
21 int recno, total_length = 0, toget = xnum, dumped_records = 0;
23 (Z_Records *) odr_malloc (odr_encode(), sizeof(*records));
24 Z_NamePlusRecordList *reclist =
25 (Z_NamePlusRecordList *) odr_malloc (odr_encode(), sizeof(*reclist));
26 Z_NamePlusRecord **list =
27 (Z_NamePlusRecord **) odr_malloc (odr_encode(), sizeof(*list) * toget);
29 records->which = Z_Records_DBOSD;
30 records->u.databaseOrSurDiagnostics = reclist;
31 reclist->num_records = 0;
32 reclist->records = list;
33 *pres = Z_PresentStatus_success;
36 for (recno = start; reclist->num_records < toget; recno++)
38 Z_NamePlusRecord *this_rec =
39 (Z_NamePlusRecord *) odr_malloc (odr_encode(), sizeof(*this_rec));
40 this_rec->databaseName = 0;
41 this_rec->which = Z_NamePlusRecord_databaseRecord;
42 this_rec->u.databaseRecord = 0;
46 sr_record (resultSetName, recno, format, comp, this_rec, records);
48 if (records->which != Z_Records_DBOSD)
50 *pres = Z_PresentStatus_failure;
54 if (this_rec->which == Z_NamePlusRecord_databaseRecord &&
55 this_rec->u.databaseRecord == 0)
56 { // handler did not return a record..
57 create_surrogateDiagnostics(odr_encode(), this_rec, 0, 14, 0);
60 * we get the number of bytes allocated on the stream before any
61 * allocation done by the backend - this should give us a reasonable
62 * idea of the total size of the data so far.
64 total_length = odr_total(odr_encode()) - dumped_records;
65 this_length = odr_total(odr_encode()) - total_length;
66 if (this_length + total_length > m_preferredMessageSize)
68 /* record is small enough, really */
69 if (this_length <= m_preferredMessageSize)
71 *pres = Z_PresentStatus_partial_2;
74 if (this_length >= m_maximumRecordSize)
75 { /* too big entirely */
76 reclist->records[reclist->num_records] = this_rec;
77 create_surrogateDiagnostics(odr_encode(), this_rec,
78 this_rec->databaseName, 17, 0);
79 reclist->num_records++;
81 dumped_records += this_length;
84 else /* record can only be fetched by itself */
88 yaz_log(YLOG_DEBUG, " Dropped it");
89 reclist->records[reclist->num_records] = this_rec;
90 create_surrogateDiagnostics(odr_encode(), this_rec,
91 this_rec->databaseName,
93 reclist->num_records++;
94 // *next = freq.last_in_set ? 0 : recno + 1;
96 dumped_records += this_length;
101 reclist->records[reclist->num_records] = this_rec;
102 reclist->num_records++;
108 void Yaz_Facility_Retrieval::fetch_via_piggyback (Z_Server *s,
109 Z_SearchRequest *req,
110 Z_SearchResponse *res)
112 bool_t *sr = (bool_t *)odr_malloc (odr_encode(), sizeof(*sr));
117 Z_RecordComposition comp, *compp = 0;
118 int hits = *res->resultCount;
120 Odr_int *nulint = (Odr_int *)odr_malloc (odr_encode(), sizeof(*nulint));
123 comp.which = Z_RecordComp_simple;
124 /* how many records does the user agent want, then? */
125 if (hits <= *req->smallSetUpperBound)
128 if ((comp.u.simple = req->smallSetElementSetNames))
131 else if (hits < *req->largeSetLowerBound)
133 toget = *req->mediumSetPresentNumber;
136 if ((comp.u.simple = req->mediumSetElementSetNames))
140 if (toget && !res->records)
142 res->presentStatus = (Odr_int *)
143 odr_malloc (odr_encode(), sizeof(Odr_int));
144 *res->presentStatus = Z_PresentStatus_success;
146 pack_records(s, req->resultSetName, 1, toget, compp,
147 res->nextResultSetPosition,
149 req->preferredRecordSyntax);
152 if (res->records->which == Z_Records_DBOSD)
153 *res->numberOfRecordsReturned =
154 res->records->u.databaseOrSurDiagnostics->num_records;
155 res->searchStatus = sr;
156 res->resultSetStatus = 0;
161 *res->nextResultSetPosition = 1;
162 res->numberOfRecordsReturned = nulint;
163 res->searchStatus = sr;
164 res->resultSetStatus = 0;
165 res->presentStatus = 0;
169 void Yaz_Facility_Retrieval::fetch_via_present (Z_Server *s,
170 Z_PresentRequest *req,
171 Z_PresentResponse *res)
174 pack_records (s, req->resultSetId,*req->resultSetStartPoint,
175 *req->numberOfRecordsRequested,
176 req->recordComposition,
177 res->nextResultSetPosition,
179 req->preferredRecordSyntax);
180 if (res->records->which == Z_Records_DBOSD)
181 *res->numberOfRecordsReturned =
182 res->records->u.databaseOrSurDiagnostics->num_records;
185 int Yaz_Facility_Retrieval::init(Z_Server *s, Z_InitRequest *initRequest,
186 Z_InitResponse *initResponse)
188 Z_Options *req = initRequest->options;
189 Z_Options *res = initResponse->options;
191 if (ODR_MASK_GET(req, Z_Options_search))
192 ODR_MASK_SET(res, Z_Options_search);
193 if (ODR_MASK_GET(req, Z_Options_present))
194 ODR_MASK_SET(res, Z_Options_present);
195 m_preferredMessageSize = *initRequest->preferredMessageSize;
196 m_maximumRecordSize = *initRequest->maximumRecordSize;
197 return sr_init (initRequest, initResponse);
200 ODR Yaz_Facility_Retrieval::odr_encode()
205 ODR Yaz_Facility_Retrieval::odr_decode()
210 int Yaz_Facility_Retrieval::recv(Z_Server *s, Z_APDU *apdu_request)
212 Z_APDU *apdu_response;
213 m_odr_encode = s->odr_encode();
214 m_odr_decode = s->odr_decode();
215 switch (apdu_request->which)
217 case Z_APDU_searchRequest:
218 apdu_response = s->create_Z_PDU(Z_APDU_searchResponse);
219 s->transfer_referenceId(apdu_request, apdu_response);
220 sr_search (apdu_request->u.searchRequest,
221 apdu_response->u.searchResponse);
222 if (!apdu_response->u.searchResponse->records)
224 fetch_via_piggyback(s, apdu_request->u.searchRequest,
225 apdu_response->u.searchResponse);
227 s->send_Z_PDU(apdu_response, 0);
229 case Z_APDU_presentRequest:
230 apdu_response = s->create_Z_PDU(Z_APDU_presentResponse);
231 s->transfer_referenceId(apdu_request, apdu_response);
232 sr_present (apdu_request->u.presentRequest,
233 apdu_response->u.presentResponse);
234 if (!apdu_response->u.presentResponse->records)
235 fetch_via_present(s, apdu_request->u.presentRequest,
236 apdu_response->u.presentResponse);
237 s->send_Z_PDU(apdu_response, 0);
245 * c-file-style: "Stroustrup"
246 * indent-tabs-mode: nil
248 * vim: shiftwidth=4 tabstop=8 expandtab