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

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

lots of stuff to do with getting documentaction working

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