Support for suggestions xml
[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
52     PAZPAR2_LAST_ERROR
53 };
54
55 // Represents a database
56 struct database {
57     char *id;
58     int num_settings;
59     struct setting **settings;
60     struct database *next;
61 };
62
63
64 // Represents a database as viewed from one session, possibly with settings overriden
65 // for that session
66 struct session_database
67 {
68     struct database *database;
69     int num_settings;
70     struct setting **settings;
71     normalize_record_t map;
72     struct session_database *next;
73 };
74
75 #define SESSION_WATCH_SHOW      0
76 #define SESSION_WATCH_RECORD    1
77 #define SESSION_WATCH_SHOW_PREF 2
78 #define SESSION_WATCH_MAX       2
79
80 #define SESSION_MAX_TERMLISTS 10
81
82 typedef void (*session_watchfun)(void *data);
83
84 struct named_termlist
85 {
86     char *name;
87     struct termlist *termlist;
88 };
89
90 struct session_watchentry {
91     void *data;
92     http_channel_observer_t obs;
93     session_watchfun fun;
94 };
95
96 struct client_list;
97
98 // End-user session
99 struct session {
100     struct conf_service *service; /* service in use for this session */
101     struct session_database *databases;  // All databases, settings overriden
102     struct client_list *clients;   // Clients connected for current search
103     NMEM session_nmem;  // Nmem for session-permanent storage
104     NMEM nmem;          // Nmem for each operation (i.e. search, result set, etc)
105     int num_termlists;
106     struct named_termlist termlists[SESSION_MAX_TERMLISTS];
107     struct relevance *relevance;
108     struct reclist *reclist;
109     struct session_watchentry watchlist[SESSION_WATCH_MAX + 1];
110     int total_records;
111     int total_merged;
112     int number_of_warnings_unknown_elements;
113     int number_of_warnings_unknown_metadata;
114     normalize_cache_t normalize_cache;
115     YAZ_MUTEX session_mutex;
116     unsigned session_id;
117     struct session_sorted_results *sorted_results;
118 };
119
120 struct statistics {
121     int num_clients;
122     int num_no_connection;
123     int num_connecting;
124     int num_working;
125     int num_idle;
126     int num_failed;
127     int num_error;
128     Odr_int num_hits;
129     int num_records;
130 };
131
132 struct hitsbytarget {
133     const char *id;
134     const char *name;
135     Odr_int hits;
136     int diagnostic;
137     int records;
138     const char *state;
139     int connected;
140     char *settings_xml;
141     char *suggestions_xml;
142 };
143
144 struct hitsbytarget *get_hitsbytarget(struct session *s, int *count, NMEM nmem);
145 struct session *new_session(NMEM nmem, struct conf_service *service,
146                             unsigned session_id);
147 void destroy_session(struct session *s);
148 void session_init_databases(struct session *s);
149 void statistics(struct session *s, struct statistics *stat);
150
151 void session_sort(struct session *se, const char *field, int increasing);
152
153 enum pazpar2_error_code session_search(struct session *s, const char *query,
154                                        const char *startrecs,
155                                        const char *maxrecs,
156                                        const char *filter, const char *limit,
157                                        const char **addinfo,
158                                        const char *sort_field, int increasing);
159 struct record_cluster **show_range_start(struct session *s,
160                                          struct reclist_sortparms *sp,
161                                          int start,
162                                          int *num, int *total, Odr_int *sumhits);
163 void show_range_stop(struct session *s, struct record_cluster **recs);
164
165 struct record_cluster *show_single_start(struct session *s, const char *id,
166                                          struct record_cluster **prev_r,
167                                          struct record_cluster **next_r);
168 void show_single_stop(struct session *s, struct record_cluster *rec);
169 struct termlist_score **get_termlist_score(struct session *s,
170                                            const char *name, int *num);
171 int session_set_watch(struct session *s, int what, session_watchfun fun, void *data, struct http_channel *c);
172 int session_active_clients(struct session *s);
173 int session_is_preferred_clients_ready(struct session *s);
174 void session_apply_setting(struct session *se, char *dbname, char *setting, char *value);
175 const char *session_setting_oneval(struct session_database *db, int offset);
176
177 int ingest_record(struct client *cl, const char *rec, int record_no, NMEM nmem);
178 void session_alert_watch(struct session *s, int what);
179 void add_facet(struct session *s, const char *type, const char *value, int count);
180
181 void perform_termlist(struct http_channel *c, struct session *se,
182                       const char *name, int num);
183 void session_log(struct session *s, int level, const char *fmt, ...)
184 #ifdef __GNUC__
185     __attribute__ ((format (printf, 3, 4)))
186 #endif
187     ;
188 #endif
189
190 /*
191  * Local variables:
192  * c-basic-offset: 4
193  * c-file-style: "Stroustrup"
194  * indent-tabs-mode: nil
195  * End:
196  * vim: shiftwidth=4 tabstop=8 expandtab
197  */
198