Happy new year
[yazpp-moved-to-github.git] / src / yaz-z-server-sr.cpp
1 /* This file is part of the yazpp toolkit.
2  * Copyright (C) 1998-2011 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                                                  Odr_int *next, Odr_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_PresentStatus_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_PresentStatus_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_PresentStatus_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     Odr_int *nulint = (Odr_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 = (Odr_int *)
140             odr_malloc (odr_encode(), sizeof(Odr_int));
141         *res->presentStatus = Z_PresentStatus_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 (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(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(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 }
239 /*
240  * Local variables:
241  * c-basic-offset: 4
242  * c-file-style: "Stroustrup"
243  * indent-tabs-mode: nil
244  * End:
245  * vim: shiftwidth=4 tabstop=8 expandtab
246  */
247