Make check works, no problems (yet) with threads
[pazpar2-moved-to-github.git] / src / client.c
index 0618d3a..fce605d 100644 (file)
@@ -1,5 +1,5 @@
 /* This file is part of Pazpar2.
-   Copyright (C) 2006-2012 Index Data
+   Copyright (C) 2006-2013 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
@@ -125,7 +125,6 @@ struct client {
     enum client_state state;
     struct show_raw *show_raw;
     ZOOM_resultset resultset;
-    YAZ_MUTEX mutex;
     int ref_count;
     char *id;
     facet_limits_t facet_limits;
@@ -513,22 +512,22 @@ static void ingest_raw_record(struct client *cl, ZOOM_record rec)
 void client_check_preferred_watch(struct client *cl)
 {
     struct session *se = cl->session;
+
+    session_enter_ro(se, "client_check_preferred_watch");
     yaz_log(YLOG_DEBUG, "client_check_preferred_watch: %s ", client_get_id(cl));
     if (se)
     {
-        client_unlock(cl);
-        /* TODO possible threading issue. Session can have been destroyed */
-        if (session_is_preferred_clients_ready(se)) {
+        assert(cl->session);
+        if (session_is_preferred_clients_ready(se))
             session_alert_watch(se, SESSION_WATCH_SHOW_PREF);
-        }
         else
             yaz_log(YLOG_DEBUG, "client_check_preferred_watch: Still locked on preferred targets.");
-
-        client_lock(cl);
+        assert(cl->session);
     }
     else
         yaz_log(YLOG_WARN, "client_check_preferred_watch: %s. No session!", client_get_id(cl));
 
+    session_leave_ro(se, "client_check_preferred_watch");
 }
 
 struct suggestions* client_suggestions_create(const char* suggestions_string);
@@ -542,6 +541,7 @@ void client_search_response(struct client *cl)
 
     const char *error, *addinfo = 0;
 
+    session_enter_rw(cl->session, "client_search_response");
     if (ZOOM_connection_error(link, &error, &addinfo))
     {
         cl->hits = 0;
@@ -559,23 +559,22 @@ void client_search_response(struct client *cl)
             client_suggestions_destroy(cl);
         cl->suggestions = client_suggestions_create(ZOOM_resultset_option_get(resultset, "suggestions"));
     }
+    session_leave_rw(cl->session, "client_search_response");
 }
 
 void client_got_records(struct client *cl)
 {
     struct session *se = cl->session;
-    if (se)
+
+    session_enter_ro(se, "client_got_records");
+    if (reclist_get_num_records(se->reclist) > 0)
     {
-        if (reclist_get_num_records(se->reclist) > 0)
-        {
-            client_unlock(cl);
-            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_RECORD);
-            client_lock(cl);
-        }
+        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_RECORD);
     }
+    session_leave_ro(se, "client_got_records");
 }
 
 static void client_record_ingest(struct client *cl)
@@ -652,6 +651,7 @@ void client_record_response(struct client *cl)
     ZOOM_resultset resultset = cl->resultset;
     const char *error, *addinfo;
 
+    session_enter_rw(cl->session, "client_record_response");
     if (ZOOM_connection_error(link, &error, &addinfo))
     {
         client_set_state(cl, Client_Error);
@@ -680,6 +680,7 @@ void client_record_response(struct client *cl)
             client_record_ingest(cl);
         }
     }
+    session_leave_rw(cl->session, "client_record_response");
 }
 
 int client_reingest(struct client *cl)
@@ -946,8 +947,6 @@ struct client *client_create(const char *id)
     cl->show_raw = 0;
     cl->resultset = 0;
     cl->suggestions = 0;
-    cl->mutex = 0;
-    pazpar2_mutex_create(&cl->mutex, "client");
     cl->preferred = 0;
     cl->ref_count = 1;
     cl->facet_limits = 0;
@@ -960,19 +959,9 @@ struct client *client_create(const char *id)
     return cl;
 }
 
