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