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

Last change on this file since 308 was 271, checked in by sjboddie, 25 years ago

Few changes to get getParent filter option to return metadata of
parents as well as current OID

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