Updates to pz:authentication documentation
[pazpar2-moved-to-github.git] / src / session.c
index c607418..49425d5 100644 (file)
@@ -113,7 +113,7 @@ static int session_use(int delta)
     return sessions;
 }
 
-int sessions_count(void)
+int sessions_get_count(void)
 {
     return session_use(0);
 }
@@ -228,18 +228,27 @@ static void session_add_id_facet(struct session *s, struct client *cl,
 }
 
 
+// Look up a facet term, and return matching id
+// If facet type not found, returns 0
+// If facet type found, but no matching term, returns ""
 const char *session_lookup_id_facet(struct session *s, struct client *cl,
                                     const char *type,
                                     const char *term)
 {
+    char *retval = 0;
     struct facet_id *t = s->facet_id_list;
-    for (; t; t = t->next)
-        if (!strcmp(client_get_id(cl), t->client_id) &&
-            !strcmp(t->type, type) && !strcmp(t->term, term))
+    for (; t; t = t->next) 
+    {
+        if (!strcmp(client_get_id(cl), t->client_id) &&  !strcmp(t->type, type) )
         {
-            return t->id;
+            retval = "";
+            if ( !strcmp(t->term, term))
+            {
+                return t->id;
+            }
         }
-    return 0;
+    }
+    return retval;
 }
 
 void add_facet(struct session *s, const char *type, const char *value, int count, struct client *cl)
@@ -561,7 +570,6 @@ static void select_targets_callback(struct session *se,
         l->next = se->clients_cached;
         se->clients_cached = l;
     }
-    /* set session always. If may be 0 if client is not active */
     client_set_session(cl, se);
 
     l = xmalloc(sizeof(*l));
@@ -610,6 +618,7 @@ static void session_remove_cached_clients(struct session *se)
         client_lock(l->client);
         client_set_session(l->client, 0);
         client_set_database(l->client, 0);
+        client_mark_dead(l->client);
         client_unlock(l->client);
         client_destroy(l->client);
         xfree(l);
@@ -877,6 +886,7 @@ enum pazpar2_error_code session_search(struct session *se,
         *addinfo = "limit";
         session_leave(se, "session_search");
         se->clients_starting = 0;
+        session_reset_active_clients(se, 0);
         return PAZPAR2_MALFORMED_PARAMETER_VALUE;
     }
 
@@ -1038,8 +1048,17 @@ void session_apply_setting(struct session *se, const char *dbname,
 void session_destroy(struct session *se)
 {
     struct session_database *sdb;
-    session_log(se, YLOG_LOG, "destroy");
-    session_use(-1);
+    struct facet_id *t;
+    int sessions_total = session_use(-1);
+    int no_facet_ids = 0;
+
+    for (t = se->facet_id_list; t; t = t->next)
+        no_facet_ids++;
+    session_log(se, YLOG_LOG, "destroy "
+                "session-total %d nmem-op %zd nmem-ses %zd facets-ids %d",
+                sessions_total,
+                nmem_total(se->nmem), nmem_total(se->session_nmem),
+                no_facet_ids);
     session_remove_cached_clients(se);
 
     for (sdb = se->databases; sdb; sdb = sdb->next)
@@ -1049,10 +1068,6 @@ void session_destroy(struct session *se)
     reclist_destroy(se->reclist);
     xfree(se->mergekey);
     xfree(se->rank);
-    if (nmem_total(se->nmem))
-        session_log(se, YLOG_DEBUG, "NMEN operation usage %zd", nmem_total(se->nmem));
-    if (nmem_total(se->session_nmem))
-        session_log(se, YLOG_DEBUG, "NMEN session usage %zd", nmem_total(se->session_nmem));
     facet_limits_destroy(se->facet_limits);
     nmem_destroy(se->nmem);
     service_destroy(se->service);
@@ -1070,8 +1085,8 @@ size_t session_get_memory_status(struct session *session) {
 }
 
 
-struct session *new_session(NMEM nmem, struct conf_service *service,
-                            unsigned session_id)
+struct session *session_create(NMEM nmem, struct conf_service *service,
+                               unsigned session_id)
 {
     int i;
     struct session *session = nmem_malloc(nmem, sizeof(*session));
@@ -1111,9 +1126,9 @@ 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);
+    i = session_use(1);
+    session_log(session, YLOG_LOG, "create session-total %d", i);
     return session;
 }