+ for (ptr = ptr->children; ptr; ptr = ptr->next)
+ {
+ if (ptr->type != XML_ELEMENT_NODE)
+ continue;
+ if (!strcmp((const char *) ptr->name, "sort"))
+ {
+ const struct _xmlAttr *attr;
+ for (attr = ptr->properties; attr; attr = attr->next)
+ {
+ if (!strcmp((const char *) attr->name, "prefetch"))
+ {
+ m_prefetch = mp::xml::get_int(attr->children, -1);
+ if (m_prefetch < 0)
+ {
+ throw mp::filter::FilterException(
+ "Bad or missing value for attribute " +
+ std::string((const char *) attr->name));
+ }
+ }
+ else if (!strcmp((const char *) attr->name, "xpath"))
+ {
+ m_xpath_expr = mp::xml::get_text(attr->children);
+ }
+ else if (!strcmp((const char *) attr->name, "namespaces"))
+ {
+ m_namespaces = mp::xml::get_text(attr->children);
+ }
+ else if (!strcmp((const char *) attr->name, "ascending"))
+ {
+ m_ascending = mp::xml::get_bool(attr->children, true);
+ }
+ else if (!strcmp((const char *) attr->name, "debug"))
+ {
+ m_debug = mp::xml::get_bool(attr->children, false);
+ }
+ else
+ throw mp::filter::FilterException(
+ "Bad attribute " +
+ std::string((const char *) attr->name));
+ }
+ }
+ else
+ {
+ throw mp::filter::FilterException
+ ("Bad element "
+ + std::string((const char *) ptr->name)
+ + " in sort filter");
+ }
+ }
+ if (m_xpath_expr.length() == 0)
+ {
+ throw mp::filter::FilterException
+ ("Missing xpath attribute for config element in sort filter");
+ }
+
+}
+
+void yf::Sort::Frontend::handle_records(mp::Package &package,
+ Z_APDU *apdu_req,
+ Z_Records *records,
+ Odr_int start_pos,
+ ResultSetPtr s,
+ Odr_oid *syntax,
+ Z_RecordComposition *comp,
+ const char *resultSetId)
+{
+ if (records && records->which == Z_Records_DBOSD && start_pos == 1)
+ {
+ std::list<RecordListPtr>::const_iterator it = s->record_lists.begin();
+
+ for (; it != s->record_lists.end(); it++)
+ if ((*it)->cmp(syntax))
+ return;
+
+ Z_NamePlusRecordList *nprl = records->u.databaseOrSurDiagnostics;
+ int i; // i is number of records fetched in last response
+
+ int pos = 1;
+ RecordListPtr rlp(new RecordList(syntax,
+ m_p->m_namespaces.c_str(),
+ m_p->m_xpath_expr.c_str(),
+ m_p->m_debug));
+ for (i = 0; i < nprl->num_records; i++, pos++)
+ rlp->add(nprl->records[i]);
+
+ int end_pos = m_p->m_prefetch;
+ if (end_pos > s->hit_count)
+ end_pos = s->hit_count;
+ while (i && pos <= end_pos)
+ {
+ mp::odr odr;
+ i = 0;
+
+ Package present_package(package.session(), package.origin());
+ present_package.copy_filter(package);
+
+ Z_APDU *p_apdu = zget_APDU(odr, Z_APDU_presentRequest);
+ Z_PresentRequest *p_req = p_apdu->u.presentRequest;
+
+ *p_req->resultSetStartPoint = pos;
+ *p_req->numberOfRecordsRequested = end_pos - pos + 1;
+ p_req->preferredRecordSyntax = syntax;
+ p_req->resultSetId = odr_strdup(odr, resultSetId);
+ p_req->recordComposition = comp;
+
+ present_package.request() = p_apdu;
+ present_package.move();
+
+ Z_GDU *gdu_res = present_package.response().get();
+ if (gdu_res && gdu_res->which == Z_GDU_Z3950 &&
+ gdu_res->u.z3950->which == Z_APDU_presentResponse)
+ {
+ Z_PresentResponse *res = gdu_res->u.z3950->u.presentResponse;
+ Z_Records *records = res->records;
+ if (records && records->which == Z_Records_DBOSD)
+ {
+ Z_NamePlusRecordList *nprl =
+ records->u.databaseOrSurDiagnostics;
+ for (i = 0; i < nprl->num_records; i++, pos++)
+ rlp->add(nprl->records[i]);
+ }
+ }
+ }
+ s->record_lists.push_back(rlp);
+ rlp->sort();
+
+ for (i = 0; i < nprl->num_records; i++)
+ nprl->records[i] = rlp->get(i, m_p->m_ascending);
+ }
+}
+
+void yf::Sort::Frontend::handle_search(mp::Package &package, Z_APDU *apdu_req)
+{
+ Z_SearchRequest *req = apdu_req->u.searchRequest;
+ std::string resultSetId = req->resultSetName;
+ mp::odr odr;
+ Odr_oid *syntax = 0;
+
+ if (req->preferredRecordSyntax)
+ syntax = odr_oiddup(odr, req->preferredRecordSyntax);
+
+ Sets_it sets_it = m_sets.find(req->resultSetName);
+ if (sets_it != m_sets.end())
+ {
+ // result set already exist
+ // if replace indicator is off: we return diagnostic if
+ // result set already exist.
+ if (*req->replaceIndicator == 0)
+ {
+ Z_APDU *apdu =
+ odr.create_searchResponse(
+ apdu_req,
+ YAZ_BIB1_RESULT_SET_EXISTS_AND_REPLACE_INDICATOR_OFF,
+ 0);
+ package.response() = apdu;
+ return;
+ }
+ m_sets.erase(resultSetId);
+ }
+ ResultSetPtr s(new ResultSet);
+ m_sets[resultSetId] = s;
+
+ Package b_package(package.session(), package.origin());
+ b_package.copy_filter(package);
+ b_package.request() = apdu_req;
+ b_package.move();
+
+ Z_GDU *gdu_res = b_package.response().get();
+ if (gdu_res && gdu_res->which == Z_GDU_Z3950 && gdu_res->u.z3950->which ==
+ Z_APDU_searchResponse)
+ {
+ Z_SearchResponse *res = gdu_res->u.z3950->u.searchResponse;
+ Z_RecordComposition *record_comp =
+ mp::util::piggyback_to_RecordComposition(odr,
+ *res->resultCount, req);
+ s->hit_count = *res->resultCount;
+ handle_records(package, apdu_req, res->records, 1, s,
+ syntax, record_comp, resultSetId.c_str());
+ package.response() = gdu_res;
+ }
+ else
+ package.response() = b_package.response();
+ if (b_package.session().is_closed())
+ b_package.session().close();