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