source: main/trunk/greenstone2/common-src/src/lib/jdbmnaiveclass.cpp@ 21438

Last change on this file since 21438 was 21438, checked in by ak19, 14 years ago

Dr Bainbridge's fixes for compilation errors after the jdbm related changes

File size: 6.4 KB
Line 
1/**********************************************************************
2 *
3 * jdbmnaiveclass.cpp --
4 * Copyright (C) 1999-2010 The New Zealand Digital Library Project
5 *
6 * A component of the Greenstone digital library software
7 * from the New Zealand Digital Library Project at the
8 * University of Waikato, New Zealand.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *
24 *********************************************************************/
25
26#include "jdbmnaiveclass.h"
27#include "gsdltools.h"
28#include "gsdlunicode.h"
29#include "fileutil.h"
30#include "stdlib.h"
31#include <cstring>
32
33#ifdef __WIN32__
34//windows functions
35#define popen _popen
36#define pclose _pclose
37#endif
38
39jdbmnaiveclass::jdbmnaiveclass(const text_t& gsdlhome)
40 : dbclass()
41{
42 gsdlhome_ = gsdlhome;
43
44 text_t jdbm_jar = filename_cat(gsdlhome_,"lib","java","jdbm.jar");
45
46 text_t jdbm_wrapper_jar = filename_cat(gsdlhome_,"bin","java","JDBMWrapper.jar");
47
48 classpath_ = pathname_cat(jdbm_wrapper_jar,jdbm_jar);
49 classpath_cstr_ = to_utf8(classpath_).getcstr();
50}
51
52jdbmnaiveclass::~jdbmnaiveclass()
53{
54 delete [] classpath_cstr_;
55}
56
57
58// returns true if opened
59bool jdbmnaiveclass::opendatabase (const text_t &filename, int mode, int num_retrys,
60#ifdef __WIN32__
61 bool need_filelock
62#else
63 bool
64#endif
65 )
66{
67 if (openfile_ == filename) {
68 return true;
69 }
70
71 if (logout == NULL) {
72 logout = &cerr;
73 }
74
75 // Map the DB mode value into equivalent JDBM 'must_exist' value
76 bool must_exist = true; // default
77
78 if (mode == DB_WRITER_CREATE) {
79 must_exist = false;
80 }
81
82 if (must_exist) {
83 // test that jdbm file exists
84
85 if (!file_exists(filename)) {
86 outconvertclass text_t2ascii;
87 (*logout) << text_t2ascii
88 << "Error: JDBM filename " << filename << "does not exist\n";
89 return false;
90 }
91 }
92
93 openfile_ = filename;
94
95 // In naive implementation, no connection to database to open, so
96 // nothing else to do at this stage
97
98 return true;
99}
100
101
102void jdbmnaiveclass::closedatabase ()
103{
104 // nothing to do in naive implementation
105}
106
107// returns file extension string
108text_t jdbmnaiveclass::getfileextension ()
109{
110 return ".jdb";
111}
112
113
114void jdbmnaiveclass::deletekey (const text_t &key)
115{
116 if (openfile_.empty()) return;
117
118 if (logout == NULL) {
119 logout = &cerr;
120 }
121
122 // get a utf-8 encoded c string of the unicode key
123 char* key_cstr = (to_utf8(key)).getcstr();
124 if (key_cstr == NULL) {
125 (*logout) << "jdbmnaiveclass: out of memory" << endl;
126 return;
127 }
128
129
130 // delete the key<
131 char* openfile_cstr = (to_utf8(openfile_)).getcstr();
132
133 char cmd[512];
134 sprintf(cmd,"java -cp %s JdbDel %s %s",classpath_cstr_,
135 openfile_cstr,key_cstr);
136
137 delete [] openfile_cstr;
138
139 int status = system(cmd);
140 if (status != 0 ) {
141 (*logout) << "jdbmnaiveclass: failed to run cmd: " << cmd << endl;
142 }
143
144 // free up the key memory
145 delete [] key_cstr;
146}
147
148
149
150// returns true on success
151bool jdbmnaiveclass::getkeydata (const text_t& key, text_t &data)
152{
153 if (openfile_.empty()) return false;
154
155 if (logout == NULL) {
156 logout = &cerr;
157 }
158
159 // get a utf-8 encoded c string of the unicode key
160 char* key_cstr = (to_utf8(key)).getcstr();
161 if (key_cstr == NULL) {
162 (*logout) << "jdbmnaiveclass: out of memory" << endl;
163
164 return false;
165 }
166
167 // fetch the result
168 char* openfile_cstr = (to_utf8(openfile_)).getcstr();
169
170 char cmd[512];
171 sprintf(cmd,"java -cp %s JdbGet %s %s",classpath_cstr_,
172 openfile_cstr,key_cstr);
173
174 delete [] openfile_cstr;
175
176 FILE* PIN = popen(cmd,"r");
177
178 if (PIN == NULL) {
179 (*logout) << "jdbmnaiveclass: failed to run cmd: " << cmd << endl;
180 }
181 else {
182 while (!feof(PIN)) {
183 char buf[256];
184 fgets(buf,256,PIN);
185
186 cerr << "**## buf = " << buf << endl;
187
188 cerr << "**## buf: ";
189
190 int blen = strlen(buf);
191 for (int i=0; i< blen; i++) {
192 cerr << "(" << buf[i] << "," << (int)buf[i] << ") ";
193 }
194 cerr << endl;
195
196
197 data.append(buf);
198 }
199
200 }
201
202 delete [] key_cstr;
203
204 cerr << "**** data before to_unicode = " << data << endl;
205
206 data = to_uni(data); // convert to unicode
207
208 return true;
209}
210
211
212// returns array of keys
213text_tarray jdbmnaiveclass::getkeys ()
214{
215 text_tarray keys;
216
217 if (openfile_.empty()) return keys;
218
219 if (logout == NULL) {
220 logout = &cerr;
221 }
222
223 char* openfile_cstr = (to_utf8(openfile_)).getcstr();
224
225 char cmd[512];
226 sprintf(cmd,"java -cp %s JdbKeys %s",classpath_cstr_,
227 openfile_cstr);
228
229 delete [] openfile_cstr;
230
231 FILE* PIN = popen(cmd,"r");
232
233 if (PIN == NULL) {
234 (*logout) << "jdbmnaiveclass: failed to run cmd: " << cmd << endl;
235 }
236 else {
237 text_t key = "";
238
239 while (!feof(PIN)) {
240
241 char buf[256];
242 fgets(buf,256,PIN);
243 key.append(buf);
244
245 keys.push_back(key);
246
247 }
248 }
249
250 pclose(PIN);
251
252 return keys;
253}
254
255
256// returns true on success
257bool jdbmnaiveclass::setkeydata (const text_t &key, const text_t &data)
258{
259 if (openfile_.empty()) return false;
260
261 if (logout == NULL) {
262 logout = &cerr;
263 }
264
265 // get a utf-8 encoded c string of the unicode key
266 char* key_cstr = (to_utf8(key)).getcstr();
267
268 if (key_cstr == NULL) {
269 (*logout) << "jdbmnaiveclass: out of memory" << endl;
270
271 return false;
272 }
273
274 char* data_cstr = (to_utf8(data)).getcstr();
275 if (data_cstr == NULL) {
276 (*logout) << "jdbmnaiveclass: out of memory" << endl;
277
278 delete [] key_cstr;
279 return false;
280 }
281 int data_len = strlen(data_cstr);
282
283 char* openfile_cstr = (to_utf8(openfile_)).getcstr();
284
285 char cmd[512];
286 sprintf(cmd,"java -cp %s JdbSet %s %s %s",
287 classpath_cstr_,openfile_cstr,key_cstr,data_cstr);
288
289 delete [] openfile_cstr;
290
291 int status = system(cmd);
292 if (status != 0) {
293 (*logout) << "jdbmnaiveclass: failed to run cmd:"
294 << cmd << endl;
295 }
296
297 delete [] key_cstr;
298 delete [] data_cstr;
299
300 return (status == 0);
301}
302
Note: See TracBrowser for help on using the repository browser.