Fix {prev,next}recid PAZ-884
[pazpar2-moved-to-github.git] / src / session.c
index 570d15b..0aaec33 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;
@@ -1254,7 +1240,6 @@ 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_enter(se->reclist);
     while ((r = reclist_read_record(se->reclist)))
@@ -1481,6 +1466,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)
     {
@@ -1500,6 +1486,26 @@ static struct record_metadata *record_metadata_init(
     return rec_md;
 }
 
+static void mergekey_norm_wr(pp2_charset_fact_t charsets,
+                             WRBUF norm_wr, const char *value)
+{
+    const char *norm_str;
+    pp2_charset_token_t prt =
+        pp2_charset_token_create(charsets, "mergekey");
+
+    pp2_charset_token_first(prt, value, 0);
+    while ((norm_str = pp2_charset_token_next(prt)))
+    {
+        if (*norm_str)
+        {
+            if (wrbuf_len(norm_wr))
+                wrbuf_puts(norm_wr, " ");
+            wrbuf_puts(norm_wr, norm_str);
+        }
+    }
+    pp2_charset_token_destroy(prt);
+}
+
 static int get_mergekey_from_doc(xmlDoc *doc, xmlNode *root, const char *name,
                                  struct conf_service *service, WRBUF norm_wr)
 {
@@ -1520,24 +1526,11 @@ static int get_mergekey_from_doc(xmlDoc *doc, xmlNode *root, const char *name,
                 xmlChar *value = xmlNodeListGetString(doc, n->children, 1);
                 if (value && *value)
                 {
-                    const char *norm_str;
-                    pp2_charset_token_t prt =
-                        pp2_charset_token_create(service->charsets, "mergekey");
-
-                    pp2_charset_token_first(prt, (const char *) value, 0);
                     if (wrbuf_len(norm_wr) > 0)
                         wrbuf_puts(norm_wr, " ");
                     wrbuf_puts(norm_wr, name);
-                    while ((norm_str =
-                            pp2_charset_token_next(prt)))
-                    {
-                        if (*norm_str)
-                        {
-                            wrbuf_puts(norm_wr, " ");
-                            wrbuf_puts(norm_wr, norm_str);
-                        }
-                    }
-                    pp2_charset_token_destroy(prt);
+                    mergekey_norm_wr(service->charsets, norm_wr,
+                                     (const char *) value);
                     no_found++;
                 }
                 if (value)
@@ -1570,21 +1563,7 @@ static const char *get_mergekey(xmlDoc *doc, struct client *cl, int record_no,
     }
     else if ((mergekey = xmlGetProp(root, (xmlChar *) "mergekey")))
     {
-        const char *norm_str;
-        pp2_charset_token_t prt =
-            pp2_charset_token_create(service->charsets, "mergekey");
-
-        pp2_charset_token_first(prt, (const char *) mergekey, 0);
-        while ((norm_str = pp2_charset_token_next(prt)))
-        {
-            if (*norm_str)
-            {
-                if (wrbuf_len(norm_wr))
-                    wrbuf_puts(norm_wr, " ");
-                wrbuf_puts(norm_wr, norm_str);
-            }
-        }
-        pp2_charset_token_destroy(prt);
+        mergekey_norm_wr(service->charsets, norm_wr, (const char *) mergekey);
         xmlFree(mergekey);
     }
     else
@@ -1745,12 +1724,15 @@ int ingest_record(struct client *cl, const char *rec,
 
 //    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_metadata *ser_md,
+static int match_metadata_local(struct conf_service *service,
+                                struct conf_metadata *ser_md,
                                 struct record_metadata *rec_md0,
                                 char **values, int num_v)
 {
     int i;
     struct record_metadata *rec_md = rec_md0;
+    WRBUF val_wr = 0;
+    WRBUF text_wr = wrbuf_alloc();
     for (i = 0; i < num_v; )
     {
         if (rec_md)
@@ -1765,21 +1747,29 @@ static int match_metadata_local(struct conf_metadata *ser_md,
             }
             else
             {
-                yaz_log(YLOG_DEBUG, "cmp: '%s' '%s'", rec_md->data.text.disp, values[i]);
-                if (!strcmp(rec_md->data.text.disp, values[i]))
+                if (!val_wr)
                 {
-                    // Value equals, should not be filtered.
-                    break;
+                    val_wr = wrbuf_alloc();
+                    mergekey_norm_wr(service->charsets, val_wr, values[i]);
                 }
+                wrbuf_rewind(text_wr);
+                mergekey_norm_wr(service->charsets, text_wr,
+                                 rec_md->data.text.disp);
+                if (!strcmp(wrbuf_cstr(val_wr), wrbuf_cstr(text_wr)))
+                    break;
             }
             rec_md = rec_md->next;
         }
         else
         {
             rec_md = rec_md0;
+            wrbuf_destroy(val_wr);
+            val_wr = 0;
             i++;
         }
     }
+    wrbuf_destroy(val_wr);
+    wrbuf_destroy(text_wr);
     return i < num_v ? 1 : 0;
 }
 
@@ -1815,7 +1805,8 @@ int session_check_cluster_limit(struct session *se, struct record_cluster *rec)
                 nmem_strsplit_escape2(nmem_tmp, "|", value, &values,
                                       &num, 1, '\\', 1);
 
-                if (!match_metadata_local(&service->metadata[md_field_id],
+                if (!match_metadata_local(service,
+                                          &service->metadata[md_field_id],
                                           rec->metadata[md_field_id],
                                           values, num))
                 {
@@ -1857,6 +1848,7 @@ static int check_limit_local(struct client *cl,
                  md_field_id++)
             {
                 if (match_metadata_local(
+                        service,
                         &service->metadata[md_field_id],
                         record->metadata[md_field_id],
                         values, num_v))
@@ -1874,6 +1866,7 @@ static int check_limit_local(struct client *cl,
                 break;
             }
             if (!match_metadata_local(
+                    service,
                     &service->metadata[md_field_id],
                     record->metadata[md_field_id],
                     values, num_v))
@@ -1963,6 +1956,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;
@@ -2190,7 +2195,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))
             {