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 typedef void (*session_watchfun)(void *data);
75
76 struct named_termlist
77 {
78     char *name;
79     struct termlist *termlist;
80     struct named_termlist *next;
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     struct named_termlist *termlists;
100     struct relevance *relevance;
101     struct reclist *reclist;
102     char *mergekey;
103     char *rank;
104     struct session_watchentry watchlist[SESSION_WATCH_MAX + 1];
105     int total_records;
106     int total_merged;
107     int number_of_warnings_unknown_elements;
108     int number_of_warnings_unknown_metadata;
109     normalize_cache_t normalize_cache;
110     YAZ_MUTEX session_mutex;
111     unsigned session_id;
112     int settings_modified;
113     facet_limits_t facet_limits;
114     int clients_starting;
115     int force_position;
116     struct reclist_sortparms *sorted_results;
117     struct facet_id *facet_id_list;
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     Odr_int approximation;
137     int diagnostic;
138     const char *message;
139     const char *addinfo;
140     int records;
141     int filtered;
142     const char *state;
143     int connected;
144     char *settings_xml;
145     char *suggestions_xml;
146     const char *query_type;
147     const char *query_data;
148 };
149
150 struct hitsbytarget *get_hitsbytarget(struct session *s, int *count, NMEM nmem);
151 struct session *session_create(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 int sessions_get_count(void);
158
159 void session_sort(struct session *se, struct reclist_sortparms *sp,
160                   const char *mergekey, const char *rank);
161
162 enum pazpar2_error_code session_search(struct session *s, const char *query,
163                                        const char *startrecs,
164                                        const char *maxrecs,
165                                        const char *filter, const char *limit,
166                                        const char **addinfo,
167                                        const char **addinfo2,
168                                        struct reclist_sortparms *sort_parm,
169                                        const char *mergekey,
170                                        const char *rank);
171 struct record_cluster **show_range_start(struct session *s,
172                                          struct reclist_sortparms *sp,
173                                          int start,
174                                          int *num, int *total,
175                                          Odr_int *sumhits,
176                                          Odr_int *approximation,
177                                          void (*ready)(void *data),
178                                          struct http_channel *chan);
179 int session_fetch_more(struct session *s);
180 void show_range_stop(struct session *s, struct record_cluster **recs);
181
182 struct record_cluster *show_single_start(struct session *s, const char *id,
183                                          struct record_cluster **prev_r,
184                                          struct record_cluster **next_r);
185 void show_single_stop(struct session *s, struct record_cluster *rec);
186 int session_set_watch(struct session *s, int what, session_watchfun fun, void *data, struct http_channel *c);
187 int session_active_clients(struct session *s);
188 int session_is_preferred_clients_ready(struct session *s);
189 void session_apply_setting(struct session *se, const char *dbname,
190                            const char *setting, const char *value);
191 const char *session_setting_oneval(struct session_database *db, int offset);
192 void session_stop(struct session *se);
193
194 int ingest_record(struct client *cl, const char *rec, int record_no, NMEM nmem);
195 int ingest_xml_record(struct client *cl, xmlDoc *xdoc,
196                       int record_no, NMEM nmem, int cached_copy);
197 void session_alert_watch(struct session *s, int what);
198 void add_facet(struct session *s, const char *type, const char *value, int count, struct client *cl);
199
200 int session_check_cluster_limit(struct session *se, struct record_cluster *rec);
201
202 void perform_termlist(struct http_channel *c, struct session *se, const char *name, int num, int version);
203
204 const char *session_lookup_id_facet(struct session *s, struct client *cl,
205                                     const char *type, const char *term);
206
207 void session_log(struct session *s, int level, const char *fmt, ...)
208 #ifdef __GNUC__
209     __attribute__ ((format (printf, 3, 4)))
210 #endif
211     ;
212 #endif
213
214 /*
215  * Local variables:
216  * c-basic-offset: 4
217  * c-file-style: "Stroustrup"
218  * indent-tabs-mode: nil
219  * End:
220  * vim: shiftwidth=4 tabstop=8 expandtab
221  */
222