Log when session terminates PAZ-905
[pazpar2-moved-to-github.git] / src / session.c
index 00af02f..9091b6b 100644 (file)
@@ -97,7 +97,6 @@ struct client_list {
 /* session counting (1) , disable client counting (0) */
 static YAZ_MUTEX g_session_mutex = 0;
 static int no_sessions = 0;
-static int no_session_total = 0;
 
 static int session_use(int delta)
 {
@@ -106,11 +105,10 @@ static int session_use(int delta)
         yaz_mutex_create(&g_session_mutex);
     yaz_mutex_enter(g_session_mutex);
     no_sessions += delta;
-    if (delta > 0)
-        no_session_total += delta;
     sessions = no_sessions;
     yaz_mutex_leave(g_session_mutex);
-    yaz_log(YLOG_DEBUG, "%s sessions=%d", delta == 0 ? "" : (delta > 0 ? "INC" : "DEC"), no_sessions);
+    yaz_log(YLOG_DEBUG, "%s sessions=%d", delta == 0 ? "" :
+            (delta > 0 ? "INC" : "DEC"), no_sessions);
     return sessions;
 }
 
@@ -119,17 +117,6 @@ int sessions_count(void)
     return session_use(0);
 }
 
-int session_count_total(void)
-{
-    int total = 0;
-    if (!g_session_mutex)
-        return 0;
-    yaz_mutex_enter(g_session_mutex);
-    total = no_session_total;
-    yaz_mutex_leave(g_session_mutex);
-    return total;
-}
-
 static void log_xml_doc(xmlDoc *doc)
 {
     FILE *lf = yaz_log_file();
@@ -162,10 +149,9 @@ static void session_leave(struct session *s, const char *caller)
         session_log(s, YLOG_DEBUG, "Session unlock by %s", caller);
 }
 
