Better error message for CCL parse errors PAZ-887
[pazpar2-moved-to-github.git] / src / session.h
1 /* This file is part of Pazpar2.
2    Copyright (C) 2006-2013 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 "facet_limit.h"
29 #include "reclists.h"
30
31 struct record;
32 struct client;
33
34
35 enum pazpar2_error_code {
36     PAZPAR2_NO_ERROR = 0,
37
38     PAZPAR2_NO_SESSION,
39     PAZPAR2_MISSING_PARAMETER,
40     PAZPAR2_MALFORMED_PARAMETER_VALUE,
41     PAZPAR2_MALFORMED_PARAMETER_ENCODING,
42     PAZPAR2_MALFORMED_SETTING,
43     PAZPAR2_HITCOUNTS_FAILED,
44     PAZPAR2_RECORD_MISSING,
45     PAZPAR2_NO_TARGETS,
46     PAZPAR2_CONFIG_TARGET,
47     PAZPAR2_RECORD_FAIL,
48     PAZPAR2_NOT_IMPLEMENTED,
49     PAZPAR2_NO_SERVICE,
50     PAZPAR2_ALREADY_BLOCKED,
51
52     PAZPAR2_LAST_ERROR
53 };
54
55 // Represents a database as viewed from one session, possibly with settings overriden
56 // for that session
57 struct session_database
58 {
59     struct database *database;
60     int num_settings;
61     struct setting **settings;
62     normalize_record_t map;
63     struct session_database *next;
64 };
65
66 #define SESSION_WATCH_SHOW      0
67 #define SESSION_WATCH_RECORD    1
68 #define SESSION_WATCH_SHOW_PREF 2
69 #define SESSION_WATCH_TERMLIST  3
70 #define SESSION_WATCH_BYTARGET  4
71 #define SESSION_WATCH_MAX       4
72
73 #define SESSION_MAX_TERMLISTS 10
74
75 typedef void (*session_watchfun)(void *data);
76
77 struct named_termlist
78 {
79     char *name;
80     struct termlist *termlist;
81 };
82
83 struct session_watchentry {
84     void *data;
85     http_channel_observer_t obs;
86     session_watchfun fun;
87 };
88
89 struct client_list;
90
91 // End-user session
92 struct session {
93     struct conf_service *service; /* service in use for this session */
94     struct session_database *databases;  // All databases, settings overriden
95     struct client_list *clients_active; // Clients connected for current search
96     struct client_list *clients_cached; // Clients in cache
97     NMEM session_nmem;  // Nmem for session-permanent storage
98     NMEM nmem;          // Nmem for each operation (i.e. search, result set, etc)
99     int num_termlists;
100     struct named_termlist termlists[SESSION_MAX_TERMLISTS];
101     struct relevance *relevance;
102     struct reclist *reclist;
103     char *mergekey;
104     char *rank;
105     struct session_watchentry watchlist[SESSION_WATCH_MAX + 1];
106     int total_records;
107     int total_merged;
108     int number_of_warnings_unknown_elements;
109     int number_of_warnings_unknown_metadata;
110     normalize_cache_t normalize_cache;
111     YAZ_MUTEX session_mutex;
112     unsigned session_id;
113     int settings_modified;
114     facet_limits_t facet_limits;
115     struct reclist_sortparms *sorted_results;
116 };
117
118 struct statistics {
119     int num_clients;
120     int num_no_connection;
121     int num_connecting;
122     int num_working;
123     int num_idle;
124     int num_failed;
125     int num_error;
126     Odr_int num_hits;
127     int num_records;
128 };
129
130 struct hitsbytarget {
131     const char *id;
132     const char *name;
133     Odr_int hits;
134     Odr_int approximation;
135     int diagnostic;
136     const char *message;
137     const char *addinfo;
138     int records;
139     int filtered;
140     const char *state;
141     int connected;
142     char *settings_xml;
143     char *suggestions_xml;
144 };
145
146 struct hitsbytarget *get_hitsbytarget(struct session *s, int *count, NMEM nmem);
147 struct session *new_session(NMEM nmem, struct conf_service *service,
148                             unsigned session_id);
149 int sessions_count(void);
150 void session_destroy(struct session *s);
151 void session_init_databases(struct session *s);
152 void statistics(struct session *s, struct statistics *stat);
153
154 void session_sort(struct session *se, struct reclist_sortparms *sp,
155                   const char *mergekey, const char *rank);
156
157 enum pazpar2_error_code session_search(struct session *s, const char *query,
158                                        const char *startrecs,
159                                        const char *maxrecs,
160                                        const char *filter, const char *limit,
161                                        const char **addinfo,
162                                        const char **addinfo2,
163                                        struct reclist_sortparms *sort_parm,
164                                        const char *mergekey,
165                                        const char *rank);
166 struct record_cluster **show_range_start(struct session *s,
167                                          struct reclist_sortparms *sp,
168                                          int start,
169                                          int *num, int *total,
170                                          Odr_int *sumhits,
171                                          Odr_int *approximation,
172                                          void (*ready)(void *data),
173                                          struct http_channel *chan);
174 int session_fetch_more(struct session *s);
175 void show_range_stop(struct session *s, struct record_cluster **recs);
176
177 struct record_cluster *show_single_start(struct session *s, const char *id,
178                                          struct record_cluster **prev_r,
179                                          struct record_cluster **next_r);
180 void show_single_stop(struct session *s, struct record_cluster *rec);
181 int session_set_watch(struct session *s, int what, session_watchfun fun, void *data, struct http_channel *c);
182 int session_active_clients(struct session *s);
183 int session_is_preferred_clients_ready(struct session *s);
184 void session_apply_setting(struct session *se, char *dbname, char *setting, char *value);
185 const char *session_setting_oneval(struct session_database *db, int offset);
186
187 int ingest_record(struct client *cl, const char *rec, int record_no, NMEM nmem);
188 void session_alert_watch(struct session *s, int what);
189 void add_facet(struct session *s, const char *type, const char *value, int count);
190
191 int session_check_cluster_limit(struct session *se, struct record_cluster *rec);
192
193 void perform_termlist(struct http_channel *c, struct session *se, const char *name, int num, int version);
194 void session_log(struct session *s, int level, const char *fmt, ...)
195 #ifdef __GNUC__
196     __attribute__ ((format (printf, 3, 4)))
197 #endif
198     ;
199 #endif
200
201 /*
202  * Local variables:
203  * c-basic-offset: 4
204  * c-file-style: "Stroustrup"
205  * indent-tabs-mode: nil
206  * End:
207  * vim: shiftwidth=4 tabstop=8 expandtab
208  */
209