Loose the query parsing so that Pazpar2 only returns error if _all_
[pazpar2-moved-to-github.git] / src / logic.c
index 05527bc..e7f2941 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: logic.c,v 1.44 2007-06-15 06:45:39 adam Exp $
+/* $Id: logic.c,v 1.48 2007-07-04 12:07:49 adam Exp $
    Copyright (c) 2006-2007, Index Data.
 
 This file is part of Pazpar2.
@@ -347,7 +347,7 @@ static int prepare_map(struct session *se, struct session_database *sdb)
             (*m)->next = 0;
             if (!((*m)->stylesheet = conf_load_stylesheet(stylesheets[i])))
             {
-                yaz_log(YLOG_FATAL, "Unable to load stylesheet: %s",
+                yaz_log(YLOG_FATAL|YLOG_ERRNO, "Unable to load stylesheet: %s",
                         stylesheets[i]);
                 return -1;
             }
@@ -384,21 +384,39 @@ static int prepare_session_database(struct session *se,
     return 0;
 }
 
+// called if watch should be removed because http_channel is to be destroyed
+static void session_watch_cancel(void *data, struct http_channel *c)
+{
+    struct session_watchentry *ent = data;
+
+    ent->fun = 0;
+    ent->data = 0;
+    ent->obs = 0;
+}
 
-void session_set_watch(struct session *s, int what, 
-                       session_watchfun fun, void *data)
+// set watch. Returns 0=OK, -1 if watch is already set
+int session_set_watch(struct session *s, int what, 
+                      session_watchfun fun, void *data,
+                      struct http_channel *chan)
 {
+    if (s->watchlist[what].fun)
+        return -1;
     s->watchlist[what].fun = fun;
     s->watchlist[what].data = data;
+    s->watchlist[what].obs = http_add_observer(chan, &s->watchlist[what],
+                                               session_watch_cancel);
+    return 0;
 }
 
 void session_alert_watch(struct session *s, int what)
 {
     if (!s->watchlist[what].fun)
         return;
+    http_remove_observer(s->watchlist[what].obs);
     (*s->watchlist[what].fun)(s->watchlist[what].data);
     s->watchlist[what].fun = 0;
     s->watchlist[what].data = 0;
+    s->watchlist[what].obs = 0;
 }
 
 //callback for grep_databases
@@ -479,6 +497,8 @@ enum pazpar2_error_code search(struct session *se,
                                const char **addinfo)
 {
     int live_channels = 0;
+    int no_working = 0;
+    int no_failed = 0;
     struct client *cl;
     struct database_criterion *criteria;
 
@@ -510,17 +530,22 @@ enum pazpar2_error_code search(struct session *se,
             *addinfo = client_get_database(cl)->database->url;
             return PAZPAR2_CONFIG_TARGET;
         }
-        // Query must parse for all targets
+        // Parse query for target
         if (client_parse_query(cl, query) < 0)
+            no_failed++;
+        else
         {
-            *addinfo = "query";
-            return PAZPAR2_MALFORMED_PARAMETER_VALUE;
+            no_working++;
+            client_prep_connection(cl);
         }
     }
 
-    for (cl = se->clients; cl; cl = client_next_in_session(cl))
-        client_prep_connection(cl);
-
+    // If no queries could be mapped, we signal an error
+    if (no_working == 0)
+    {
+        *addinfo = "query";
+        return PAZPAR2_MALFORMED_PARAMETER_VALUE;
+    }
     return PAZPAR2_NO_ERROR;
 }
 
@@ -576,7 +601,7 @@ static void session_database_destroy(struct session_database *sdb)
 void session_init_databases(struct session *se)
 {
     se->databases = 0;
-    grep_databases(se, 0, session_init_databases_fun);
+    predef_grep_databases(se, 0, session_init_databases_fun);
 }
 
 // Probably session_init_databases_fun should be refactored instead of
@@ -683,7 +708,7 @@ struct session *new_session(NMEM nmem)
     session->session_nmem = nmem;
     session->nmem = nmem_create();
     session->wrbuf = wrbuf_alloc();
-    session_init_databases(session);
+    session->databases = 0;
     for (i = 0; i <= SESSION_WATCH_MAX; i++)
     {
         session->watchlist[i].data = 0;