+Z_Records *yf::SPARQL::Session::explain_fetch(
+ Package &package,
+ FrontendSetPtr fset,
+ ODR odr, Odr_oid *preferredRecordSyntax,
+ Z_ElementSetNames *esn,
+ int start, int number, int &error_code, std::string &addinfo,
+ int *number_returned, int *next_position)
+{
+ Z_Records *rec = (Z_Records *) odr_malloc(odr, sizeof(Z_Records));
+ rec->which = Z_Records_DBOSD;
+ rec->u.databaseOrSurDiagnostics = (Z_NamePlusRecordList *)
+ odr_malloc(odr, sizeof(Z_NamePlusRecordList));
+ rec->u.databaseOrSurDiagnostics->records = (Z_NamePlusRecord **)
+ odr_malloc(odr, sizeof(Z_NamePlusRecord *) * number);
+ int i;
+ for (i = 0; i < number; i++)
+ {
+ unsigned int idx = start + i - 1;
+ if ( idx >= fset->explaindblist.size() )
+ break;
+ ConfPtr cp = fset->explaindblist[idx];
+ mp::wrbuf w;
+ wrbuf_puts(w,"<info>\n");
+ wrbuf_puts(w," <databaseInfo>\n");
+ wrbuf_puts(w," <title>");
+ wrbuf_xmlputs(w, cp->db.c_str());
+ wrbuf_puts(w,"</title>\n");
+ wrbuf_puts(w," </databaseInfo>\n");
+ yaz_sparql_explain_indexes( cp->s, w, 2);
+ wrbuf_puts(w,"</info>\n");
+
+ rec->u.databaseOrSurDiagnostics->records[i] = (Z_NamePlusRecord *)
+ odr_malloc(odr, sizeof(Z_NamePlusRecord));
+ Z_NamePlusRecord *npr = rec->u.databaseOrSurDiagnostics->records[i];
+ npr->databaseName = odr_strdup(odr, fset->db.c_str());
+ npr->which = Z_NamePlusRecord_databaseRecord;
+ npr->u.databaseRecord =
+ z_ext_record_xml(odr, w.buf(), w.len() );
+ }
+ rec->u.databaseOrSurDiagnostics->num_records = i;
+ *number_returned = i;
+ if (start + number > (int)fset->explaindblist.size())
+ *next_position = 0;
+ else
+ *next_position = start + number;
+ return rec;
+}
+
+
+
+Z_APDU *yf::SPARQL::Session::explain_search(mp::Package &package,
+ Z_APDU *apdu_req,
+ mp::odr &odr,
+ const char *explain_query,
+ FrontendSetPtr fset)
+{
+ Z_SearchRequest *req = apdu_req->u.searchRequest;
+ Z_APDU *apdu_res = 0;
+ //mp::wrbuf w;
+
+ package.log("sparql", YLOG_LOG, "Explain search '%s'", explain_query );
+ const char *term = explain_query + strlen(explain_query);
+ while ( term > explain_query && *term != ' ')
+ term--;
+ term++;
+ if ( ! isalpha( *term) )
+ term=""; // anything non-alpha is taken to mean all
+ // Empty string is seen here as two double quotes ""
+ // so it returns all bases as well
+ int numbases = 0;
+ std::list<ConfPtr>::const_iterator it = m_sparql->db_conf.begin();
+ m_frontend_sets[req->resultSetName] = fset;
+ fset->explaindblist.clear();
+ fset->explaindblist.reserve(m_sparql->db_conf.size());
+
+ for (; it != m_sparql->db_conf.end(); it++)
+ if ( (*it)->schema.length() > 0 && // searchable db
+ (!*term || strcmp(term,(*it)->db.c_str())==0) )
+ { // and want all, or found the matching one
+ numbases++;
+ package.log("sparql", YLOG_LOG, "Explain %d: '%s'",
+ numbases, (*it)->db.c_str() );
+ fset->explaindblist.push_back(*it);
+ }
+ int number_returned = 0;
+ int next_position = 0;
+ Z_Records *records = 0;
+ int error_code = 0;
+ std::string addinfo;
+
+ Odr_int number = 0;
+ const char *element_set_name = 0;
+ mp::util::piggyback_sr(req, numbases, number, &element_set_name);
+ if (number)
+ {
+ Z_ElementSetNames *esn;
+
+ if (number > *req->smallSetUpperBound)
+ esn = req->mediumSetElementSetNames;
+ else
+ esn = req->smallSetElementSetNames;
+ records = explain_fetch(package, fset,
+ odr, req->preferredRecordSyntax, esn,
+ 1, number,
+ error_code, addinfo,
+ &number_returned,
+ &next_position);
+ }
+
+ if (error_code)
+ {
+ apdu_res = odr.create_searchResponse(
+ apdu_req, error_code, addinfo.c_str());
+ }
+ else
+ {
+ apdu_res = odr.create_searchResponse(apdu_req, 0, 0);
+ Z_SearchResponse *resp = apdu_res->u.searchResponse;
+ *resp->resultCount = numbases;
+ *resp->numberOfRecordsReturned = number_returned;
+ *resp->nextResultSetPosition = next_position;
+ resp->records = records;
+ }
+
+ return apdu_res;
+}
+
+