f27e9bb3d24d7382c0ac9c84c4438495e0935ba1
[pazpar2-moved-to-github.git] / src / pazpar2.h
1 /* This file is part of Pazpar2.
2    Copyright (C) 2006-2008 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_H
21 #define PAZPAR2_H
22
23
24 #include <netdb.h>
25
26 #include <libxslt/xsltutils.h>
27 #include <libxslt/transform.h>
28
29 #include <yaz/comstack.h>
30 #include <yaz/pquery.h>
31 #include <yaz/ccl.h>
32 #include <yaz/yaz-ccl.h>
33
34 #include "termlists.h"
35 #include "relevance.h"
36 #include "reclists.h"
37 #include "eventl.h"
38 #include "config.h"
39 #include "parameters.h"
40 #include "http.h"
41
42 struct record;
43 struct client;
44
45
46 enum pazpar2_error_code {
47     PAZPAR2_NO_ERROR = 0,
48
49     PAZPAR2_NO_SESSION,
50     PAZPAR2_MISSING_PARAMETER,
51     PAZPAR2_MALFORMED_PARAMETER_VALUE,
52     PAZPAR2_MALFORMED_PARAMETER_ENCODING,
53     PAZPAR2_MALFORMED_SETTING,
54     PAZPAR2_HITCOUNTS_FAILED,
55     PAZPAR2_RECORD_MISSING,
56     PAZPAR2_NO_TARGETS,
57     PAZPAR2_CONFIG_TARGET,
58     PAZPAR2_RECORD_FAIL,
59     PAZPAR2_NOT_IMPLEMENTED,
60
61     PAZPAR2_LAST_ERROR
62 };
63
64 // Represents a (virtual) database on a host
65 struct database {
66     struct host *host;
67     char *url;
68     char **databases;
69     int errors;
70     struct zr_explain *explain;
71     struct setting **settings;
72     struct database *next;
73 };
74
75 struct database_criterion_value {
76     char *value;
77     struct database_criterion_value *next;
78 };
79
80 struct database_criterion {
81     char *name;
82     struct database_criterion_value *values;
83     struct database_criterion *next;
84 };
85
86 // Normalization filter. Turns incoming record into internal representation
87 // Simple sequence of stylesheets run in series.
88 struct database_retrievalmap {
89     xsltStylesheet *stylesheet;
90     struct database_retrievalmap *next;
91 };
92
93 // Represents a database as viewed from one session, possibly with settings overriden
94 // for that session
95 struct session_database
96 {
97     struct database *database;
98     struct setting **settings;
99     yaz_marc_t yaz_marc;
100     struct database_retrievalmap *map;
101     struct session_database *next;
102 };
103
104
105
106 #define SESSION_WATCH_SHOW      0
107 #define SESSION_WATCH_RECORD    1
108 #define SESSION_WATCH_MAX       1
109
110 #define SESSION_MAX_TERMLISTS 10
111
112 typedef void (*session_watchfun)(void *data);
113
114 struct named_termlist
115 {
116     char *name;
117     struct termlist *termlist;
118 };
119
120 struct session_watchentry {
121     void *data;
122     http_channel_observer_t obs;
123     session_watchfun fun;
124 };
125
126 // End-user session
127 struct session {
128     struct session_database *databases;  // All databases, settings overriden
129     struct client *clients;              // Clients connected for current search
130     int requestid; 
131     NMEM session_nmem;  // Nmem for session-permanent storage
132     NMEM nmem;          // Nmem for each operation (i.e. search, result set, etc)
133     WRBUF wrbuf;        // Wrbuf for scratch(i.e. search)
134     int num_termlists;
135     struct named_termlist termlists[SESSION_MAX_TERMLISTS];
136     struct relevance *relevance;
137     struct reclist *reclist;
138     struct session_watchentry watchlist[SESSION_WATCH_MAX + 1];
139     int expected_maxrecs;
140     int total_hits;
141     int total_records;
142     int total_merged;
143 };
144
145 struct statistics {
146     int num_clients;
147     int num_no_connection;
148     int num_connecting;
149     int num_initializing;
150     int num_searching;
151     int num_presenting;
152     int num_idle;
153     int num_failed;
154     int num_error;
155     int num_hits;
156     int num_records;
157 };
158
159 struct hitsbytarget {
160     char *id;
161     const char *name;
162     int hits;
163     int diagnostic;
164     int records;
165     const char *state;
166     int connected;
167 };
168
169 struct hitsbytarget *hitsbytarget(struct session *s, int *count, NMEM nmem);
170 int select_targets(struct session *se, struct database_criterion *crit);
171 struct session *new_session(NMEM nmem);
172 void destroy_session(struct session *s);
173 void session_init_databases(struct session *s);
174 int load_targets(struct session *s, const char *fn);
175 void statistics(struct session *s, struct statistics *stat);
176 enum pazpar2_error_code search(struct session *s, char *query, 
177                                char *filter, const char **addinfo);
178 struct record_cluster **show(struct session *s, struct reclist_sortparms *sp, int start,
179         int *num, int *total, int *sumhits, NMEM nmem_show);
180 struct record_cluster *show_single(struct session *s, const char *id);
181 struct termlist_score **termlist(struct session *s, const char *name, int *num);
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 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 void start_http_listener(void);
188 void start_proxy(void);
189
190 void pazpar2_add_channel(IOCHAN c);
191 void pazpar2_event_loop(void);
192
193 int host_getaddrinfo(struct host *host);
194
195 xmlDoc *normalize_record(struct session_database *sdb, struct session *se,
196         Z_External *rec);
197 xmlDoc *record_to_xml(struct session_database *sdb, Z_External *rec);
198
199 struct record *ingest_record(struct client *cl, Z_External *rec,
200                              int record_no);
201 void session_alert_watch(struct session *s, int what);
202 void pull_terms(NMEM nmem, struct ccl_rpn_node *n, char **termlist, int *num);
203
204 #endif
205
206 /*
207  * Local variables:
208  * c-basic-offset: 4
209  * indent-tabs-mode: nil
210  * End:
211  * vim: shiftwidth=4 tabstop=8 expandtab
212  */