URSULA / Z-ruth service.
[yazpp-moved-to-github.git] / src / yaz-z-server-sr.cpp
1 /*
2  * Copyright (c) 2000-2001, Index Data.
3  * See the file LICENSE for details.
4  * 
5  * $Log: yaz-z-server-sr.cpp,v $
6  * Revision 1.2  2001-04-04 14:02:49  adam
7  * URSULA / Z-ruth service.
8  *
9  * Revision 1.1  2001/03/27 15:02:14  adam
10  * New server facility scheme.
11  *
12  */
13
14 #include <yaz/log.h>
15 #include <yaz++/yaz-z-server.h>
16
17 Z_Records *Yaz_Facility_Retrieval::pack_records (Yaz_Z_Server *s,
18                                                  const char *resultSetName,
19                                                  int start, int xnum,
20                                                  Z_RecordComposition *comp,
21                                                  int *next, int *pres,
22                                                  int *format)
23 {
24     int recno, total_length = 0, toget = xnum, dumped_records = 0;
25     Z_Records *records =
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);
31
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;
37     *next = 0;
38
39     yaz_log(LOG_LOG, "Request to pack %d+%d", start, toget);
40     yaz_log(LOG_LOG, "pms=%d, mrs=%d", m_preferredMessageSize,
41             m_maximumRecordSize);
42     for (recno = start; reclist->num_records < toget; recno++)
43     {
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;
49
50         int this_length = 0;
51
52         sr_record (resultSetName, recno, format, comp, this_rec, records);
53
54         if (records->which != Z_Records_DBOSD)
55         {
56             *pres = Z_PRES_FAILURE;
57             return records;
58         }
59
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);
64         }
65         /*
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.
69          */
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)
75         {
76             /* record is small enough, really */
77             if (this_length <= m_preferredMessageSize)
78             {
79                 yaz_log(LOG_LOG, "  Dropped last normal-sized record");
80                 *pres = Z_PRES_PARTIAL_2;
81                 break;
82             }
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++;
90                 *next = recno + 1;
91                 dumped_records += this_length;
92                 continue;
93             }
94             else /* record can only be fetched by itself */
95             {
96                 yaz_log(LOG_LOG, "  Record > prefmsgsz");
97                 if (toget > 1)
98                 {
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,
103                                                 16, 0);
104                     reclist->num_records++;
105                     // *next = freq.last_in_set ? 0 : recno + 1;
106                     *next = recno + 1;
107                     dumped_records += this_length;
108                     continue;
109                 }
110             }
111         }
112         reclist->records[reclist->num_records] = this_rec;
113         reclist->num_records++;
114         *next = recno + 1;
115     }
116     return records;
117 }
118
119 void Yaz_Facility_Retrieval::fetch_via_piggyback (Yaz_Z_Server *s,
120                                                   Z_SearchRequest *req,
121                                                   Z_SearchResponse *res)
122 {
123     bool_t *sr = (bool_t *)odr_malloc (odr_encode(), sizeof(*sr));
124     *sr = 1;
125     
126     int toget = 0;
127     
128     Z_RecordComposition comp, *compp = 0;
129     int hits = *res->resultCount;
130     
131     int *nulint = (int *)odr_malloc (odr_encode(), sizeof(*nulint));
132     *nulint = 0;
133     
134     comp.which = Z_RecordComp_simple;
135     /* how many records does the user agent want, then? */
136     if (hits <= *req->smallSetUpperBound)
137     {
138         toget = hits;
139         if ((comp.u.simple = req->smallSetElementSetNames))
140             compp = &comp;
141     }
142     else if (hits < *req->largeSetLowerBound)
143     {
144         toget = *req->mediumSetPresentNumber;
145         if (toget > hits)
146             toget = hits;
147         if ((comp.u.simple = req->mediumSetElementSetNames))
148             compp = &comp;
149     }
150     
151     if (toget && !res->records)
152     {
153         res->presentStatus = (int *) odr_malloc (odr_encode(), sizeof(int));
154         *res->presentStatus = Z_PRES_SUCCESS;
155         res->records =
156             pack_records(s, req->resultSetName, 1, toget, compp, 
157                          res->nextResultSetPosition,
158                          res->presentStatus,
159                          req->preferredRecordSyntax);
160         if (!res->records)
161             return;
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;
167     }
168     else
169     {
170         if (hits)
171             *res->nextResultSetPosition = 1;
172         res->numberOfRecordsReturned = nulint;
173         res->searchStatus = sr;
174         res->resultSetStatus = 0;
175         res->presentStatus = 0;
176     }
177 }
178
179 void Yaz_Facility_Retrieval::fetch_via_present (Yaz_Z_Server *s,
180                                                 Z_PresentRequest *req,
181                                                 Z_PresentResponse *res)
182 {
183     res->records =
184         pack_records (s, req->resultSetId,*req->resultSetStartPoint, 
185                       *req->numberOfRecordsRequested,
186                       req->recordComposition,
187                       res->nextResultSetPosition,
188                       res->presentStatus,
189                       req->preferredRecordSyntax);
190     if (res->records->which == Z_Records_DBOSD)
191         *res->numberOfRecordsReturned =
192             res->records->u.databaseOrSurDiagnostics->num_records;
193 }
194
195 int Yaz_Facility_Retrieval::init(Yaz_Z_Server *s, Z_InitRequest *initRequest,
196                                  Z_InitResponse *initResponse)
197 {
198     Z_Options *req = initRequest->options;
199     Z_Options *res = initResponse->options;
200     
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);
208 }
209
210 ODR Yaz_Facility_Retrieval::odr_encode()
211 {
212     return m_odr_encode;
213 }
214
215 ODR Yaz_Facility_Retrieval::odr_decode()
216 {
217     return m_odr_decode;
218 }
219
220 int Yaz_Facility_Retrieval::recv(Yaz_Z_Server *s, Z_APDU *apdu_request)
221 {   
222     Z_APDU *apdu_response;
223     m_odr_encode = s->odr_encode();
224     m_odr_decode = s->odr_decode();
225     switch (apdu_request->which)
226     {
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)
233         {
234             fetch_via_piggyback(s, apdu_request->u.searchRequest,
235                                 apdu_response->u.searchResponse);
236         }
237         s->send_Z_PDU(apdu_response);
238         return 1;
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);
248         return 1;
249     }
250     return 0;
251 }