source: trunk/gsdl/src/colservr/mggdbmsource.cpp@ 367

Last change on this file since 367 was 350, checked in by rjmcnab, 25 years ago

broke search_index into index+subcollection+language
within mgsearch

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 9.3 KB
Line 
1/**********************************************************************
2 *
3 * mggdbmsource.cpp --
4 * Copyright (C) 1999 The New Zealand Digital Library Project
5 *
6 * PUT COPYRIGHT NOTICE HERE
7 *
8 * $Id: mggdbmsource.cpp 350 1999-07-07 06:17:48Z rjmcnab $
9 *
10 *********************************************************************/
11
12/*
13 $Log$
14 Revision 1.12 1999/07/07 06:17:47 rjmcnab
15 broke search_index into index+subcollection+language
16 within mgsearch
17
18 Revision 1.11 1999/07/01 03:49:54 rjmcnab
19 fixed a small warning.
20
21 Revision 1.10 1999/06/29 23:06:07 sjboddie
22 Fixed up default index for get_document
23
24 Revision 1.9 1999/06/16 02:00:34 sjboddie
25 Few changes to get getParent filter option to return metadata of
26 parents as well as current OID
27
28 Revision 1.8 1999/05/10 03:43:48 sjboddie
29 lots of changes to lots of files - getting document action going
30
31 Revision 1.7 1999/04/30 02:00:47 sjboddie
32 lots of stuff to do with getting documentaction working
33
34 Revision 1.6 1999/04/21 22:40:44 sjboddie
35 made another change to the one I just committed. if requested metadata doesn't
36 exist it now puts an empty string in the response array so we don't always
37 have to test that a value exists before using it.
38
39 Revision 1.5 1999/04/21 05:23:46 sjboddie
40
41 changed the way metadata is returned
42
43 Revision 1.4 1999/04/19 23:56:07 rjmcnab
44 Finished the gdbm metadata stuff
45
46 Revision 1.3 1999/04/12 10:30:33 rjmcnab
47 Made a little more progress.
48
49 Revision 1.2 1999/04/12 05:21:51 rjmcnab
50 Started on a mg and gdbm source.
51
52 Revision 1.1 1999/04/12 03:40:40 rjmcnab
53 Initial revision.
54
55 */
56
57
58#include "mggdbmsource.h"
59#include "fileutil.h"
60#include "OIDtools.h"
61
62
63mggdbmsourceclass::mggdbmsourceclass () {
64 gdbmptr = NULL;
65 mgsearchptr = NULL;
66}
67
68mggdbmsourceclass::~mggdbmsourceclass () {
69}
70
71void mggdbmsourceclass::configure (const text_t &key, const text_tarray &cfgline) {
72 if (cfgline.size() >= 1) {
73 const text_t &value = cfgline[0];
74
75 if (key == "collection") collection = value;
76 else if (key == "collectdir") collectdir = value;
77 else if (key == "gsdlhome") gsdlhome = value;
78 }
79
80 if (key == "indexmap") {
81 indexmap.importmap (cfgline);
82
83 } else if (key == "defaultindex") {
84 indexmap.from2to (cfgline[0], defaultindex);
85
86 } else if (key == "subcollectionmap") {
87 subcollectionmap.importmap (cfgline);
88
89 } else if (key == "defaultsubcollection") {
90 subcollectionmap.from2to (cfgline[0], defaultsubcollection);
91
92 } else if (key == "languagemap") {
93 languagemap.importmap (cfgline);
94
95 } else if (key == "defaultlanguage")
96 languagemap.from2to (cfgline[0], defaultlanguage);
97}
98
99bool mggdbmsourceclass::init (ostream &logout) {
100 outconvertclass text_t2ascii;
101
102 if (!sourceclass::init (logout)) return false;
103
104 // get the collection directory name
105 if (collectdir.empty()) {
106 collectdir = filename_cat (gsdlhome, "collect", collection);
107 }
108
109 // get the filename for the database and make sure it exists
110 gdbm_filename = filename_cat(collectdir,"index","text",collection);
111#ifdef _LITTLE_ENDIAN
112 gdbm_filename += ".ldb";
113#else
114 gdbm_filename += ".bdb";
115#endif
116 if (!file_exists(gdbm_filename)) {
117 logout << text_t2ascii
118 << "error: gdbm database \""
119 << gdbm_filename << "\" does not exist\n\n";
120 return false;
121 }
122
123 return true;
124}
125
126bool mggdbmsourceclass::translate_OID (const text_t &OIDin, text_t &OIDout,
127 comerror_t &err, ostream &logout) {
128
129 outconvertclass text_t2ascii;
130
131 err = noError;
132 if (gdbmptr == NULL) {
133 // most likely a configuration problem
134 logout << text_t2ascii
135 << "configuration error: mggdbmsource contains a null gdbmclass\n\n";
136 err = configurationError;
137 return true;
138 }
139
140 // open the database
141 gdbmptr->setlogout(&logout);
142 if (!gdbmptr->opendatabase (gdbm_filename)) {
143 // most likely a system problem (we have already checked that the
144 // gdbm database exists)
145 logout << text_t2ascii
146 << "system problem: open on gdbm database \""
147 << gdbm_filename << "\" failed\n\n";
148 err = systemProblem;
149 return true;
150 }
151
152 infodbclass info;
153 OIDout = gdbmptr->translate_OID (OIDin, info);
154 return true;
155}
156
157bool mggdbmsourceclass::get_metadata (const text_t &/*requestParams*/, const text_t &/*refParams*/,
158 bool getParents, const text_tarray &fields, const text_t &OID,
159 MetadataInfo_tarray &metadata, comerror_t &err, ostream &logout) {
160 outconvertclass text_t2ascii;
161
162 metadata.erase(metadata.begin(), metadata.end());
163
164 err = noError;
165 if (gdbmptr == NULL) {
166 // most likely a configuration problem
167 logout << text_t2ascii
168 << "configuration error: mggdbmsource contains a null gdbmclass\n\n";
169 err = configurationError;
170 return true;
171 }
172
173 // open the database
174 gdbmptr->setlogout(&logout);
175 if (!gdbmptr->opendatabase (gdbm_filename)) {
176 // most likely a system problem (we have already checked that the
177 // gdbm database exists)
178 logout << text_t2ascii
179 << "system problem: open on gdbm database \""
180 << gdbm_filename << "\" failed\n\n";
181 err = systemProblem;
182 return true;
183 }
184
185 // get the metadata - if getParents is set we need to get
186 // info for all parents of OID as well as OID
187 vector<infodbclass> info_array;
188 text_tarray OIDs;
189 if (getParents) get_parents_array (OID, OIDs);
190 OIDs.push_back (OID);
191
192 text_tarray::const_iterator this_OID = OIDs.begin();
193 text_tarray::const_iterator end_OID = OIDs.end();
194
195 while (this_OID != end_OID) {
196 infodbclass info;
197 if (!gdbmptr->getinfo(*this_OID, info)) return false;
198
199 // adjust the metadata
200 text_t &contains = info["contains"];
201 if (contains.empty()) info["haschildren"] = 0;
202 else info["haschildren"] = 1;
203 contains.clear();
204
205 info_array.push_back(info);
206 this_OID ++;
207 }
208
209 // collect together the metadata
210 bool donenextprevtest = false;
211 bool hasnext=false, hasprevious=false;
212 MetadataInfo_t this_metadata;
213 text_t *pos_metadata;
214 text_tarray::const_iterator fields_here = fields.begin();
215 text_tarray::const_iterator fields_end = fields.end();
216 while (fields_here != fields_end) {
217 this_metadata.clear();
218 this_metadata.isRef = false;
219
220 vector<infodbclass>::iterator this_info = info_array.begin();
221 vector<infodbclass>::iterator end_info = info_array.end();
222
223 while (this_info != end_info) {
224
225 pos_metadata = (*this_info).getinfo(*fields_here);
226
227 if ((*fields_here == "hasnext" || *fields_here == "hasprevious")) {
228
229 // collect metadata
230 if (!donenextprevtest) {
231 donenextprevtest = true;
232
233 // cache parent contents array
234 text_t thisparent = get_parent (OID);
235 if (thisparent != parentOID) {
236 parentOID = thisparent;
237 parentcontents.erase(parentcontents.begin(), parentcontents.end());
238 if (gdbmptr->getinfo(parentOID, parentinfo)) {
239 text_t &parentinfocontains = parentinfo["contains"];
240 if (!parentinfocontains.empty())
241 splitchar (parentinfocontains.begin(), parentinfocontains.end(), ';', parentcontents);
242 }
243 }
244
245 // do tests
246 text_tarray::const_iterator parentcontents_here = parentcontents.begin();
247 text_tarray::const_iterator parentcontents_end = parentcontents.end();
248 text_t shrunk_OID = OID;
249 shrink_parent (shrunk_OID);
250 while (parentcontents_here != parentcontents_end) {
251 if (*parentcontents_here == shrunk_OID) {
252 if (parentcontents_here == parentcontents.begin()) hasprevious = false;
253 else hasprevious = true;
254
255 parentcontents_here++;
256
257 if (parentcontents_here == parentcontents.end()) hasnext = false;
258 else hasnext = true;
259
260 break;
261 }
262
263 parentcontents_here ++;
264 }
265 }
266
267 // fill in metadata
268 if ((*fields_here == "hasnext" && hasnext) ||
269 (*fields_here == "hasprevious" && hasprevious))
270 this_metadata.values.push_back("1");
271 else
272 this_metadata.values.push_back("0");
273
274 }
275 else if (pos_metadata != NULL && *fields_here != "contains")
276 this_metadata.values.push_back(*pos_metadata);
277 else
278 this_metadata.values.push_back("");
279
280
281 this_info++;
282 }
283 metadata.push_back(this_metadata);
284
285 fields_here++;
286 }
287
288 return true;
289}
290
291
292bool mggdbmsourceclass::get_document (const text_t &OID, text_t &doc,
293 comerror_t &err, ostream &logout) {
294
295 outconvertclass text_t2ascii;
296
297 err = noError;
298 if (gdbmptr == NULL) {
299 // most likely a configuration problem
300 logout << text_t2ascii
301 << "configuration error: mggdbmsource contains a null gdbmclass\n\n";
302 err = configurationError;
303 return true;
304 }
305
306 // open the database
307 gdbmptr->setlogout(&logout);
308 if (!gdbmptr->opendatabase (gdbm_filename)) {
309 // most likely a system problem (we have already checked that the
310 // gdbm database exists)
311 logout << text_t2ascii
312 << "system problem: open on gdbm database \""
313 << gdbm_filename << "\" failed\n\n";
314 err = systemProblem;
315 return true;
316 }
317
318 text_t tOID = OID;
319 if (needs_translating (OID))
320 translate_OID (OID, tOID, err, logout);
321 infodbclass info;
322 if (!gdbmptr->getinfo(tOID, info)) return false;
323
324 if (info["hastxt"].getint() == 1) {
325 int docnum = info["docnum"].getint();
326
327 // set the collection directory
328 mgsearchptr->setcollectdir (collectdir);
329
330 // get the text
331 mgsearchptr->docTargetDocument(defaultindex, defaultsubcollection,
332 defaultlanguage, collection, docnum, doc);
333 }
334 return true;
335}
Note: See TracBrowser for help on using the repository browser.