-static void session_normalize_facet(struct session *s, const char *type,
-                                    const char *value,
-                                    WRBUF display_wrbuf,
-                                    WRBUF facet_wrbuf)
+static void session_normalize_facet(struct session *s,
+                                    const char *type, const char *value,
+                                    WRBUF display_wrbuf, WRBUF facet_wrbuf)
 {
     struct conf_service *service = s->service;
     pp2_charset_token_t prt;
@@ -311,7 +297,8 @@ static void insert_settings_parameters(struct session_database *sdb,
 
 // Add static values from session database settings if applicable
 static void insert_settings_values(struct session_database *sdb, xmlDoc *doc,
-    struct conf_service *service)
+                                   xmlNode *root,
+                                   struct conf_service *service)
 {
     int i;
 
@@ -326,8 +313,7 @@ static void insert_settings_values(struct session_database *sdb, xmlDoc *doc,
             const char *val = session_setting_oneval(sdb, offset);
             if (val)
             {
-                xmlNode *r = xmlDocGetRootElement(doc);
-                xmlNode *n = xmlNewTextChild(r, 0, (xmlChar *) "metadata",
+                xmlNode *n = xmlNewTextChild(root, 0, (xmlChar *) "metadata",
                                              (xmlChar *) val);
                 xmlSetProp(n, (xmlChar *) "type", (xmlChar *) md->name);
             }
@@ -352,17 +338,6 @@ static xmlDoc *normalize_record(struct session *se,
         {
             session_log(se, YLOG_WARN, "Normalize failed");
         }
-        else
-        {
-            insert_settings_values(sdb, rdoc, service);
-
-            if (global_parameters.dump_records)
-            {
-                session_log(se, YLOG_LOG, "Normalized record from %s",
-                            sdb->database->id);
-                log_xml_doc(rdoc);
-            }
-        }
     }
     return rdoc;
 }
@@ -762,6 +737,7 @@ enum pazpar2_error_code session_search(struct session *se,
                                        const char *filter,
                                        const char *limit,
                                        const char **addinfo,
+                                       const char **addinfo2,
                                        struct reclist_sortparms *sp,
                                        const char *mergekey,
                                        const char *rank)
@@ -772,6 +748,11 @@ enum pazpar2_error_code session_search(struct session *se,
     int no_failed_limit = 0;
     struct client_list *l, *l0;
 
+    session_alert_watch(se, SESSION_WATCH_SHOW);
+    session_alert_watch(se, SESSION_WATCH_BYTARGET);
+    session_alert_watch(se, SESSION_WATCH_TERMLIST);
+    session_alert_watch(se, SESSION_WATCH_SHOW_PREF);
+
     session_log(se, YLOG_DEBUG, "Search");
 
     *addinfo = 0;
@@ -827,7 +808,7 @@ enum pazpar2_error_code session_search(struct session *se,
         if (prepare_map(se, client_get_database(cl)) < 0)
             continue;
 
-        parse_ret = client_parse_query(cl, query, se->facet_limits);
+        parse_ret = client_parse_query(cl, query, se->facet_limits, addinfo2);
         if (parse_ret == -1)
             no_failed_query++;
         else if (parse_ret == -2)
@@ -859,7 +840,6 @@ enum pazpar2_error_code session_search(struct session *se,
         else
             return PAZPAR2_NO_TARGETS;
     }
-    session_log(se, YLOG_LOG, "session_start_search done");
     return PAZPAR2_NO_ERROR;
 }
 
@@ -960,7 +940,7 @@ void session_apply_setting(struct session *se, char *dbname, char *setting,
 void session_destroy(struct session *se)
 {
     struct session_database *sdb;
-    session_log(se, YLOG_DEBUG, "Destroying");
+    session_log(se, YLOG_LOG, "destroy");
     session_use(-1);
     session_remove_cached_clients(se);
 
@@ -1030,6 +1010,8 @@ struct session *new_session(NMEM nmem, struct conf_service *service,
     session->normalize_cache = normalize_cache_create();
     session->session_mutex = 0;
     pazpar2_mutex_create(&session->session_mutex, tmp_str);
+    session_log(session, YLOG_LOG, "create");
+
     session_use(1);
     return session;
 }
@@ -1254,7 +1236,7 @@ struct record_cluster *show_single_start(struct session *se, const char *id,
     session_enter(se, "show_single_start");
     *prev_r = 0;
     *next_r = 0;
-    reclist_limit(se->reclist, se);
+    reclist_limit(se->reclist, se, 1);
 
     reclist_enter(se->reclist);
     while ((r = reclist_read_record(se->reclist)))
@@ -1328,7 +1310,7 @@ struct record_cluster **show_range_start(struct session *se,
     *sumhits = 0;
     *approx_hits = 0;
     *total = 0;
-    reclist_limit(se->reclist, se);
+    reclist_limit(se->reclist, se, 0);
     if (se->relevance)
     {
         for (spp = sp; spp; spp = spp->next)
@@ -1481,6 +1463,7 @@ static struct record_metadata *record_metadata_init(
 
         rec_md->data.text.disp = p;
         rec_md->data.text.sort = 0;
+        rec_md->data.text.snippet = 0;
     }
     else if (type == Metadata_type_year || type == Metadata_type_date)
     {
@@ -1556,12 +1539,12 @@ static int get_mergekey_from_doc(xmlDoc *doc, xmlNode *root, const char *name,
     return no_found;
 }
 
-static const char *get_mergekey(xmlDoc *doc, struct client *cl, int record_no,
+static const char *get_mergekey(xmlDoc *doc, xmlNode *root, 
+                                struct client *cl, int record_no,
                                 struct conf_service *service, NMEM nmem,
                                 const char *session_mergekey)
 {
     char *mergekey_norm = 0;
-    xmlNode *root = xmlDocGetRootElement(doc);
     WRBUF norm_wr = wrbuf_alloc();
     xmlChar *mergekey;
 
@@ -1680,13 +1663,49 @@ static int check_record_filter(xmlNode *root, struct session_database *sdb)
     return match;
 }
 
-
 static int ingest_to_cluster(struct client *cl,
                              xmlDoc *xdoc,
                              xmlNode *root,
                              int record_no,
                              const char *mergekey_norm);
 
+static int ingest_sub_record(struct client *cl, xmlDoc *xdoc, xmlNode *root,
+                             int record_no, NMEM nmem,
+                             struct session_database *sdb,
+                             const char **mergekey_norm)
+{
+    int ret = 0;
+    struct session *se = client_get_session(cl);
+    struct conf_service *service = se->service;
+
+    insert_settings_values(sdb, xdoc, root, service);
+
+    if (!check_record_filter(root, sdb))
+    {
+        session_log(se, YLOG_LOG,
+                    "Filtered out record no %d from %s",
+                    record_no, sdb->database->id);
+        return 0;
+    }
+    if (!*mergekey_norm)
+    {
+        *mergekey_norm = get_mergekey(xdoc, root, cl, record_no, service, nmem,
+                                      se->mergekey);
+    }
+    if (!*mergekey_norm)
+    {
+        session_log(se, YLOG_WARN, "Got no mergekey for record no %d from %s",
+                    record_no, sdb->database->id);
+        return -1;
+    }
+    session_enter(se, "ingest_sub_record");
+    if (client_get_session(cl) == se && se->relevance)
+        ret = ingest_to_cluster(cl, xdoc, root, record_no, *mergekey_norm);
+    session_leave(se, "ingest_sub_record");
+
+    return ret;
+}
+
 /** \brief ingest XML record
     \param cl client holds the result set for record
     \param rec record buffer (0 terminated)
@@ -1700,42 +1719,52 @@ int ingest_record(struct client *cl, const char *rec,
                   int record_no, NMEM nmem)
 {
     struct session *se = client_get_session(cl);
-    int ret = 0;
     struct session_database *sdb = client_get_database(cl);
     struct conf_service *service = se->service;
     xmlDoc *xdoc = normalize_record(se, sdb, service, rec, nmem);
+    int r = 0;
     xmlNode *root;
-    const char *mergekey_norm;
+    const char *mergekey_norm = 0;
 
     if (!xdoc)
         return -1;
 
+    if (global_parameters.dump_records)
+    {
+        session_log(se, YLOG_LOG, "Normalized record from %s",
+                    sdb->database->id);
+        log_xml_doc(xdoc);
+    }
+
     root = xmlDocGetRootElement(xdoc);
 
-    if (!check_record_filter(root, sdb))
+    if (!strcmp((const char *) root->name, "cluster"))
     {
-        session_log(se, YLOG_LOG, "Filtered out record no %d from %s", record_no, sdb->database->id);
-        xmlFreeDoc(xdoc);
-        return -2;
+        for (root = root->children; root; root = root->next)
+            if (root->type == XML_ELEMENT_NODE)
+            {
+                r = ingest_sub_record(cl, xdoc, root, record_no, nmem, sdb,
+                    &mergekey_norm);
+                if (r)
+                    break;
+            }
     }
-
-    mergekey_norm = get_mergekey(xdoc, cl, record_no, service, nmem,
-        se->mergekey);
-    if (!mergekey_norm)
+    else if (!strcmp((const char *) root->name, "record"))
     {
-        session_log(se, YLOG_WARN, "Got no mergekey");
-        xmlFreeDoc(xdoc);
-        return -1;
+        r = ingest_sub_record(cl, xdoc, root, record_no, nmem, sdb,
+                              &mergekey_norm);
+    }
+    else
+    {
+        session_log(se, YLOG_WARN, "Bad pz root element: %s",
+                    (const char *) root->name);
+        r = -1;
     }
-    session_enter(se, "ingest_record");
-    if (client_get_session(cl) == se && se->relevance)
-        ret = ingest_to_cluster(cl, xdoc, root, record_no, mergekey_norm);
-    session_leave(se, "ingest_record");
-
     xmlFreeDoc(xdoc);
-    return ret;
+    return r;
 }
 
+
 //    struct conf_metadata *ser_md = &service->metadata[md_field_id];
 //    struct record_metadata *rec_md = record->metadata[md_field_id];
 static int match_metadata_local(struct conf_service *service,
@@ -1970,6 +1999,18 @@ static int ingest_to_cluster(struct client *cl,
                             "for element '%s'", value, type);
                 continue;
             }
+
+            if (ser_md->type == Metadata_type_generic)
+            {
+                WRBUF w = wrbuf_alloc();
+                if (relevance_snippet(se->relevance,
+                                      (char*) value, ser_md->name, w))
+                    rec_md->data.text.snippet = nmem_strdup(se->nmem,
+                                                            wrbuf_cstr(w));
+                wrbuf_destroy(w);
+            }
+
+
             wheretoput = &record->metadata[md_field_id];
             while (*wheretoput)
                 wheretoput = &(*wheretoput)->next;
@@ -2197,7 +2238,6 @@ static int ingest_to_cluster(struct client *cl,
                 relevance_countwords(se->relevance, cluster,
                                      (char *) value, rank, ser_md->name);
             }
-
             // construct facets ... unless the client already has reported them
             if (ser_md->termlist && !client_has_facet(cl, (char *) type))
             {