Towards LOC-7: Config example and experiments
[mp-sparql-moved-to-github.git] / src / filter_sparql.cpp
index 235989b..97c8a4d 100644 (file)
@@ -38,6 +38,7 @@ namespace metaproxy_1 {
             class Session;
             class Rep;
             class Conf;
+            class Result;
             class FrontendSet;
 
             typedef boost::shared_ptr<Session> SessionPtr;
@@ -70,17 +71,23 @@ namespace metaproxy_1 {
             boost::mutex m_mutex;
             std::map<mp::Session,SessionPtr> m_clients;
         };
-        class SPARQL::FrontendSet {
+        class SPARQL::Result {
         public:
-            FrontendSet();
-            ~FrontendSet();
+            Result();
+            ~Result();
         private:
+            friend class FrontendSet;
             friend class Session;
-            Odr_int hits;
-            std::string db;
             ConfPtr conf;
             xmlDoc *doc;
         };
+        class SPARQL::FrontendSet {
+        private:
+            friend class Session;
+            Odr_int hits;
+            std::string db;
+            std::list<Result> results;
+        };
         class SPARQL::Session {
         public:
             Session(const SPARQL *);
@@ -90,7 +97,7 @@ namespace metaproxy_1 {
                                Z_APDU *apdu_req,
                                mp::odr &odr,
                                const char *sparql_query,
-                               ConfPtr conf);
+                               ConfPtr conf, FrontendSetPtr fset);
             Z_Records *fetch(
                 FrontendSetPtr fset,
                 ODR odr, Odr_oid *preferredRecordSyntax,
@@ -106,13 +113,13 @@ namespace metaproxy_1 {
     }
 }
 
-yf::SPARQL::FrontendSet::~FrontendSet()
+yf::SPARQL::Result::~Result()
 {
     if (doc)
         xmlFreeDoc(doc);
 }
 
-yf::SPARQL::FrontendSet::FrontendSet()
+yf::SPARQL::Result::Result()
 {
     doc = 0;
 }
@@ -129,16 +136,31 @@ void yf::SPARQL::configure(const xmlNode *xmlnode, bool test_only,
                            const char *path)
 {
     const xmlNode *ptr = xmlnode->children;
+    std::string uri;
 
     for (; ptr; ptr = ptr->next)
     {
         if (ptr->type != XML_ELEMENT_NODE)
             continue;
-        if (!strcmp((const char *) ptr->name, "db"))
+        if (!strcmp((const char *) ptr->name, "defaults"))
+        {
+            const struct _xmlAttr *attr;
+            for (attr = ptr->properties; attr; attr = attr->next)
+            {
+                if (!strcmp((const char *) attr->name, "uri"))
+                    uri = mp::xml::get_text(attr->children);
+                else
+                    throw mp::filter::FilterException(
+                        "Bad attribute " + std::string((const char *)
+                                                       attr->name));
+            }
+        }
+        else if (!strcmp((const char *) ptr->name, "db"))
         {
             yaz_sparql_t s = yaz_sparql_create();
             ConfPtr conf(new Conf);
             conf->s = s;
+            conf->uri = uri;
 
             const struct _xmlAttr *attr;
             for (attr = ptr->properties; attr; attr = attr->next)
@@ -269,14 +291,24 @@ void yf::SPARQL::release_session(Package &package) const
     }
 }
 
-static xmlNode *get_result(xmlDoc *doc, Odr_int *sz, Odr_int pos)
+static bool get_result(xmlDoc *doc, Odr_int *sz, Odr_int pos,
+                       xmlDoc **ndoc)
 {
     xmlNode *ptr = xmlDocGetRootElement(doc);
+    xmlNode *q0;
     Odr_int cur = 0;
 
+    if (ndoc)
+        *ndoc = xmlNewDoc(BAD_CAST "1.0");
+
     if (ptr->type == XML_ELEMENT_NODE &&
         !strcmp((const char *) ptr->name, "RDF"))
     {
+        if (ndoc)
+        {
+            q0 = xmlCopyNode(ptr, 2);
+            xmlDocSetRootElement(*ndoc, q0);
+        }
         ptr = ptr->children;
 
         while (ptr && ptr->type != XML_ELEMENT_NODE)
@@ -296,7 +328,14 @@ static xmlNode *get_result(xmlDoc *doc, Odr_int *sz, Odr_int pos)
                         !strcmp((const char *) ptr->name, "solution"))
                     {
                         if (cur++ == pos)
+                        {
+                            if (ndoc)
+                            {
+                                xmlNode *q1 = xmlCopyNode(ptr, 1);
+                                xmlAddChild(q0, q1);
+                            }
                             break;
+                        }
                     }
             }
             else
@@ -306,7 +345,14 @@ static xmlNode *get_result(xmlDoc *doc, Odr_int *sz, Odr_int pos)
                         !strcmp((const char *) ptr->name, "Description"))
                     {
                         if (cur++ == pos)
-                            break;
+                        {
+                            if (ndoc)
+                            {
+                                xmlNode *q1 = xmlCopyNode(ptr, 1);
+                                xmlAddChild(q0, q1);
+                            }
+                            return true;
+                        }
                     }
             }
         }
