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