Updated footer comment
[yazpp-moved-to-github.git] / src / yaz-z-server-sr.cpp
1 /* This file is part of the yazpp toolkit.
2  * Copyright (C) 1998-2009 Index Data and Mike Taylor
3  * See the file LICENSE for details.
4  */
5
6 #include <yaz/log.h>
7 #include <yazpp/z-server.h>
8
9 using namespace yazpp_1;
10
11 Z_Records *Yaz_Facility_Retrieval::pack_records (Z_Server *s,
12                                                  const char *resultSetName,
13                                                  int start, int xnum,
14                                                  Z_RecordComposition *comp,
15                                                  int *next, int *pres,
16                                                  Odr_oid *format)
17 {
18     int recno, total_length = 0, toget = xnum, dumped_records = 0;
19     Z_Records *records =
20         (Z_Records *) odr_malloc (odr_encode(), sizeof(*records));
21     Z_NamePlusRecordList *reclist =
22         (Z_NamePlusRecordList *) odr_malloc (odr_encode(), sizeof(*reclist));
23     Z_NamePlusRecord **list =
24         (Z_NamePlusRecord **) odr_malloc (odr_encode(), sizeof(*list) * toget);
25
26     records->which = Z_Records_DBOSD;
27     records->u.databaseOrSurDiagnostics = reclist;
28     reclist->num_records = 0;
29     reclist->records = list;
30     *pres = Z_PRES_SUCCESS;
31     *next = 0;
32
33     for (recno = start; reclist->num_records < toget; recno++)
34     {
35         Z_NamePlusRecord *this_rec =
36             (Z_NamePlusRecord *) odr_malloc (odr_encode(), sizeof(*this_rec));
37         this_rec->databaseName = 0;
38         this_rec->which = Z_NamePlusRecord_databaseRecord;
39         this_rec->u.databaseRecord = 0;
40
41         int this_length = 0;
42
43         sr_record (resultSetName, recno, format, comp, this_rec, records);
44
45         if (records->which != Z_Records_DBOSD)
46         {
47             *pres = Z_PRES_FAILURE;
48             return records;
49         }
50
51         if (this_rec->which == Z_NamePlusRecord_databaseRecord &&
52             this_rec->u.databaseRecord == 0)
53         {   // handler did not return a record..
54             create_surrogateDiagnostics(odr_encode(), this_rec, 0, 14, 0);
55         }
56         /*
57          * we get the number of bytes allocated on the stream before any
58          * allocation done by the backend - this should give us a reasonable
59          * idea of the total size of the data so far.
60          */
61         total_length = odr_total(odr_encode()) - dumped_records;
62         this_length = odr_total(odr_encode()) - total_length;
63         if (this_length + total_length > m_preferredMessageSize)
64         {
65             /* record is small enough, really */
66             if (this_length <= m_preferredMessageSize)
67             {
68                 *pres = Z_PRES_PARTIAL_2;
69                 break;
70             }
71             if (this_length >= m_maximumRecordSize)
72             {   /* too big entirely */
73                 reclist->records[reclist->num_records] = this_rec;
74                 create_surrogateDiagnostics(odr_encode(), this_rec,
75                                             this_rec->databaseName, 17, 0);
76                 reclist->num_records++;
77                 *next = recno + 1;
78                 dumped_records += this_length;
79                 continue;
80             }
81             else /* record can only be fetched by itself */
82             {
83                 if (toget > 1)
84                 {
85                     yaz_log(YLOG_DEBUG, "  Dropped it");
86                     reclist->records[reclist->num_records] = this_rec;
87                     create_surrogateDiagnostics(odr_encode(), this_rec,
88                                                 this_rec->databaseName,
89                                                 16, 0);
90                     reclist->num_records++;
91                     // *next = freq.last_in_set ? 0 : recno + 1;
92                     *next = recno + 1;
93                     dumped_records += this_length;
94                     continue;
95                 }
96             }
97         }
98         reclist->records[reclist->num_records] = this_rec;
99         reclist->num_records++;
100         *next = recno + 1;
101     }
102     return records;
103 }
104
105 void Yaz_Facility_Retrieval::fetch_via_piggyback (Z_Server *s,
106                                                   Z_SearchRequest *req,
107                                                   Z_SearchResponse *res)
108 {
109     bool_t *sr = (bool_t *)odr_malloc (odr_encode(), sizeof(*sr));
110     *sr = 1;
111     
112     int toget = 0;
113     
114     Z_RecordComposition comp, *compp = 0;
115     int hits = *res->resultCount;
116     
117     int *nulint = (int *)odr_malloc (odr_encode(), sizeof(*nulint));
118     *nulint = 0;
119     
120     comp.which = Z_RecordComp_simple;
121     /* how many records does the user agent want, then? */
122     if (hits <= *req->smallSetUpperBound)
123     {
124         toget = hits;
125         if ((comp.u.simple = req->smallSetElementSetNames))
126             compp = &comp;
127     }
128     else if (hits < *req->largeSetLowerBound)
129     {
130         toget = *req->mediumSetPresentNumber;
131         if (toget > hits)
132             toget = hits;
133         if ((comp.u.simple = req->mediumSetElementSetNames))
134             compp = &comp;
135     }
136     
137     if (toget && !res->records)
138     {
139         res->presentStatus = (int *) odr_malloc (odr_encode(), sizeof(int));
140         *res->presentStatus = Z_PRES_SUCCESS;
141         res->records =
142             pack_records(s, req->resultSetName, 1, toget, compp, 
143                          res->nextResultSetPosition,
144                          res->presentStatus,
145                          req->preferredRecordSyntax);
146         if (!res->records)
147             return;
148         if (res->records->which == Z_Records_DBOSD)
149             *res->numberOfRecordsReturned =
150                 res->records->u.databaseOrSurDiagnostics->num_records;
151         res->searchStatus = sr;
152         res->resultSetStatus = 0;
153     }
154     else
155     {
156         if (hits)
157             *res->nextResultSetPosition = 1;
158         res->numberOfRecordsReturned = nulint;
159         res->searchStatus = sr;
160         res->resultSetStatus = 0;
161         res->presentStatus = 0;
162     }
163 }
164
165 void Yaz_Facility_Retrieval::fetch_via_present (Z_Server *s,
166                                                 Z_PresentRequest *req,
167                                                 Z_PresentResponse *res)
168 {
169     res->records =
170         pack_records (s, req->resultSetId,*req->resultSetStartPoint, 
171                       *req->numberOfRecordsRequested,
172                       req->recordComposition,
173                       res->nextResultSetPosition,
174                       res->presentStatus,
175                       req->preferredRecordSyntax);
176     if (res->records->which == Z_Records_DBOSD)
177         *res->numberOfRecordsReturned =
178             res->records->u.databaseOrSurDiagnostics->num_records;
179 }
180
181 int Yaz_Facility_Retrieval::init(Z_Server *s, Z_InitRequest *initRequest,
182                                  Z_InitResponse *initResponse)
183 {
184     Z_Options *req = initRequest->options;
185     Z_Options *res = initResponse->options;
186     
187     if (ODR_MASK_GET(req, Z_Options_search))
188         ODR_MASK_SET(res, Z_Options_search);
189     if (ODR_MASK_GET(req, Z_Options_present))
190         ODR_MASK_SET(res, Z_Options_present);
191     m_preferredMessageSize = *initRequest->preferredMessageSize;
192     m_maximumRecordSize = *initRequest->maximumRecordSize;
193     return sr_init (initRequest, initResponse);
194 }
195
196 ODR Yaz_Facility_Retrieval::odr_encode()
197 {
198     return m_odr_encode;
199 }
200
201 ODR Yaz_Facility_Retrieval::odr_decode()
202 {
203     return m_odr_decode;
204 }
205
206 int Yaz_Facility_Retrieval::recv(Z_Server *s, Z_APDU *apdu_request)
207 {   
208     Z_APDU *apdu_response;
209     m_odr_encode = s->odr_encode();
210     m_odr_decode = s->odr_decode();
211     switch (apdu_request->which)
212     {
213     case Z_APDU_searchRequest:
214         apdu_response = s->create_Z_PDU(Z_APDU_searchResponse);
215         s->transfer_referenceId(apdu_request, apdu_response);
216         sr_search (apdu_request->u.searchRequest,
217                        apdu_response->u.searchResponse);
218         if (!apdu_response->u.searchResponse->records)
219         {
220             fetch_via_piggyback(s, apdu_request->u.searchRequest,
221                                 apdu_response->u.searchResponse);
222         }
223         s->send_Z_PDU(apdu_response, 0);
224         return 1;
225     case Z_APDU_presentRequest:
226         apdu_response = s->create_Z_PDU(Z_APDU_presentResponse);
227         s->transfer_referenceId(apdu_request, apdu_response);
228         sr_present (apdu_request->u.presentRequest,
229                     apdu_response->u.presentResponse);
230         if (!apdu_response->u.presentResponse->records)
231             fetch_via_present(s, apdu_request->u.presentRequest,
232                               apdu_response->u.presentResponse);
233         s->send_Z_PDU(apdu_response, 0);
234         return 1;
235     }
236     return 0;
237 }
238 /*
239  * Local variables:
240  * c-basic-offset: 4
241  * c-file-style: "Stroustrup"
242  * indent-tabs-mode: nil
243  * End:
244  * vim: shiftwidth=4 tabstop=8 expandtab
245  */
246