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