Remove a few log stms
[pazpar2-moved-to-github.git] / src / session.c
index e2c66d3..9875515 100644 (file)
@@ -1,5 +1,5 @@
 /* This file is part of Pazpar2.
-   Copyright (C) 2006-2010 Index Data
+   Copyright (C) 2006-2011 Index Data
 
 Pazpar2 is free software; you can redistribute it and/or modify it under
 the terms of the GNU General Public License as published by the Free
@@ -25,6 +25,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 #include <config.h>
 #endif
 
+#include <time.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
@@ -34,9 +35,13 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 #if HAVE_UNISTD_H
 #include <unistd.h>
 #endif
+#ifdef WIN32
+#include <windows.h>
+#endif
 #include <signal.h>
 #include <ctype.h>
 #include <assert.h>
+#include <math.h>
 
 #include <yaz/marcdisp.h>
 #include <yaz/comstack.h>
@@ -51,12 +56,14 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 #include <yaz/querytowrbuf.h>
 #include <yaz/oid_db.h>
 #include <yaz/snprintf.h>
+#include <yaz/gettimeofday.h>
 
 #define USE_TIMING 0
 #if USE_TIMING
 #include <yaz/timing.h>
 #endif
 
+#include "ppmutex.h"
 #include "parameters.h"
 #include "session.h"
 #include "eventl.h"
@@ -73,13 +80,56 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
 #define MAX_CHUNK 15
 
+#define MAX(a,b) ((a)>(b)?(a):(b))
+
 // Note: Some things in this structure will eventually move to configuration
 struct parameters global_parameters = 
 {
     0,   // dump_records
-    0    // debug_mode
+    0,   // debug_mode
+    0,   // predictable sessions
+};
+
+struct client_list {
+    struct client *client;
+    struct client_list *next;
 };
 
+/* 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)
+{
+    int sessions;
+    if (!g_session_mutex)
+        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 sesions=%d", delta == 0 ? "" : (delta > 0 ? "INC" : "DEC"), no_sessions);
+    return sessions;
+}
+
+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();
@@ -100,12 +150,12 @@ static void log_xml_doc(xmlDoc *doc)
 
 static void session_enter(struct session *s)
 {
-    yaz_mutex_enter(s->mutex);
+    yaz_mutex_enter(s->session_mutex);
 }
 
 static void session_leave(struct session *s)
 {
-    yaz_mutex_leave(s->mutex);
+    yaz_mutex_leave(s->session_mutex);
 }
 
 // Recursively traverse query structure to extract terms.
@@ -135,32 +185,84 @@ void pull_terms(NMEM nmem, struct ccl_rpn_node *n, char **termlist, int *num)
 }
 
 
-static void add_facet(struct session *s, const char *type, const char *value)
+void add_facet(struct session *s, const char *type, const char *value, int count)
 {
+    struct conf_service *service = s->service;
+    pp2_charset_token_t prt;
+    const char *facet_component;
+    WRBUF facet_wrbuf = wrbuf_alloc();
+    WRBUF display_wrbuf = wrbuf_alloc();
     int i;
+    const char *icu_chain_id = 0;
 
-    if (!*value)
+    for (i = 0; i < service->num_metadata; i++)
+        if (!strcmp((service->metadata + i)->name, type))
+            icu_chain_id = (service->metadata + i)->facetrule;
+    if (!icu_chain_id)
+        icu_chain_id = "facet";
+    prt = pp2_charset_token_create(service->charsets, icu_chain_id);
+    if (!prt)
+    {
+        yaz_log(YLOG_FATAL, "Unknown ICU chain '%s' for facet of type '%s'",
+                icu_chain_id, type);
+        wrbuf_destroy(facet_wrbuf);
+        wrbuf_destroy(display_wrbuf);
         return;
-    for (i = 0; i < s->num_termlists; i++)
-        if (!strcmp(s->termlists[i].name, type))
-            break;
-    if (i == s->num_termlists)
+    }
+    pp2_charset_token_first(prt, value, 0);
+    while ((facet_component = pp2_charset_token_next(prt)))
     {
-        if (i == SESSION_MAX_TERMLISTS)
+        const char *display_component;
+        if (*facet_component)
         {
-            yaz_log(YLOG_FATAL, "Too many termlists");
-            return;
+            if (wrbuf_len(facet_wrbuf))
+                wrbuf_puts(facet_wrbuf, " ");
+            wrbuf_puts(facet_wrbuf, facet_component);
+        }
+        display_component = pp2_get_display(prt);
+        if (display_component)
+        {
+            if (wrbuf_len(display_wrbuf))
+                wrbuf_puts(display_wrbuf, " ");
+            wrbuf_puts(display_wrbuf, display_component);
         }
-
-        s->termlists[i].name = nmem_strdup(s->nmem, type);
-        s->termlists[i].termlist 
-            = termlist_create(s->nmem, TERMLIST_HIGH_SCORE);
-        s->num_termlists = i + 1;
     }
-    termlist_insert(s->termlists[i].termlist, value);
+    pp2_charset_token_destroy(prt);
+    if (wrbuf_len(facet_wrbuf))
+    {
+        int i;
+        for (i = 0; i < s->num_termlists; i++)
+            if (!strcmp(s->termlists[i].name, type))
+                break;
+        if (i == s->num_termlists)
+        {
+            if (i == SESSION_MAX_TERMLISTS)
+            {
+                session_log(s, YLOG_FATAL, "Too many termlists");
+                wrbuf_destroy(facet_wrbuf);
+                wrbuf_destroy(display_wrbuf);
+                return;
+            }
+            
+            s->termlists[i].name = nmem_strdup(s->nmem, type);
+            s->termlists[i].termlist 
+                = termlist_create(s->nmem, TERMLIST_HIGH_SCORE);
+            s->num_termlists = i + 1;
+        }
+        
+#if 0
+        session_log(s, YLOG_DEBUG, "Facets for %s: %s norm:%s (%d)", type, value, wrbuf_cstr(facet_wrbuf), count);
+#endif
+        termlist_insert(s->termlists[i].termlist, wrbuf_cstr(display_wrbuf),
+                        wrbuf_cstr(facet_wrbuf), count);
+    }
+    wrbuf_destroy(facet_wrbuf);
+    wrbuf_destroy(display_wrbuf);
 }
 
-static xmlDoc *record_to_xml(struct session_database *sdb, const char *rec)
+static xmlDoc *record_to_xml(struct session *se,
+                             struct session_database *sdb, const char *rec)
 {
     struct database *db = sdb->database;
     xmlDoc *rdoc = 0;
@@ -169,14 +271,14 @@ static xmlDoc *record_to_xml(struct session_database *sdb, const char *rec)
 
     if (!rdoc)
     {
-        yaz_log(YLOG_FATAL, "Non-wellformed XML received from %s",
-                db->url);
+        session_log(se, YLOG_FATAL, "Non-wellformed XML received from %s",
+                    db->url);
         return 0;
     }
 
     if (global_parameters.dump_records)
     {
-        yaz_log(YLOG_LOG, "Un-normalized record from %s", db->url);
+        session_log(se, YLOG_LOG, "Un-normalized record from %s", db->url);
         log_xml_doc(rdoc);
     }
 
@@ -248,11 +350,12 @@ static void insert_settings_values(struct session_database *sdb, xmlDoc *doc,
     }
 }
 
-static xmlDoc *normalize_record(struct session_database *sdb,
+static xmlDoc *normalize_record(struct session *se,
+                                struct session_database *sdb,
                                 struct conf_service *service,
                                 const char *rec, NMEM nmem)
 {
-    xmlDoc *rdoc = record_to_xml(sdb, rec);
+    xmlDoc *rdoc = record_to_xml(se, sdb, rec);
 
     if (rdoc)
     {
@@ -262,7 +365,8 @@ static xmlDoc *normalize_record(struct session_database *sdb,
         
         if (normalize_record_transform(sdb->map, &rdoc, (const char **)parms))
         {
-            yaz_log(YLOG_WARN, "Normalize failed from %s", sdb->database->url);
+            session_log(se, YLOG_WARN, "Normalize failed from %s",
+                        sdb->database->url);
         }
         else
         {
@@ -270,8 +374,8 @@ static xmlDoc *normalize_record(struct session_database *sdb,
             
             if (global_parameters.dump_records)
             {
-                yaz_log(YLOG_LOG, "Normalized record from %s", 
-                        sdb->database->url);
+                session_log(se, YLOG_LOG, "Normalized record from %s", 
+                            sdb->database->url);
                 log_xml_doc(rdoc);
             }
         }
@@ -323,7 +427,7 @@ static int prepare_map(struct session *se, struct session_database *sdb)
 
     if (!sdb->settings)
     {
-        yaz_log(YLOG_WARN, "No settings on %s", sdb->database->url);
+        session_log(se, YLOG_WARN, "No settings on %s", sdb->database->url);
         return -1;
     }
     if ((s = session_setting_oneval(sdb, PZ_XSLT)))
@@ -349,11 +453,12 @@ static int prepare_map(struct session *se, struct session_database *sdb)
             }
             else
             {
-                yaz_log(YLOG_WARN, "No pz:requestsyntax for auto stylesheet");
+                session_log(se, YLOG_WARN,
+                            "No pz:requestsyntax for auto stylesheet");
             }
         }
         sdb->map = normalize_cache_get(se->normalize_cache,
-                                       se->service, s);
+                                       se->service->server->config, s);
         if (!sdb->map)
             return -1;
     }
@@ -367,7 +472,7 @@ static int prepare_session_database(struct session *se,
 {
     if (!sdb->settings)
     {
-        yaz_log(YLOG_WARN, 
+        session_log(se, YLOG_WARN, 
                 "No settings associated with %s", sdb->database->url);
         return -1;
     }
@@ -409,7 +514,7 @@ int session_set_watch(struct session *s, int what,
         ret = 0;
     }
     session_leave(s);
-    return 0;
+    return ret;
 }
 
 void session_alert_watch(struct session *s, int what)
@@ -423,7 +528,7 @@ void session_alert_watch(struct session *s, int what)
         session_watchfun fun;
 
         http_remove_observer(s->watchlist[what].obs);
-        fun = s->watchlist[what].fun;
+        fun  = s->watchlist[what].fun;
         data = s->watchlist[what].data;
 
         /* reset watch before fun is invoked - in case fun wants to set
@@ -433,6 +538,8 @@ void session_alert_watch(struct session *s, int what)
         s->watchlist[what].obs = 0;
 
         session_leave(s);
+        session_log(s, YLOG_DEBUG,
+                    "Alert Watch: %d calling function: %p", what, fun);
         fun(data);
     }
     else
@@ -444,21 +551,37 @@ static void select_targets_callback(void *context, struct session_database *db)
 {
     struct session *se = (struct session*) context;
     struct client *cl = client_create();
+    struct client_list *l;
     client_set_database(cl, db);
+
     client_set_session(cl, se);
+
+    l = xmalloc(sizeof(*l));
+    l->client = cl;
+    l->next = se->clients;
+    se->clients = l;
 }
 
 static void session_remove_clients(struct session *se)
 {
-    struct client *cl = se->clients;
-    while (cl)
+    struct client_list *l;
+
+    session_enter(se);
+    l = se->clients;
+    se->clients = 0;
+    session_leave(se);
+
+    while (l)
     {
-        struct client *cl_next = client_next_in_session(cl);
-        client_remove_from_session(cl);
-        client_destroy(cl);
-        cl = cl_next;
+        struct client_list *l_next = l->next;
+        client_lock(l->client);
+        client_set_session(l->client, 0);
+        client_set_database(l->client, 0);
+        client_unlock(l->client);
+        client_destroy(l->client);
+        xfree(l);
+        l = l_next;
     }
-    se->clients = 0;
 }
 
 // Associates a set of clients with a session;
@@ -466,43 +589,58 @@ static void session_remove_clients(struct session *se)
 // setting overrides
 static int select_targets(struct session *se, const char *filter)
 {
-    session_remove_clients(se);
     return session_grep_databases(se, filter, select_targets_callback);
 }
 
 int session_active_clients(struct session *s)
 {
-    struct client *c;
+    struct client_list *l;
     int res = 0;
 
-    for (c = s->clients; c; c = client_next_in_session(c))
-        if (client_is_active(c))
+    for (l = s->clients; l; l = l->next)
+        if (client_is_active(l->client))
             res++;
 
     return res;
 }
 
+int session_is_preferred_clients_ready(struct session *s)
+{
+    struct client_list *l;
+    int res = 0;
+
+    for (l = s->clients; l; l = l->next)
+        if (client_is_active_preferred(l->client))
+            res++;
+    session_log(s, YLOG_DEBUG, "Has %d active preferred clients.", res);
+    return res == 0;
+}
 
 enum pazpar2_error_code search(struct session *se,
                                const char *query,
                                const char *startrecs, const char *maxrecs,
                                const char *filter,
+                               const char *limit,
                                const char **addinfo)
 {
     int live_channels = 0;
     int no_working = 0;
     int no_failed = 0;
-    struct client *cl;
+    struct client_list *l;
+    struct timeval tval;
+    facet_limits_t facet_limits;
 
-    yaz_log(YLOG_DEBUG, "Search");
+    session_log(se, YLOG_DEBUG, "Search");
 
     *addinfo = 0;
+
+    session_remove_clients(se);
     
     session_enter(se);
     reclist_destroy(se->reclist);
     se->reclist = 0;
+    relevance_destroy(&se->relevance);
     nmem_reset(se->nmem);
-    se->relevance = 0;
     se->total_records = se->total_hits = se->total_merged = 0;
     se->num_termlists = 0;
     live_channels = select_targets(se, filter);
@@ -513,26 +651,40 @@ enum pazpar2_error_code search(struct session *se,
     }
     se->reclist = reclist_create(se->nmem);
 
-    for (cl = se->clients; cl; cl = client_next_in_session(cl))
+    yaz_gettimeofday(&tval);
+    
+    tval.tv_sec += 5;
+
+    facet_limits = facet_limits_create(limit);
+    if (!facet_limits)
+    {
+        *addinfo = "limit";
+        session_leave(se);
+        return PAZPAR2_MALFORMED_PARAMETER_VALUE;
+    }
+    for (l = se->clients; l; l = l->next)
     {
+        struct client *cl = l->client;
+
         if (maxrecs)
             client_set_maxrecs(cl, atoi(maxrecs));
         if (startrecs)
             client_set_startrecs(cl, atoi(startrecs));
         if (prepare_session_database(se, client_get_database(cl)) < 0)
-            continue;
-        // Parse query for target
-        if (client_parse_query(cl, query) < 0)
+            ;
+        else if (client_parse_query(cl, query, facet_limits) < 0)
             no_failed++;
         else
         {
             no_working++;
             if (client_prep_connection(cl, se->service->z3950_operation_timeout,
                                        se->service->z3950_session_timeout,
-                                       se->service->server->iochan_man))
+                                       se->service->server->iochan_man,
+                                       &tval))
                 client_start_search(cl);
         }
     }
+    facet_limits_destroy(facet_limits);
     session_leave(se);
     if (no_working == 0)
     {
@@ -642,29 +794,51 @@ void session_apply_setting(struct session *se, char *dbname, char *setting,
     }
 }
 
-void destroy_session(struct session *s)
-{
+void session_destroy(struct session *se) {
     struct session_database *sdb;
+    session_log(se, YLOG_DEBUG, "Destroying");
+    session_use(-1);
+    session_remove_clients(se);
 
-    session_remove_clients(s);
-
-    for (sdb = s->databases; sdb; sdb = sdb->next)
+    for (sdb = se->databases; sdb; sdb = sdb->next)
         session_database_destroy(sdb);
-    normalize_cache_destroy(s->normalize_cache);
-    reclist_destroy(s->reclist);
-    nmem_destroy(s->nmem);
-    service_destroy(s->service);
-    yaz_mutex_destroy(&s->mutex);
-    wrbuf_destroy(s->wrbuf);
+    normalize_cache_destroy(se->normalize_cache);
+    relevance_destroy(&se->relevance);
+    reclist_destroy(se->reclist);
+    nmem_destroy(se->nmem);
+    service_destroy(se->service);
+    yaz_mutex_destroy(&se->session_mutex);
 }
 
-struct session *new_session(NMEM nmem, struct conf_service *service) 
+/* Depreciated: use session_destroy */
+void destroy_session(struct session *se)
+{
+    session_destroy(se);
+}
+
+size_t session_get_memory_status(struct session *session) {
+    size_t session_nmem;
+    if (session == 0)
+        return 0;
+    session_enter(session);
+    session_nmem = nmem_total(session->nmem);
+    session_leave(session);
+    return session_nmem;
+}
+
+
+struct session *new_session(NMEM nmem, struct conf_service *service,
+                            unsigned session_id)
 {
     int i;
     struct session *session = nmem_malloc(nmem, sizeof(*session));
 
-    yaz_log(YLOG_DEBUG, "New Pazpar2 session");
+    char tmp_str[50];
+
+    sprintf(tmp_str, "session#%u", session_id);
 
+    session->session_id = session_id;
+    session_log(session, YLOG_DEBUG, "New");
     session->service = service;
     session->relevance = 0;
     session->total_hits = 0;
@@ -676,7 +850,6 @@ struct session *new_session(NMEM nmem, struct conf_service *service)
     session->clients = 0;
     session->session_nmem = nmem;
     session->nmem = nmem_create();
-    session->wrbuf = wrbuf_alloc();
     session->databases = 0;
     for (i = 0; i <= SESSION_WATCH_MAX; i++)
     {
@@ -684,26 +857,27 @@ struct session *new_session(NMEM nmem, struct conf_service *service)
         session->watchlist[i].fun = 0;
     }
     session->normalize_cache = normalize_cache_create();
-    session->mutex = 0;
-    yaz_mutex_create(&session->mutex);
-
+    session->session_mutex = 0;
+    pazpar2_mutex_create(&session->session_mutex, tmp_str);
+    session_use(1);
     return session;
 }
 
 struct hitsbytarget *hitsbytarget(struct session *se, int *count, NMEM nmem)
 {
     struct hitsbytarget *res = 0;
-    struct client *cl;
+    struct client_list *l;
     size_t sz = 0;
 
     session_enter(se);
-    for (cl = se->clients; cl; cl = client_next_in_session(cl))
+    for (l = se->clients; l; l = l->next)
         sz++;
 
     res = nmem_malloc(nmem, sizeof(*res) * sz);
     *count = 0;
-    for (cl = se->clients; cl; cl = client_next_in_session(cl))
+    for (l = se->clients; l; l = l->next)
     {
+        struct client *cl = l->client;
         WRBUF w = wrbuf_alloc();
         const char *name = session_setting_oneval(client_get_database(cl),
                                                   PZ_NAME);
@@ -716,26 +890,28 @@ struct hitsbytarget *hitsbytarget(struct session *se, int *count, NMEM nmem)
         res[*count].state = client_get_state_str(cl);
         res[*count].connected  = client_get_connection(cl) ? 1 : 0;
         session_settings_dump(se, client_get_database(cl), w);
-        res[*count].settings_xml = w;
+        res[*count].settings_xml = nmem_strdup(nmem, wrbuf_cstr(w));
+        wrbuf_destroy(w);
         (*count)++;
     }
     session_leave(se);
     return res;
 }
 
