Command bytarget addinfo from diagnostics
[pazpar2-moved-to-github.git] / src / session.h
1 /* This file is part of Pazpar2.
2    Copyright (C) 2006-2011 Index Data
3
4 Pazpar2 is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 */
19
20 #ifndef PAZPAR2_SESSION_H
21 #define PAZPAR2_SESSION_H
22
23 #include <yaz/comstack.h>
24 #include <yaz/pquery.h>
25 #include <yaz/ccl.h>
26 #include <yaz/yaz-ccl.h>
27
28 #include "termlists.h"
29 #include "reclists.h"
30 #include "http.h"
31
32 struct record;
33 struct client;
34
35
36 enum pazpar2_error_code {
37     PAZPAR2_NO_ERROR = 0,
38
39     PAZPAR2_NO_SESSION,
40     PAZPAR2_MISSING_PARAMETER,
41     PAZPAR2_MALFORMED_PARAMETER_VALUE,
42     PAZPAR2_MALFORMED_PARAMETER_ENCODING,
43     PAZPAR2_MALFORMED_SETTING,
44     PAZPAR2_HITCOUNTS_FAILED,
45     PAZPAR2_RECORD_MISSING,
46     PAZPAR2_NO_TARGETS,
47     PAZPAR2_CONFIG_TARGET,
48     PAZPAR2_RECORD_FAIL,
49     PAZPAR2_NOT_IMPLEMENTED,
50     PAZPAR2_NO_SERVICE,
51     PAZPAR2_ALREADY_BLOCKED,
52
53     PAZPAR2_LAST_ERROR
54 };
55
56 // Represents a database
57 struct database {
58     char *id;
59     int num_settings;
60     struct setting **settings;
61     struct database *next;
62 };
63
64
65 // Represents a database as viewed from one session, possibly with settings overriden
66 // for that session
67 struct session_database
68 {
69     struct database *database;
70     int num_settings;
71     struct setting **settings;
72     normalize_record_t map;
73     struct session_database *next;
74 };
75
76 #define SESSION_WATCH_SHOW      0
77 #define SESSION_WATCH_RECORD    1
78 #define SESSION_WATCH_SHOW_PREF 2
79 #define SESSION_WATCH_TERMLIST  3
80 #define SESSION_WATCH_BYTARGET  4
81 #define SESSION_WATCH_MAX       4
82
83 #define SESSION_MAX_TERMLISTS 10
84
85 typedef void (*session_watchfun)(void *data);
86
87 struct named_termlist
88 {
89     char *name;
90     struct termlist *termlist;
91 };
92
93 struct session_watchentry {
94     void *data;
95     http_channel_observer_t obs;
96     session_watchfun fun;
97 };
98
99 struct client_list;
100
101 // End-user session
102 struct session {
103     struct conf_service *service; /* service in use for this session */
104     struct session_database *databases;  // All databases, settings overriden
105     struct client_list *clients_active; // Clients connected for current search
106     struct client_list *clients_cached; // Clients in cache
107     NMEM session_nmem;  // Nmem for session-permanent storage
108     NMEM nmem;          // Nmem for each operation (i.e. search, result set, etc)
109     int num_termlists;
110     struct named_termlist termlists[SESSION_MAX_TERMLISTS];
111     struct relevance *relevance;
112     struct reclist *reclist;
113     struct session_watchentry watchlist[SESSION_WATCH_MAX + 1];
114     int total_records;
115     int total_merged;
116     int number_of_warnings_unknown_elements;
117     int number_of_warnings_unknown_metadata;
118     normalize_cache_t normalize_cache;
119     YAZ_MUTEX session_mutex;
120     unsigned session_id;
121     int settings_modified;
122     struct session_sorted_results *sorted_results;
123 };
124
125 struct statistics {
126     int num_clients;
127     int num_no_connection;
128     int num_connecting;
129     int num_working;
130     int num_idle;
131     int num_failed;
132     int num_error;
133     Odr_int num_hits;
134     int num_records;
135 };
136
137 struct hitsbytarget {
138     const char *id;
139     const char *name;
140     Odr_int hits;
141     int diagnostic;
142     const char *addinfo;
143     int records;
144     const char *state;
145     int connected;
146     char *settings_xml;
147     char *suggestions_xml;
148 };
149
150 struct hitsbytarget *get_hitsbytarget(struct session *s, int *count, NMEM nmem);
151 struct session *new_session(NMEM nmem, struct conf_service *service,
152                             unsigned session_id);
153 void session_destroy(struct session *s);
154 void session_init_databases(struct session *s);
155 void statistics(struct session *s, struct statistics *stat);
156
157 void session_sort(struct session *se, const char *field, int increasing);
158
159 enum pazpar2_error_code session_search(struct session *s, const char *query,
160                                        const char *startrecs,
161                                        const char *maxrecs,
162                                        const char *filter, const char *limit,
163                                        const char **addinfo,
164                                        const char *sort_field, int increasing);
165 struct record_cluster **show_range_start(struct session *s,
166                                          struct reclist_sortparms *sp,
167                                          int start,
168                                          int *num, int *total, Odr_int *sumhits);
169 void show_range_stop(struct session *s, struct record_cluster **recs);
170
171 struct record_cluster *show_single_start(struct session *s, const char *id,
172                                          struct record_cluster **prev_r,
173                                          struct record_cluster **next_r);
174 void show_single_stop(struct session *s, struct record_cluster *rec);
175 struct termlist_score **get_termlist_score(struct session *s,
176                                            const char *name, int *num);
177 int session_set_watch(struct session *s, int what, session_watchfun fun, void *data, struct http_channel *c);
178 int session_active_clients(struct session *s);
179 int session_is_preferred_clients_ready(struct session *s);
180 void session_apply_setting(struct session *se, char *dbname, char *setting, char *value);
181 const char *session_setting_oneval(struct session_database *db, int offset);
182
183 int ingest_record(struct client *cl, const char *rec, int record_no, NMEM nmem);
184 void session_alert_watch(struct session *s, int what);
185 void add_facet(struct session *s, const char *type, const char *value, int count);
186
187
188 void perform_termlist(struct http_channel *c, struct session *se,
189                       const char *name, int num);
190 void session_log(struct session *s, int level, const char *fmt, ...)
191 #ifdef __GNUC__
192     __attribute__ ((format (printf, 3, 4)))
193 #endif
194     ;
195 #endif
196
197 struct session_sorted_results {
198     const char *field;
199     int increasing;
200     struct session_sorted_results *next;
201 };
202
203 /*
204  * Local variables:
205  * c-basic-offset: 4
206  * c-file-style: "Stroustrup"
207  * indent-tabs-mode: nil
208  * End:
209  * vim: shiftwidth=4 tabstop=8 expandtab
210  */
211