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

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

(Untangling colservr/recpt) Removed unnecessary inclusion of infodbclass.h.

  • Property svn:keywords set to Author Date Id Revision
File size: 17.4 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.gdbmhome = 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
261 // configure the filters
262 filtermapclass::iterator filter_here = filters.begin();
263 filtermapclass::iterator filter_end = filters.end();
264 while (filter_here != filter_end) {
265 assert ((*filter_here).second.f != NULL);
266 if ((*filter_here).second.f != NULL)
267 (*filter_here).second.f->configure(key, cfgline);
268
269 ++filter_here;
270 }
271
272 // configure the sources
273 sourcelistclass::iterator source_here = sources.begin();
274 sourcelistclass::iterator source_end = sources.end();
275 while (source_here != source_end) {
276 assert ((*source_here).s != NULL);
277 if ((*source_here).s != NULL)
278 (*source_here).s->configure(key, cfgline);
279
280 ++source_here;
281 }
282}
283
284
285void collectserver::configure (const text_t &key, const text_t &value) {
286 text_tarray cfgline;
287 cfgline.push_back (value);
288 configure(key, cfgline);
289}
290
291void collectserver::ping (bool &wasSuccess, comerror_t &error, ostream &logout) {
292 // if we've not been properly configured, then it is a foregone
293 // conclusion that we cannot be active
294 if (this->configinfo.collection == "null")
295 {
296 wasSuccess = false;
297 }
298 // if no build date exists, then the collection was probably not built;
299 // ditto if the number of documents is zero, then something is pretty
300 // wrong
301 else if (this->collectinfo.buildDate == 0 ||
302 this->collectinfo.numDocs == 0)
303 {
304 wasSuccess = false;
305 }
306 // it is probably okay
307 else
308 wasSuccess = true;
309}
310
311
312bool collectserver::init (ostream &logout) {
313 // delete the indexmap
314 indexmap.clear();
315
316 // init the filters
317 filtermapclass::iterator filter_here = filters.begin();
318 filtermapclass::iterator filter_end = filters.end();
319 while (filter_here != filter_end) {
320 assert ((*filter_here).second.f != NULL);
321 if (((*filter_here).second.f != NULL) &&
322 !(*filter_here).second.f->init(logout)) return false;
323
324 ++filter_here;
325 }
326
327 // init the sources
328 sourcelistclass::iterator source_here = sources.begin();
329 sourcelistclass::iterator source_end = sources.end();
330 while (source_here != source_end) {
331 assert ((*source_here).s != NULL);
332 if (((*source_here).s != NULL) &&
333 !(*source_here).s->init(logout)) return false;
334
335 ++source_here;
336 }
337
338 return true;
339}
340
341
342void collectserver::get_collectinfo (ColInfoResponse_t &reponse,
343 comerror_t &err, ostream &/*logout*/) {
344 reponse = collectinfo;
345 err = noError;
346}
347
348void collectserver::get_filterinfo (InfoFiltersResponse_t &response,
349 comerror_t &err, ostream &/*logout*/) {
350 response.clear ();
351
352 // get a list of filter names
353 filtermapclass::iterator filter_here = filters.begin();
354 filtermapclass::iterator filter_end = filters.end();
355 while (filter_here != filter_end) {
356 response.filterNames.insert ((*filter_here).first);
357 ++filter_here;
358 }
359
360 err = noError;
361}
362
363void collectserver::get_filteroptions (const InfoFilterOptionsRequest_t &request,
364 InfoFilterOptionsResponse_t &response,
365 comerror_t &err, ostream &logout) {
366 outconvertclass text_t2ascii;
367
368 filterclass *thisfilter = filters.getfilter(request.filterName);
369 if (thisfilter != NULL) {
370 thisfilter->get_filteroptions (response, err, logout);
371 } else {
372 response.clear ();
373 err = protocolError;
374 logout << text_t2ascii << "Protocol Error: filter options requested for non-existent\n"
375 << "filter \"" << request.filterName << "\".\n\n";
376 }
377}
378
379void collectserver::filter (FilterRequest_t &request,
380 FilterResponse_t &response,
381 comerror_t &err, ostream &logout) {
382 outconvertclass text_t2ascii;
383
384 // translate any ".fc", ".pr" etc. stuff in the docSet
385 text_t translatedOID;
386 text_tarray translatedOIDs;
387 text_tarray::iterator doc_here = request.docSet.begin();
388 text_tarray::iterator doc_end = request.docSet.end();
389 while (doc_here != doc_end) {
390 if (needs_translating (*doc_here)) {
391 sourcelistclass::iterator source_here = sources.begin();
392 sourcelistclass::iterator source_end = sources.end();
393 while (source_here != source_end) {
394 assert ((*source_here).s != NULL);
395 if (((*source_here).s != NULL) &&
396 ((*source_here).s->translate_OID (*doc_here, translatedOID, err, logout))) {
397 if (err != noError) return;
398 break;
399 }
400 ++source_here;
401 }
402 translatedOIDs.push_back (translatedOID);
403 } else {
404 translatedOIDs.push_back (*doc_here);
405 }
406 ++doc_here;
407 }
408 request.docSet = translatedOIDs;
409
410 response.clear();
411
412 filterclass *thisfilter = filters.getfilter(request.filterName);
413 if (thisfilter != NULL) {
414 // filter the data
415 thisfilter->filter (request, response, err, logout);
416 if (err != noError) return;
417 // fill in the metadata for each of the OIDs (if it is requested)
418 if (request.filterResultOptions & FRmetadata) {
419 bool processed = false;
420 ResultDocInfo_tarray::iterator resultdoc_here = response.docInfo.begin();
421 ResultDocInfo_tarray::iterator resultdoc_end = response.docInfo.end();
422 while (resultdoc_here != resultdoc_end) {
423 // try each of the sources in turn
424 sourcelistclass::iterator source_here = sources.begin();
425 sourcelistclass::iterator source_end = sources.end();
426 while (source_here != source_end) {
427 assert ((*source_here).s != NULL);
428 if (((*source_here).s != NULL) &&
429 ((*source_here).s->get_metadata(request.requestParams, request.refParams,
430 request.getParents, request.fields,
431 (*resultdoc_here).OID, (*resultdoc_here).metadata,
432 err, logout))) {
433 if (err != noError) return;
434 processed = true;
435 break;
436 }
437 ++source_here;
438 }
439 if (!processed) {
440
441 logout << text_t2ascii << "Protocol Error: nothing processed for "
442 << "filter \"" << request.filterName << "\".\n\n";
443
444 err = protocolError;
445 return;
446 }
447 ++resultdoc_here;
448 }
449 }
450
451 } else {
452 response.clear ();
453 err = protocolError;
454 logout << text_t2ascii << "Protocol Error: filter options requested for non-existent\n"
455 << "filter \"" << request.filterName << "\".\n\n";
456 }
457
458 err = noError;
459}
460
461void collectserver::get_document (const DocumentRequest_t &request,
462 DocumentResponse_t &response,
463 comerror_t &err, ostream &logout) {
464
465 sourcelistclass::iterator source_here = sources.begin();
466 sourcelistclass::iterator source_end = sources.end();
467 while (source_here != source_end) {
468 assert ((*source_here).s != NULL);
469 if (((*source_here).s != NULL) &&
470 ((*source_here).s->get_document (request.OID, response.doc, err, logout))) {
471 if (err != noError) return;
472 break;
473 }
474 ++source_here;
475 }
476}
477
478void collectserver::is_searchable (bool &issearchable, comerror_t &err,
479 ostream &logout) {
480
481 sourcelistclass::iterator source_here = sources.begin();
482 sourcelistclass::iterator source_end = sources.end();
483 while (source_here != source_end) {
484 assert ((*source_here).s != NULL);
485 if (((*source_here).s != NULL) &&
486 ((*source_here).s->is_searchable (issearchable, err, logout))) {
487 if (err != noError) return;
488 break;
489 }
490 ++source_here;
491 }
492}
493
494
495bool operator==(const collectserverptr &x, const collectserverptr &y) {
496 return (x.c == y.c);
497}
498
499bool operator<(const collectserverptr &x, const collectserverptr &y) {
500 return (x.c < y.c);
501}
502
503
504// thecollectserver remains the property of the calling code but
505// should not be deleted until it is removed from this list.
506void collectservermapclass::addcollectserver (collectserver *thecollectserver) {
507 // can't add a null collection server
508 assert (thecollectserver != NULL);
509 if (thecollectserver == NULL) return;
510
511 // can't add an collection server with no collection name
512 assert (!(thecollectserver->get_collection_name()).empty());
513 if ((thecollectserver->get_collection_name()).empty()) return;
514
515 collectserverptr cptr;
516 cptr.c = thecollectserver;
517 collectserverptrs[thecollectserver->get_collection_name()] = cptr;
518}
519
520// getcollectserver will return NULL if the collectserver could not be found
521collectserver *collectservermapclass::getcollectserver (const text_t &collection) {
522 // can't find a collection with no name
523 if (collection.empty()) return NULL;
524
525 iterator here = collectserverptrs.find (collection);
526 if (here == collectserverptrs.end()) return NULL;
527
528 return (*here).second.c;
529}
Note: See TracBrowser for help on using the repository browser.