2 * Copyright (c) 2000-2001, Index Data.
3 * See the file LICENSE for details.
5 * $Log: yaz-z-server-sr.cpp,v $
6 * Revision 1.2 2001-04-04 14:02:49 adam
7 * URSULA / Z-ruth service.
9 * Revision 1.1 2001/03/27 15:02:14 adam
10 * New server facility scheme.
15 #include <yaz++/yaz-z-server.h>
17 Z_Records *Yaz_Facility_Retrieval::pack_records (Yaz_Z_Server *s,
18 const char *resultSetName,
20 Z_RecordComposition *comp,
24 int recno, total_length = 0, toget = xnum, dumped_records = 0;
26 (Z_Records *) odr_malloc (odr_encode(), sizeof(*records));
27 Z_NamePlusRecordList *reclist =
28 (Z_NamePlusRecordList *) odr_malloc (odr_encode(), sizeof(*reclist));
29 Z_NamePlusRecord **list =
30 (Z_NamePlusRecord **) odr_malloc (odr_encode(), sizeof(*list) * toget);
32 records->which = Z_Records_DBOSD;
33 records->u.databaseOrSurDiagnostics = reclist;
34 reclist->num_records = 0;
35 reclist->records = list;
36 *pres = Z_PRES_SUCCESS;
39 yaz_log(LOG_LOG, "Request to pack %d+%d", start, toget);
40 yaz_log(LOG_LOG, "pms=%d, mrs=%d", m_preferredMessageSize,
42 for (recno = start; reclist->num_records < toget; recno++)
44 Z_NamePlusRecord *this_rec =
45 (Z_NamePlusRecord *) odr_malloc (odr_encode(), sizeof(*this_rec));
46 this_rec->databaseName = 0;
47 this_rec->which = Z_NamePlusRecord_databaseRecord;
48 this_rec->u.databaseRecord = 0;
52 sr_record (resultSetName, recno, format, comp, this_rec, records);
54 if (records->which != Z_Records_DBOSD)
56 *pres = Z_PRES_FAILURE;
60 if (this_rec->which == Z_NamePlusRecord_databaseRecord &&
61 this_rec->u.databaseRecord == 0)
62 { // handler did not return a record..
63 create_surrogateDiagnostics(odr_encode(), this_rec, 0, 14, 0);
66 * we get the number of bytes allocated on the stream before any
67 * allocation done by the backend - this should give us a reasonable
68 * idea of the total size of the data so far.
70 total_length = odr_total(odr_encode()) - dumped_records;
71 this_length = odr_total(odr_encode()) - total_length;
72 yaz_log(LOG_LOG, " fetched record, len=%d, total=%d",
73 this_length, total_length);
74 if (this_length + total_length > m_preferredMessageSize)
76 /* record is small enough, really */
77 if (this_length <= m_preferredMessageSize)
79 yaz_log(LOG_LOG, " Dropped last normal-sized record");
80 *pres = Z_PRES_PARTIAL_2;
83 if (this_length >= m_maximumRecordSize)
84 { /* too big entirely */
85 yaz_log(LOG_LOG, "Record > maxrcdsz");
86 reclist->records[reclist->num_records] = this_rec;
87 create_surrogateDiagnostics(odr_encode(), this_rec,
88 this_rec->databaseName, 17, 0);
89 reclist->num_records++;
91 dumped_records += this_length;
94 else /* record can only be fetched by itself */
96 yaz_log(LOG_LOG, " Record > prefmsgsz");
99 yaz_log(LOG_DEBUG, " Dropped it");
100 reclist->records[reclist->num_records] = this_rec;
101 create_surrogateDiagnostics(odr_encode(), this_rec,
102 this_rec->databaseName,
104 reclist->num_records++;
105 // *next = freq.last_in_set ? 0 : recno + 1;
107 dumped_records += this_length;
112 reclist->records[reclist->num_records] = this_rec;
113 reclist->num_records++;
119 void Yaz_Facility_Retrieval::fetch_via_piggyback (Yaz_Z_Server *s,
120 Z_SearchRequest *req,
121 Z_SearchResponse *res)
123 bool_t *sr = (bool_t *)odr_malloc (odr_encode(), sizeof(*sr));
128 Z_RecordComposition comp, *compp = 0;
129 int hits = *res->resultCount;
131 int *nulint = (int *)odr_malloc (odr_encode(), sizeof(*nulint));
134 comp.which = Z_RecordComp_simple;
135 /* how many records does the user agent want, then? */
136 if (hits <= *req->smallSetUpperBound)
139 if ((comp.u.simple = req->smallSetElementSetNames))
142 else if (hits < *req->largeSetLowerBound)
144 toget = *req->mediumSetPresentNumber;
147 if ((comp.u.simple = req->mediumSetElementSetNames))
151 if (toget && !res->records)
153 res->presentStatus = (int *) odr_malloc (odr_encode(), sizeof(int));
154 *res->presentStatus = Z_PRES_SUCCESS;
156 pack_records(s, req->resultSetName, 1, toget, compp,
157 res->nextResultSetPosition,
159 req->preferredRecordSyntax);
162 if (res->records->which == Z_Records_DBOSD)
163 *res->numberOfRecordsReturned =
164 res->records->u.databaseOrSurDiagnostics->num_records;
165 res->searchStatus = sr;
166 res->resultSetStatus = 0;
171 *res->nextResultSetPosition = 1;
172 res->numberOfRecordsReturned = nulint;
173 res->searchStatus = sr;
174 res->resultSetStatus = 0;
175 res->presentStatus = 0;
179 void Yaz_Facility_Retrieval::fetch_via_present (Yaz_Z_Server *s,
180 Z_PresentRequest *req,
181 Z_PresentResponse *res)
184 pack_records (s, req->resultSetId,*req->resultSetStartPoint,
185 *req->numberOfRecordsRequested,
186 req->recordComposition,
187 res->nextResultSetPosition,
189 req->preferredRecordSyntax);
190 if (res->records->which == Z_Records_DBOSD)
191 *res->numberOfRecordsReturned =
192 res->records->u.databaseOrSurDiagnostics->num_records;
195 int Yaz_Facility_Retrieval::init(Yaz_Z_Server *s, Z_InitRequest *initRequest,
196 Z_InitResponse *initResponse)
198 Z_Options *req = initRequest->options;
199 Z_Options *res = initResponse->options;
201 if (ODR_MASK_GET(req, Z_Options_search))
202 ODR_MASK_SET(res, Z_Options_search);
203 if (ODR_MASK_GET(req, Z_Options_present))
204 ODR_MASK_SET(res, Z_Options_present);
205 m_preferredMessageSize = *initRequest->preferredMessageSize;
206 m_maximumRecordSize = *initRequest->maximumRecordSize;
207 return sr_init (initRequest, initResponse);
210 ODR Yaz_Facility_Retrieval::odr_encode()
215 ODR Yaz_Facility_Retrieval::odr_decode()
220 int Yaz_Facility_Retrieval::recv(Yaz_Z_Server *s, Z_APDU *apdu_request)
222 Z_APDU *apdu_response;
223 m_odr_encode = s->odr_encode();
224 m_odr_decode = s->odr_decode();
225 switch (apdu_request->which)
227 case Z_APDU_searchRequest:
228 yaz_log (LOG_LOG, "got SearchRequest p=%p", this);
229 apdu_response = s->create_Z_PDU(Z_APDU_searchResponse);
230 sr_search (apdu_request->u.searchRequest,
231 apdu_response->u.searchResponse);
232 if (!apdu_response->u.searchResponse->records)
234 fetch_via_piggyback(s, apdu_request->u.searchRequest,
235 apdu_response->u.searchResponse);
237 s->send_Z_PDU(apdu_response);
239 case Z_APDU_presentRequest:
240 yaz_log (LOG_LOG, "got PresentRequest p=%p", this);
241 apdu_response = s->create_Z_PDU(Z_APDU_presentResponse);
242 sr_present (apdu_request->u.presentRequest,
243 apdu_response->u.presentResponse);
244 if (!apdu_response->u.presentResponse->records)
245 fetch_via_present(s, apdu_request->u.presentRequest,
246 apdu_response->u.presentResponse);
247 s->send_Z_PDU(apdu_response);