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

Last change on this file since 8453 was 8453, checked in by jrm21, 20 years ago

accept the 'collectionmacro' keyword for configure() from collect.cfg
and store any/all macros into the ColInfoResponse_t struct.

  • Property svn:keywords set to Author Date Id Revision
File size: 14.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 "infodbclass.h"
29#include "OIDtools.h"
30#include <assert.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 initialisation.
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 == "gsdlhome") configinfo.gsdlhome = value;
67 else if (key == "gdbmhome") configinfo.gdbmhome = value;
68 else if (key == "collection") {
69 configinfo.collection = value;
70 collectinfo.shortInfo.name = value;
71 } else if (key == "collectdir") configinfo.collectdir = value;
72 else if (key == "host") collectinfo.shortInfo.host = value;
73 else if (key == "port") collectinfo.shortInfo.port = value.getint();
74 else if (key == "public") {
75 if (value == "true") collectinfo.isPublic = true;
76 else collectinfo.isPublic = false;
77 } else if (key == "beta") {
78 if (value == "true") collectinfo.isBeta = true;
79 else collectinfo.isBeta = false;
80 } else if ((key == "ccscols") || (key == "supercollection")) collectinfo.ccsCols = cfgline;
81 else if (key == "builddate") collectinfo.buildDate = value.getint();
82 else if (key == "languages") collectinfo.languages = cfgline;
83 else if (key == "numdocs") collectinfo.numDocs = value.getint();
84 else if (key == "numsections") collectinfo.numSections = value.getint();
85 else if (key == "numwords") collectinfo.numWords = value.getint();
86 else if (key == "numbytes") collectinfo.numBytes = value.getint();
87 else if (key == "collectionmeta") {
88 if (cfgline.size() == 2)
89 collectinfo.collectionmeta[cfgline[0]] = cfgline[1];
90 else if (cfgline.size() == 3 &&
91 collectinfo.collectionmeta[cfgline[0]].empty() )
92 collectinfo.collectionmeta[cfgline[0]] = cfgline[2];
93 } else if (key == "collectionmacro") {
94 if (cfgline.size() == 2) // no params for this macro
95 collectinfo.collection_macros
96 .insert( make_pair(cfgline[0], make_pair(g_EmptyText,cfgline[1])) );
97 else if (cfgline.size() == 3) {// has params
98 // strip [ ] brackets from params
99 text_t::const_iterator first=cfgline[1].begin()+1;
100 text_t::const_iterator last=cfgline[1].end()-1;
101 text_t nobrackets=substr(first, last);
102 collectinfo.collection_macros
103 .insert( make_pair(cfgline[0], make_pair(nobrackets,cfgline[2])) );
104 }
105 } else if (key == "format" && cfgline.size() == 2)
106 collectinfo.format[cfgline[0]] = cfgline[1];
107 else if (key == "building" && cfgline.size() == 2)
108 collectinfo.building[cfgline[0]] = cfgline[1];
109 else if (key == "httpdomain") collectinfo.httpdomain = value;
110 else if (key == "httpprefix") collectinfo.httpprefix = value;
111 else if (key == "receptionist") collectinfo.receptionist = value;
112 else if (key == "buildtype") collectinfo.buildType = value;
113 else if (key == "searchtype") { // means buildtype is mgpp
114 collectinfo.buildType = "mgpp";
115 collectinfo.searchTypes = cfgline;
116 }
117 else if (key == "separate_cjk") {
118 if (value == "true") collectinfo.isSegmented = true;
119 else collectinfo.isSegmented = false;
120 }
121 // What have we set in our collect.cfg file : document or collection ?
122 else if (key == "authenticate") collectinfo.authenticate = value;
123
124 // What have we set for our group list
125 else if (key == "auth_group")
126 {
127 // use the joinchar helper function from
128 // text_t.h, it takes in the whole cfgline
129 // array and a separator aka a comma in our
130 // case and returns a sting separated by a
131 // comma like this:
132 //
133 // Rene,Kolla,Crystal,Stefan,Aly,Ian
134
135 joinchar(cfgline,',',collectinfo.auth_group);
136
137 //outconvertclass t;
138 //cerr << t << collectinfo.auth_group << "\n";
139 }
140
141 // In the map the key-value pair contain the same
142 // data i.e key == data, if key is 2 then data is 2
143
144 // What have we set for our public_documents ACL
145 else if (key == "public_documents")
146 {
147 text_tarray::const_iterator begin = cfgline.begin();
148 text_tarray::const_iterator end = cfgline.end();
149 while(begin != end)
150 {
151 // key = data i.e if key is 2 then data is 2
152 // collectinfo.public_documents[*begin] is the key
153 // *begin is the data value
154
155 collectinfo.public_documents[*begin] = *begin;
156 begin++;
157 }
158 }
159
160 // What have we set for our private_documents ACL
161 else if (key == "private_documents")
162 {
163 text_tarray::const_iterator begin = cfgline.begin();
164 text_tarray::const_iterator end = cfgline.end();
165 while(begin != end)
166 {
167 // key = data i.e if key is 2 then data is 2
168 // collectinfo.public_documents[*begin] is the key
169 // *begin is the data value
170
171 collectinfo.private_documents[*begin] = *begin;
172 begin++;
173 }
174 }
175 }
176
177 // configure the filters
178 filtermapclass::iterator filter_here = filters.begin();
179 filtermapclass::iterator filter_end = filters.end();
180 while (filter_here != filter_end) {
181 assert ((*filter_here).second.f != NULL);
182 if ((*filter_here).second.f != NULL)
183 (*filter_here).second.f->configure(key, cfgline);
184
185 filter_here++;
186 }
187
188 // configure the sources
189 sourcelistclass::iterator source_here = sources.begin();
190 sourcelistclass::iterator source_end = sources.end();
191 while (source_here != source_end) {
192 assert ((*source_here).s != NULL);
193 if ((*source_here).s != NULL)
194 (*source_here).s->configure(key, cfgline);
195
196 source_here++;
197 }
198}
199
200void collectserver::configure (const text_t &key, const text_t &value) {
201 text_tarray cfgline;
202 cfgline.push_back (value);
203 configure(key, cfgline);
204}
205
206void collectserver::ping (bool &wasSuccess, comerror_t &error, ostream &logout) {
207 // if we've not been properly configured, then it is a foregone
208 // conclusion that we cannot be active
209 if (this->configinfo.collection == "null")
210 {
211 wasSuccess = false;
212 }
213 // if no build date exists, then the collection was probably not built;
214 // ditto if the number of documents is zero, then something is pretty
215 // wrong
216 else if (this->collectinfo.buildDate == 0 ||
217 this->collectinfo.numDocs == 0)
218 {
219 wasSuccess = false;
220 }
221 // it is probably okay
222 else
223 wasSuccess = true;
224}
225
226
227bool collectserver::init (ostream &logout) {
228 // init the filters
229 filtermapclass::iterator filter_here = filters.begin();
230 filtermapclass::iterator filter_end = filters.end();
231 while (filter_here != filter_end) {
232 assert ((*filter_here).second.f != NULL);
233 if (((*filter_here).second.f != NULL) &&
234 !(*filter_here).second.f->init(logout)) return false;
235
236 filter_here++;
237 }
238
239 // init the sources
240 sourcelistclass::iterator source_here = sources.begin();
241 sourcelistclass::iterator source_end = sources.end();
242 while (source_here != source_end) {
243 assert ((*source_here).s != NULL);
244 if (((*source_here).s != NULL) &&
245 !(*source_here).s->init(logout)) return false;
246
247 source_here++;
248 }
249
250 return true;
251}
252
253
254void collectserver::get_collectinfo (ColInfoResponse_t &reponse,
255 comerror_t &err, ostream &/*logout*/) {
256 reponse = collectinfo;
257 err = noError;
258}
259
260void collectserver::get_filterinfo (InfoFiltersResponse_t &response,
261 comerror_t &err, ostream &/*logout*/) {
262 response.clear ();
263
264 // get a list of filter names
265 filtermapclass::iterator filter_here = filters.begin();
266 filtermapclass::iterator filter_end = filters.end();
267 while (filter_here != filter_end) {
268 response.filterNames.insert ((*filter_here).first);
269 filter_here++;
270 }
271
272 err = noError;
273}
274
275void collectserver::get_filteroptions (const InfoFilterOptionsRequest_t &request,
276 InfoFilterOptionsResponse_t &response,
277 comerror_t &err, ostream &logout) {
278 outconvertclass text_t2ascii;
279
280 filterclass *thisfilter = filters.getfilter(request.filterName);
281 if (thisfilter != NULL) {
282 thisfilter->get_filteroptions (response, err, logout);
283 } else {
284 response.clear ();
285 err = protocolError;
286 logout << text_t2ascii << "Protocol Error: filter options requested for non-existent\n"
287 << "filter \"" << request.filterName << "\".\n\n";
288 }
289}
290
291void collectserver::filter (FilterRequest_t &request,
292 FilterResponse_t &response,
293 comerror_t &err, ostream &logout) {
294 outconvertclass text_t2ascii;
295
296 // translate any ".fc", ".pr" etc. stuff in the docSet
297 text_t translatedOID;
298 text_tarray translatedOIDs;
299 text_tarray::iterator doc_here = request.docSet.begin();
300 text_tarray::iterator doc_end = request.docSet.end();
301 while (doc_here != doc_end) {
302 if (needs_translating (*doc_here)) {
303 sourcelistclass::iterator source_here = sources.begin();
304 sourcelistclass::iterator source_end = sources.end();
305 while (source_here != source_end) {
306 assert ((*source_here).s != NULL);
307 if (((*source_here).s != NULL) &&
308 ((*source_here).s->translate_OID (*doc_here, translatedOID, err, logout))) {
309 if (err != noError) return;
310 break;
311 }
312 source_here++;
313 }
314 translatedOIDs.push_back (translatedOID);
315 } else {
316 translatedOIDs.push_back (*doc_here);
317 }
318 doc_here ++;
319 }
320 request.docSet = translatedOIDs;
321
322 response.clear();
323
324 filterclass *thisfilter = filters.getfilter(request.filterName);
325 if (thisfilter != NULL) {
326 // filter the data
327 thisfilter->filter (request, response, err, logout);
328 if (err != noError) return;
329 // fill in the metadata for each of the OIDs (if it is requested)
330 if (request.filterResultOptions & FRmetadata) {
331 bool processed = false;
332 ResultDocInfo_tarray::iterator resultdoc_here = response.docInfo.begin();
333 ResultDocInfo_tarray::iterator resultdoc_end = response.docInfo.end();
334 while (resultdoc_here != resultdoc_end) {
335 // try each of the sources in turn
336 sourcelistclass::iterator source_here = sources.begin();
337 sourcelistclass::iterator source_end = sources.end();
338 while (source_here != source_end) {
339 assert ((*source_here).s != NULL);
340 if (((*source_here).s != NULL) &&
341 ((*source_here).s->get_metadata(request.requestParams, request.refParams,
342 request.getParents, request.fields,
343 (*resultdoc_here).OID, (*resultdoc_here).metadata,
344 err, logout))) {
345 if (err != noError) return;
346 processed = true;
347 break;
348 }
349 source_here++;
350 }
351 if (!processed) {
352 err = protocolError;
353 return;
354 }
355 resultdoc_here++;
356 }
357 }
358
359 } else {
360 response.clear ();
361 err = protocolError;
362 logout << text_t2ascii << "Protocol Error: filter options requested for non-existent\n"
363 << "filter \"" << request.filterName << "\".\n\n";
364 }
365
366 err = noError;
367}
368
369void collectserver::get_document (const DocumentRequest_t &request,
370 DocumentResponse_t &response,
371 comerror_t &err, ostream &logout) {
372
373 sourcelistclass::iterator source_here = sources.begin();
374 sourcelistclass::iterator source_end = sources.end();
375 while (source_here != source_end) {
376 assert ((*source_here).s != NULL);
377 if (((*source_here).s != NULL) &&
378 ((*source_here).s->get_document (request.OID, response.doc, err, logout))) {
379 if (err != noError) return;
380 break;
381 }
382 source_here++;
383 }
384}
385
386void collectserver::is_searchable (bool &issearchable, comerror_t &err,
387 ostream &logout) {
388
389 sourcelistclass::iterator source_here = sources.begin();
390 sourcelistclass::iterator source_end = sources.end();
391 while (source_here != source_end) {
392 assert ((*source_here).s != NULL);
393 if (((*source_here).s != NULL) &&
394 ((*source_here).s->is_searchable (issearchable, err, logout))) {
395 if (err != noError) return;
396 break;
397 }
398 source_here++;
399 }
400}
401
402
403bool operator==(const collectserverptr &x, const collectserverptr &y) {
404 return (x.c == y.c);
405}
406
407bool operator<(const collectserverptr &x, const collectserverptr &y) {
408 return (x.c < y.c);
409}
410
411
412// thecollectserver remains the property of the calling code but
413// should not be deleted until it is removed from this list.
414void collectservermapclass::addcollectserver (collectserver *thecollectserver) {
415 // can't add a null collection server
416 assert (thecollectserver != NULL);
417 if (thecollectserver == NULL) return;
418
419 // can't add an collection server with no collection name
420 assert (!(thecollectserver->get_collection_name()).empty());
421 if ((thecollectserver->get_collection_name()).empty()) return;
422
423 collectserverptr cptr;
424 cptr.c = thecollectserver;
425 collectserverptrs[thecollectserver->get_collection_name()] = cptr;
426}
427
428// getcollectserver will return NULL if the collectserver could not be found
429collectserver *collectservermapclass::getcollectserver (const text_t &collection) {
430 // can't find a collection with no name
431 if (collection.empty()) return NULL;
432
433 iterator here = collectserverptrs.find (collection);
434 if (here == collectserverptrs.end()) return NULL;
435
436 return (*here).second.c;
437}
Note: See TracBrowser for help on using the repository browser.