2 * Copyright (c) 2000-2004, Index Data.
3 * See the file LICENSE for details.
5 * $Id: yaz-z-server-sr.cpp,v 1.12 2006-03-29 13:14:18 adam Exp $
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,
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_PRES_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_PRES_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_PRES_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 int *nulint = (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 = (int *) odr_malloc (odr_encode(), sizeof(int));
143 *res->presentStatus = Z_PRES_SUCCESS;
145 pack_records(s, req->resultSetName, 1, toget, compp,
146 res->nextResultSetPosition,
148 req->preferredRecordSyntax);
151 if (res->records->which == Z_Records_DBOSD)
152 *res->numberOfRecordsReturned =
153 res->records->u.databaseOrSurDiagnostics->num_records;
154 res->searchStatus = sr;
155 res->resultSetStatus = 0;
160 *res->nextResultSetPosition = 1;
161 res->numberOfRecordsReturned = nulint;
162 res->searchStatus = sr;
163 res->resultSetStatus = 0;
164 res->presentStatus = 0;
168 void Yaz_Facility_Retrieval::fetch_via_present (Z_Server *s,
169 Z_PresentRequest *req,
170 Z_PresentResponse *res)
173 pack_records (s, req->resultSetId,*req->resultSetStartPoint,
174 *req->numberOfRecordsRequested,
175 req->recordComposition,
176 res->nextResultSetPosition,
178 req->preferredRecordSyntax);
179 if (res->records->which == Z_Records_DBOSD)
180 *res->numberOfRecordsReturned =
181 res->records->u.databaseOrSurDiagnostics->num_records;
184 int Yaz_Facility_Retrieval::init(Z_Server *s, Z_InitRequest *initRequest,
185 Z_InitResponse *initResponse)
187 Z_Options *req = initRequest->options;
188 Z_Options *res = initResponse->options;
190 if (ODR_MASK_GET(req, Z_Options_search))
191 ODR_MASK_SET(res, Z_Options_search);
192 if (ODR_MASK_GET(req, Z_Options_present))
193 ODR_MASK_SET(res, Z_Options_present);
194 m_preferredMessageSize = *initRequest->preferredMessageSize;
195 m_maximumRecordSize = *initRequest->maximumRecordSize;
196 return sr_init (initRequest, initResponse);
199 ODR Yaz_Facility_Retrieval::odr_encode()
204 ODR Yaz_Facility_Retrieval::odr_decode()
209 int Yaz_Facility_Retrieval::recv(Z_Server *s, Z_APDU *apdu_request)
211 Z_APDU *apdu_response;
212 m_odr_encode = s->odr_encode();
213 m_odr_decode = s->odr_decode();
214 switch (apdu_request->which)
216 case Z_APDU_searchRequest:
217 apdu_response = s->create_Z_PDU(Z_APDU_searchResponse);
218 s->transfer_referenceId(apdu_request, apdu_response);
219 sr_search (apdu_request->u.searchRequest,
220 apdu_response->u.searchResponse);
221 if (!apdu_response->u.searchResponse->records)
223 fetch_via_piggyback(s, apdu_request->u.searchRequest,
224 apdu_response->u.searchResponse);
226 s->send_Z_PDU(apdu_response, 0);
228 case Z_APDU_presentRequest:
229 apdu_response = s->create_Z_PDU(Z_APDU_presentResponse);
230 s->transfer_referenceId(apdu_request, apdu_response);
231 sr_present (apdu_request->u.presentRequest,
232 apdu_response->u.presentResponse);
233 if (!apdu_response->u.presentResponse->records)
234 fetch_via_present(s, apdu_request->u.presentRequest,
235 apdu_response->u.presentResponse);
236 s->send_Z_PDU(apdu_response, 0);
244 * indent-tabs-mode: nil
246 * vim: shiftwidth=4 tabstop=8 expandtab