Handle present out of range better in sample server.
[yazpp-moved-to-github.git] / src / yaz-z-server-sr.cpp
1 /*
2  * Copyright (c) 2000-2004, Index Data.
3  * See the file LICENSE for details.
4  * 
5  * $Id: yaz-z-server-sr.cpp,v 1.8 2004-12-13 20:50:54 adam Exp $
6  *
7  */
8
9 #include <yaz/log.h>
10 #include <yaz++/z-server.h>
11
12 Z_Records *Yaz_Facility_Retrieval::pack_records (Yaz_Z_Server *s,
13                                                  const char *resultSetName,
14                                                  int start, int xnum,
15                                                  Z_RecordComposition *comp,
16                                                  int *next, int *pres,
17                                                  int *format)
18 {
19     int recno, total_length = 0, toget = xnum, dumped_records = 0;
20     Z_Records *records =
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);
26
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;
32     *next = 0;
33
34     for (recno = start; reclist->num_records < toget; recno++)
35     {
36         Z_NamePlusRecord *this_rec =
37             (Z_NamePlusRecord *) odr_malloc (odr_encode(), sizeof(*this_rec));
38         this_rec->databaseName = 0;
39         this_rec->which = Z_NamePlusRecord_databaseRecord;
40         this_rec->u.databaseRecord = 0;
41
42         int this_length = 0;
43
44         sr_record (resultSetName, recno, format, comp, this_rec, records);
45
46         if (records->which != Z_Records_DBOSD)
47         {
48             *pres = Z_PRES_FAILURE;
49             return records;
50         }
51
52         if (this_rec->which == Z_NamePlusRecord_databaseRecord &&
53             this_rec->u.databaseRecord == 0)
54         {   // handler did not return a record..
55             create_surrogateDiagnostics(odr_encode(), this_rec, 0, 14, 0);
56         }
57         /*
58          * we get the number of bytes allocated on the stream before any
59          * allocation done by the backend - this should give us a reasonable
60          * idea of the total size of the data so far.
61          */
62         total_length = odr_total(odr_encode()) - dumped_records;
63         this_length = odr_total(odr_encode()) - total_length;
64         if (this_length + total_length > m_preferredMessageSize)
65         {
66             /* record is small enough, really */
67             if (this_length <= m_preferredMessageSize)
68             {
69                 *pres = Z_PRES_PARTIAL_2;
70                 break;
71             }
72             if (this_length >= m_maximumRecordSize)
73             {   /* too big entirely */
74                 reclist->records[reclist->num_records] = this_rec;
75                 create_surrogateDiagnostics(odr_encode(), this_rec,
76                                             this_rec->databaseName, 17, 0);
77                 reclist->num_records++;
78                 *next = recno + 1;
79                 dumped_records += this_length;
80                 continue;
81             }
82             else /* record can only be fetched by itself */
83             {
84                 if (toget > 1)
85                 {
86                     yaz_log(YLOG_DEBUG, "  Dropped it");
87                     reclist->records[reclist->num_records] = this_rec;
88                     create_surrogateDiagnostics(odr_encode(), this_rec,
89                                                 this_rec->databaseName,
90                                                 16, 0);
91                     reclist->num_records++;
92                     // *next = freq.last_in_set ? 0 : recno + 1;
93                     *next = recno + 1;
94                     dumped_records += this_length;
95                     continue;
96                 }
97             }
98         }
99         reclist->records[reclist->num_records] = this_rec;
100         reclist->num_records++;
101         *next = recno + 1;
102     }
103     return records;
104 }
105
106 void Yaz_Facility_Retrieval::fetch_via_piggyback (Yaz_Z_Server *s,
107                                                   Z_SearchRequest *req,
108                                                   Z_SearchResponse *res)
109 {
110     bool_t *sr = (bool_t *)odr_malloc (odr_encode(), sizeof(*sr));
111     *sr = 1;
112     
113     int toget = 0;
114     
115     Z_RecordComposition comp, *compp = 0;
116     int hits = *res->resultCount;
117     
118     int *nulint = (int *)odr_malloc (odr_encode(), sizeof(*nulint));
119     *nulint = 0;
120     
121     comp.which = Z_RecordComp_simple;
122     /* how many records does the user agent want, then? */
123     if (hits <= *req->smallSetUpperBound)
124     {
125         toget = hits;
126         if ((comp.u.simple = req->smallSetElementSetNames))
127             compp = &comp;
128     }
129     else if (hits < *req->largeSetLowerBound)
130     {
131         toget = *req->mediumSetPresentNumber;
132         if (toget > hits)
133             toget = hits;
134         if ((comp.u.simple = req->mediumSetElementSetNames))
135             compp = &comp;
136     }
137     
138     if (toget && !res->records)
139     {
140         res->presentStatus = (int *) odr_malloc (odr_encode(), sizeof(int));
141         *res->presentStatus = Z_PRES_SUCCESS;
142         res->records =
143             pack_records(s, req->resultSetName, 1, toget, compp, 
144                          res->nextResultSetPosition,
145                          res->presentStatus,
146                          req->preferredRecordSyntax);
147         if (!res->records)
148             return;
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;
154     }
155     else
156     {
157         if (hits)
158             *res->nextResultSetPosition = 1;
159         res->numberOfRecordsReturned = nulint;
160         res->searchStatus = sr;
161         res->resultSetStatus = 0;
162         res->presentStatus = 0;
163     }
164 }
165
166 void Yaz_Facility_Retrieval::fetch_via_present (Yaz_Z_Server *s,
167                                                 Z_PresentRequest *req,
168                                                 Z_PresentResponse *res)
169 {
170     res->records =
171         pack_records (s, req->resultSetId,*req->resultSetStartPoint, 
172                       *req->numberOfRecordsRequested,
173                       req->recordComposition,
174                       res->nextResultSetPosition,
175                       res->presentStatus,
176                       req->preferredRecordSyntax);
177     if (res->records->which == Z_Records_DBOSD)
178         *res->numberOfRecordsReturned =
179             res->records->u.databaseOrSurDiagnostics->num_records;
180 }
181
182 int Yaz_Facility_Retrieval::init(Yaz_Z_Server *s, Z_InitRequest *initRequest,
183                                  Z_InitResponse *initResponse)
184 {
185     Z_Options *req = initRequest->options;
186     Z_Options *res = initResponse->options;
187     
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);
195 }
196
197 ODR Yaz_Facility_Retrieval::odr_encode()
198 {
199     return m_odr_encode;
200 }
201
202 ODR Yaz_Facility_Retrieval::odr_decode()
203 {
204     return m_odr_decode;
205 }
206
207 int Yaz_Facility_Retrieval::recv(Yaz_Z_Server *s, Z_APDU *apdu_request)
208 {   
209     Z_APDU *apdu_response;
210     m_odr_encode = s->odr_encode();
211     m_odr_decode = s->odr_decode();
212     switch (apdu_request->which)
213     {
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)
220         {
221             fetch_via_piggyback(s, apdu_request->u.searchRequest,
222                                 apdu_response->u.searchResponse);
223         }
224         s->send_Z_PDU(apdu_response, 0);
225         return 1;
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);
235         return 1;
236     }
237     return 0;
238 }