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