-struct termlist_score **termlist(struct session *s, const char *name, int *num)
+struct termlist_score **get_termlist_score(struct session *se,
+                                           const char *name, int *num)
 {
     int i;
     struct termlist_score **tl = 0;
 
-    session_enter(s);
-    for (i = 0; i < s->num_termlists; i++)
-        if (!strcmp((const char *) s->termlists[i].name, name))
+    session_enter(se);
+    for (i = 0; i < se->num_termlists; i++)
+        if (!strcmp((const char *) se->termlists[i].name, name))
         {
-            tl = termlist_highscore(s->termlists[i].termlist, num);
+            tl = termlist_highscore(se->termlists[i].termlist, num);
             break;
         }
-    session_leave(s);
+    session_leave(se);
     return tl;
 }
 
@@ -752,50 +928,52 @@ void report_nmem_stats(void)
 }
 #endif
 
-struct record_cluster *show_single_start(struct session *s, const char *id,
+struct record_cluster *show_single_start(struct session *se, const char *id,
                                          struct record_cluster **prev_r,
                                          struct record_cluster **next_r)
 {
-    struct record_cluster *r;
+    struct record_cluster *r = 0;
 
-    session_enter(s);
-    reclist_enter(s->reclist);
+    session_enter(se);
     *prev_r = 0;
     *next_r = 0;
-    while ((r = reclist_read_record(s->reclist)))
+    if (se->reclist)
     {
-        if (!strcmp(r->recid, id))
+        reclist_enter(se->reclist);
+        while ((r = reclist_read_record(se->reclist)))
         {
-            *next_r = reclist_read_record(s->reclist);
-            break;
+            if (!strcmp(r->recid, id))
+            {
+                *next_r = reclist_read_record(se->reclist);
+                break;
+            }
+            *prev_r = r;
         }
-        *prev_r = r;
+        reclist_leave(se->reclist);
     }
-    reclist_leave(s->reclist);
     if (!r)
-        session_leave(s);
+        session_leave(se);
     return r;
 }
 
-void show_single_stop(struct session *s, struct record_cluster *rec)
+void show_single_stop(struct session *se, struct record_cluster *rec)
 {
-    session_leave(s);
+    session_leave(se);
 }
 
-struct record_cluster **show_range_start(struct session *s,
+struct record_cluster **show_range_start(struct session *se,
                                          struct reclist_sortparms *sp, 
                                          int start, int *num, int *total, Odr_int *sumhits)
 {
-    struct record_cluster **recs = nmem_malloc(s->nmem, *num 
-                                               * sizeof(struct record_cluster *));
+    struct record_cluster **recs;
     struct reclist_sortparms *spp;
     int i;
 #if USE_TIMING    
     yaz_timing_t t = yaz_timing_create();
 #endif
-
-    session_enter(s);
-    if (!s->relevance)
+    session_enter(se);
+    recs = nmem_malloc(se->nmem, *num * sizeof(struct record_cluster *));
+    if (!se->relevance)
     {
         *num = 0;
         *total = 0;
@@ -807,17 +985,17 @@ struct record_cluster **show_range_start(struct session *s,
         for (spp = sp; spp; spp = spp->next)
             if (spp->type == Metadata_sortkey_relevance)
             {
-                relevance_prepare_read(s->relevance, s->reclist);
+                relevance_prepare_read(se->relevance, se->reclist);
                 break;
             }
-        reclist_sort(s->reclist, sp);
+        reclist_sort(se->reclist, sp);
         
-        reclist_enter(s->reclist);
-        *total = reclist_get_num_records(s->reclist);
-        *sumhits = s->total_hits;
+        reclist_enter(se->reclist);
+        *total = reclist_get_num_records(se->reclist);
+        *sumhits = se->total_hits;
         
         for (i = 0; i < start; i++)
-            if (!reclist_read_record(s->reclist))
+            if (!reclist_read_record(se->reclist))
             {
                 *num = 0;
                 recs = 0;
@@ -826,7 +1004,7 @@ struct record_cluster **show_range_start(struct session *s,
         
         for (i = 0; i < *num; i++)
         {
-            struct record_cluster *r = reclist_read_record(s->reclist);
+            struct record_cluster *r = reclist_read_record(se->reclist);
             if (!r)
             {
                 *num = i;
@@ -834,7 +1012,7 @@ struct record_cluster **show_range_start(struct session *s,
             }
             recs[i] = r;
         }
-        reclist_leave(s->reclist);
+        reclist_leave(se->reclist);
     }
 #if USE_TIMING
     yaz_timing_stop(t);
@@ -846,19 +1024,20 @@ struct record_cluster **show_range_start(struct session *s,
     return recs;
 }
 
-void show_range_stop(struct session *s, struct record_cluster **recs)
+void show_range_stop(struct session *se, struct record_cluster **recs)
 {
-    session_leave(s);
+    session_leave(se);
 }
 
 void statistics(struct session *se, struct statistics *stat)
 {
-    struct client *cl;
+    struct client_list *l;
     int count = 0;
 
     memset(stat, 0, sizeof(*stat));
-    for (cl = se->clients; cl; cl = client_next_in_session(cl))
+    for (l = se->clients; l; l = l->next)
     {
+        struct client *cl = l->client;
         if (!client_get_connection(cl))
             stat->num_no_connection++;
         switch (client_get_state(cl))
@@ -943,22 +1122,24 @@ static int get_mergekey_from_doc(xmlDoc *doc, xmlNode *root, const char *name,
         if (!strcmp((const char *) n->name, "metadata"))
         {
             xmlChar *type = xmlGetProp(n, (xmlChar *) "type");
-            if (!strcmp(name, (const char *) type))
+            if (type == NULL) {
+                yaz_log(YLOG_FATAL, "Missing type attribute on metadata element. Skipping!");
+            }
+            else if (!strcmp(name, (const char *) type))
             {
                 xmlChar *value = xmlNodeListGetString(doc, n->children, 1);
                 if (value)
                 {
                     const char *norm_str;
-                    pp2_relevance_token_t prt =
-                        pp2_relevance_tokenize(
-                            service->mergekey_pct,
-                            (const char *) value, 0);
+                    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_relevance_token_next(prt)))
+                            pp2_charset_token_next(prt)))
                     {
                         if (*norm_str)
                         {
@@ -967,7 +1148,7 @@ static int get_mergekey_from_doc(xmlDoc *doc, xmlNode *root, const char *name,
                         }
                     }
                     xmlFree(value);
-                    pp2_relevance_token_destroy(prt);
+                    pp2_charset_token_destroy(prt);
                     no_found++;
                 }
             }
@@ -989,12 +1170,11 @@ static const char *get_mergekey(xmlDoc *doc, struct client *cl, int record_no,
     if (mergekey)
     {
         const char *norm_str;
-        pp2_relevance_token_t prt =
-            pp2_relevance_tokenize(
-                service->mergekey_pct,
-                (const char *) mergekey, 0);
-        
-        while ((norm_str = pp2_relevance_token_next(prt)))
+        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)
             {
@@ -1003,7 +1183,7 @@ static const char *get_mergekey(xmlDoc *doc, struct client *cl, int record_no,
                 wrbuf_puts(norm_wr, norm_str);
             }
         }
-        pp2_relevance_token_destroy(prt);
+        pp2_charset_token_destroy(prt);
         xmlFree(mergekey);
     }
     else
@@ -1070,9 +1250,15 @@ static int check_record_filter(xmlNode *root, struct session_database *sdb)
             if (type)
             {
                 size_t len;
-                const char *eq = strchr(s, '~');
-                if (eq)
-                    len = eq - s;
+               int substring;
+                const char *eq;
+
+                if ((eq = strchr(s, '=')))
+                   substring = 0;
+               else if ((eq = strchr(s, '~')))
+                   substring = 1;
+               if (eq)
+                   len = eq - s;
                 else
                     len = strlen(s);
                 if (len == strlen((const char *)type) &&
@@ -1081,7 +1267,9 @@ static int check_record_filter(xmlNode *root, struct session_database *sdb)
                     xmlChar *value = xmlNodeGetContent(n);
                     if (value && *value)
                     {
-                        if (!eq || strstr((const char *) value, eq+1))
+                        if (!eq ||
+                           (substring && strstr((const char *) value, eq+1)) ||
+                           (!substring && !strcmp((const char *) value, eq + 1)))
                             match = 1;
                     }
                     xmlFree(value);
@@ -1104,17 +1292,19 @@ static int ingest_to_cluster(struct client *cl,
     \param cl client holds the result set for record
     \param rec record buffer (0 terminated)
     \param record_no record position (1, 2, ..)
+    \param nmem working NMEM
     \retval 0 OK
     \retval -1 failure
+    \retval -2 Filtered
 */
 int ingest_record(struct client *cl, const char *rec,
                   int record_no, NMEM nmem)
 {
     struct session *se = client_get_session(cl);
-    int ret;
+    int ret = 0;
     struct session_database *sdb = client_get_database(cl);
     struct conf_service *service = se->service;
-    xmlDoc *xdoc = normalize_record(sdb, service, rec, nmem);
+    xmlDoc *xdoc = normalize_record(se, sdb, service, rec, nmem);
     xmlNode *root;
     const char *mergekey_norm;
     
@@ -1125,21 +1315,22 @@ int ingest_record(struct client *cl, const char *rec,
     
     if (!check_record_filter(root, sdb))
     {
-        yaz_log(YLOG_WARN, "Filtered out record no %d from %s", record_no,
-                sdb->database->url);
+        session_log(se, YLOG_LOG, "Filtered out record no %d from %s",
+                    record_no, sdb->database->url);
         xmlFreeDoc(xdoc);
-        return -1;
+        return -2;
     }
     
     mergekey_norm = get_mergekey(xdoc, cl, record_no, service, nmem);
     if (!mergekey_norm)
     {
-        yaz_log(YLOG_WARN, "Got no mergekey");
+        session_log(se, YLOG_WARN, "Got no mergekey");
         xmlFreeDoc(xdoc);
         return -1;
     }
     session_enter(se);
-    ret = ingest_to_cluster(cl, xdoc, root, record_no, mergekey_norm);
+    if (client_get_session(cl) == se)
+        ret = ingest_to_cluster(cl, xdoc, root, record_no, mergekey_norm);
     session_leave(se);
     
     xmlFreeDoc(xdoc);
@@ -1167,17 +1358,31 @@ static int ingest_to_cluster(struct client *cl,
                                                     record,
                                                     mergekey_norm,
                                                     &se->total_merged);
+
+    const char *use_term_factor_str = session_setting_oneval(sdb, PZ_TERMLIST_TERM_FACTOR);
+    int use_term_factor = 0;
+    int term_factor = 1; 
+    if (use_term_factor_str && use_term_factor_str[0] != 0)
+       use_term_factor =  atoi(use_term_factor_str);
+    if (use_term_factor) {
+        int maxrecs = client_get_maxrecs(cl);
+        int hits = (int) client_get_hits(cl);
+        term_factor = MAX(hits, maxrecs) /  MAX(1, maxrecs);
+        assert(term_factor >= 1);
+        yaz_log(YLOG_DEBUG, "Using term factor: %d (%d / %d)", term_factor, MAX(hits, maxrecs), MAX(1, maxrecs));
+    }
+
     if (!cluster)
         return -1;
     if (global_parameters.dump_records)
-        yaz_log(YLOG_LOG, "Cluster id %s from %s (#%d)", cluster->recid,
-                sdb->database->url, record_no);
+        session_log(se, YLOG_LOG, "Cluster id %s from %s (#%d)", cluster->recid,
+                    sdb->database->url, record_no);
     relevance_newrec(se->relevance, cluster);
     
     // now parsing XML record and adding data to cluster or record metadata
     for (n = root->children; n; n = n->next)
     {
-        pp2_relevance_token_t prt;
+        pp2_charset_token_t prt;
         if (type)
             xmlFree(type);
         if (value)
@@ -1207,7 +1412,7 @@ static int ingest_to_cluster(struct client *cl,
             {
                 if (se->number_of_warnings_unknown_metadata == 0)
                 {
-                    yaz_log(YLOG_WARN, 
+                    session_log(se, YLOG_WARN, 
                             "Ignoring unknown metadata element: %s", type);
                 }
                 se->number_of_warnings_unknown_metadata++;
@@ -1226,8 +1431,8 @@ static int ingest_to_cluster(struct client *cl,
                                           ser_md->type, n->properties);
             if (!rec_md)
             {
-                yaz_log(YLOG_WARN, "bad metadata data '%s' for element '%s'",
-                        value, type);
+                session_log(se, YLOG_WARN, "bad metadata data '%s' "
+                            "for element '%s'", value, type);
                 continue;
             }
             wheretoput = &record->metadata[md_field_id];
@@ -1244,16 +1449,15 @@ static int ingest_to_cluster(struct client *cl,
             // assign cluster or record based on merge action
             if (ser_md->merge == Metadata_merge_unique)
             {
-                struct record_metadata *mnode;
-                for (mnode = *wheretoput; mnode; mnode = mnode->next)
-                    if (!strcmp((const char *) mnode->data.text.disp, 
+                while (*wheretoput)
+                {
+                    if (!strcmp((const char *) (*wheretoput)->data.text.disp, 
                                 rec_md->data.text.disp))
                         break;
-                if (!mnode)
-                {
-                    rec_md->next = *wheretoput;
-                    *wheretoput = rec_md;
+                    wheretoput = &(*wheretoput)->next;
                 }
+                if (!*wheretoput)
+                    *wheretoput = rec_md;
             }
             else if (ser_md->merge == Metadata_merge_longest)
             {
@@ -1273,11 +1477,13 @@ static int ingest_to_cluster(struct client *cl,
                                 nmem_malloc(se->nmem, 
                                             sizeof(union data_types));
                          
-                        prt = pp2_relevance_tokenize(
-                            service->sort_pct,
-                            rec_md->data.text.disp, skip_article);
+                        prt =
+                            pp2_charset_token_create(service->charsets, "sort");
+
+                        pp2_charset_token_first(prt, rec_md->data.text.disp,
+                                                skip_article);
 
-                        pp2_relevance_token_next(prt);
+                        pp2_charset_token_next(prt);
                          
                         sort_str = pp2_get_sort(prt);
                          
@@ -1286,24 +1492,19 @@ static int ingest_to_cluster(struct client *cl,
                         if (!sort_str)
                         {
                             sort_str = rec_md->data.text.disp;
-                            yaz_log(YLOG_WARN, 
+                            session_log(se, YLOG_WARN, 
                                     "Could not make sortkey. Bug #1858");
                         }
                         cluster->sortkeys[sk_field_id]->text.sort = 
                             nmem_strdup(se->nmem, sort_str);
-#if 0
-                        yaz_log(YLOG_LOG, "text disp=%s",
-                                cluster->sortkeys[sk_field_id]->text.disp);
-                        yaz_log(YLOG_LOG, "text sort=%s",
-                                cluster->sortkeys[sk_field_id]->text.sort);
-#endif
-                        pp2_relevance_token_destroy(prt);
+                        pp2_charset_token_destroy(prt);
                     }
                 }
             }
             else if (ser_md->merge == Metadata_merge_all)
             {
-                rec_md->next = *wheretoput;
+                while (*wheretoput)
+                    wheretoput = &(*wheretoput)->next;
                 *wheretoput = rec_md;
             }
             else if (ser_md->merge == Metadata_merge_range)
@@ -1333,22 +1534,23 @@ static int ingest_to_cluster(struct client *cl,
                                      (char *) value, ser_md->rank,
                                      ser_md->name);
 
-            // construct facets ... 
-            if (ser_md->termlist)
+            // construct facets ... unless the client already has reported them
+            if (ser_md->termlist && !client_has_facet(cl, (char *) type))
             {
                 if (ser_md->type == Metadata_type_year)
                 {
                     char year[64];
                     sprintf(year, "%d", rec_md->data.number.max);
-                    add_facet(se, (char *) type, year);
+
+                    add_facet(se, (char *) type, year, term_factor);
                     if (rec_md->data.number.max != rec_md->data.number.min)
                     {
                         sprintf(year, "%d", rec_md->data.number.min);
-                        add_facet(se, (char *) type, year);
+                        add_facet(se, (char *) type, year, term_factor);
                     }
                 }
                 else
-                    add_facet(se, (char *) type, (char *) value);
+                    add_facet(se, (char *) type, (char *) value, term_factor);
             }
 
             // cleaning up
@@ -1359,7 +1561,7 @@ static int ingest_to_cluster(struct client *cl,
         else
         {
             if (se->number_of_warnings_unknown_elements == 0)
-                yaz_log(YLOG_WARN,
+                session_log(se, YLOG_WARN,
                         "Unexpected element in internal record: %s", n->name);
             se->number_of_warnings_unknown_elements++;
         }
@@ -1375,6 +1577,18 @@ static int ingest_to_cluster(struct client *cl,
     return 0;
 }
 
+void session_log(struct session *s, int level, const char *fmt, ...)
+{
+    char buf[1024];
+    va_list ap;
+    va_start(ap, fmt);
+
+    yaz_vsnprintf(buf, sizeof(buf)-30, fmt, ap);
+    yaz_log(level, "Session (%u): %s", s->session_id, buf);
+
+    va_end(ap);
+}
+
 /*
  * Local variables:
  * c-basic-offset: 4