Mutex protect normalize_record cache
[pazpar2-moved-to-github.git] / src / client.c
index e3b34cb..3e641c5 100644 (file)
@@ -1,5 +1,5 @@
 /* This file is part of Pazpar2.
-   Copyright (C) 2006-2009 Index Data
+   Copyright (C) 2006-2010 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
@@ -72,7 +72,7 @@ struct client {
     struct session *session;
     char *pquery; // Current search
     char *cqlquery; // used for SRU targets only
-    int hits;
+    Odr_int hits;
     int record_offset;
     int maxrecs;
     int startrecs;
@@ -104,8 +104,6 @@ static const char *client_states[] = {
     "Client_Disconnected"
 };
 
-static struct client *client_freelist = 0; /* thread pr */
-
 const char *client_get_state_str(struct client *cl)
 {
     return client_states[cl->state];
@@ -499,7 +497,7 @@ void client_start_search(struct client *cl)
     const char *opt_maxrecs = session_setting_oneval(sdb, PZ_MAXRECS);
     const char *opt_sru = session_setting_oneval(sdb, PZ_SRU);
     const char *opt_sort = session_setting_oneval(sdb, PZ_SORT);
-    char maxrecs_str[24];
+    char maxrecs_str[24], startrecs_str[24];
 
     assert(link);
 
@@ -534,13 +532,8 @@ void client_start_search(struct client *cl)
     else
         ZOOM_connection_option_set(link, "presentChunk", opt_maxrecs);
 
-    if (cl->startrecs)
-    {
-        char startrecs_str[24];
-        sprintf(startrecs_str, "%d", cl->startrecs);
-        
-        ZOOM_connection_option_set(link, "start", startrecs_str);
-    }
+    sprintf(startrecs_str, "%d", cl->startrecs);
+    ZOOM_connection_option_set(link, "start", startrecs_str);
 
     if (databaseName)
         ZOOM_connection_option_set(link, "databaseName", databaseName);
@@ -567,14 +560,7 @@ void client_start_search(struct client *cl)
 
 struct client *client_create(void)
 {
-    struct client *r;
-    if (client_freelist)
-    {
-        r = client_freelist;
-        client_freelist = client_freelist->next;
-    }
-    else
-        r = xmalloc(sizeof(struct client));
+    struct client *r = xmalloc(sizeof(*r));
     r->maxrecs = 100;
     r->startrecs = 0;
     r->pquery = 0;
@@ -612,9 +598,7 @@ void client_destroy(struct client *c)
         connection_release(c->connection);
 
     ZOOM_resultset_destroy(c->resultset);
-    c->resultset = 0;
-    c->next = client_freelist;
-    client_freelist = c;
+    xfree(c);
 }
 
 void client_set_connection(struct client *cl, struct connection *con)
@@ -700,6 +684,7 @@ int client_parse_query(struct client *cl, const char *query)
     CCL_bibset ccl_map = prepare_cclmap(cl);
     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);
 
     if (!ccl_map)
         return -1;
@@ -720,7 +705,26 @@ int client_parse_query(struct client *cl, const char *query)
         wrbuf_puts(se->wrbuf, pqf_prefix);
         wrbuf_puts(se->wrbuf, " ");
     }
-    ccl_pquery(se->wrbuf, cn);
+    if (!pqf_strftime || !*pqf_strftime)
+        ccl_pquery(se->wrbuf, cn);
+    else
+    {
+        time_t cur_time = time(0);
+        struct tm *tm =  localtime(&cur_time);
+        char tmp_str[300];
+        const char *cp = tmp_str;
+
+        /* see man strftime(3) for things .. In particular %% gets converted
+         to %.. And That's our original query .. */
+        strftime(tmp_str, sizeof(tmp_str)-1, pqf_strftime, tm);
+        for (; *cp; cp++)
+        {
+            if (cp[0] == '%')
+                ccl_pquery(se->wrbuf, cn);
+            else
+                wrbuf_putc(se->wrbuf, cp[0]);
+        }
+    }
     xfree(cl->pquery);
     cl->pquery = xstrdup(wrbuf_cstr(se->wrbuf));
 
@@ -770,7 +774,7 @@ struct client *client_next_in_session(struct client *cl)
 
 }
 
-int client_get_hits(struct client *cl)
+Odr_int client_get_hits(struct client *cl)
 {
     return cl->hits;
 }
@@ -780,6 +784,11 @@ int client_get_num_records(struct client *cl)
     return cl->record_offset;
 }
 
+void client_set_diagnostic(struct client *cl, int diagnostic)
+{
+    cl->diagnostic = diagnostic;
+}
+
 int client_get_diagnostic(struct client *cl)
 {
     return cl->diagnostic;