@@ -319,6 +365,11 @@ static xmlNode *get_result(xmlDoc *doc, Odr_int *sz, Odr_int pos)
                 break;
         if (ptr)
         {
+            if (ndoc)
+            {
+                q0 = xmlCopyNode(ptr, 2);
+                xmlDocSetRootElement(*ndoc, q0);
+            }
             for (ptr = ptr->children; ptr; ptr = ptr->next)
                 if (ptr->type == XML_ELEMENT_NODE &&
                     !strcmp((const char *) ptr->name, "results"))
@@ -326,18 +377,31 @@ static xmlNode *get_result(xmlDoc *doc, Odr_int *sz, Odr_int pos)
         }
         if (ptr)
         {
+            xmlNode *q1 = 0;
+            if (ndoc)
+            {
+                q1 = xmlCopyNode(ptr, 0);
+                xmlAddChild(q0, q1);
+            }
             for (ptr = ptr->children; ptr; ptr = ptr->next)
                 if (ptr->type == XML_ELEMENT_NODE &&
                     !strcmp((const char *) ptr->name, "result"))
                 {
                     if (cur++ == pos)
-                        break;
+                    {
+                        if (ndoc)
+                        {
+                            xmlNode *q2 = xmlCopyNode(ptr, 1);
+                            xmlAddChild(q1, q2);
+                        }
+                        return true;
+                    }
                 }
         }
     }
     if (sz)
         *sz = cur;
