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