Fixed bug #1507: Command record=..&id=.. should block if it does not exist.
[pazpar2-moved-to-github.git] / src / pazpar2.h
1 /* $Id: pazpar2.h,v 1.49 2007-09-05 08:40:12 adam Exp $
2    Copyright (c) 2006-2007, Index Data.
3
4 This file is part of Pazpar2.
5
6 Pazpar2 is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
9 version.
10
11 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Pazpar2; see the file LICENSE.  If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA.
20  */
21
22 #ifndef PAZPAR2_H
23 #define PAZPAR2_H
24
25
26 #include <netdb.h>
27
28 #include <libxslt/xsltutils.h>
29 #include <libxslt/transform.h>
30
31 #include <yaz/comstack.h>
32 #include <yaz/pquery.h>
33 #include <yaz/ccl.h>
34 #include <yaz/yaz-ccl.h>
35
36 #include "termlists.h"
37 #include "relevance.h"
38 #include "reclists.h"
39 #include "eventl.h"
40 #include "config.h"
41 #include "parameters.h"
42 #include "http.h"
43
44 struct record;
45 struct client;
46
47
48 enum pazpar2_error_code {
49     PAZPAR2_NO_ERROR = 0,
50
51     PAZPAR2_NO_SESSION,
52     PAZPAR2_MISSING_PARAMETER,
53     PAZPAR2_MALFORMED_PARAMETER_VALUE,
54     PAZPAR2_MALFORMED_PARAMETER_ENCODING,
55     PAZPAR2_MALFORMED_SETTING,
56     PAZPAR2_HITCOUNTS_FAILED,
57     PAZPAR2_RECORD_MISSING,
58     PAZPAR2_NO_TARGETS,
59     PAZPAR2_CONFIG_TARGET,
60     PAZPAR2_RECORD_FAIL,
61     PAZPAR2_NOT_IMPLEMENTED,
62
63     PAZPAR2_LAST_ERROR
64 };
65
66 // Represents a (virtual) database on a host
67 struct database {
68     struct host *host;
69     char *url;
70     char **databases;
71     int errors;
72     struct zr_explain *explain;
73     struct setting **settings;
74     struct database *next;
75 };
76
77 struct database_criterion_value {
78     char *value;
79     struct database_criterion_value *next;
80 };
81
82 struct database_criterion {
83     char *name;
84     struct database_criterion_value *values;
85     struct database_criterion *next;
86 };
87
88 // Normalization filter. Turns incoming record into internal representation
89 // Simple sequence of stylesheets run in series.
90 struct database_retrievalmap {
91     xsltStylesheet *stylesheet;
92     struct database_retrievalmap *next;
93 };
94
95 // Represents a database as viewed from one session, possibly with settings overriden
96 // for that session
97 struct session_database
98 {
99     pp2_charset_t pct;
100     struct database *database;
101     struct setting **settings;
102     yaz_marc_t yaz_marc;
103     struct database_retrievalmap *map;
104     struct session_database *next;
105 };
106
107
108
109 #define SESSION_WATCH_SHOW      0
110 #define SESSION_WATCH_RECORD    1
111 #define SESSION_WATCH_MAX       1
112
113 #define SESSION_MAX_TERMLISTS 10
114
115 typedef void (*session_watchfun)(void *data);
116
117 struct named_termlist
118 {
119     char *name;
120     struct termlist *termlist;
121 };
122
123 struct session_watchentry {
124     void *data;
125     http_channel_observer_t obs;
126     session_watchfun fun;
127 };
128
129 // End-user session
130 struct session {
131     struct session_database *databases;  // All databases, settings overriden
132     struct client *clients;              // Clients connected for current search
133     int requestid; 
134     NMEM session_nmem;  // Nmem for session-permanent storage
135     NMEM nmem;          // Nmem for each operation (i.e. search, result set, etc)
136     WRBUF wrbuf;        // Wrbuf for scratch(i.e. search)
137     int num_termlists;
138     struct named_termlist termlists[SESSION_MAX_TERMLISTS];
139     struct relevance *relevance;
140     struct reclist *reclist;
141     struct session_watchentry watchlist[SESSION_WATCH_MAX + 1];
142     int expected_maxrecs;
143     int total_hits;
144     int total_records;
145     int total_merged;
146 };
147
148 struct statistics {
149     int num_clients;
150     int num_no_connection;
151     int num_connecting;
152     int num_initializing;
153     int num_searching;
154     int num_presenting;
155     int num_idle;
156     int num_failed;
157     int num_error;
158     int num_hits;
159     int num_records;
160 };
161
162 struct hitsbytarget {
163     char *id;
164     const char *name;
165     int hits;
166     int diagnostic;
167     int records;
168     const char *state;
169     int connected;
170 };
171
172 struct hitsbytarget *hitsbytarget(struct session *s, int *count, NMEM nmem);
173 int select_targets(struct session *se, struct database_criterion *crit);
174 struct session *new_session(NMEM nmem);
175 void destroy_session(struct session *s);
176 void session_init_databases(struct session *s);
177 int load_targets(struct session *s, const char *fn);
178 void statistics(struct session *s, struct statistics *stat);
179 enum pazpar2_error_code search(struct session *s, char *query, 
180                                char *filter, const char **addinfo);
181 struct record_cluster **show(struct session *s, struct reclist_sortparms *sp, int start,
182         int *num, int *total, int *sumhits, NMEM nmem_show);
183 struct record_cluster *show_single(struct session *s, const char *id);
184 struct termlist_score **termlist(struct session *s, const char *name, int *num);
185 int session_set_watch(struct session *s, int what, session_watchfun fun, void *data, struct http_channel *c);
186 int session_active_clients(struct session *s);
187 void session_apply_setting(struct session *se, char *dbname, char *setting, char *value);
188 const char *session_setting_oneval(struct session_database *db, int offset);
189
190 void start_http_listener(void);
191 void start_proxy(void);
192
193 void pazpar2_add_channel(IOCHAN c);
194 void pazpar2_event_loop(void);
195
196 int host_getaddrinfo(struct host *host);
197
198 xmlDoc *normalize_record(struct session_database *sdb, struct session *se,
199         Z_External *rec);
200 xmlDoc *record_to_xml(struct session_database *sdb, Z_External *rec);
201
202 struct record *ingest_record(struct client *cl, Z_External *rec,
203                              int record_no);
204 void session_alert_watch(struct session *s, int what);
205 void pull_terms(NMEM nmem, struct ccl_rpn_node *n, char **termlist, int *num);
206
207 int pazpar2_process(int debug, int daemon,
208                     void (*work)(void *data), void *data,
209                     const char *pidfile, const char *uid);
210
211
212 #endif
213
214 /*
215  * Local variables:
216  * c-basic-offset: 4
217  * indent-tabs-mode: nil
218  * End:
219  * vim: shiftwidth=4 tabstop=8 expandtab
220  */