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 |
|
---|
39 | jdbmnaiveclass::jdbmnaiveclass(const text_t& gsdlhome)
|
---|
40 | : dbclass(gsdlhome)
|
---|
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 |
|
---|
52 | jdbmnaiveclass::~jdbmnaiveclass()
|
---|
53 | {
|
---|
54 | delete [] classpath_cstr_;
|
---|
55 | }
|
---|
56 |
|
---|
57 |
|
---|
58 | // returns true if opened
|
---|
59 | bool 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 |
|
---|
102 | void jdbmnaiveclass::closedatabase ()
|
---|
103 | {
|
---|
104 | // nothing to do in naive implementation
|
---|
105 | }
|
---|
106 |
|
---|
107 | // returns file extension string
|
---|
108 | text_t jdbmnaiveclass::getfileextension ()
|
---|
109 | {
|
---|
110 | return ".jdb";
|
---|
111 | }
|
---|
112 |
|
---|
113 |
|
---|
114 | void 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
|
---|
151 | bool 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 | /*
|
---|
187 | * debuging code
|
---|
188 |
|
---|
189 | cerr << "****## buf = " << buf << endl;
|
---|
190 |
|
---|
191 | cerr << "****## buf: ";
|
---|
192 |
|
---|
193 | int blen = strlen(buf);
|
---|
194 | for (int i=0; i< blen; i++) {
|
---|
195 | cerr << "(" << buf[i] << "," << (int)buf[i] << ") ";
|
---|
196 | }
|
---|
197 |
|
---|
198 | cerr << endl;
|
---|
199 | */ // *****
|
---|
200 |
|
---|
201 |
|
---|
202 | data.append(buf);
|
---|
203 | }
|
---|
204 |
|
---|
205 | }
|
---|
206 |
|
---|
207 | delete [] key_cstr;
|
---|
208 |
|
---|
209 | // cerr << "**** data before to_unicode = " << data << endl;
|
---|
210 |
|
---|
211 | data = to_uni(data); // convert to unicode
|
---|
212 |
|
---|
213 | pclose(PIN);
|
---|
214 |
|
---|
215 | return true;
|
---|
216 | }
|
---|
217 |
|
---|
218 |
|
---|
219 | // returns array of keys
|
---|
220 | text_tarray jdbmnaiveclass::getkeys ()
|
---|
221 | {
|
---|
222 | text_tarray keys;
|
---|
223 |
|
---|
224 | if (openfile_.empty()) return keys;
|
---|
225 |
|
---|
226 | if (logout == NULL) {
|
---|
227 | logout = &cerr;
|
---|
228 | }
|
---|
229 |
|
---|
230 | char* openfile_cstr = (to_utf8(openfile_)).getcstr();
|
---|
231 |
|
---|
232 | char cmd[512];
|
---|
233 | sprintf(cmd,"java -cp \"%s\" JdbKeys \"%s\"",classpath_cstr_,
|
---|
234 | openfile_cstr);
|
---|
235 |
|
---|
236 | delete [] openfile_cstr;
|
---|
237 |
|
---|
238 | FILE* PIN = popen(cmd,"r");
|
---|
239 |
|
---|
240 | if (PIN == NULL) {
|
---|
241 | (*logout) << "jdbmnaiveclass: failed to run cmd: " << cmd << endl;
|
---|
242 | }
|
---|
243 | else {
|
---|
244 | text_t key = "";
|
---|
245 |
|
---|
246 | while (!feof(PIN)) {
|
---|
247 |
|
---|
248 | char buf[256];
|
---|
249 | fgets(buf,256,PIN);
|
---|
250 | key.append(buf);
|
---|
251 |
|
---|
252 | keys.push_back(key);
|
---|
253 |
|
---|
254 | }
|
---|
255 | }
|
---|
256 |
|
---|
257 | pclose(PIN);
|
---|
258 |
|
---|
259 | return keys;
|
---|
260 | }
|
---|
261 |
|
---|
262 |
|
---|
263 | // returns true on success
|
---|
264 | bool jdbmnaiveclass::setkeydata (const text_t &key, const text_t &data)
|
---|
265 | {
|
---|
266 | if (openfile_.empty()) return false;
|
---|
267 |
|
---|
268 | if (logout == NULL) {
|
---|
269 | logout = &cerr;
|
---|
270 | }
|
---|
271 |
|
---|
272 | // get a utf-8 encoded c string of the unicode key
|
---|
273 | char* key_cstr = (to_utf8(key)).getcstr();
|
---|
274 |
|
---|
275 | if (key_cstr == NULL) {
|
---|
276 | (*logout) << "jdbmnaiveclass: out of memory" << endl;
|
---|
277 |
|
---|
278 | return false;
|
---|
279 | }
|
---|
280 |
|
---|
281 | char* data_cstr = (to_utf8(data)).getcstr();
|
---|
282 | if (data_cstr == NULL) {
|
---|
283 | (*logout) << "jdbmnaiveclass: out of memory" << endl;
|
---|
284 |
|
---|
285 | delete [] key_cstr;
|
---|
286 | return false;
|
---|
287 | }
|
---|
288 | int data_len = strlen(data_cstr);
|
---|
289 |
|
---|
290 | char* openfile_cstr = (to_utf8(openfile_)).getcstr();
|
---|
291 |
|
---|
292 | char cmd[512];
|
---|
293 | sprintf(cmd,"java -cp \"%s\" JdbSet \"%s\" %s %s",
|
---|
294 | classpath_cstr_,openfile_cstr,key_cstr,data_cstr);
|
---|
295 |
|
---|
296 | delete [] openfile_cstr;
|
---|
297 |
|
---|
298 | int status = system(cmd);
|
---|
299 | if (status != 0) {
|
---|
300 | (*logout) << "jdbmnaiveclass: failed to run cmd:"
|
---|
301 | << cmd << endl;
|
---|
302 | }
|
---|
303 |
|
---|
304 | delete [] key_cstr;
|
---|
305 | delete [] data_cstr;
|
---|
306 |
|
---|
307 | return (status == 0);
|
---|
308 | }
|
---|
309 |
|
---|