- else
- return 0;
-}
-
-static 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));
- r->database = 0;
- r->connection = 0;
- r->session = 0;
- r->hits = 0;
- r->records = 0;
- r->setno = 0;
- r->requestid = -1;
- r->diagnostic = 0;
- r->state = Client_Disconnected;
- r->next = 0;
- return r;
-}
-
-void client_destroy(struct client *c)
-{
- struct session *se = c->session;
- if (c == se->clients)
- se->clients = c->next;
- else
- {
- struct client *cc;
- for (cc = se->clients; cc && cc->next != c; cc = cc->next)
- ;
- if (cc)
- cc->next = c->next;
- }
- if (c->connection)
- connection_release(c->connection);
- c->next = client_freelist;
- client_freelist = c;
-}
-
-void session_set_watch(struct session *s, int what, session_watchfun fun, void *data)
-{
- s->watchlist[what].fun = fun;
- s->watchlist[what].data = data;
-}
-
-void session_alert_watch(struct session *s, int what)
-{
- if (!s->watchlist[what].fun)
- return;
- (*s->watchlist[what].fun)(s->watchlist[what].data);
- s->watchlist[what].fun = 0;
- s->watchlist[what].data = 0;
-}
-
-//callback for grep_databases
-static void select_targets_callback(void *context, struct session_database *db)
-{
- struct session *se = (struct session*) context;
- struct client *cl = client_create();
- cl->database = db;
- cl->session = se;
- cl->next = se->clients;
- se->clients = cl;
-}
-
-// Associates a set of clients with a session;
-// Note: Session-databases represent databases with per-session setting overrides
-int select_targets(struct session *se, struct database_criterion *crit)
-{
- while (se->clients)
- client_destroy(se->clients);
-
- return session_grep_databases(se, crit, select_targets_callback);
-}
-
-int session_active_clients(struct session *s)
-{
- struct client *c;
- int res = 0;
-
- for (c = s->clients; c; c = c->next)
- if (c->connection && (c->state == Client_Connecting ||
- c->state == Client_Initializing ||
- c->state == Client_Searching ||
- c->state == Client_Presenting))
- res++;
-
- return res;
-}
-
-// parses crit1=val1,crit2=val2|val3,...
-static struct database_criterion *parse_filter(NMEM m, const char *buf)
-{
- struct database_criterion *res = 0;
- char **values;
- int num;
- int i;
-
- if (!buf || !*buf)
- return 0;
- nmem_strsplit(m, ",", buf, &values, &num);
- for (i = 0; i < num; i++)
- {
- char **subvalues;
- int subnum;
- int subi;
- struct database_criterion *new = nmem_malloc(m, sizeof(*new));
- char *eq = strchr(values[i], '=');
- if (!eq)
- {
- yaz_log(YLOG_WARN, "Missing equal-sign in filter");
- return 0;
- }
- *(eq++) = '\0';
- new->name = values[i];
- nmem_strsplit(m, "|", eq, &subvalues, &subnum);
- new->values = 0;
- for (subi = 0; subi < subnum; subi++)
- {
- struct database_criterion_value *newv = nmem_malloc(m, sizeof(*newv));
- newv->value = subvalues[subi];
- newv->next = new->values;
- new->values = newv;
- }
- new->next = res;
- res = new;
- }
- return res;
-}
-
-char *search(struct session *se, char *query, char *filter)
-{
- int live_channels = 0;
- struct client *cl;
- struct database_criterion *criteria;
-
- yaz_log(YLOG_DEBUG, "Search");
-
- nmem_reset(se->nmem);
- criteria = parse_filter(se->nmem, filter);
- strcpy(se->query, query);
- se->requestid++;
- select_targets(se, criteria);
- for (cl = se->clients; cl; cl = cl->next)
- {
- if (client_prep_connection(cl))
- live_channels++;
- }
- if (live_channels)
- {
- int maxrecs = live_channels * global_parameters.toget;
- se->num_termlists = 0;
- se->reclist = reclist_create(se->nmem, maxrecs);
- // This will be initialized in send_search()
- se->relevance = 0;
- se->total_records = se->total_hits = se->total_merged = 0;
- se->expected_maxrecs = maxrecs;
- }
- else
- return "NOTARGETS";
-
- return 0;
-}
-
-// Apply a session override to a database
-void session_apply_setting(struct session *se, char *dbname, char *setting, char *value)
-{
- struct session_database *sdb;
-
- for (sdb = se->databases; sdb; sdb = sdb->next)
- if (!strcmp(dbname, sdb->database->url))
- {
- struct setting *new = nmem_malloc(se->session_nmem, sizeof(*new));
- int offset = settings_offset(setting);
-
- if (offset < 0)
- {
- yaz_log(YLOG_WARN, "Unknown setting %s", setting);
- return;
- }
- new->precedence = 0;
- new->target = dbname;
- new->name = setting;
- new->value = value;
- new->next = sdb->settings[offset];
- sdb->settings[offset] = new;
- break;
- }
- if (!sdb)
- yaz_log(YLOG_WARN, "Unknown database in setting override: %s", dbname);
-}
-
-void session_init_databases_fun(void *context, struct database *db)
-{
- struct session *se = (struct session *) context;
- struct session_database *new = nmem_malloc(se->session_nmem, sizeof(*new));
- int num = settings_num();
- int i;
-
- new->database = db;
- new->settings = nmem_malloc(se->session_nmem, sizeof(struct settings *) * num);
- for (i = 0; i < num; i++)
- new->settings[i] = db->settings[i];
- new->next = se->databases;
- se->databases = new;
-}
-
-// Initialize session_database list -- this represents this session's view
-// of the database list -- subject to modification by the settings ws command
-void session_init_databases(struct session *se)
-{
- se->databases = 0;
- grep_databases(se, 0, session_init_databases_fun);
-}
-
-void destroy_session(struct session *s)
-{
- yaz_log(YLOG_LOG, "Destroying session");
- while (s->clients)
- client_destroy(s->clients);
- nmem_destroy(s->nmem);
- wrbuf_destroy(s->wrbuf);
-}
-
-struct session *new_session(NMEM nmem)
-{
- int i;
- struct session *session = nmem_malloc(nmem, sizeof(*session));
-
- yaz_log(YLOG_DEBUG, "New Pazpar2 session");
-
- session->total_hits = 0;
- session->total_records = 0;
- session->num_termlists = 0;
- session->reclist = 0;
- session->requestid = -1;
- session->clients = 0;
- session->expected_maxrecs = 0;
- session->query[0] = '\0';
- session->session_nmem = nmem;
- session->nmem = nmem_create();
- session->wrbuf = wrbuf_alloc();
- session_init_databases(session);
- for (i = 0; i <= SESSION_WATCH_MAX; i++)
- {
- session->watchlist[i].data = 0;
- session->watchlist[i].fun = 0;
- }
-
- return session;
-}
-
-struct hitsbytarget *hitsbytarget(struct session *se, int *count)
-{
- static struct hitsbytarget res[1000]; // FIXME MM
- struct client *cl;
-
- *count = 0;
- for (cl = se->clients; cl; cl = cl->next)