24 #include <yaz/copy_types.h>
25 #include <metaproxy/package.hpp>
26 #include <metaproxy/util.hpp>
29 namespace yf = mp::filter;
37 void process(metaproxy_1::Package & package);
39 void chunk_it(metaproxy_1::Package & package, Z_APDU *apdu);
46 yf::PresentChunk::PresentChunk() : m_p(new
Impl)
50 yf::PresentChunk::~PresentChunk()
54 void yf::PresentChunk::configure(
const xmlNode *xmlnode,
bool test_only,
57 m_p->configure(xmlnode);
60 void yf::PresentChunk::process(mp::Package &package)
const
62 m_p->process(package);
65 yf::PresentChunk::Impl::Impl() : chunk_number(0)
69 yf::PresentChunk::Impl::~Impl()
73 void yf::PresentChunk::Impl::configure(
const xmlNode *ptr)
75 for (ptr = ptr->children; ptr; ptr = ptr->next)
77 if (ptr->type != XML_ELEMENT_NODE)
79 if (!strcmp((
const char *) ptr->name,
"chunk"))
81 chunk_number = mp::xml::get_int(ptr, 0);
85 throw mp::filter::FilterException(
"Bad element "
86 + std::string((
const char *)
92 void yf::PresentChunk::Impl::chunk_it(mp::Package &package,
96 Z_PresentRequest *pr = apdu->u.presentRequest;
98 Odr_int total = *pr->numberOfRecordsRequested;
99 Odr_int start = *pr->resultSetStartPoint;
101 Z_NamePlusRecordList *npl = (Z_NamePlusRecordList *)
102 odr_malloc(odr,
sizeof(*npl));
103 npl->num_records = total;
104 npl->records = (Z_NamePlusRecord **)
105 odr_malloc(odr,
sizeof(*npl->records) * total);
106 while (offset < total)
108 Odr_int left = total - offset;
110 Package pp(package.session(), package.origin());
112 *pr->numberOfRecordsRequested =
113 left > chunk_number ? chunk_number : left;
115 *pr->resultSetStartPoint = start + offset;
117 pp.copy_filter(package);
122 if (pp.session().is_closed())
124 package.session().close();
127 Z_GDU *gdu_res = pp.response().get();
128 if (gdu_res && gdu_res->which == Z_GDU_Z3950 &&
129 gdu_res->u.z3950->which == Z_APDU_presentResponse)
131 Z_PresentResponse *pres =
132 gdu_res->u.z3950->u.presentResponse;
134 pres->records->which == Z_Records_DBOSD)
136 Z_NamePlusRecordList *nprl =
137 pres->records->u.databaseOrSurDiagnostics;
139 for (i = 0; i < nprl->num_records; i++)
142 npl->records[offset+i] = yaz_clone_z_NamePlusRecord(
143 nprl->records[i], o->mem);
145 offset += nprl->num_records;
149 package.response() = pp.response();
155 package.response() = pp.response();
160 yaz_log(YLOG_LOG,
"building response . %lld", offset);
162 Z_APDU *a = zget_APDU(odr, Z_APDU_presentResponse);
163 Z_PresentResponse *pres = a->u.presentResponse;
164 pres->records = (Z_Records *)
165 odr_malloc(odr,
sizeof(Z_Records));
166 pres->records->which = Z_Records_DBOSD;
167 pres->records->u.databaseOrSurDiagnostics = npl;
168 npl->num_records = offset;
169 *pres->numberOfRecordsReturned = offset;
171 package.response() = a;
174 void yf::PresentChunk::Impl::process(mp::Package &package)
176 Z_GDU *gdu = package.request().get();
177 if (gdu && gdu->which == Z_GDU_Z3950)
179 Z_APDU *apdu = gdu->u.z3950;
180 if (apdu->which == Z_APDU_presentRequest && chunk_number > 0)
181 chunk_it(package, apdu);
191 return new mp::filter::PresentChunk;
struct metaproxy_1_filter_struct metaproxy_1_filter_present_chunk
static mp::filter::Base * filter_creator()