source: trunk/gsdl/src/colservr/collectserver.cpp@ 12276

Last change on this file since 12276 was 12018, checked in by davidb, 18 years ago

Tweak to error reporting to provide better diagnostic.

  • Property svn:keywords set to Author Date Id Revision
File size: 16.7 KB
Line 
1
2/**********************************************************************
3 *
4 * collectserver.cpp --
5 * Copyright (C) 1999 The New Zealand Digital Library Project
6 *
7 * A component of the Greenstone digital library software
8 * from the New Zealand Digital Library Project at the
9 * University of Waikato, New Zealand.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 *
25 *********************************************************************/
26
27#include "collectserver.h"
28#include "infodbclass.h"
29#include "OIDtools.h"
30#include <assert.h>
31#include "display.h"
32
33
34collectserver::collectserver () {
35 configinfo.collection = "null";
36}
37
38collectserver::~collectserver () {
39
40 // clean up the sources
41 sourcelistclass::iterator source_here = sources.begin();
42 sourcelistclass::iterator source_end = sources.end();
43 while (source_here != source_end) {
44 if ((*source_here).s != NULL)
45 delete (*source_here).s;
46 ++source_here;
47 }
48 sources.clear();
49
50 // clean up the filters
51 filtermapclass::iterator filter_here = filters.begin();
52 filtermapclass::iterator filter_end = filters.end();
53 while (filter_here != filter_end) {
54 if ((*filter_here).second.f != NULL)
55 delete (*filter_here).second.f;
56 ++filter_here;
57 }
58 filters.clear();
59}
60
61// configure should be called for each line in the
62// configuration files to configure the collection server and everything
63// it contains. The configuration should take place just before initialisation.
64void collectserver::configure (const text_t &key, const text_tarray &cfgline) {
65 if (cfgline.size() >= 1) {
66 const text_t &value = cfgline[0];
67 if (key == "gsdlhome") configinfo.gsdlhome = value;
68 else if (key == "gdbmhome") configinfo.gdbmhome = value;
69 else if (key == "collection") {
70 configinfo.collection = value;
71 collectinfo.shortInfo.name = value;
72 } else if (key == "collectdir") configinfo.collectdir = value;
73 else if (key == "host") collectinfo.shortInfo.host = value;
74 else if (key == "port") collectinfo.shortInfo.port = value.getint();
75 else if (key == "public") {
76 if (value == "true") collectinfo.isPublic = true;
77 else collectinfo.isPublic = false;
78 } else if (key == "beta") {
79 if (value == "true") collectinfo.isBeta = true;
80 else collectinfo.isBeta = false;
81 } else if ((key == "ccscols") || (key == "supercollection")) collectinfo.ccsCols = cfgline;
82 else if (key == "supercollectionoptions") {
83 text_tarray::const_iterator begin = cfgline.begin();
84 text_tarray::const_iterator end = cfgline.end();
85 while(begin != end) {
86
87 if (*begin == "uniform_search_results_formatting") {
88 collectinfo.ccsOptions |= CCSUniformSearchResultsFormatting;
89 }
90 begin++;
91 }
92 }
93 else if (key == "builddate") collectinfo.buildDate = value.getint();
94 else if (key == "languages") collectinfo.languages = cfgline;
95 else if (key == "numdocs") collectinfo.numDocs = value.getint();
96 else if (key == "numsections") collectinfo.numSections = value.getint();
97 else if (key == "numwords") collectinfo.numWords = value.getint();
98 else if (key == "numbytes") collectinfo.numBytes = value.getint();
99 else if (key == "collectionmeta") {
100 // genuine collmeta get added as collectionmeta and collection_macros
101 // .collmeta just get added as collection_macros
102 text_t params;
103 if (cfgline.size() == 3) {
104 // get the params for later
105 text_t::const_iterator first=cfgline[1].begin()+1;
106 text_t::const_iterator last=cfgline[1].end()-1;
107 params=substr(first, last);
108 }
109
110 text_t meta_name = cfgline[0];
111 if (*(meta_name.begin())=='.') {
112 // a .xxx collectionmeta. strip off the . and
113 // look it up in the indexmap to get the actual value
114
115 text_t name = substr(cfgline[0].begin()+1,cfgline[0].end());
116 text_t new_name;
117 if (indexmap.from2to(name, new_name)) {
118 meta_name = new_name;
119 }
120 } else {
121 // add them to collectionmeta
122 text_tmap lang_map = collectinfo.collectionmeta[cfgline[0]];
123 if (cfgline.size() == 2) {
124 lang_map[g_EmptyText] = cfgline[1];
125 } else if (cfgline.size() == 3 ) {
126 // get the lang out of params
127 paramhashtype params_hash;
128 splitparams(params, params_hash);
129
130 text_t lang = params_hash["l"];
131 lang_map[lang] = cfgline[2];
132 if (lang_map[g_EmptyText].empty()) {
133 // want the first one as the default if no default specified
134 lang_map[g_EmptyText] = cfgline[2];
135 }
136 }
137 collectinfo.collectionmeta[cfgline[0]] = lang_map;
138
139 }
140
141 // add all collectionmeta to macro list
142 text_tmap params_map = collectinfo.collection_macros[meta_name];
143
144 if (cfgline.size() == 2) {// no params for this macro
145 params_map[g_EmptyText] = cfgline[1];
146 }
147 else if (cfgline.size() == 3) {// has params
148 params_map[params] = cfgline[2];
149 if (params_map[g_EmptyText].empty()) {
150 params_map[g_EmptyText] = cfgline[2];
151 }
152 }
153 collectinfo.collection_macros[meta_name] = params_map;
154 }
155 else if (key == "collectionmacro") {
156 text_t nobrackets;
157 text_tmap params_map = collectinfo.collection_macros[cfgline[0]];
158 // add all to macro list
159 if (cfgline.size() == 2) { // no params for this macro
160 params_map[g_EmptyText] = cfgline[1];
161 }
162 else if (cfgline.size() == 3) {// has params
163 // strip [ ] brackets from params
164 text_t::const_iterator first=cfgline[1].begin()+1;
165 text_t::const_iterator last=cfgline[1].end()-1;
166 nobrackets=substr(first, last);
167 params_map[nobrackets] = cfgline[2];
168 }
169 collectinfo.collection_macros[cfgline[0]] = params_map;
170
171 } else if (key == "format" && cfgline.size() == 2)
172 collectinfo.format[cfgline[0]] = cfgline[1];
173 else if (key == "building" && cfgline.size() == 2)
174 collectinfo.building[cfgline[0]] = cfgline[1];
175 else if (key == "httpdomain") collectinfo.httpdomain = value;
176 else if (key == "httpprefix") collectinfo.httpprefix = value;
177 else if (key == "receptionist") collectinfo.receptionist = value;
178 else if (key == "buildtype") collectinfo.buildType = value;
179 // backwards compatibility - searchytpes is now a format statement
180 else if (key == "searchtype") { // means buildtype is mgpp
181 if (collectinfo.buildType.empty()) {
182 collectinfo.buildType = "mgpp";
183 }
184 joinchar(cfgline, ',', collectinfo.format["SearchTypes"]);
185 //collectinfo.searchTypes = cfgline;
186 }
187 else if (key == "separate_cjk") {
188 if (value == "true") collectinfo.isSegmented = true;
189 else collectinfo.isSegmented = false;
190 }
191 // What have we set in our collect.cfg file : document or collection ?
192 else if (key == "authenticate") collectinfo.authenticate = value;
193
194 // What have we set for our group list
195 else if (key == "auth_group") joinchar(cfgline,',',collectinfo.auth_group);
196
197 // store all the mappings for use when collection meta is read later
198 // (build.cfg read before collect.cfg)
199 else if (key == "indexmap" || key == "indexfieldmap" || key == "subcollectionmap" || key == "languagemap" || key == "levelmap") {
200 indexmap.importmap (cfgline, true);
201
202 }
203 // In the map the key-value pair contain the same
204 // data i.e key == data, if key is 2 then data is 2
205
206 // What have we set for our public_documents ACL
207 else if (key == "public_documents")
208 {
209 text_tarray::const_iterator begin = cfgline.begin();
210 text_tarray::const_iterator end = cfgline.end();
211 while(begin != end)
212 {
213 // key = data i.e if key is 2 then data is 2
214 // collectinfo.public_documents[*begin] is the key
215 // *begin is the data value
216
217 collectinfo.public_documents[*begin] = *begin;
218 ++begin;
219 }
220 }
221
222 // What have we set for our private_documents ACL
223 else if (key == "private_documents")
224 {
225 text_tarray::const_iterator begin = cfgline.begin();
226 text_tarray::const_iterator end = cfgline.end();
227 while(begin != end)
228 {
229 // key = data i.e if key is 2 then data is 2
230 // collectinfo.public_documents[*begin] is the key
231 // *begin is the data value
232
233 collectinfo.private_documents[*begin] = *begin;
234 ++begin;
235 }
236 }
237 }
238
239 // configure the filters
240 filtermapclass::iterator filter_here = filters.begin();
241 filtermapclass::iterator filter_end = filters.end();
242 while (filter_here != filter_end) {
243 assert ((*filter_here).second.f != NULL);
244 if ((*filter_here).second.f != NULL)
245 (*filter_here).second.f->configure(key, cfgline);
246
247 ++filter_here;
248 }
249
250 // configure the sources
251 sourcelistclass::iterator source_here = sources.begin();
252 sourcelistclass::iterator source_end = sources.end();
253 while (source_here != source_end) {
254 assert ((*source_here).s != NULL);
255 if ((*source_here).s != NULL)
256 (*source_here).s->configure(key, cfgline);
257
258 ++source_here;
259 }
260}
261
262
263void collectserver::configure (const text_t &key, const text_t &value) {
264 text_tarray cfgline;
265 cfgline.push_back (value);
266 configure(key, cfgline);
267}
268
269void collectserver::ping (bool &wasSuccess, comerror_t &error, ostream &logout) {
270 // if we've not been properly configured, then it is a foregone
271 // conclusion that we cannot be active
272 if (this->configinfo.collection == "null")
273 {
274 wasSuccess = false;
275 }
276 // if no build date exists, then the collection was probably not built;
277 // ditto if the number of documents is zero, then something is pretty
278 // wrong
279 else if (this->collectinfo.buildDate == 0 ||
280 this->collectinfo.numDocs == 0)
281 {
282 wasSuccess = false;
283 }
284 // it is probably okay
285 else
286 wasSuccess = true;
287}
288
289
290bool collectserver::init (ostream &logout) {
291 // delete the indexmap
292 indexmap.clear();
293
294 // init the filters
295 filtermapclass::iterator filter_here = filters.begin();
296 filtermapclass::iterator filter_end = filters.end();
297 while (filter_here != filter_end) {
298 assert ((*filter_here).second.f != NULL);
299 if (((*filter_here).second.f != NULL) &&
300 !(*filter_here).second.f->init(logout)) return false;
301
302 ++filter_here;
303 }
304
305 // init the sources
306 sourcelistclass::iterator source_here = sources.begin();
307 sourcelistclass::iterator source_end = sources.end();
308 while (source_here != source_end) {
309 assert ((*source_here).s != NULL);
310 if (((*source_here).s != NULL) &&
311 !(*source_here).s->init(logout)) return false;
312
313 ++source_here;
314 }
315
316 return true;
317}
318
319
320void collectserver::get_collectinfo (ColInfoResponse_t &reponse,
321 comerror_t &err, ostream &/*logout*/) {
322 reponse = collectinfo;
323 err = noError;
324}
325
326void collectserver::get_filterinfo (InfoFiltersResponse_t &response,
327 comerror_t &err, ostream &/*logout*/) {
328 response.clear ();
329
330 // get a list of filter names
331 filtermapclass::iterator filter_here = filters.begin();
332 filtermapclass::iterator filter_end = filters.end();
333 while (filter_here != filter_end) {
334 response.filterNames.insert ((*filter_here).first);
335 ++filter_here;
336 }
337
338 err = noError;
339}
340
341void collectserver::get_filteroptions (const InfoFilterOptionsRequest_t &request,
342 InfoFilterOptionsResponse_t &response,
343 comerror_t &err, ostream &logout) {
344 outconvertclass text_t2ascii;
345
346 filterclass *thisfilter = filters.getfilter(request.filterName);
347 if (thisfilter != NULL) {
348 thisfilter->get_filteroptions (response, err, logout);
349 } else {
350 response.clear ();
351 err = protocolError;
352 logout << text_t2ascii << "Protocol Error: filter options requested for non-existent\n"
353 << "filter \"" << request.filterName << "\".\n\n";
354 }
355}
356
357void collectserver::filter (FilterRequest_t &request,
358 FilterResponse_t &response,
359 comerror_t &err, ostream &logout) {
360 outconvertclass text_t2ascii;
361
362 // translate any ".fc", ".pr" etc. stuff in the docSet
363 text_t translatedOID;
364 text_tarray translatedOIDs;
365 text_tarray::iterator doc_here = request.docSet.begin();
366 text_tarray::iterator doc_end = request.docSet.end();
367 while (doc_here != doc_end) {
368 if (needs_translating (*doc_here)) {
369 sourcelistclass::iterator source_here = sources.begin();
370 sourcelistclass::iterator source_end = sources.end();
371 while (source_here != source_end) {
372 assert ((*source_here).s != NULL);
373 if (((*source_here).s != NULL) &&
374 ((*source_here).s->translate_OID (*doc_here, translatedOID, err, logout))) {
375 if (err != noError) return;
376 break;
377 }
378 ++source_here;
379 }
380 translatedOIDs.push_back (translatedOID);
381 } else {
382 translatedOIDs.push_back (*doc_here);
383 }
384 ++doc_here;
385 }
386 request.docSet = translatedOIDs;
387
388 response.clear();
389
390 filterclass *thisfilter = filters.getfilter(request.filterName);
391 if (thisfilter != NULL) {
392 // filter the data
393 thisfilter->filter (request, response, err, logout);
394 if (err != noError) return;
395 // fill in the metadata for each of the OIDs (if it is requested)
396 if (request.filterResultOptions & FRmetadata) {
397 bool processed = false;
398 ResultDocInfo_tarray::iterator resultdoc_here = response.docInfo.begin();
399 ResultDocInfo_tarray::iterator resultdoc_end = response.docInfo.end();
400 while (resultdoc_here != resultdoc_end) {
401 // try each of the sources in turn
402 sourcelistclass::iterator source_here = sources.begin();
403 sourcelistclass::iterator source_end = sources.end();
404 while (source_here != source_end) {
405 assert ((*source_here).s != NULL);
406 if (((*source_here).s != NULL) &&
407 ((*source_here).s->get_metadata(request.requestParams, request.refParams,
408 request.getParents, request.fields,
409 (*resultdoc_here).OID, (*resultdoc_here).metadata,
410 err, logout))) {
411 if (err != noError) return;
412 processed = true;
413 break;
414 }
415 ++source_here;
416 }
417 if (!processed) {
418
419 logout << text_t2ascii << "Protocol Error: nothing processed for "
420 << "filter \"" << request.filterName << "\".\n\n";
421
422 err = protocolError;
423 return;
424 }
425 ++resultdoc_here;
426 }
427 }
428
429 } else {
430 response.clear ();
431 err = protocolError;
432 logout << text_t2ascii << "Protocol Error: filter options requested for non-existent\n"
433 << "filter \"" << request.filterName << "\".\n\n";
434 }
435
436 err = noError;
437}
438
439void collectserver::get_document (const DocumentRequest_t &request,
440 DocumentResponse_t &response,
441 comerror_t &err, ostream &logout) {
442
443 sourcelistclass::iterator source_here = sources.begin();
444 sourcelistclass::iterator source_end = sources.end();
445 while (source_here != source_end) {
446 assert ((*source_here).s != NULL);
447 if (((*source_here).s != NULL) &&
448 ((*source_here).s->get_document (request.OID, response.doc, err, logout))) {
449 if (err != noError) return;
450 break;
451 }
452 ++source_here;
453 }
454}
455
456void collectserver::is_searchable (bool &issearchable, comerror_t &err,
457 ostream &logout) {
458
459 sourcelistclass::iterator source_here = sources.begin();
460 sourcelistclass::iterator source_end = sources.end();
461 while (source_here != source_end) {
462 assert ((*source_here).s != NULL);
463 if (((*source_here).s != NULL) &&
464 ((*source_here).s->is_searchable (issearchable, err, logout))) {
465 if (err != noError) return;
466 break;
467 }
468 ++source_here;
469 }
470}
471
472
473bool operator==(const collectserverptr &x, const collectserverptr &y) {
474 return (x.c == y.c);
475}
476
477bool operator<(const collectserverptr &x, const collectserverptr &y) {
478 return (x.c < y.c);
479}
480
481
482// thecollectserver remains the property of the calling code but
483// should not be deleted until it is removed from this list.
484void collectservermapclass::addcollectserver (collectserver *thecollectserver) {
485 // can't add a null collection server
486 assert (thecollectserver != NULL);
487 if (thecollectserver == NULL) return;
488
489 // can't add an collection server with no collection name
490 assert (!(thecollectserver->get_collection_name()).empty());
491 if ((thecollectserver->get_collection_name()).empty()) return;
492
493 collectserverptr cptr;
494 cptr.c = thecollectserver;
495 collectserverptrs[thecollectserver->get_collection_name()] = cptr;
496}
497
498// getcollectserver will return NULL if the collectserver could not be found
499collectserver *collectservermapclass::getcollectserver (const text_t &collection) {
500 // can't find a collection with no name
501 if (collection.empty()) return NULL;
502
503 iterator here = collectserverptrs.find (collection);
504 if (here == collectserverptrs.end()) return NULL;
505
506 return (*here).second.c;
507}
Note: See TracBrowser for help on using the repository browser.