2 * Copyright (c) 2000-2001, Index Data.
3 * See the file LICENSE for details.
5 * $Id: yaz-z-server-sr.cpp,v 1.6 2003-10-01 13:13:51 adam Exp $
10 #include <yaz++/z-server.h>
12 Z_Records *Yaz_Facility_Retrieval::pack_records (Yaz_Z_Server *s,
13 const char *resultSetName,
15 Z_RecordComposition *comp,
19 int recno, total_length = 0, toget = xnum, dumped_records = 0;
21 (Z_Records *) odr_malloc (odr_encode(), sizeof(*records));
22 Z_NamePlusRecordList *reclist =
23 (Z_NamePlusRecordList *) odr_malloc (odr_encode(), sizeof(*reclist));
24 Z_NamePlusRecord **list =
25 (Z_NamePlusRecord **) odr_malloc (odr_encode(), sizeof(*list) * toget);
27 records->which = Z_Records_DBOSD;
28 records->u.databaseOrSurDiagnostics = reclist;
29 reclist->num_records = 0;
30 reclist->records = list;
31 *pres = Z_PRES_SUCCESS;
34 yaz_log(LOG_LOG, "Request to pack %d+%d", start, toget);
35 yaz_log(LOG_LOG, "pms=%d, mrs=%d", m_preferredMessageSize,
37 for (recno = start; reclist->num_records < toget; recno++)
39 Z_NamePlusRecord *this_rec =
40 (Z_NamePlusRecord *) odr_malloc (odr_encode(), sizeof(*this_rec));
41 this_rec->databaseName = 0;
42 this_rec->which = Z_NamePlusRecord_databaseRecord;
43 this_rec->u.databaseRecord = 0;
47 sr_record (resultSetName, recno, format, comp, this_rec, records);
49 if (records->which != Z_Records_DBOSD)
51 *pres = Z_PRES_FAILURE;
55 if (this_rec->which == Z_NamePlusRecord_databaseRecord &&
56 this_rec->u.databaseRecord == 0)
57 { // handler did not return a record..
58 create_surrogateDiagnostics(odr_encode(), this_rec, 0, 14, 0);
61 * we get the number of bytes allocated on the stream before any
62 * allocation done by the backend - this should give us a reasonable
63 * idea of the total size of the data so far.
65 total_length = odr_total(odr_encode()) - dumped_records;
66 this_length = odr_total(odr_encode()) - total_length;
67 yaz_log(LOG_LOG, " fetched record, len=%d, total=%d",
68 this_length, total_length);
69 if (this_length + total_length > m_preferredMessageSize)
71 /* record is small enough, really */
72 if (this_length <= m_preferredMessageSize)
74 yaz_log(LOG_LOG, " Dropped last normal-sized record");
75 *pres = Z_PRES_PARTIAL_2;
78 if (this_length >= m_maximumRecordSize)
79 { /* too big entirely */
80 yaz_log(LOG_LOG, "Record > maxrcdsz");
81 reclist->records[reclist->num_records] = this_rec;
82 create_surrogateDiagnostics(odr_encode(), this_rec,
83 this_rec->databaseName, 17, 0);
84 reclist->num_records++;
86 dumped_records += this_length;
89 else /* record can only be fetched by itself */
91 yaz_log(LOG_LOG, " Record > prefmsgsz");
94 yaz_log(LOG_DEBUG, " Dropped it");
95 reclist->records[reclist->num_records] = this_rec;
96 create_surrogateDiagnostics(odr_encode(), this_rec,
97 this_rec->databaseName,
99 reclist->num_records++;
100 // *next = freq.last_in_set ? 0 : recno + 1;
102 dumped_records += this_length;
107 reclist->records[reclist->num_records] = this_rec;
108 reclist->num_records++;
114 void Yaz_Facility_Retrieval::fetch_via_piggyback (Yaz_Z_Server *s,
115 Z_SearchRequest *req,
116 Z_SearchResponse *res)
118 bool_t *sr = (bool_t *)odr_malloc (odr_encode(), sizeof(*sr));
123 Z_RecordComposition comp, *compp = 0;
124 int hits = *res->resultCount;
126 int *nulint = (int *)odr_malloc (odr_encode(), sizeof(*nulint));
129 comp.which = Z_RecordComp_simple;
130 /* how many records does the user agent want, then? */
131 if (hits <= *req->smallSetUpperBound)
134 if ((comp.u.simple = req->smallSetElementSetNames))
137 else if (hits < *req->largeSetLowerBound)
139 toget = *req->mediumSetPresentNumber;
142 if ((comp.u.simple = req->mediumSetElementSetNames))
146 if (toget && !res->records)
148 res->presentStatus = (int *) odr_malloc (odr_encode(), sizeof(int));
149 *res->presentStatus = Z_PRES_SUCCESS;
151 pack_records(s, req->resultSetName, 1, toget, compp,
152 res->nextResultSetPosition,
154 req->preferredRecordSyntax);
157 if (res->records->which == Z_Records_DBOSD)
158 *res->numberOfRecordsReturned =
159 res->records->u.databaseOrSurDiagnostics->num_records;
160 res->searchStatus = sr;
161 res->resultSetStatus = 0;
166 *res->nextResultSetPosition = 1;
167 res->numberOfRecordsReturned = nulint;
168 res->searchStatus = sr;
169 res->resultSetStatus = 0;
170 res->presentStatus = 0;
174 void Yaz_Facility_Retrieval::fetch_via_present (Yaz_Z_Server *s,
175 Z_PresentRequest *req,
176 Z_PresentResponse *res)
179 pack_records (s, req->resultSetId,*req->resultSetStartPoint,
180 *req->numberOfRecordsRequested,
181 req->recordComposition,
182 res->nextResultSetPosition,
184 req->preferredRecordSyntax);
185 if (res->records->which == Z_Records_DBOSD)
186 *res->numberOfRecordsReturned =
187 res->records->u.databaseOrSurDiagnostics->num_records;
190 int Yaz_Facility_Retrieval::init(Yaz_Z_Server *s, Z_InitRequest *initRequest,
191 Z_InitResponse *initResponse)
193 Z_Options *req = initRequest->options;
194 Z_Options *res = initResponse->options;
196 if (ODR_MASK_GET(req, Z_Options_search))
197 ODR_MASK_SET(res, Z_Options_search);
198 if (ODR_MASK_GET(req, Z_Options_present))
199 ODR_MASK_SET(res, Z_Options_present);
200 m_preferredMessageSize = *initRequest->preferredMessageSize;
201 m_maximumRecordSize = *initRequest->maximumRecordSize;
202 return sr_init (initRequest, initResponse);
205 ODR Yaz_Facility_Retrieval::odr_encode()
210 ODR Yaz_Facility_Retrieval::odr_decode()
215 int Yaz_Facility_Retrieval::recv(Yaz_Z_Server *s, Z_APDU *apdu_request)
217 Z_APDU *apdu_response;
218 m_odr_encode = s->odr_encode();
219 m_odr_decode = s->odr_decode();
220 switch (apdu_request->which)
222 case Z_APDU_searchRequest:
223 yaz_log (LOG_LOG, "got SearchRequest p=%p", this);
224 apdu_response = s->create_Z_PDU(Z_APDU_searchResponse);
225 s->transfer_referenceId(apdu_request, apdu_response);
226 sr_search (apdu_request->u.searchRequest,
227 apdu_response->u.searchResponse);
228 if (!apdu_response->u.searchResponse->records)
230 fetch_via_piggyback(s, apdu_request->u.searchRequest,
231 apdu_response->u.searchResponse);
233 s->send_Z_PDU(apdu_response, 0);
235 case Z_APDU_presentRequest:
236 yaz_log (LOG_LOG, "got PresentRequest p=%p", this);
237 apdu_response = s->create_Z_PDU(Z_APDU_presentResponse);
238 s->transfer_referenceId(apdu_request, apdu_response);
239 sr_present (apdu_request->u.presentRequest,
240 apdu_response->u.presentResponse);
241 if (!apdu_response->u.presentResponse->records)
242 fetch_via_present(s, apdu_request->u.presentRequest,
243 apdu_response->u.presentResponse);
244 s->send_Z_PDU(apdu_response, 0);