1 | /**********************************************************************
|
---|
2 | *
|
---|
3 | * mggdbmsource.cpp --
|
---|
4 | * Copyright (C) 1999 The New Zealand Digital Library Project
|
---|
5 | *
|
---|
6 | * A component of the Greenstone digital library software
|
---|
7 | * from the New Zealand Digital Library Project at the
|
---|
8 | * University of Waikato, New Zealand.
|
---|
9 | *
|
---|
10 | * This program is free software; you can redistribute it and/or modify
|
---|
11 | * it under the terms of the GNU General Public License as published by
|
---|
12 | * the Free Software Foundation; either version 2 of the License, or
|
---|
13 | * (at your option) any later version.
|
---|
14 | *
|
---|
15 | * This program is distributed in the hope that it will be useful,
|
---|
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
18 | * GNU General Public License for more details.
|
---|
19 | *
|
---|
20 | * You should have received a copy of the GNU General Public License
|
---|
21 | * along with this program; if not, write to the Free Software
|
---|
22 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
23 | *
|
---|
24 | * $Id: mggdbmsource.cpp 650 1999-10-10 08:20:37Z sjboddie $
|
---|
25 | *
|
---|
26 | *********************************************************************/
|
---|
27 |
|
---|
28 | /*
|
---|
29 | $Log$
|
---|
30 | Revision 1.16 1999/10/10 08:20:36 sjboddie
|
---|
31 | - metadata now returns map rather than array
|
---|
32 | - redesigned browsing support (although it's not finished so
|
---|
33 | won't currently work ;-)
|
---|
34 |
|
---|
35 | Revision 1.15 1999/09/07 04:57:22 sjboddie
|
---|
36 | added gpl notice
|
---|
37 |
|
---|
38 | Revision 1.14 1999/08/31 22:40:44 rjmcnab
|
---|
39 | A couple of gdbm changes.
|
---|
40 |
|
---|
41 | Revision 1.13 1999/08/13 04:20:27 sjboddie
|
---|
42 | added ability to get return all available metadata when 'fields' is empty
|
---|
43 |
|
---|
44 | Revision 1.12 1999/07/07 06:17:47 rjmcnab
|
---|
45 | broke search_index into index+subcollection+language
|
---|
46 | within mgsearch
|
---|
47 |
|
---|
48 | Revision 1.11 1999/07/01 03:49:54 rjmcnab
|
---|
49 | fixed a small warning.
|
---|
50 |
|
---|
51 | Revision 1.10 1999/06/29 23:06:07 sjboddie
|
---|
52 | Fixed up default index for get_document
|
---|
53 |
|
---|
54 | Revision 1.9 1999/06/16 02:00:34 sjboddie
|
---|
55 | Few changes to get getParent filter option to return metadata of
|
---|
56 | parents as well as current OID
|
---|
57 |
|
---|
58 | Revision 1.8 1999/05/10 03:43:48 sjboddie
|
---|
59 | lots of changes to lots of files - getting document action going
|
---|
60 |
|
---|
61 | Revision 1.7 1999/04/30 02:00:47 sjboddie
|
---|
62 | lots of stuff to do with getting documentaction working
|
---|
63 |
|
---|
64 | Revision 1.6 1999/04/21 22:40:44 sjboddie
|
---|
65 | made another change to the one I just committed. if requested metadata doesn't
|
---|
66 | exist it now puts an empty string in the response array so we don't always
|
---|
67 | have to test that a value exists before using it.
|
---|
68 |
|
---|
69 | Revision 1.5 1999/04/21 05:23:46 sjboddie
|
---|
70 |
|
---|
71 | changed the way metadata is returned
|
---|
72 |
|
---|
73 | Revision 1.4 1999/04/19 23:56:07 rjmcnab
|
---|
74 | Finished the gdbm metadata stuff
|
---|
75 |
|
---|
76 | Revision 1.3 1999/04/12 10:30:33 rjmcnab
|
---|
77 | Made a little more progress.
|
---|
78 |
|
---|
79 | Revision 1.2 1999/04/12 05:21:51 rjmcnab
|
---|
80 | Started on a mg and gdbm source.
|
---|
81 |
|
---|
82 | Revision 1.1 1999/04/12 03:40:40 rjmcnab
|
---|
83 | Initial revision.
|
---|
84 |
|
---|
85 | */
|
---|
86 |
|
---|
87 |
|
---|
88 | #include "mggdbmsource.h"
|
---|
89 | #include "fileutil.h"
|
---|
90 | #include "OIDtools.h"
|
---|
91 |
|
---|
92 |
|
---|
93 | mggdbmsourceclass::mggdbmsourceclass () {
|
---|
94 | gdbmptr = NULL;
|
---|
95 | mgsearchptr = NULL;
|
---|
96 | }
|
---|
97 |
|
---|
98 | mggdbmsourceclass::~mggdbmsourceclass () {
|
---|
99 | }
|
---|
100 |
|
---|
101 | void mggdbmsourceclass::configure (const text_t &key, const text_tarray &cfgline) {
|
---|
102 | if (cfgline.size() >= 1) {
|
---|
103 | const text_t &value = cfgline[0];
|
---|
104 |
|
---|
105 | if (key == "collection") collection = value;
|
---|
106 | else if (key == "collectdir") collectdir = value;
|
---|
107 | else if (key == "gsdlhome") gsdlhome = value;
|
---|
108 | }
|
---|
109 |
|
---|
110 | if (key == "indexmap") {
|
---|
111 | indexmap.importmap (cfgline);
|
---|
112 |
|
---|
113 | } else if (key == "defaultindex") {
|
---|
114 | indexmap.from2to (cfgline[0], defaultindex);
|
---|
115 |
|
---|
116 | } else if (key == "subcollectionmap") {
|
---|
117 | subcollectionmap.importmap (cfgline);
|
---|
118 |
|
---|
119 | } else if (key == "defaultsubcollection") {
|
---|
120 | subcollectionmap.from2to (cfgline[0], defaultsubcollection);
|
---|
121 |
|
---|
122 | } else if (key == "languagemap") {
|
---|
123 | languagemap.importmap (cfgline);
|
---|
124 |
|
---|
125 | } else if (key == "defaultlanguage")
|
---|
126 | languagemap.from2to (cfgline[0], defaultlanguage);
|
---|
127 | }
|
---|
128 |
|
---|
129 | bool mggdbmsourceclass::init (ostream &logout) {
|
---|
130 | outconvertclass text_t2ascii;
|
---|
131 |
|
---|
132 | if (!sourceclass::init (logout)) return false;
|
---|
133 |
|
---|
134 | // get the collection directory name
|
---|
135 | if (collectdir.empty()) {
|
---|
136 | collectdir = filename_cat (gsdlhome, "collect", collection);
|
---|
137 | }
|
---|
138 |
|
---|
139 | // get the filename for the database and make sure it exists
|
---|
140 | gdbm_filename = filename_cat(collectdir,"index","text",collection);
|
---|
141 |
|
---|
142 | #ifdef _LITTLE_ENDIAN
|
---|
143 | gdbm_filename += ".ldb";
|
---|
144 | #else
|
---|
145 | gdbm_filename += ".bdb";
|
---|
146 | #endif
|
---|
147 | if (!file_exists(gdbm_filename)) {
|
---|
148 | logout << text_t2ascii
|
---|
149 | << "error: gdbm database \""
|
---|
150 | << gdbm_filename << "\" does not exist\n\n";
|
---|
151 | return false;
|
---|
152 | }
|
---|
153 |
|
---|
154 | return true;
|
---|
155 | }
|
---|
156 |
|
---|
157 | bool mggdbmsourceclass::translate_OID (const text_t &OIDin, text_t &OIDout,
|
---|
158 | comerror_t &err, ostream &logout) {
|
---|
159 |
|
---|
160 | outconvertclass text_t2ascii;
|
---|
161 |
|
---|
162 | err = noError;
|
---|
163 | if (gdbmptr == NULL) {
|
---|
164 | // most likely a configuration problem
|
---|
165 | logout << text_t2ascii
|
---|
166 | << "configuration error: mggdbmsource contains a null gdbmclass\n\n";
|
---|
167 | err = configurationError;
|
---|
168 | return true;
|
---|
169 | }
|
---|
170 |
|
---|
171 | // open the database
|
---|
172 | gdbmptr->setlogout(&logout);
|
---|
173 | if (!gdbmptr->opendatabase (gdbm_filename, GDBM_READER, 100, false)) {
|
---|
174 | // most likely a system problem (we have already checked that the
|
---|
175 | // gdbm database exists)
|
---|
176 | logout << text_t2ascii
|
---|
177 | << "system problem: open on gdbm database \""
|
---|
178 | << gdbm_filename << "\" failed\n\n";
|
---|
179 | err = systemProblem;
|
---|
180 | return true;
|
---|
181 | }
|
---|
182 |
|
---|
183 | infodbclass info;
|
---|
184 | OIDout = gdbmptr->translate_OID (OIDin, info);
|
---|
185 | return true;
|
---|
186 | }
|
---|
187 |
|
---|
188 | bool mggdbmsourceclass::get_metadata (const text_t &/*requestParams*/, const text_t &/*refParams*/,
|
---|
189 | bool getParents, const text_tset &fields,
|
---|
190 | const text_t &OID, MetadataInfo_tmap &metadata,
|
---|
191 | comerror_t &err, ostream &logout) {
|
---|
192 | outconvertclass text_t2ascii;
|
---|
193 |
|
---|
194 | metadata.erase(metadata.begin(), metadata.end());
|
---|
195 |
|
---|
196 | err = noError;
|
---|
197 | if (gdbmptr == NULL) {
|
---|
198 | // most likely a configuration problem
|
---|
199 | logout << text_t2ascii
|
---|
200 | << "configuration error: mggdbmsource contains a null gdbmclass\n\n";
|
---|
201 | err = configurationError;
|
---|
202 | return true;
|
---|
203 | }
|
---|
204 |
|
---|
205 | // open the database
|
---|
206 | gdbmptr->setlogout(&logout);
|
---|
207 | if (!gdbmptr->opendatabase (gdbm_filename, GDBM_READER, 100, false)) {
|
---|
208 | // most likely a system problem (we have already checked that the
|
---|
209 | // gdbm database exists)
|
---|
210 | logout << text_t2ascii
|
---|
211 | << "system problem: open on gdbm database \""
|
---|
212 | << gdbm_filename << "\" failed\n\n";
|
---|
213 | err = systemProblem;
|
---|
214 | return true;
|
---|
215 | }
|
---|
216 |
|
---|
217 | // get the metadata - if getParents is set we need to get
|
---|
218 | // info for all parents of OID as well as OID
|
---|
219 | vector<infodbclass> info_array;
|
---|
220 | text_tarray OIDs;
|
---|
221 | if (getParents) get_parents_array (OID, OIDs);
|
---|
222 | OIDs.push_back (OID);
|
---|
223 |
|
---|
224 | text_tarray::const_iterator this_OID = OIDs.begin();
|
---|
225 | text_tarray::const_iterator end_OID = OIDs.end();
|
---|
226 |
|
---|
227 | while (this_OID != end_OID) {
|
---|
228 | infodbclass info;
|
---|
229 | if (!gdbmptr->getinfo(*this_OID, info)) return false;
|
---|
230 |
|
---|
231 | // adjust the metadata
|
---|
232 | text_t &contains = info["contains"];
|
---|
233 | if (contains.empty()) info["haschildren"] = 0;
|
---|
234 | else info["haschildren"] = 1;
|
---|
235 | contains.clear();
|
---|
236 |
|
---|
237 | info_array.push_back(info);
|
---|
238 | this_OID ++;
|
---|
239 | }
|
---|
240 |
|
---|
241 | // if fields set is empty we want to get all available metadata
|
---|
242 | text_tset tfields = fields;
|
---|
243 | if (tfields.empty() && !info_array.empty()) {
|
---|
244 | infodbclass::iterator t_info = info_array[0].begin();
|
---|
245 | infodbclass::iterator e_info = info_array[0].end();
|
---|
246 | while (t_info != e_info) {
|
---|
247 | if ((*t_info).first != "contains")
|
---|
248 | tfields.insert ((*t_info).first);
|
---|
249 | t_info ++;
|
---|
250 | }
|
---|
251 | tfields.insert ("hasnext");
|
---|
252 | tfields.insert ("hasprevious");
|
---|
253 | }
|
---|
254 |
|
---|
255 | // collect together the metadata
|
---|
256 | bool donenextprevtest = false;
|
---|
257 | bool hasnext=false, hasprevious=false;
|
---|
258 | MetadataInfo_t this_metadata;
|
---|
259 | text_tarray *pos_metadata;
|
---|
260 | text_tset::const_iterator fields_here = tfields.begin();
|
---|
261 | text_tset::const_iterator fields_end = tfields.end();
|
---|
262 | while (fields_here != fields_end) {
|
---|
263 | this_metadata.clear();
|
---|
264 | this_metadata.isRef = false;
|
---|
265 |
|
---|
266 | vector<infodbclass>::reverse_iterator this_info = info_array.rbegin();
|
---|
267 | vector<infodbclass>::reverse_iterator end_info = info_array.rend();
|
---|
268 | MetadataInfo_t *tmetaptr = &this_metadata;
|
---|
269 | while (this_info != end_info) {
|
---|
270 |
|
---|
271 | pos_metadata = (*this_info).getmultinfo(*fields_here);
|
---|
272 |
|
---|
273 | if ((*fields_here == "hasnext" || *fields_here == "hasprevious")) {
|
---|
274 |
|
---|
275 | // collect metadata
|
---|
276 | if (!donenextprevtest) {
|
---|
277 | donenextprevtest = true;
|
---|
278 |
|
---|
279 | // cache parent contents array
|
---|
280 | text_t thisparent = get_parent (OID);
|
---|
281 | if (thisparent != parentOID) {
|
---|
282 | parentOID = thisparent;
|
---|
283 | parentcontents.erase(parentcontents.begin(), parentcontents.end());
|
---|
284 | if (gdbmptr->getinfo(parentOID, parentinfo)) {
|
---|
285 | text_t &parentinfocontains = parentinfo["contains"];
|
---|
286 | if (!parentinfocontains.empty())
|
---|
287 | splitchar (parentinfocontains.begin(), parentinfocontains.end(),
|
---|
288 | ';', parentcontents);
|
---|
289 | }
|
---|
290 | }
|
---|
291 |
|
---|
292 | // do tests
|
---|
293 | text_tarray::const_iterator parentcontents_here = parentcontents.begin();
|
---|
294 | text_tarray::const_iterator parentcontents_end = parentcontents.end();
|
---|
295 | text_t shrunk_OID = OID;
|
---|
296 | shrink_parent (shrunk_OID);
|
---|
297 | while (parentcontents_here != parentcontents_end) {
|
---|
298 | if (*parentcontents_here == shrunk_OID) {
|
---|
299 | if (parentcontents_here == parentcontents.begin()) hasprevious = false;
|
---|
300 | else hasprevious = true;
|
---|
301 |
|
---|
302 | parentcontents_here++;
|
---|
303 |
|
---|
304 | if (parentcontents_here == parentcontents.end()) hasnext = false;
|
---|
305 | else hasnext = true;
|
---|
306 |
|
---|
307 | break;
|
---|
308 | }
|
---|
309 |
|
---|
310 | parentcontents_here ++;
|
---|
311 | }
|
---|
312 | }
|
---|
313 |
|
---|
314 | // fill in metadata
|
---|
315 | if ((*fields_here == "hasnext" && hasnext) ||
|
---|
316 | (*fields_here == "hasprevious" && hasprevious))
|
---|
317 | tmetaptr->values.push_back("1");
|
---|
318 | else
|
---|
319 | tmetaptr->values.push_back("0");
|
---|
320 |
|
---|
321 | }
|
---|
322 | else if (pos_metadata != NULL && *fields_here != "contains")
|
---|
323 | tmetaptr->values = *pos_metadata;
|
---|
324 | else
|
---|
325 | tmetaptr->values.push_back("");
|
---|
326 |
|
---|
327 | this_info ++;
|
---|
328 | if (this_info != end_info) {
|
---|
329 | tmetaptr->parent = new MetadataInfo_t();
|
---|
330 | tmetaptr = tmetaptr->parent;
|
---|
331 | }
|
---|
332 | }
|
---|
333 | metadata[*fields_here] = this_metadata;
|
---|
334 |
|
---|
335 | fields_here++;
|
---|
336 | }
|
---|
337 |
|
---|
338 | return true;
|
---|
339 | }
|
---|
340 |
|
---|
341 |
|
---|
342 | bool mggdbmsourceclass::get_document (const text_t &OID, text_t &doc,
|
---|
343 | comerror_t &err, ostream &logout) {
|
---|
344 |
|
---|
345 | outconvertclass text_t2ascii;
|
---|
346 |
|
---|
347 | err = noError;
|
---|
348 | if (gdbmptr == NULL) {
|
---|
349 | // most likely a configuration problem
|
---|
350 | logout << text_t2ascii
|
---|
351 | << "configuration error: mggdbmsource contains a null gdbmclass\n\n";
|
---|
352 | err = configurationError;
|
---|
353 | return true;
|
---|
354 | }
|
---|
355 |
|
---|
356 | // open the database
|
---|
357 | gdbmptr->setlogout(&logout);
|
---|
358 | if (!gdbmptr->opendatabase (gdbm_filename, GDBM_READER, 100, false)) {
|
---|
359 | // most likely a system problem (we have already checked that the
|
---|
360 | // gdbm database exists)
|
---|
361 | logout << text_t2ascii
|
---|
362 | << "system problem: open on gdbm database \""
|
---|
363 | << gdbm_filename << "\" failed\n\n";
|
---|
364 | err = systemProblem;
|
---|
365 | return true;
|
---|
366 | }
|
---|
367 |
|
---|
368 | text_t tOID = OID;
|
---|
369 | if (needs_translating (OID))
|
---|
370 | translate_OID (OID, tOID, err, logout);
|
---|
371 | infodbclass info;
|
---|
372 | if (!gdbmptr->getinfo(tOID, info)) return false;
|
---|
373 |
|
---|
374 | if (info["hastxt"].getint() == 1) {
|
---|
375 | int docnum = info["docnum"].getint();
|
---|
376 |
|
---|
377 | // set the collection directory
|
---|
378 | mgsearchptr->setcollectdir (collectdir);
|
---|
379 |
|
---|
380 | // get the text
|
---|
381 | mgsearchptr->docTargetDocument(defaultindex, defaultsubcollection,
|
---|
382 | defaultlanguage, collection, docnum, doc);
|
---|
383 | }
|
---|
384 | return true;
|
---|
385 | }
|
---|