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 |
|
---|
34 | jdbmnaiveclass::jdbmnaiveclass(const text_t& gsdlhome)
|
---|
35 | : dbclass()
|
---|
36 | {
|
---|
37 | gsdlhome_ = gsdlhome;
|
---|
38 |
|
---|
39 | text_t jdbm_jar = filename_cat(gsdlhome_,"lib","java","jdbm.jar");
|
---|
40 |
|
---|
41 | text_t jdbm_wrapper_jar = filename_cat(gsdlhome_,"bin","java","JDBMWrapper.jar");
|
---|
42 |
|
---|
43 | classpath_ = pathname_cat(jdbm_wrapper_jar,jdbm_jar);
|
---|
44 | classpath_cstr_ = to_utf8(classpath_).getcstr();
|
---|
45 | }
|
---|
46 |
|
---|
47 | jdbmnaiveclass::~jdbmnaiveclass()
|
---|
48 | {
|
---|
49 | delete [] classpath_cstr_;
|
---|
50 | }
|
---|
51 |
|
---|
52 |
|
---|
53 | // returns true if opened
|
---|
54 | bool jdbmnaiveclass::opendatabase (const text_t &filename, int mode, int num_retrys,
|
---|
55 | #ifdef __WIN32__
|
---|
56 | bool need_filelock
|
---|
57 | #else
|
---|
58 | bool
|
---|
59 | #endif
|
---|
60 | )
|
---|
61 | {
|
---|
62 | if (openfile_ == filename) {
|
---|
63 | return true;
|
---|
64 | }
|
---|
65 |
|
---|
66 | if (logout == NULL) {
|
---|
67 | logout = &cerr;
|
---|
68 | }
|
---|
69 |
|
---|
70 | // Map the DB mode value into equivalent JDBM 'must_exist' value
|
---|
71 | bool must_exist = true; // default
|
---|
72 |
|
---|
73 | if (mode == DB_WRITER_CREATE) {
|
---|
74 | must_exist = false;
|
---|
75 | }
|
---|
76 |
|
---|
77 | if (must_exist) {
|
---|
78 | // test that jdbm file exists
|
---|
79 |
|
---|
80 | if (!file_exists(filename)) {
|
---|
81 | outconvertclass text_t2ascii;
|
---|
82 | (*logout) << text_t2ascii
|
---|
83 | << "Error: JDBM filename " << filename << "does not exist\n";
|
---|
84 | return false;
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | openfile_ = filename;
|
---|
89 |
|
---|
90 | // In naive implementation, no connection to database to open, so
|
---|
91 | // nothing else to do at this stage
|
---|
92 |
|
---|
93 | return true;
|
---|
94 | }
|
---|
95 |
|
---|
96 |
|
---|
97 | void jdbmnaiveclass::closedatabase ()
|
---|
98 | {
|
---|
99 | // nothing to do in naive implementation
|
---|
100 | }
|
---|
101 |
|
---|
102 | // returns file extension string
|
---|
103 | text_t jdbmnaiveclass::getfileextension ()
|
---|
104 | {
|
---|
105 | return ".jdb";
|
---|
106 | }
|
---|
107 |
|
---|
108 |
|
---|
109 | void jdbmnaiveclass::deletekey (const text_t &key)
|
---|
110 | {
|
---|
111 | if (openfile_.empty()) return;
|
---|
112 |
|
---|
113 | if (logout == NULL) {
|
---|
114 | logout = &cerr;
|
---|
115 | }
|
---|
116 |
|
---|
117 | // get a utf-8 encoded c string of the unicode key
|
---|
118 | char* key_cstr = (to_utf8(key)).getcstr();
|
---|
119 | if (key_cstr == NULL) {
|
---|
120 | (*logout) << "jdbmnaiveclass: out of memory" << endl;
|
---|
121 | return;
|
---|
122 | }
|
---|
123 |
|
---|
124 |
|
---|
125 | // delete the key<
|
---|
126 | char* openfile_cstr = (to_utf8(openfile_)).getcstr();
|
---|
127 |
|
---|
128 | char cmd[512];
|
---|
129 | sprintf(cmd,"java -cp %s JdbDel %s %s",classpath_cstr_,
|
---|
130 | openfile_cstr,key_cstr);
|
---|
131 |
|
---|
132 | delete [] openfile_cstr;
|
---|
133 |
|
---|
134 | int status = system(cmd);
|
---|
135 | if (status != 0 ) {
|
---|
136 | (*logout) << "jdbmnaiveclass: failed to run cmd: " << cmd << endl;
|
---|
137 | }
|
---|
138 |
|
---|
139 | // free up the key memory
|
---|
140 | delete [] key_cstr;
|
---|
141 | }
|
---|
142 |
|
---|
143 |
|
---|
144 |
|
---|
145 | // returns true on success
|
---|
146 | bool jdbmnaiveclass::getkeydata (const text_t& key, text_t &data)
|
---|
147 | {
|
---|
148 | if (openfile_.empty()) return false;
|
---|
149 |
|
---|
150 | if (logout == NULL) {
|
---|
151 | logout = &cerr;
|
---|
152 | }
|
---|
153 |
|
---|
154 | // get a utf-8 encoded c string of the unicode key
|
---|
155 | char* key_cstr = (to_utf8(key)).getcstr();
|
---|
156 | if (key_cstr == NULL) {
|
---|
157 | (*logout) << "jdbmnaiveclass: out of memory" << endl;
|
---|
158 |
|
---|
159 | return false;
|
---|
160 | }
|
---|
161 |
|
---|
162 | // fetch the result
|
---|
163 | char* openfile_cstr = (to_utf8(openfile_)).getcstr();
|
---|
164 |
|
---|
165 | char cmd[512];
|
---|
166 | sprintf(cmd,"java -cp %s JdbGet %s %s",classpath_cstr_,
|
---|
167 | openfile_cstr,key_cstr);
|
---|
168 |
|
---|
169 | delete [] openfile_cstr;
|
---|
170 |
|
---|
171 | FILE* PIN = popen(cmd,"r");
|
---|
172 |
|
---|
173 | if (PIN == NULL) {
|
---|
174 | (*logout) << "jdbmnaiveclass: failed to run cmd: " << cmd << endl;
|
---|
175 | }
|
---|
176 | else {
|
---|
177 | while (!feof(PIN)) {
|
---|
178 | char buf[256];
|
---|
179 | fgets(buf,256,PIN);
|
---|
180 |
|
---|
181 | cerr << "**## buf = " << buf << endl;
|
---|
182 |
|
---|
183 | cerr << "**## buf: ";
|
---|
184 |
|
---|
185 | int blen = strlen(buf);
|
---|
186 | for (int i=0; i< blen; i++) {
|
---|
187 | cerr << "(" << buf[i] << "," << (int)buf[i] << ") ";
|
---|
188 | }
|
---|
189 | cerr << endl;
|
---|
190 |
|
---|
191 |
|
---|
192 | data.append(buf);
|
---|
193 | }
|
---|
194 |
|
---|
195 | }
|
---|
196 |
|
---|
197 | delete [] key_cstr;
|
---|
198 |
|
---|
199 | cerr << "**** data before to_unicode = " << data << endl;
|
---|
200 |
|
---|
201 | data = to_uni(data); // convert to unicode
|
---|
202 |
|
---|
203 | return true;
|
---|
204 | }
|
---|
205 |
|
---|
206 |
|
---|
207 | // returns array of keys
|
---|
208 | text_tarray jdbmnaiveclass::getkeys ()
|
---|
209 | {
|
---|
210 | text_tarray keys;
|
---|
211 |
|
---|
212 | if (openfile_.empty()) return keys;
|
---|
213 |
|
---|
214 | if (logout == NULL) {
|
---|
215 | logout = &cerr;
|
---|
216 | }
|
---|
217 |
|
---|
218 | char* openfile_cstr = (to_utf8(openfile_)).getcstr();
|
---|
219 |
|
---|
220 | char cmd[512];
|
---|
221 | sprintf(cmd,"java -cp %s JdbKeys %s",classpath_cstr_,
|
---|
222 | openfile_cstr);
|
---|
223 |
|
---|
224 | delete [] openfile_cstr;
|
---|
225 |
|
---|
226 | FILE* PIN = popen(cmd,"r");
|
---|
227 |
|
---|
228 | if (PIN == NULL) {
|
---|
229 | (*logout) << "jdbmnaiveclass: failed to run cmd: " << cmd << endl;
|
---|
230 | }
|
---|
231 | else {
|
---|
232 | text_t key = "";
|
---|
233 |
|
---|
234 | while (!feof(PIN)) {
|
---|
235 |
|
---|
236 | char buf[256];
|
---|
237 | fgets(buf,256,PIN);
|
---|
238 | key.append(buf);
|
---|
239 |
|
---|
240 | keys.push_back(key);
|
---|
241 |
|
---|
242 | }
|
---|
243 | }
|
---|
244 |
|
---|
245 | pclose(PIN);
|
---|
246 |
|
---|
247 | return keys;
|
---|
248 | }
|
---|
249 |
|
---|
250 |
|
---|
251 | // returns true on success
|
---|
252 | bool jdbmnaiveclass::setkeydata (const text_t &key, const text_t &data)
|
---|
253 | {
|
---|
254 | if (openfile_.empty()) return false;
|
---|
255 |
|
---|
256 | if (logout == NULL) {
|
---|
257 | logout = &cerr;
|
---|
258 | }
|
---|
259 |
|
---|
260 | // get a utf-8 encoded c string of the unicode key
|
---|
261 | char* key_cstr = (to_utf8(key)).getcstr();
|
---|
262 |
|
---|
263 | if (key_cstr == NULL) {
|
---|
264 | (*logout) << "jdbmnaiveclass: out of memory" << endl;
|
---|
265 |
|
---|
266 | return false;
|
---|
267 | }
|
---|
268 |
|
---|
269 | char* data_cstr = (to_utf8(data)).getcstr();
|
---|
270 | if (data_cstr == NULL) {
|
---|
271 | (*logout) << "jdbmnaiveclass: out of memory" << endl;
|
---|
272 |
|
---|
273 | delete [] key_cstr;
|
---|
274 | return false;
|
---|
275 | }
|
---|
276 | int data_len = strlen(data_cstr);
|
---|
277 |
|
---|
278 | char* openfile_cstr = (to_utf8(openfile_)).getcstr();
|
---|
279 |
|
---|
280 | char cmd[512];
|
---|
281 | sprintf(cmd,"java -cp %s JdbSet %s %s %s",
|
---|
282 | classpath_cstr_,openfile_cstr,key_cstr,data_cstr);
|
---|
283 |
|
---|
284 | delete [] openfile_cstr;
|
---|
285 |
|
---|
286 | int status = system(cmd);
|
---|
287 | if (status != 0) {
|
---|
288 | (*logout) << "jdbmnaiveclass: failed to run cmd:"
|
---|
289 | << cmd << endl;
|
---|
290 | }
|
---|
291 |
|
---|
292 | delete [] key_cstr;
|
---|
293 | delete [] data_cstr;
|
---|
294 |
|
---|
295 | return (status == 0);
|
---|
296 | }
|
---|
297 |
|
---|