-void client_lock(struct client *c)
-{
-    yaz_mutex_enter(c->mutex);
-}
-
-void client_unlock(struct client *c)
-{
-    yaz_mutex_leave(c->mutex);
-}
-
 void client_incref(struct client *c)
 {
-    pazpar2_incref(&c->ref_count, c->mutex);
+    c->ref_count++;
     yaz_log(YLOG_DEBUG, "client_incref c=%p %s cnt=%d",
             c, client_get_id(c), c->ref_count);
 }
@@ -983,7 +972,7 @@ int client_destroy(struct client *c)
     {
         yaz_log(YLOG_DEBUG, "client_destroy c=%p %s cnt=%d",
                 c, client_get_id(c), c->ref_count);
-        if (!pazpar2_decref(&c->ref_count, c->mutex))
+        if (--c->ref_count == 0)
         {
             xfree(c->pquery);
             c->pquery = 0;
@@ -1003,7 +992,6 @@ int client_destroy(struct client *c)
             {
                 ZOOM_resultset_destroy(c->resultset);
             }
-            yaz_mutex_destroy(&c->mutex);
             xfree(c);
             client_use(-1);
             return 1;
@@ -1174,7 +1162,8 @@ const char *client_get_facet_limit_local(struct client *cl,
 
 static int apply_limit(struct session_database *sdb,
                        facet_limits_t facet_limits,
-                       WRBUF w_pqf, CCL_bibset ccl_map)
+                       WRBUF w_pqf, CCL_bibset ccl_map,
+                       struct conf_service *service)
 {
     int ret = 0;
     int i = 0;
@@ -1268,8 +1257,22 @@ static int apply_limit(struct session_database *sdb,
         }
         if (!s)
         {
-            yaz_log(YLOG_WARN, "Target %s: limit %s used, but no limitmap defined",
-                    (sdb->database ? sdb->database->id : "<no id>"), name);
+            int i;
+            for (i = 0; i < service->num_metadata; i++)
+            {
+                struct conf_metadata *md = service->metadata + i;
+                if (!strcmp(md->name, name) && md->limitcluster)
+                {
+                    yaz_log(YLOG_LOG, "limitcluster in use for %s",
+                            md->name);
+                    break;
+                }
+            }
+            if (i == service->num_metadata)
+            {
+                yaz_log(YLOG_WARN, "Target %s: limit %s used, but no limitmap defined",
+                        (sdb->database ? sdb->database->id : "<no id>"), name);
+            }
         }
     }
     nmem_destroy(nmem_tmp);
@@ -1283,15 +1286,15 @@ static int apply_limit(struct session_database *sdb,
 // return -1 on query error
 // return -2 on limit error
 int client_parse_query(struct client *cl, const char *query,
-                       facet_limits_t facet_limits,
-                       CCL_bibset bibset)
+                       facet_limits_t facet_limits)
 {
     struct session *se = client_get_session(cl);
+    struct conf_service *service = se->service;
     struct session_database *sdb = client_get_database(cl);
     struct ccl_rpn_node *cn;
     int cerror, cpos;
     ODR odr_out;
-    CCL_bibset ccl_map = prepare_cclmap(cl, bibset);
+    CCL_bibset ccl_map = prepare_cclmap(cl, service->ccl_bibset);
     const char *sru = session_setting_oneval(sdb, PZ_SRU);
     const char *pqf_prefix = session_setting_oneval(sdb, PZ_PQF_PREFIX);
     const char *pqf_strftime = session_setting_oneval(sdb, PZ_PQF_STRFTIME);
@@ -1313,7 +1316,7 @@ int client_parse_query(struct client *cl, const char *query,
         wrbuf_puts(w_pqf, " ");
     }
 
-    if (apply_limit(sdb, facet_limits, w_pqf, ccl_map))
+    if (apply_limit(sdb, facet_limits, w_pqf, ccl_map, service))
     {
         ccl_qual_rm(&ccl_map);
         return -2;
@@ -1591,12 +1594,6 @@ int client_get_maxrecs(struct client *cl)
     return cl->maxrecs;
 }
 
-void client_set_preferred(struct client *cl, int v)
-{
-    cl->preferred = v;
-}
-
-
 struct suggestions* client_suggestions_create(const char* suggestions_string)
 {
     int i;