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

Last change on this file since 16027 was 16027, checked in by mdewsnip, 16 years ago

Fix to bug where "noError" was returned when the filter was non-existant.

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