-    return ptr;
+    return false;
 }
 
 Z_Records *yf::SPARQL::Session::fetch(
@@ -348,10 +412,17 @@ Z_Records *yf::SPARQL::Session::fetch(
     int *number_returned, int *next_position)
 {
     Z_Records *rec = (Z_Records *) odr_malloc(odr, sizeof(Z_Records));
-    if (esn && esn->which == Z_ElementSetNames_generic &&
-        fset->conf->schema.length())
+    std::list<Result>::iterator it = fset->results.begin();
+    if (esn && esn->which == Z_ElementSetNames_generic && esn->u.generic)
     {
-        if (strcmp(esn->u.generic, fset->conf->schema.c_str()))
+        for (; it != fset->results.end(); it++)
+        {
+            yaz_log(YLOG_LOG, "checking xmldoc=%p schema=%s user-schema=%s",
+                    it->doc, it->conf->schema.c_str(), esn->u.generic);
+            if (!strcmp(esn->u.generic, it->conf->schema.c_str()))
+                break;
+        }
+        if (it == fset->results.end())
         {
             rec->which = Z_Records_NSD;
             rec->u.nonSurrogateDiagnostic =
@@ -375,17 +446,25 @@ Z_Records *yf::SPARQL::Session::fetch(
         Z_NamePlusRecord *npr = rec->u.databaseOrSurDiagnostics->records[i];
         npr->databaseName = odr_strdup(odr, fset->db.c_str());
         npr->which = Z_NamePlusRecord_databaseRecord;
+        xmlDoc *ndoc = 0;
 
-        xmlNode *node = get_result(fset->doc, 0, start - 1 + i);
-        if (!node)
+        if (!get_result(it->doc, 0, start - 1 + i, &ndoc))
+        {
+            if (ndoc)
+                xmlFreeDoc(ndoc);
+            break;
+        }
+        xmlNode *ndoc_root = xmlDocGetRootElement(ndoc);
+        if (!ndoc_root)
+        {
+            xmlFreeDoc(ndoc);
             break;
-        assert(node->type == XML_ELEMENT_NODE);
-        xmlNode *tmp = xmlCopyNode(node, 1);
+        }
         xmlBufferPtr buf = xmlBufferCreate();
-        xmlNodeDump(buf, tmp->doc, tmp, 0, 0);
+        xmlNodeDump(buf, ndoc, ndoc_root, 0, 0);
         npr->u.databaseRecord =
             z_ext_record_xml(odr, (const char *) buf->content, buf->use);
-        xmlFreeNode(tmp);
+        xmlFreeDoc(ndoc);
         xmlBufferFree(buf);
     }
     rec->u.databaseOrSurDiagnostics->num_records = i;
@@ -401,7 +480,7 @@ Z_APDU *yf::SPARQL::Session::run_sparql(mp::Package &package,
                                         Z_APDU *apdu_req,
                                         mp::odr &odr,
                                         const char *sparql_query,
-                                        ConfPtr conf)
+                                        ConfPtr conf, FrontendSetPtr fset)
 {
     Z_SearchRequest *req = apdu_req->u.searchRequest;
     Package http_package(package.session(), package.origin());
@@ -452,26 +531,30 @@ Z_APDU *yf::SPARQL::Session::run_sparql(mp::Package &package,
     else
     {
         Z_HTTP_Response *resp = gdu_resp->u.HTTP_Response;
-        FrontendSetPtr fset(new FrontendSet);
-
-        fset->doc = xmlParseMemory(resp->content_buf, resp->content_len);
-        fset->db = req->databaseNames[0];
-        fset->conf = conf;
-        if (!fset->doc)
+        xmlDocPtr doc = xmlParseMemory(resp->content_buf, resp->content_len);
+        if (!doc)
             apdu_res = odr.create_searchResponse(apdu_req,
                                              YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
                                              "invalid XML from backendbackend");
         else
         {
+            Result result;
             Z_Records *records = 0;
             int number_returned = 0;
             int next_position = 0;
             int error_code = 0;
             std::string addinfo;
 
-            get_result(fset->doc, &fset->hits, -1);
+            result.doc = doc;
+            result.conf = conf;
+            fset->results.push_back(result);
+            yaz_log(YLOG_LOG, "saving sparql result xmldoc=%p", doc);
+
+            get_result(result.doc, &fset->hits, -1, 0);
             m_frontend_sets[req->resultSetName] = fset;
 
+            result.doc = 0;
+
             Odr_int number = 0;
             const char *element_set_name = 0;
             mp::util::piggyback_sr(req, fset->hits, number, &element_set_name);
@@ -570,7 +653,7 @@ void yf::SPARQL::Session::handle_z(mp::Package &package, Z_APDU *apdu_req)
                         apdu_req,
                         YAZ_BIB1_RESULT_SET_EXISTS_AND_REPLACE_INDICATOR_OFF,
                         0);
-                package.response() = apdu_res;
+                package.response() = apdu;
             }
             m_frontend_sets.erase(fset_it);
         }
@@ -589,39 +672,43 @@ void yf::SPARQL::Session::handle_z(mp::Package &package, Z_APDU *apdu_req)
         {
             std::string db = req->databaseNames[0];
             std::list<ConfPtr>::const_iterator it;
+            FrontendSetPtr fset(new FrontendSet);
 
+            m_frontend_sets.erase(req->resultSetName);
+            fset->db = db;
             it = m_sparql->db_conf.begin();
             for (; it != m_sparql->db_conf.end(); it++)
                 if (yaz_match_glob((*it)->db.c_str(), db.c_str()))
-                    break;
-            if (it == m_sparql->db_conf.end())
+                {
+                    WRBUF addinfo_wr = wrbuf_alloc();
+                    WRBUF sparql_wr = wrbuf_alloc();
+                    int error =
+                        yaz_sparql_from_rpn_wrbuf((*it)->s,
+                                                  addinfo_wr, sparql_wr,
+                                                  req->query->u.type_1);
+                    if (error)
+                    {
+                        apdu_res = odr.create_searchResponse(
+                            apdu_req, error,
+                            wrbuf_len(addinfo_wr) ?
+                            wrbuf_cstr(addinfo_wr) : 0);
+                    }
+                    else
+                    {
+                        Z_APDU *apdu_1 = run_sparql(package, apdu_req, odr,
+                                                    wrbuf_cstr(sparql_wr), *it,
+                                                    fset);
+                        if (!apdu_res)
+                            apdu_res = apdu_1;
+                    }
+                    wrbuf_destroy(addinfo_wr);
+                    wrbuf_destroy(sparql_wr);
+                }
+            if (apdu_res == 0)
             {
                 apdu_res = odr.create_searchResponse(
                     apdu_req, YAZ_BIB1_DATABASE_DOES_NOT_EXIST, db.c_str());
             }
-            else
-            {
-                WRBUF addinfo_wr = wrbuf_alloc();
-                WRBUF sparql_wr = wrbuf_alloc();
-                int error =
-                    yaz_sparql_from_rpn_wrbuf((*it)->s,
-                                              addinfo_wr, sparql_wr,
-                                              req->query->u.type_1);
-                if (error)
-                {
-                    apdu_res = odr.create_searchResponse(
-                        apdu_req, error,
-                        wrbuf_len(addinfo_wr) ?
-                        wrbuf_cstr(addinfo_wr) : 0);
-                }
-                else
-                {
-                    apdu_res = run_sparql(package, apdu_req, odr,
-                                          wrbuf_cstr(sparql_wr), *it);
-                }
-                wrbuf_destroy(addinfo_wr);
-                wrbuf_destroy(sparql_wr);
-            }
         }
     }
     else if (apdu_req->which == Z_APDU_presentRequest)